Why does the ternary operator prevent Return-Value Optimization (RVO) in MSVC? Consider the following complete example program:
#include
st
This related question contains the answer.
The standard says when copy or move elision is allowed in a return statement: (12.8.31)
So basically copy elision only occures in the following cases:
If your expression is not a named object or a temporary, you fall back to copy.
Some Interesting behaviors:
return (name); does not prevent copy elision (see this question)return true?name:name; should prevent copy elision but gcc 4.6 at least is wrong on this one (cf. this question) EDIT:
I left my original answer above but Christian Hackl is correct in his comment, it does not answer the question.
As far as the rules are concerned, the ternary operator in the example yields a temporary object so 12.8.31 allows the copy/move to be elided. So from the C++ language point of view. The compiler is perfectly allowed to elide the copy when returning from FunctionUsingTernaryOperator.
Now obviously the elision is not done. I suppose the only reason is that Visual Studio Compiler team simply did not implement it yet. And because in theory they could, maybe in a future release they will.