Tom wrote:
Test a = func();
cout <<"main(): Address of a: " <<&a <<endl;
Output:
func(): Address of tmp: 0x22ff30
main(): Address of a: 0x22ff30
The compiler had the option to create two objects, and call a copy
constructor.
However, you wrote a detector for the "return value optimization". Google
for that. It essentially means the compiler is free to secretly pass a
_into_ func(), to construct it in that context, and to skip the copy
construction at return time.
The Standard defines this optimization because without it, the compiler
would not know how to optimize this situation, because a programmer might
depend on a side-effect of the copy constructor. Without the RVO rule, if
the compiler aggressively optimized this code, the side-effect would
disappear and the code would break.
With this rule, developers are instructed not to rely on side-effects, such
as the one you discovered. And the compiler can optimize better. Without
this rule, we would be forced to write ugly code that avoided the extra
constructor and copy operation. So the rule permits cleaner code.
--
Phlip
http://www.greencheese.us/ZeekLand <-- NOT a blog!!!