Difficulty: Intermediate
What are lvalues and rvalues? What are move semantics and why did C++11 introduce rvalue references?
Move semantics is probably the single biggest performance feature C++11 gave us, and it rests on one distinction: is this object something that will keep existing, or is it about to disappear? Let's build up to it.
A classic informal rule: an lvalue has a name and an address you can take, so it persists beyond the current expression (a variable, an array element, a dereferenced pointer). An rvalue is a temporary, usually without a name, that is about to be destroyed (the result of a + b, a literal like 42, a function returning by value). Formally, C++11 refines this into lvalues, xvalues and prvalues, but the simple version is enough to reason with: 'can I take its address?' If yes, it is an lvalue.
Before C++11, passing or returning a large object by value meant a deep copy, even when the source was a temporary that would be thrown away immediately. That is like photocopying a 500-page report to hand to a colleague while you are about to shred the original. Move semantics lets the destination simply take over the source's internals: a vector's move constructor just copies three pointers and nulls the source, an O(1) operation instead of an O(n) copy.
To make this expressible, C++11 introduced the rvalue reference T&&, which binds to temporaries. Overloading on it lets a class provide both a copy constructor T(const T&) for lvalues and a move constructor T(T&&) for rvalues. The compiler picks the right one automatically. When you have an lvalue that you know you no longer need, you can call std::move(x). Despite the name, std::move does not move anything: it is simply a cast to an rvalue reference, giving permission for a move constructor or assignment to pull resources out of x.
Important subtleties. A named rvalue reference is itself an lvalue inside the function: given 'void f(int&& r)', the expression r is an lvalue, so calling g(r) picks the lvalue overload unless you write g(std::move(r)). After a move, the source object is in a valid but unspecified state; you may destroy it or assign a new value to it, but should not rely on its contents. Also, do not std::move a const object (it silently falls back to a copy) and do not std::move a local in a return statement, since it can block copy elision.
In practice you benefit from moves without writing anything: standard containers and strings are move-enabled, returning a vector from a function is cheap, and vector::push_back(std::move(s)) avoids a string copy. When you write your own resource-owning class, provide move operations, mark them noexcept, and follow the rule of five.
#include <iostream>
#include <string>
#include <utility>
#include <vector>
void f(int&) { std::cout << "lvalue\n"; }
void f(int&&) { std::cout << "rvalue\n"; }
int main() {
int x = 1;
f(x); // lvalue
f(2); // rvalue (literal)
f(std::move(x)); // rvalue (cast)
int&& r = 3;
f(r); // lvalue: a named rvalue reference is an lvalue
std::string s = "a fairly long string that lives on the heap, probably";
std::vector<std::string> v;
v.push_back(std::move(s)); // steals the buffer instead of copying
std::cout << v[0].size() << "\n";
std::cout << (s.empty() ? "moved-from is empty (typical)" : "not empty") << "\n";
}
The moved-from string is valid but its contents are unspecified; emptying it is what implementations do in practice. Never depend on it.
lvalue, rvalue, move semantics, rvalue reference