Lvalue vs rvalue
An lvalue is an expression that names a durable object you can take the address of. An rvalue is a temporary or a value that is about to expire, which is why std::move and move constructors talk to rvalues and leave lvalues alone.
Detailed Explanation
The names come from assignment: historically the left side had to be a location, the right side a value. C++ kept the words and then built move semantics on top of them. You need the distinction the first time you write std::string s = name; versus std::string s = std::move(name); and want to know why one copies and the other steals.
Start with what you can observe. If you can write &expr, expr is an lvalue. A variable name is an lvalue. v[0] is an lvalue. A function that returns int& produces an lvalue. You can assign to it (unless it is const) and it will still be there after the statement.
If you cannot take the address, you are looking at an rvalue. 42 is an rvalue. std::string("tmp") is an rvalue: a temporary the compiler created to hold the result. A function that returns std::string by value produces an rvalue. The object exists until the end of the full expression, then it is destroyed.
C++11 split the old "rvalue" idea so move semantics could be precise. The standard now has five value categories. You can work with three of them:
| Category | What it is | Binds to |
|---|---|---|
| lvalue | A named object, or something that acts like one | T&, const T& |
| xvalue | An object whose resources you are allowed to steal ("expiring") | T&&, const T& |
| prvalue | A pure value: a literal, a temporary being created | T&&, const T& |
xvalue plus prvalue are the rvalues. std::move(x) does not move anything. It is a cast from an lvalue to an xvalue, so overload resolution picks the T&& overload. That overload is allowed to hollow x out. After the move, x is still an object, but its guts may be gone; you may assign to it or destroy it, and you should not read it as if it were unchanged.
Why the split exists: copying a std::string or a std::vector duplicates the heap buffer. When the source is a temporary, that copy is wasted work. The move constructor takes the buffer pointer and leaves the source empty. The language only lets that constructor run when the source is an rvalue, so a named variable you still need is not silently emptied.
const T& is the compatibility hatch. It binds to lvalues and rvalues, which is why old APIs that take const std::string& still accept temporaries. T&& as a function parameter that is not a forwarding reference binds only to rvalues. A forwarding reference (template<class T> void f(T&&)) is a different rule: T collapses so the same template can forward lvalues and rvalues without copying.
A practical test: if the object has a name you will use on the next line, it is an lvalue and a copy is the safe default. If it is a temporary or you have finished with the name, treat it as an rvalue and move.
Code Examples
#include <string>
#include <utility>
void takeCopy(std::string s); // always a new string
void takeLref(std::string& s); // needs a named object
void takeRref(std::string&& s); // needs a temporary or move
void takeConst(const std::string& s);
int main() {
std::string name = "ada";
takeCopy(name); // copy from lvalue
takeCopy(std::string("tmp")); // construct from prvalue (often elided)
takeLref(name); // ok
// takeLref(std::string("tmp")); // error: temporary is not an lvalue
takeRref(std::string("tmp")); // ok: prvalue
takeRref(std::move(name)); // ok: name is now an xvalue; do not read name
takeConst(std::string("tmp")); // const& extends the temporary for the call
}#include <iostream>
#include <string>
#include <utility>
int main() {
std::string a = "left";
std::string b = std::move(a); // b steals a's buffer
std::cout << b << '\n'; // prints: left
// a is valid but unspecified; assign before reading
a = "again";
std::cout << a << '\n'; // prints: again
}Common Gotchas
Using an object after moving from it
the object is still alive. Its state is valid but unspecified for standard library types. `a.size()` after a move is often `0`, but the standard does not promise that for every type. Assign or destroy; do not assume contents.
A forwarding reference is not "an rvalue reference"
`void f(std::string&&)` accepts only rvalues. `template<class T> void f(T&&)` accepts both, because of reference collapsing.