Here is an extract from item 56 of the book \"C++ Gotchas\":
It\'s not uncommon to see a simple initialization of a Y object written any of three
The compiler is permitted to optimize cases b
and c
to be the same as a
. Further, copy construction and assignment operator calls can be wholly eliminated by the compiler anyway, so whatever you see isn't necessarily the same with different compilers or even compiler settings.
As of C++17, all three of these are equivalent (unless Y::Y(int)
is explicit
, which would simply disallow c
) because of what is often called mandatory copy elision.
Even Y c = 1066;
creates only the one Y
object because the result of the implicit conversion to Y
is a prvalue that is used to initialize c
rather than to create a temporary.
The syntax
X a = b;
where a and b are of type X has always meant copy construction. Whatever variants, such as:
X a = X();
are used, there is no assignment going on, and never has been. Construct and assign would be something like:
X a;
a = X();