This is a case where the common, layman's description of move semantics often creates confusion.
C++11 has move operations which are essentially a transfer of ownership
Although often described as a "transfer of ownership", this is just a description of the effects of move semantics when this "transfer of ownership" actually means something for the object being moved.
But if there is no meaningful "transfer of ownership" then a move effectively devolves into an ordinary copy, so:
it make sense to move a local variable in this case?
Maybe, maybe not. It depends. Heap-based object management is the most common use case where move semantics "means something".
Perhaps the best way to understand what moving an object means, is to consider a move to be equivalent to copying an object, but the moved-from object may end up in some "valid, but unspecified state" (this is the wording specified for the C++ library's move semnatics, but it is accepted to be a description of move semantics, in general).
Normally, when you copy an object (unless the object has a weird operator=
overload) the copied-from object is expected to be unchanged, and the copied-to object is expected to end up being a logical copy of the copied-from object.
The only thing that a move does, is add an additional property that the moved-from object ends up in some "valid, but unspecified state".
That's it.
That's all that a move is.
For a classical example of a container, with the container's contents on the heap, a move is equivalent to swapping some pointers and ancillary metadata. But, to answer your question directly:
it make sense to move a local variable in this case
The answer is: it depends on what the variable is. It can certainly be possible that, for some complicated class, a lot more work is required to create a duplicate copy of a copied-from object. But, if an object is being moved, it is a less-expensive operation, leaving in the moved-from object in some "valid, but unspecified state".
Nothing here requires the object to contain any internal data on the heap.
A lame example: object represents a file, identified as a simple int
(the actual filename might merely be this integer value, as varying part of a fixed filename). Copying an object requires the underlying file to be copied in its entirety. But, moving this hypothetical object requires merely the moved-from object to be set to be the logical equivalent of /dev/null
. So, moving a "local variable", here, might actually mean something.