C++ Weekly - Ep 123 - Using in_place_t

Поділитися
Вставка
  • Опубліковано 12 січ 2025

КОМЕНТАРІ • 21

  • @Nitronoid
    @Nitronoid 5 років тому +6

    And with C++17: `std::optional x(std::vector{1, 2, 3});`
    Thanks to class template argument deduction.

  • @vorpal22
    @vorpal22 6 років тому +24

    Am I the only one who reads std as "standard?"

    • @rabbitdrink
      @rabbitdrink 5 років тому +26

      its better than reading it as sexually transmitted disease

    • @RaaynML
      @RaaynML 2 роки тому +2

      I go between "standard" and "stud"

  • @connorhorman
    @connorhorman 6 років тому

    I personally use in_place_type_t whenever I want to pass a type into a constructor. My PolymorphicWrapper type uses it to allow you to construct an object in the owned storage “In Place”
    Also there is a std::in_place_type variable template.

  • @jakearkinstall5313
    @jakearkinstall5313 6 років тому +12

    One of the more obscure parts of the language - a parameter that literally serves as a bodge to select the correct constructor. IMO a bad design choice - though I admit that an alternative, taking into account limitations of the language, is hard to come up with.

    • @michal.gawron
      @michal.gawron 6 років тому +3

      It could have been auto x = std::variant::make_in_place (a, b, c);
      And depend on guaranteed copy elision. But it's not much difference here, and it's perhaps even worse to read. ;-)

    • @_RMSG_
      @_RMSG_ 2 роки тому

      @@michal.gawron Would it have been possible to just have it as std::make?

    • @michal.gawron
      @michal.gawron 2 роки тому

      @@_RMSG_ What about other types : T2, T3, ..., Tn?

  • @_RMSG_
    @_RMSG_ 2 роки тому

    I see that it optimized to mov a value of 6 but it still allocated the vector? or is the operator new for the 6? does std::optional prevent allocation elision?

    • @cppweekly
      @cppweekly  2 роки тому

      std::optional is complex, and that video is old. It's very possible that a modern compiler or std::optional implementation would do something different there. But in any case, we are hoping/relying on the optimizer to do things.

  • @DamianReloaded
    @DamianReloaded 6 років тому +1

    I was thinking the other day on the bus, that it'd be nice if they added a layer of syntax sugaring ala c# for c++ (possibly between the parse tree and the preprocessor?) where things like in-place initialization could be replaced by this (or some other code) and possibly have some compile-time reflection maybe? And that it would be a compiler parameter that you can turn on/off?

    • @bit2shift
      @bit2shift 6 років тому +1

      Compile-time reflection has been in the works since July 2014, specifically for enums. [1]
      Lets hope it makes it to C++20.
      [1] www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4113.pdf

    • @DamianReloaded
      @DamianReloaded 6 років тому

      Thanks for the info!

  • @MaceUA
    @MaceUA 6 років тому +4

    But is it really better than simply constructing an object from an rvalue? Can't compilers optimize calls like these so that their assembly would be identical to what's generated for "std::in_place[_...]"?

  • @erichopper4979
    @erichopper4979 6 років тому

    It would've been nice to see why plain old ::std::in_place wouldn't work for variant or any. Also, what the neck is the index one for?

    • @michal.gawron
      @michal.gawron 6 років тому +5

      Both std::variant and std::any can take multiple types, so you have to specify which type you have in mind when initializing them. That's why you use std::in_place_type_t. And in variant you can specify which type you mean by indexing it, say std::variant you have 0 for int, 1, for double and 2 for Bleh.

    • @erichopper4979
      @erichopper4979 6 років тому +2

      Michał Gawron - I know. And this is meant to be a sort of tutorial video for people who want to learn C++ better. So I think being explicit about that in the video would've been a good idea.