In C++, what distinguishes an lvalue, an xvalue and a prvalue, and which category does std::move(s) produce?
answer
- identity versus initializing a value
- glvalue and rvalue are the umbrellas
- an expiring object you may reuse
- decltype((e)) reveals the category
basics
~20 sEvery C++ expression is exactly one of three: an lvalue names an object you keep using, an xvalue denotes an object whose resources may be reused, and a prvalue computes a value or initializes an object. std::move(s) is an xvalue.
solid answer
~40 sThe standard defines a **glvalue** as an expression whose evaluation determines the identity of an object or function, and a **prvalue** as one that initializes an object or computes an operand's value. An **xvalue** is a glvalue whose object's resources can be reused; an **lvalue** is any other glvalue; **rvalue** means prvalue or xvalue. So `s`, `*p`, `v[0]` and even `"literal"` are lvalues; `std::move(s)` and a call returning `T&&` are xvalues; `42`, `s + "!"` and a call returning `T` by value are prvalues. Categories belong to expressions, not objects or types: a variable declared `std::string&& r` is an lvalue when you name it. `decltype((e))` exposes the category - `T&` for an lvalue, `T&&` for an xvalue, plain `T` for a prvalue.
code
cpp · 25 lines#include <iostream>
#include <string>
#include <type_traits>
#include <utility>
template <class T> constexpr const char* category() {
if constexpr (std::is_lvalue_reference_v<T>) return "lvalue";
else if constexpr (std::is_rvalue_reference_v<T>) return "xvalue";
else return "prvalue";
}
#define CAT(e) std::cout << #e << " -> " << category<decltype((e))>() << '\n'
std::string make() { return "tmp"; }
int main() {
std::string s = "hi";
std::string&& r = std::move(s);
CAT(s);
CAT(r);
CAT(std::move(s));
CAT(make());
CAT(42);
CAT("literal");
CAT(s + "!");
}go deeper
Know the three names and one example of each: a variable is an lvalue, std::move(x) is an xvalue, a literal like 42 is a prvalue.
Define them by identity and reusability, name the glvalue and rvalue umbrellas, and explain that a named rvalue reference is an lvalue.
Classify tricky expressions on sight - string literals, member access on an xvalue, calls returning T&& - and prove it with decltype((e)).
Use categories to explain API behaviour to a team: why an overload set or a ref-qualified member function picks the path it does.
## Two questions classify every expression C++ sorts every expression into exactly one of three **fundamental value categories**. The draft's definitions turn on two properties: - **Does the expression have identity?** A **glvalue** ("generalized lvalue") is an expression whose evaluation determines the identity of an object, function, non-static data member or base-class relationship. You can take its address or bind a reference to that same object. - **May its resources be reused?** An **xvalue** ("eXpiring value") is a glvalue denoting an object whose resources can be reused, usually because it is near the end of its lifetime. From those two: | Category | Identity | Resources reusable | Typical examples | |---|---|---|---| | **lvalue** | yes | no | `s`, `*p`, `v[0]`, `++i`, a call returning `T&`, a string literal | | **xvalue** | yes | yes | `std::move(s)`, a call returning `T&&`, `std::move(obj).member` | | **prvalue** | no | yes | `42`, `s + "!"`, `make()` returning `T`, `i++`, `nullptr` | Two umbrella terms group them: **glvalue** = lvalue or xvalue, and **rvalue** = prvalue or xvalue. The C++ draft describes the names historically: lvalues and rvalues once meant the left and right side of an assignment, which is no longer generally true. ## Categories belong to expressions The draft insists these terms apply to **expressions, not values**. The same object can be reached through expressions of different categories: - `std::string s;` then `s` is an lvalue, while `std::move(s)` names the *same* object as an xvalue. - `std::string&& r = make();` declares a variable of rvalue reference type, yet the expression `r` is an **lvalue**: it has a name and you can use it again. The draft states the effect directly: named rvalue references are treated as lvalues and unnamed rvalue references to objects as xvalues. - A **cast to a non-reference type**, such as `std::string(s)` or `static_cast<std::string>(s)`, is a prvalue: it produces a new value rather than naming `s`. - A **string literal** such as `"literal"` is an lvalue - it is an array object with static storage - while the numeric literal `42` is a prvalue. - A member access follows its object: `make().size()` is a prvalue because `size()` returns by value, but `std::move(obj).name` is an xvalue when `name` is a non-reference data member. A function call's category comes straight from its declared return type: | Return type | Call expression | |---|---| | `T&` | lvalue | | `T&&` (object type) | xvalue | | `T` | prvalue | ## Observing the category with decltype The parenthesized form `decltype((e))` encodes the category in a type: `T&&` for an xvalue, `T&` for an lvalue, and `T` for a prvalue. A three-line trait turns that into a printable answer, as the code example for this question shows. It is the quickest way to settle an argument in code review. ## Why C++11 needed a third category Before C++11 the language had only lvalues and rvalues, and rvalues were temporaries. Move semantics needed a way to say "this named object may be pillaged" without pretending it was a temporary. The xvalue fills that slot: it keeps the object's identity, like an lvalue, so a reference can bind to the existing object, but it counts as an rvalue for overload resolution, so a `T&&` overload is preferred. Every `std::move` call lands in this category. ## How categories drive what the compiler does 1. **Reference binding.** A non-const lvalue reference binds only to lvalues; an rvalue reference binds only to rvalues; `const T&` binds to both. 2. **Overload resolution.** When both `const T&` and `T&&` overloads accept an argument, an rvalue argument prefers `T&&`. 3. **Operators.** Built-in assignment needs an lvalue on its left; `&` needs an lvalue operand, so `&42` and `&make()` do not compile. 4. **Prvalues and objects.** A prvalue is not itself an object; when one must be used as an object, the **temporary materialization conversion** creates one, and how long it lives is a separate topic covered with temporaries and lifetime extension. Remember the two questions - identity, and permission to reuse - and every row of the first table follows from them.
- Why is a string literal an lvalue when an integer literal is a prvalue?A string literal denotes an array object of `const char` with static storage duration, so it has identity: you can take its address and bind `const char (&)[N]` to it. An integer literal such as `42` denotes no object at all; it is a value that initializes whatever it is used for, which is exactly what a prvalue is.
- What category is `std::move(obj).name` when name is a std::string data member?An xvalue. A class member access designating a non-static data member of non-reference type, whose object expression is an xvalue, is itself an xvalue. That is how a moved-from aggregate lets each member be moved. If `name` were declared as a reference member, the access would be an lvalue instead.
- An lvalue is anything that can appear on the left of an assignment.
- A variable declared as std::string&& is an rvalue wherever it is used.
- Every rvalue is a temporary object created by the compiler.
- String literals are prvalues like every other literal.
- Value categories describe objects, so an object is either an lvalue or an rvalue.