Looking at the program output, it seems to me that, indeed, the compiler is eliding in both cases, why?
Because, if no elide was activated, the correct output would be:
- construct the example object at function return;
- copy it to a temporary;
- copy the temporary to the object defined in main function.
So, I would expect, at least 2 "copy" output in my screen. Indeed, If I execute your program, compiled with g++, with -fno-elide-constructor, I got 2 copy messages from each function.
Interesting enough, If I do the same with clang, I got 3 "copy" message when the function FunctionUsingTernaryOperator(0);
is called and, I guess, this is due how the ternary is implemented by the compiler. I guess it is generating a temporary to solve the ternary operator and copying this temporary to the return statement.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…