There are actually two copies being performed on each Range
object passed to the constructor. The first happens when copying the temporary Range
object into the function parameter. This can be elided as per the reference given in 101010's answer. There are specific circumstances in which copy elision can be performed.
The second copy happens when copying the function parameter into the member (as specified in the constructor initialization list). This cannot be elided, and this is why you still see a single copy being made for each parameter in YSC's answer.
When the copy constructor has side-effects (such as the prints in YSC's answer), copy elision can still be performed for the first copy, but the second copy must remain.
However, the compiler is always free to make changes if they do not alter the observed behavior of the program (this is known as the "as-if" rule). This means that if the copy constructor has no side effects and removing the constructor call will not change the result, the compiler is free to remove even the second copy.
You can see this by analyzing the generated assembly. In this example, the compiler optimizes out not only the copies, but even the construction of the Box
object itself:
Box box(Range(a,b),Range(c,d));
std::cout << box.x.from;
Generates identical assembly as:
std::cout << a;
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…