Why should someone create a default move constructor but delete the move assignment operator?

Viewed 91

Today i came across a class with following layout (c++14)

class A {
//ctor  
A(..some specific parameter) :..init some members .. {}; 
//copy ctor
A(A const&) = delete;
//copy assign op
A& operator=(A const&) = delete;
//move operator
A(A&&) = default;
//move assign op
A& operator=(A&&) = delete;
//dtor
~A() = default;
}

What could be the intention behind creating a default move consructor but deleting the move-assignment operator?

And what is the impact/restrictions when using the class?


Update: No I cannot share class details, however, the comments and answers are pointing to the right direction, it's all about ressource allocation and initialisation.

Thank you all soo much - you made my day!!!

1 Answers

What this means is that, once an object is created, some aspect of its value cannot later be changed. You can move from it of course, but once that's done, you can't move back to it.

Consider a unique_ptr that had this design. Such a pointer would allow you to transfer ownership, but only to a newly constructed unique_ptr. So basically, if you create a unique_ptr, that pointer object instance will either be managing that specific object, or it will manage nothing, because you moved from it earlier. It will never be managing a different object.

Related