Thank you to everyone who replied.
I used an online c++ compiler (
http://codepad.org) to compile the above code.
And the output I got was:
Output:
Default constructor called.
Copy constructor called.
Which probably means that the g++ compiler was optimizing the code, as many of you pointed out.
-----------------------
I also noticed a difference between initializing x1 and x2 with the online compiler
1 2 3 4
|
X x0; // default constructor
X x1 = x0;
X x2 = X(x0);
|
x1 - leads to a single copy constructor call
x2 - leads to two copy constructor calls - assuming one to create a temp and the second to initialize x2 with that temp.
------------------------------
And for those who are wondering, the online compiler is setup as:
C++: g++ 4.1.2
flags: -O -std=c++98 -pedantic-errors -Wfatal-errors -Werror -Wall -Wextra -Wno-missing-field-initializers -Wwrite-strings -Wno-deprecated -Wno-unused -Wno-non-virtual-dtor -Wno-variadic-macros -fmessage-length=0 -ftemplate-depth-128 -fno-merge-constants -fno-nonansi-builtins -fno-gnu-keywords -fno-elide-constructors -fstrict-aliasing -fstack-protector-all -Winvalid-pch
http://codepad.org/about
With -fno-elide-constructors being the option of interest. This option is supposed to be the default behavior of any c++ compiler according to the ANSI standard (g++ man-page), but this doesn't seem to be the case with g++.