C++ Reference ยท Types and Language Concepts
Rvalue references and move semantics
Reference for C++ rvalue references T&& and move semantics: std::move, move constructors and assignment, the moved-from state, and when moves happen.
An rvalue reference (T&&) binds to temporaries and to objects you explicitly mark as "no longer needed". Its purpose is move semantics: instead of copying an object's resources (a heap buffer, a file handle), the receiving object can steal them and leave the source in an empty but valid state. Moving a std::vector of a million elements swaps three pointers; copying it allocates and copies a million elements.
std::move(x) does not move anything. It is a cast that turns x into an rvalue so that overload resolution picks the move constructor or move assignment operator. The actual work happens in those special member functions, which you write yourself only when your class directly owns a resource (see the rule of five).
std::string a = "hello"; std::string b = std::move(a); // b takes a's buffer; a is valid but unspecified
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Syntax
T&& r = std::move(x); // r refers to x, treated as an rvalue T(T&& other) noexcept; // move constructor T& operator=(T&& other) noexcept; // move assignment operator std::move(x) // <utility>: static_cast<T&&>(x)
| Name | Description |
|---|---|
T&& (non-template) |
An rvalue reference: binds to rvalues, never to plain lvalues. |
std::move(x) |
Casts x to an xvalue (T&&). Never throws, does nothing at run time. |
| Move constructor | Initializes the new object by taking resources from other. |
| Move assignment | Releases the object's own resources and takes other's. |
std::exchange(obj, new_value) |
Returns the old value of obj and stores new_value: handy in move operations. |
Errors / undefined behavior: using a moved-from object in a way that requires a precondition (e.g. calling front() on a moved-from vector, dereferencing a moved-from unique_ptr that is now null) is undefined behavior. Only destroying and assigning to a moved-from object is always safe. Moving from an object while still using references into it is also a bug.
Usage notes
- Moved-from means "valid but unspecified" for standard library types. In practice a moved-from
std::vectororstd::stringis usually empty, and a moved-fromunique_ptris guaranteed null, but do not depend on emptiness for other types. - Name = lvalue. A parameter of type
T&&is itself an lvalue inside the function (it has a name), so passing it on requiresstd::move(param)again. - Do not
std::movea local on return.return local;is already treated as a move (or elided entirely);return std::move(local);can prevent copy elision and triggers-Wpessimizing-move. - Mark move operations
noexcept.std::vectoronly moves elements on reallocation when the move constructor isnoexcept; otherwise it copies to keep the strong guarantee. - Move-only types such as
std::unique_ptr,std::threadand streams delete the copy operations; you muststd::movethem to transfer ownership. - Const prevents moves.
std::moveof a const object yieldsconst T&&, which cannot bind toT&&, so a copy happens. - Cost. A move is typically a few pointer copies; for types without owned resources (e.g.
int,std::array<int, 100>) a move is the same as a copy. - Self-move.
x = std::move(x);must leavexvalid; the checkif (this != &other)guards against double release. - Since C++11.