• jormaig@programming.dev
    link
    fedilink
    English
    arrow-up
    2
    ·
    1 year ago

    What’s happening in the example is Return Value Optimization. However, this doesn’t work if you are returning two objects wrapped in a std::tuple. If you do that, you need to move the two objects before constructing the tuple.

  • the_artic_one@programming.dev
    link
    fedilink
    English
    arrow-up
    1
    ·
    edit-2
    1 year ago

    If temporaries needed to be moved, you’d want to use std::move every single time you created one. Are C++ language ergonomics really so bad that that people expect such a ridiculous level of boilerplate?

    • rmam@programming.devOP
      link
      fedilink
      English
      arrow-up
      2
      ·
      edit-2
      1 year ago

      If temporaries needed to be moved, you’d want to use std::move every single time you created one.

      I don’t think that that’s true. I mean std::move only cast objects to rvalue references, and temporaries are already that.

      https://en.cppreference.com/w/cpp/utility/move

      Are C++ language ergonomics really so bad that that people expect such a ridiculous level of boilerplate?

      I think you’re expressing misconceptions.

      • the_artic_one@programming.dev
        link
        fedilink
        English
        arrow-up
        1
        ·
        edit-2
        1 year ago

        I don’t think that that’s true. I mean std::move only cast objects to rvalue references, and temporaries are already that.

        I know, this article is telling you why you shouldn’t move temporaries and I’m questioning why anyone would think they need to move temporaries in the first place.

        • rmam@programming.devOP
          link
          fedilink
          English
          arrow-up
          1
          ·
          1 year ago

          The go-to rule is that if you want an object moved instead of copied, you cast the type to a rvalue reference.

          The nuance in this article is that return type optimization (RTO) is better than moving, but up until C++17 RTO it was allowed but not guaranteed. Once RTO is enforced, those who care about the tradeoffs between copying and moving are better off leaving compilers doing what they do best.

          I guess that it’s important to stress that this is only valid after C++17.

          • the_artic_one@programming.dev
            link
            fedilink
            English
            arrow-up
            1
            ·
            1 year ago

            Did you mean to link a different article? This one doesn’t mention RTO at all, just mandatory copy elision and it only shows examples of how moving temporaries is unnecessary (which I was saying should be obvious).

            The fact that moving local variables when returning is bad thanks to RTO is a lot less obvious so that’s definitely a useful tip for devs who are still getting the hang of move semantics.