No, not at all.
However, when writing code, you can follow the guidelines.
Lossless return value optimization
This works a lot every time you return a temporary one, even in debug mode.
return MyObject(....);
Name optimization
This almost works every time a function always returns the same temporary object:
MyObject func() { MyObject result; if (...) { return result; } result.push(0); return result; }
You can mix them, but the compiler can hardly use RVO in this case:
MyObject func() { MyObject result; if (...) { return MyObject(...); } return result; }
Here, probably, one return will be beneficial from the RVO, and the other not. And I would bet that the first one is optimized because you are stuck if you speculatively create result in the reverse slot and suddenly you need to take the if branch. Note that simply reordering the statements just works:
MyObject func() { if (...) { return MyObject(...); } MyObject result; return result; }
So, the rule of thumb for NRVO is that there should be no return between the result declaration and the return result; that returns anything else but result .
If you follow this, you add up the odds in your favor. And then it's just a matter of code review.
And you also make it easier to read code, since you do not declare variables before you know that you really need them.
source share