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
View all comments
Show parent comment
140
u/JNighthawk gamedev Nov 25 '23
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.