Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What can I do with a moved-from object?

People also ask

What does do not rely on value of moved from an object?

Do not rely on the value of a moved-from object unless the type of the object is documented to be in a well-specified state. While the object is guaranteed to be in a valid state, relying on unspecified values leads to unspecified behavior.

What is move constructor C++?

A move constructor enables the resources owned by an rvalue object to be moved into an lvalue without copying.


17.6.5.15 [lib.types.movedfrom]

Objects of types defined in the C++ standard library may be moved from (12.8). Move operations may be explicitly specified or implicitly generated. Unless otherwise specified, such moved-from objects shall be placed in a valid but unspecified state.

When an object is in an unspecified state, you can perform any operation on the object which has no preconditions. If there is an operation with preconditions you wish to perform, you can not directly perform that operation because you do not know if the unspecified-state of the object satisfies the preconditions.

Examples of operations that generally do not have preconditions:

  • destruction
  • assignment
  • const observers such as get, empty, size

Examples of operations that generally do have preconditions:

  • dereference
  • pop_back

This answer now appears in video format here: http://www.youtube.com/watch?v=vLinb2fgkHk&t=47m10s


Moved-from objects exist in an unspecified, but valid, state. That suggests that whilst the object might not be capable of doing much anymore, all of its member functions should still exhibit defined behaviour — including operator= — and all its members in a defined state- and it still requires destruction. The Standard gives no specific definitions because it would be unique to each UDT, but you might be able to find specifications for Standard types. Some like containers are relatively obvious — they just move their contents around and an empty container is a well-defined valid state. Primitives don't modify the moved-from object.

Side note: I believe it's T c = std::move(a) so that if the move constructor (or copy constructor if no move is provided) is explicit the function will fail.