move
std::string source = "payload";
std::string moved = std::move(source);
check(moved == "payload", "move");
What move does
std::move does not move anything
It casts to an rvalue reference so an overload can take ownership of resources
The target then owns the buffer
Moved from state
The moved from object is valid but unspecified You may assign to it or destroy it Do not assume it is empty
Rule of zero again
If your class owns no raw resource, let the compiler generate move operations Writing custom move constructors without a reason introduces bugs
When to use it
Passing large objects into storage
Returning values, where the compiler often elides copies anyway
Do not sprinkle std::move on every return, it can block copy elision
Next
The horizon in 12 current
Posted