|
|
|
|
|
by cobalt
3444 days ago
|
|
except they are not all equal. hence why there's 3 ways to do it. (Optimizations can change things, but per the spec, they are not the same) the first creates a new variable with a default value and then copies 0 into it. (This is trivial with an int, but not so with a more complex type) the second case creates x using the copy constructor the third uses an initializer list, and works similarly to #2 |
|
No, no default constructor is invoked. In this specific example, until C++17, a temporary int object is created then x is copy constructed from it [1]. The compiler is explicitly allowed to omit the temporary+copy and directly construct from the parameter as per int x(0), but the constructor must be non-explicit.
From c++-17 on this is actually required and additionally a copy constructor is not required to exist. In practice is equivalent to #2 except for the non-explicit requirement.
Pedantic, I know, but as long as we are trying to clarify the rules is better to be clear.
[1] note: is different from default initialize then assign.