A function that is deleted is still declared, only the definition is deleted. Expanding that in your class definition:
struct B : A {
using A::operator=; // A& operator=(const A&)
B& operator=(const B&) = delete;
};
At this point, you can note that there are two declarations for operator=
in the derived type, the first one (brought into scope by means of a using-declaration) takes a const A&
argument, while the second one takes a const B&
and is deleted.
When you later try the assignment:
B b1, b2;
b1 = b2;
Both declarations are seen by the compiler and the second one is a better match. Because it is marked as deleted you get the error. If you had, on the other hand assigned a A
object it would have worked as expected:
B b1, b2;
b1 = static_cast<A&>(b2); // works, BUT...
The problem with this approach is that it is only copying the base subobjects which is probably not what you want. If you just want the same behavior you would have had if the assignment had been generated by the compiler you need to ask for it:
struct B : A {
// ...
B& operator=(const B&) = default;
};