# Value categories

> Replace circular left-of-equals folklore with C++17's two orthogonal expression axes—identity and movability—plus temporary materialization and decltype probes.

- **Source:** C++ Atlas (https://cppatlas.com/topics/value-categories)
- **Track:** Value semantics
- **Estimated reading time:** 9 minutes
- **ISO C++ clauses:** [basic.lval] (https://eel.is/c++draft/basic.lval), [conv.rval] (https://eel.is/c++draft/conv.rval), [lex.string] (https://eel.is/c++draft/lex.string), [expr.ref] (https://eel.is/c++draft/expr.ref), [dcl.type.decltype] (https://eel.is/c++draft/dcl.type.decltype), [expr.pre.incr] (https://eel.is/c++draft/expr.pre.incr), [expr.post.incr] (https://eel.is/c++draft/expr.post.incr)
- **cppreference links:** [Value categories](https://en.cppreference.com/w/cpp/language/value_category), [String literal](https://en.cppreference.com/w/cpp/language/string_literal), [decltype specifier](https://en.cppreference.com/w/cpp/language/decltype), [Increment/decrement operators](https://en.cppreference.com/w/cpp/language/operator_incdec), [Copy elision](https://en.cppreference.com/w/cpp/language/copy_elision)
- **Prerequisites:** initialization-forms, casts-and-qualifiers

Traditional C textbooks define lvalues and rvalues by where they sit around an assignment operator. In modern C++, that rule collapses immediately: a `const int` cannot sit on the left of `=`, yet it is an lvalue; meanwhile, a class rvalue like `std::string{} = "ok"` is allowed on the left of `=`, yet it is a prvalue. Every expression in C++—never a variable declaration—belongs to a value category governed by two independent properties.

## Expressions have value categories, not variables

A **variable** has a declared type, such as `int`, `const Widget&`, or `Widget&&`. An **expression** appearing in code has both a non-reference type (references are stripped during expression analysis) and a **value category** (`[basic.lval]`). Keeping those two dimensions separate resolves the single biggest source of confusion around move semantics:

```cpp
#include <utility>

struct Widget {};
void consume(Widget& w);
void consume(Widget&& w);

void relay(Widget&& param) {
    // param's declared type is Widget&&, yet the expression `param` is an lvalue
    consume(param);            // calls consume(Widget&)
    consume(std::move(param)); // std::move returns unnamed Widget&& -> xvalue
}
```

Inside `relay`, `param` has a name and an addressable stack location, so the expression `param` is an **lvalue**, selecting `consume(Widget&)`. Calling `std::move(param)` produces an unnamed `Widget&&`, which is an **xvalue**, selecting `consume(Widget&&)`.

> **Pitfall.** Inside a function body, a parameter declared as `Widget&& param` has type "rvalue reference to `Widget`", yet the expression `param` itself is an lvalue because it has a name and an address.

## The two orthogonal axes: glvalue and rvalue

C++17 organizes value categories around two orthogonal questions:

1. **Has identity (`glvalue`)?** Does evaluating the expression identify a specific object, bit-field, or function whose address or identity can be compared against another?
2. **Can be moved from (`rvalue`)?** Is the expression disposable or explicitly marked expiring so its resources may be safely transferred without another observer noticing?

```text
                  expression
                 /          \
           glvalue          rvalue
           /     \          /    \
       lvalue      xvalue      prvalue
```

Combining these two axes yields three primary, mutually exclusive leaf categories, alongside two compound categories (`glvalue` = `lvalue` or `xvalue`; `rvalue` = `prvalue` or `xvalue`):

| Value category | Has identity (`glvalue`)? | Movable (`rvalue`)? | Addressable with `&`? | Representative expressions |
| --- | --- | --- | --- | --- |
| `lvalue` | Yes | No | Yes (unless bit-field) | `var`, `"cpp"`, `++i`, `func_returning_lref()` |
| `xvalue` | Yes | Yes | Not directly | `std::move(x)`, `Widget{}.member`, `func_returning_rref()` |
| `prvalue` | No | Yes | No | `42`, `Widget{1}`, `i++`, `func_returning_value()` |

> **Mental model.** Every expression falls into one of three leaves: an `lvalue` has durable identity, a `prvalue` is an unmaterialized recipe with no address yet, and an `xvalue` is an existing object whose resources are cleared for reuse.

## Prvalues since C++17: recipes, not objects

Before C++17, a prvalue of class type represented a real temporary object in memory that compilers tried to optimize away. C++17 inverted the model in `[basic.lval]` and `[conv.rval]`. A **prvalue** is no longer an object at all—it is an unmaterialized computation or **initialization recipe** waiting to construct an object directly inside whatever destination slot the caller provides.

A prvalue becomes an actual object in storage—turning into an **xvalue**—only when **temporary materialization conversion** (`[conv.rval]`) fires. Materialization happens at exact language boundaries that demand object identity:

- Binding a reference (`const Widget&` or `Widget&&`) to a prvalue.
- Accessing a non-static data member (`make_widget().id`) or invoking a member function on a prvalue.
- Subscripting a prvalue array (`using A = int[3]; A{1, 2, 3}[0]`).
- Discarding a class or array prvalue as a standalone expression statement (`make_widget();`), so its constructor and destructor still execute deterministically.

If none of those triggers occur—such as `Widget w = make_widget();`—no temporary object is ever created; the prvalue initializes `w` directly in place.

## Tricky expression taxonomy in the wild

Several everyday C++ expressions defy naive intuition until checked against `[basic.lval]`:

- **String literals vs scalar literals**: Numeric, boolean, and `nullptr` literals (`42`, `true`) are prvalues. A string literal such as `"hello"`, however, resides in static storage for the whole program and is an **lvalue** of type `const char[6]` (`[lex.string]`), which is why `&"hello"` compiles cleanly while `&42` is ill-formed.
- **Prefix vs postfix increment**: Built-in `++i` mutates `i` and returns the object itself as an lvalue (`++ ++i` is legal). Built-in `i++` yields a prvalue copy of the old value (`&(i++)` and `i++ = 5` fail to compile).
- **Member access (`E1.E2`)**: Under `[expr.ref]`, if `E1` is an rvalue (prvalue or xvalue) and `E2` names a non-static **object** member, `E1.E2` triggers materialization if needed and produces an **xvalue**. If `E2` is a static data member or a **reference** member (`int& ref;`), `E1.E2` is an **lvalue** because the member's storage outlives `E1` or belongs independently elsewhere.
- **Conditional operator (`cond ? a : b`)**: Yields an lvalue only when both `a` and `b` are lvalues of matching or reference-compatible types; otherwise it yields a prvalue (or xvalue when both match as xvalues).

> **Standardese.** Under `[lex.string]`, a string literal is an lvalue of array type `const char[N]` with static storage duration, whereas numeric, character, and pointer literals are prvalues.

## Probing value categories with decltype

Because `decltype` distinguishes unparenthesized names from general expressions (`[dcl.type.decltype]`), wrapping any expression `e` in an extra pair of parentheses `decltype((e))` turns it into an exact compile-time value-category probe:

- `decltype((e))` yields `T&` if `e` is an **lvalue**.
- `decltype((e))` yields `T&&` if `e` is an **xvalue**.
- `decltype((e))` yields `T` if `e` is a **prvalue**.

```cpp
#include <type_traits>
#include <utility>

struct Box { int value; };
struct AliasBox { int& ref; };

Box make_box();
AliasBox make_alias();

void verify_categories(int i) {
    // Unparenthesized id-expression yields declared type (int), not lvalue category
    static_assert(std::is_same_v<decltype(i), int>);
    // Parenthesized expression probes value category
    static_assert(std::is_same_v<decltype((i)), int&>);             // lvalue
    static_assert(std::is_same_v<decltype((std::move(i))), int&&>); // xvalue
    static_assert(std::is_same_v<decltype((42)), int>);             // prvalue

    static_assert(std::is_same_v<decltype(("hi")), const char (&)[3]>); // lvalue
    static_assert(std::is_same_v<decltype((++i)), int&>);               // lvalue
    static_assert(std::is_same_v<decltype((i++)), int>);                // prvalue
    static_assert(std::is_same_v<decltype((make_box().value)), int&&>);  // xvalue
    static_assert(std::is_same_v<decltype((make_alias().ref)), int&>);   // lvalue
}
```

## What to remember

- Value categories classify expressions, never variables; a named rvalue reference `T&&` evaluates as an lvalue expression.
- `lvalue` = identity without movability; `prvalue` = movability without identity (an unmaterialized initializer); `xvalue` = both identity and movability.
- Temporary materialization (`[conv.rval]`) turns a prvalue into an xvalue only when reference binding, member access, array subscripting, or discarding demands an object in storage.
- String literals are lvalues (`const char[N]`), `E1.E2` on an rvalue yields an xvalue for data members (or an lvalue for reference/static members), and `decltype((expr))` directly reports `T&`, `T&&`, or `T`.

## Concept Map Outline

- **Value categories:** Every C++ expression is classified by object identity and eligibility to be moved from.
  - **Expression vs variable:** Types belong to declarations whereas value categories describe evaluated expressions.
    - **Named T&& parameter:** A named rvalue-reference parameter has type T&& yet evaluates as an lvalue expression.
    - **Function return types:** Calls returning T, T&, and T&& produce prvalues, lvalues, and xvalues respectively.
  - **Two orthogonal axes:** Combining glvalue identity and rvalue movability partitions all expressions into three leaves.
    - **lvalue (glvalue only):** Designates an existing addressable object or function that cannot be implicitly moved.
    - **xvalue (glvalue and rvalue):** Designates an existing expiring object whose resources may be safely transferred.
    - **prvalue (rvalue only):** Represents an unmaterialized computation or initializer with no storage address yet.
  - **Temporary materialization:** C++17 converts a prvalue to an xvalue only when an operation requires object identity.
    - **Reference binding:** Binding const T& or T&& materializes a prvalue into a temporary xvalue.
    - **Member and array access:** Reading a member or subscripting a temporary array triggers materialization first.
    - **Discarded prvalue expression:** Evaluating a standalone class prvalue materializes it so constructor and destructor run.
  - **Subtle expression rules:** Several common C++ expressions break naive left-of-equals intuition.
    - **String vs numeric literals:** String literals are static lvalues of const char[N], whereas numeric literals are prvalues.
    - **Prefix vs postfix ++:** Built-in ++i returns an lvalue to i while i++ yields a prvalue copy.
    - **Member access E1.E2:** A data member on an rvalue yields an xvalue, whereas a reference member yields an lvalue.
  - **decltype probes:** Parenthesized decltype((e)) reveals lvalue, xvalue, or prvalue directly in the type system.
    - **Unparenthesized id-expression:** decltype(x) inspects the declared type of x rather than its expression category.
    - **Parenthesized expression:** decltype((e)) produces T& for lvalues, T&& for xvalues, and T for prvalues.

## Standardese Quiz Questions

### Question 1 [warmup] ([basic.lval])

Inside `void f(Widget&& w)`, what is the value category of the expression `w` when passed to `g(w)`?

```cpp
struct Widget {};
void g(Widget&);
void g(Widget&&);
void f(Widget&& w) {
    g(w);
}
```

A. xvalue, because w was declared as Widget&&
B. prvalue, because rvalue parameters hold temporaries
C. lvalue, because w is a named variable with an addressable location
D. glvalue that refuses to bind to either overload

**Answer (C):** lvalue, because w is a named variable with an addressable location

**Why:** Value categories belong to expressions, not types. Even though w has declared type Widget&&, the id-expression w names an object with identity, making it an lvalue that calls g(Widget&).

### Question 2 [warmup] ([expr.pre.incr], [expr.post.incr])

Which built-in increment expression is well-formed in C++ for `int i = 0;`?

```cpp
int i = 0;
```

A. ++ ++i
B. &(i++)
C. (i++) = 3
D. int& r = i++;

**Answer (A):** ++ ++i

**Why:** Built-in prefix ++i increments i and returns i as an lvalue, so ++ ++i compiles and increments i twice. Built-in postfix i++ produces a prvalue copy, which cannot have its address taken, be assigned to, or bind to int&.

### Question 3 [tricky] ([lex.string])

What are the value category and exact type of the string literal `"lucid"`?

```cpp
auto ptr = &"lucid";
```

A. prvalue of type const char*
B. lvalue of type const char[6]
C. prvalue of type const char[6]
D. xvalue of type const char(&)[6]

**Answer (B):** lvalue of type const char[6]

**Why:** Unlike integer or floating literals (which are prvalues), string literals have static storage duration and are lvalues of array type const char[N] including the null terminator, which is why &"lucid" compiles.

### Question 4 [tricky] ([dcl.type.decltype])

Given `int x = 5;`, what types do `decltype(x)` and `decltype((x))` deduce?

```cpp
int x = 5;
using A = decltype(x);
using B = decltype((x));
```

A. A is int, B is int
B. A is int&, B is int&
C. A is int&&, B is int&
D. A is int, B is int&

**Answer (D):** A is int, B is int&

**Why:** Under [dcl.type.decltype], decltype applied to an unparenthesized id-expression returns its declared type (int). Parenthesizing it into (x) triggers the general expression rule, yielding int& for an lvalue.

### Question 5 [standardese] ([expr.ref], [conv.rval])

Given `struct S { int m; int& r; };` and `S make();`, what are the value categories of `make().m` and `make().r`?

```cpp
struct S { int m; int& r; };
S make();
```

A. make().m is an xvalue, and make().r is an lvalue
B. make().m is a prvalue, and make().r is an xvalue
C. Both make().m and make().r are xvalues
D. Both make().m and make().r are lvalues

**Answer (A):** make().m is an xvalue, and make().r is an lvalue

**Why:** Under [expr.ref], make() first undergoes temporary materialization ([conv.rval]) to become an xvalue. A non-static data member access on an rvalue yields an xvalue when the member has object type (m), whereas a member of reference type (r) always yields an lvalue.

### Question 6 [standardese] ([basic.lval], [conv.rval])

In C++17 and later, which statement best describes a class prvalue such as `Widget{42}` before temporary materialization conversion occurs?

A. It is a temporary object allocated in caller stack memory awaiting copy elision
B. It is an unmaterialized initialization recipe with no object identity or storage address yet
C. It is an expiring glvalue whose destructor runs at the next comma operator
D. It is an lvalue stored in static read-only memory

**Answer (B):** It is an unmaterialized initialization recipe with no object identity or storage address yet

**Why:** C++17 redefined prvalues in [basic.lval] as unmaterialized value computations or initializers. A temporary object is only created if temporary materialization conversion ([conv.rval]) is triggered by reference binding, member access, or discarding.
