- The copy constructor would not be invoked on line 7.
By definition, a copy constructor for a class
X is a one-argument constructor that takes a reference to (const)
X. (roughly).
The one-argument constructor taking
int is called a
converting constructor, because it allows users to convert an
int into an
X.
- The copy constructor would be invoked on line 8 and line 9.
- The copy constructor wouldn't be invoked on line 10, but that's not really a beginner's question:
The initializer expression is a
prvalue with type
cv X, so
copy elision occurs. No temporary object is materialized and copied. Instead, the variable
x2 is treated as the result object of the initializer expression and is constructed in-place.
https://en.cppreference.com/w/cpp/language/copy_elision
This is the C++17 behavior. In C++14, compilers are
allowed but not required to omit the invocation of the copy constructor even if it has side effects. Before C++14, the
as-if rule is applied here, and copy construction must occur, but only if that copy constructor has observable side-effects.
- The copy constructor would invoked on line 12: the argument
y is copied into the parameter of
print2().