warning C4512: 'B' : assignment operator could not be generated
Question 1: Why this warning?
References can only be initialized once when they are created. You cannot reassign a reference to another same type variable after creation because Reference is just an alias of the type variable for which it was created and will continue to remain so. Attempting to reassign it generates an error.
Usually, a compiler generates an implicit bit wise assignment operator every class by default but in this case Since the class B
has an reference as an member m_a
, If the compiler were to generate an implicit assignment operator it would break the fundamental rule that references cannot be reassigned. So the compiler generates this warning to inform you that it could not generate the implicit assignment operator.
Question 2: But shouldn't the compiler generating an error for the B b1 = b; statement too?
The generated warning and this particular operation have no relation at all.
B b1 = b;
invokes the implicit(as rightly pointed out by @AndreyT) copy constructor B::B(const B&)
. Implicit copy constructor is one of the member functions which a class generates by default. So there is no warning or error for it.
Question 3: It is like it generated copy constructor but didn't generate assignment operator. Aren't these two inherently linked to one another ?
No they are not related at all. Yes compiler generated a copy constructor but it could not generate a assignment operator for the reason specified in answer to Question 1 above. This is because the member reference m_a
could be initialized in the body of the constructor itself. it's just the initial assignment at time of creation not assignment as in case of =
.
Question 4: Does it makes sense to generate the default implementation for only one of them when the other could not be generated?
Answer to the 3 Question seems to answer this.
Just to reiterate the operations being performed in your code example:
B b(a);
invokes the conversion copy constructor B::B(A&)
B b1 = b;
invokes the default copy constructor B::B(const B&)
Consider the additional scenarios.
If you had B b1 = a;
it would call B::B(A&)
and hence no error again.
But compiler would mark a error if B::B(A&)
was declared explicit
and would not be allowed for any implicit conversions
by acting as a conversion function
.
Check the same here.
4.5.0
doesn't give any warning even with-Wall -pedantic
. – Fanjetoperator=
for a class (because the class contains a reference or a constant member). GCC waits till you actually try to use the operator. – Aggression