std::move
std::move(x) is a cast that turns x into an rvalue. It does not move anything by itself. It makes overload resolution pick a move constructor or move assignment if one exists, so the next use of x is allowed to steal from it.
Detailed Explanation
The name is the most misleading identifier in the standard library. std::move is static_cast<T&&>(x) with a bit of reference collapsing, declared in <utility>. After the cast, x is an xvalue. If the next thing that happens is T b = std::move(x); and T has T(T&&), that constructor runs and may hollow x out. If the next thing is f(std::move(x)) and f takes const T&, nothing is stolen; the cast was wasted and x is still intact.
Use std::move when you have a named object you will not use again (except to assign to or destroy), and you want the destination to steal. Typical sites:
- Passing a local into a sink:
items.push_back(std::move(line)); - Transferring a
unique_ptr:p2 = std::move(p1); - Implementing your own move constructor: initialise members with
std::move(other.member) - Returning a member (not a local):
return std::move(name_);can be right becausename_is not eligible for copy elision the way a local is. Returning a local should still bereturn local;.
Do not use std::move on a value you will read on the next line. Do not wrap a temporary: f(std::move(std::string("x"))) is pointless; the temporary is already an rvalue. Do not move an object into two destinations.
std::move does not change the object. It changes the value category of an expression. The object becomes "moved from" only if a move operation actually runs. Moving a const std::string calls the copy constructor, because const string&& will not bind to string&&.
std::forward<T>(x) is the related cast for forwarding references. It preserves lvalue-ness. If you wrote std::move on a forwarding reference, you would steal from lvalue callers. Use std::forward in templates that take T&& as a forwarding reference; use std::move when you know you have an object you want treated as expiring.
After std::move(x) and a real move, treat x as unspecified. For std::unique_ptr, it is empty. For std::string, it is empty on every major library. For your types, document the state. The language does not reset the bits for you.
Code Examples
#include <iostream>
#include <string>
#include <utility>
#include <vector>
int main() {
std::string line = "ready";
std::vector<std::string> lines;
lines.push_back(std::move(line)); // steal the buffer
std::cout << lines[0] << '\n'; // prints: ready
std::cout << line.size() << '\n'; // prints: 0 on typical libraries
line = "again"; // assign is always allowed
std::cout << line << '\n'; // prints: again
}#include <memory>
#include <utility>
struct Node {
std::unique_ptr<Node> next;
int value;
};
void attach(Node& parent, std::unique_ptr<Node> child) {
parent.next = std::move(child); // unique_ptr is move-only
}
int main() {
Node root{nullptr, 0};
auto child = std::make_unique<Node>(Node{nullptr, 1});
attach(root, std::move(child)); // child is empty after this
}Common Gotchas
Moving a `const` object copies
`const std::string s = "x"; auto t = std::move(s);` copies. The cast cannot strip `const` in a way that binds to `string&&`.