Reputation: 818
#include <type_traits>
struct A {};
struct B {
B(A& a) : a(a) {}
//B& operator=(B const& b) = default; // LINE 1
//B& operator=(B const& b) { a = b.a; return *this; } // LINE 2
A& a;
};
static_assert(std::is_copy_constructible<B>::value, ""); // pass
static_assert(std::is_copy_assignable<B>::value, ""); // fail
int main() {
A a;
B b(a);
return 0;
}
Compiler: g++ 5.1
The default copy constructor is fine, but the default copy assignment operator fails (even with LINE 1 uncommented).
Declaring it explicitly (uncommenting LINE 2) works though. Is it a bug?
Upvotes: 2
Views: 188
Reputation: 103703
No, it's not a bug. Your class doesn't have a defined copy assignment operator, because you have a reference member. If you have a class with reference semantics, it is not obvious whether you would want assignment to just assign from one reference to another (which results in copy assigning the referenced object), or whether the reference should be rebound (something you can't actually do, although this is usually what you would want). So the standard simply doesn't generate a default assignment operator, but allows you to define one manually with the semantics you want.
Upvotes: 6