This question raises a lack of understanding of what movement is in C ++ 11.
When you copy an object with pointers or otherwise own resources, there are two ways to make this copy. You can either copy the links of pointers / resources, or you can select new objects / resources and copy the value of the original to new ones.
In the first case, you are left with two objects that have links to the same object. Qt does it a lot. If you use one object to change what it refers to, you also modify another. Usually you need some kind of reference counter so as not to double delete the pointer or double release the resource.
In the second case, you are left with two completely separate objects. This is commonly referred to as "semantics of values" because if you copy one POD to another, you have two completely separate objects. Changing one does not change the other.
Moving semantics is a C ++ 11 mechanism that allows objects that usually have value semantics to have reference semantics under certain conditions. This essentially allows the object to steal pointers / resources referenced by another instance of the object.
For example, take std::vector
; it's just a wrapper around a dynamically allocated and resized array. As with most standard C ++ library objects, vector
implements the semantics of values. If you copy vector
, the new vector should select a new array and copy each element from the old to the new. Once you're done, you have two completely different arrays.
Moving semantics is a way of transferring an array contained in vector
to another. After the operation, the displacement source object is "empty" (technically in the undefined state, but it is effectively separated from the data that it allocated). The new vector
now has the same pointer as the old vector
; the new vector
will delete the memory that it did not allocate.
As you can see, all this is based on the concept of an object that owns resources. vector
allocates and owns an array; this destructor will destroy it. This is how you determine ownership. The motion concerns the transfer of ownership.
PODs cannot express ownership of pointers or resources because PODs must have trivial destructors (i.e., destructors that do nothing). Since PODs cannot express their property, there is nothing to move. You cannot transfer ownership between objects if they do not own anything.