http://coliru.stacked-crooked.com/a/8356f09dff0c9308
#include
struct A
{
A(int& var) : r(var) {}
int &r;
};
int main(int argc,
A a1(x);
is fine because it's constructing an instance of A
with a reference (x
is turned into a reference and the constructor A(int&)
is called)
A a2 = a1;
Is also fine because it is still construction. Copy construction, in fact. It's okay to initialize a reference with another reference.
For instance:
int a = 1;
int& b = a;
int& c = b;
Is okay because this is all construction (Demo)
However, you cannot assign a reference, which is what a2 = a1
will attempt to do through a compiler-generated copy-assignment operator. However, the compiler recognized this and did not generate such an operator. Since the operator does not exist, you got a compiler error.
Assignment and construction are two different beasts.
The default assignment operator cannot exist because references cannot be assigned to (if you try in "user-land" code, you will actually be assigning to the referant).
The default copy constructor has no such problem, since we can create references just fine.
Copy assignment operator and Copy constructor are TWO different things. It does NOT guarantee that when copy assignment operator exist, copy constructor also exist, but they follow their own rules.
From copy assignment operator, it mentions :
If T has a non-static data member of a reference type, then the defaulted copy assignment operator for class T is defined as 'Deleted'. That;s the case :D
For the Copy constructor, it has its own rule to decide whether deleted or not, for example, if Non static rvalue reference data member exist in a class, then the default copy constructor will be defined as 'Deleted'. You can see more by visit docs.