I canβt think of a reason, the only trait of having a reference or const member, or it wonβt mean that the class must have one of these traits (not copyable or fixed)
I think it's wrong.
The presence of a const
data member or a reference data element does not make the class non-copyable: it simply makes it impossible to assign it or move from it in a destructive way.
This is because the destructive move operation does not sound for objects or const
links: it will be against their semantics, since the state of a const
object should never change, and the links cannot be disconnected.
However, the fact that X
not assignable for copying or forwarding does not jeopardize the ability to create copies of these objects, which - as you indicate - is a sound operation. For example, the following program will compile:
struct X { X() : x(0), rx(x) { } const int x; const int& rx; }; int main() { X x; X x1 = x;
The compiler implicitly generates a copy constructor for you, and movements will degenerate into a copy. If this degeneration of moving into a copy is not suitable for the semantics of your class, you may not have const
or reference elements.
source share