Why copy constructor is not called in this case?

From another comment: “So by default I should not rely on it (as it may depend on the compiler)”

No, it does not depend on the compiler, practically anyway. Any compiler worth a grain of sand won’t waste time constructing an A, then copying it over.

In the standard it explicitly says that it is completely acceptable for T = x; to be equivalent to saying T(x);. (ยง12.8.15, pg. 211) Doing this with T(T(x)) is obviously redundant, so it removes the inner T.

To get the desired behavior, you’d force the compiler to default construct the first A:

A a;
// A is now a fully constructed object,
// so it can't call constructors again:
a = A(5);

Leave a Comment