From the wikipedia page for copy constructors:
X a = X();
// valid given X(const X& copy_from_me) but not valid given X(X& copy_from_me)
// bec
It's simply a matter of understanding the grammar of C++. The statement X a = X();
is a declaration statement with initializer, and not an assignment expression. The grammatical meaning of this statement is to declare a variable a
of type X
and copy-initialize it from the expression X()
. There is no assignment involved here in any way.
Because the code is constructing an object. The = sign here is initializing, not assigning. You can only assign to an existing object, not to one that's under construction.