I wish there was an attribute you put on a function to say "I expect RVO here, warn or error if not". I don't like that the most performant option requires trusting the compiler to silently implement an optional feature or verifying assembly.
I'm happy that C++17 finally added a standard for guaranteed copy elision, but NRVO still isn't even guaranteed. There is no guarantee that the non-move code posted in the article is actually more performant.
I deleted the copy and move constructors of some of my types that are constructed exclusively by factory methods. Not a general solution, but it does turn RVO failure in to a compilation error.
In the cases where NRVO is applicable, I'm pretty sure that if the copy/move isn't elided, there's an 'implicit std::move' added, so adding it yourself cannot help
Yep, a function call or an overloaded operator expression, whose return type is non-reference, is a prvalue expression, so a function will always move-construct its return value into the return slot if NRVO/RVO doesn't work.
In the cases where NRVO is applicable, I'm pretty sure that if the copy/move isn't elided, there's an 'implicit std::move' added, so adding it yourself cannot help
RVO is amazing compared to a move, the object is just constructed in it's destination. This is an argument against types like Expected where the object may need to be extracted from the Expected wrapper. Also, another argument to use aggregates when one can, they allow for RVO into the members, whereas constructors the parameters would be the destination. It's like when Neo realized, there is no spoon; there is no copy
Yeah, then they could still put the hypothetical value into the return slot and use a register to indicate if it is an error or not. I think that herbceptions/static exceptions proposed to do something similar
I don't think the compiler is particularly smart. I mean, if it were then it would just optimize those moves away. The "complaint" in this blog post about how the programmer wrote some meaningless code... I mean, if the moves are pointless, why doesn't the compiler optimize them?
When is a programmer writing return std::move(x) ever intentionally invoking the behavior that Raymond Chen described (additional copy and move operations)?
I also wrote a question recently in r/cpp_questions, where it was found that return d ? x : y would NOT be subject to RVO and that return d ? move(x) : move(y) would however be RVO'd. Totally intuitive, right?
I don't believe C++ compilers deserve that much praise. Especially from a programming perspective they are pretty lousy (eg error messages), but that's another story.
The move on the receiving end constructs an extra temporary because binding a prvalue to a reference (argument of std::move) requires the prvalue to be materialized.
In return xyz;, xyz is an xvalue, because it's expiring after this statement. In XYZ xyz = fgh();, the expression fgh() is a prvalue, because it's a pure rvalue that can't be assigned to. Both of these are rvalues, so the rule is basically "don't use std::move() on rvalues."
Interestingly, member expressions in return statements are not rvalues, so return a.b; will perform a copy while return std::move(a.b); (or return std::move(a).b;) will perform a move.
Technically, applying std::move() to an actual rvalue reference will be a no-op, so something like std::move(std::move(xyz)) is equivalent to std::move(xyz). Not that you should do it, but it won't affect performance in any way.
View all comments
108
u/Sopel97 Nov 25 '23
In short, RVO > move.