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);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…