Value Categories in C++

· updated · 4 min read

Three macros are enough to ask the compiler for the value category of any expression:

#include <type_traits>

#define IS_LVALUE(expr)  (std::is_lvalue_reference<decltype((expr))>{})
#define IS_XVALUE(expr)  (std::is_rvalue_reference<decltype((expr))>{})
#define IS_PRVALUE(expr) (!std::is_reference<decltype((expr))>{})

By the end of this post it should be clear why they work, and why the doubled parentheses are not a typo.

Categories belong to expressions#

The single most useful fact about value categories is that they describe expressions, not values or objects. The variable x is not an lvalue; the expression x is. The same object can be reached through an lvalue in one place and an xvalue in another.

Every expression has exactly one of three primary categories:

  • an lvalue, such as x, *p or "hello";
  • a prvalue (“pure rvalue”), such as 3, x + 1 or std::string("hi");
  • an xvalue (“expiring value”), such as std::move(x).

Two umbrella terms group them: a glvalue is an lvalue or an xvalue, and an rvalue is a prvalue or an xvalue.

The old intuition still works for the first split. You can write x = 3 but not 3 = x, so x, which may appear on the left of an assignment, is an lvalue, and 3 is not. The modern categories add a second question. A glvalue refers to an object or function that already exists, so it has an identity. A prvalue does not refer to anything; it is a recipe for a value, and it produces an object only when it is used to initialize one. Among the glvalues, the xvalues are the ones whose resources may be taken, because the code has declared that it no longer needs them.

Temporary materialization#

The case that makes xvalues click is temporary materialization:

struct S { int m; };

int i = S().m;

S() is a prvalue, and no S object exists yet. Member access, however, needs an object to reach into. In the C++17 model, the compiler resolves this by materializing a temporary: it creates an S from the prvalue and gives you an xvalue that refers to it. S().m is therefore an xvalue as well. The same conversion happens whenever a prvalue meets something that needs an object, such as a reference binding or a call to a member function. The cppreference page on implicit conversions spells out the rule.

This is the real distinction between the two kinds of rvalue. A prvalue is not an object until something needs one. An xvalue names an object that exists right now and is about to be discarded. There is nothing to reuse in the number 3, but the buffer owned by a temporary std::string is worth stealing, and that is exactly what move constructors do with xvalues.

Asking decltype#

Applied to an expression of type T, decltype encodes the category in the type it returns:

Categorydecltype yields
lvalueT&
xvalueT&&
prvalueT

There is one exception. For an unparenthesized name, decltype reports the declared type of the entity instead. Given int x = 3;, decltype(x) is int, even though x is an lvalue. cppreference states the rule plainly:

If the argument is an unparenthesized id-expression or an unparenthesized class member access expression, then decltype yields the type of the entity named by this expression.

Adding parentheses turns the name into an ordinary expression, so decltype((x)) is int&. That is why the macros wrap their argument twice. Here they are in action; every assertion holds with GCC and Clang in C++17 mode:

#include <string>
#include <utility>

struct S { int m; };

int main()
{
    int x = 3;
    static_assert(IS_LVALUE(x));
    static_assert(IS_LVALUE("hello"));             // string literals are lvalues
    static_assert(IS_PRVALUE(3));
    static_assert(IS_PRVALUE(x + 1));
    static_assert(IS_PRVALUE(std::string("hi")));
    static_assert(IS_XVALUE(std::move(x)));
    static_assert(IS_XVALUE(S().m));               // via temporary materialization
}

What std::move actually does#

Despite its name, std::move moves nothing. It is a cast: std::move(t) is exactly static_cast<std::remove_reference_t<T>&&>(t), an xvalue that refers to t. Its only effect is to change the category of an expression, which in turn changes which overload is chosen. When the chosen overload is a move constructor or move assignment operator, that function does the moving.

Read std::move(x) as a promise rather than an action: “I am done with x; take whatever you can use.”

Because it is only a cast, std::move can quietly do nothing useful. Applied to a const object it produces a const T&&, which cannot bind to the move constructor’s T&& but binds happily to the copy constructor’s const T&. The code compiles, and it copies:

const std::string a = make_string();
std::string b = std::move(a);   // copies: a is const

Clang-Tidy’s performance-move-const-arg check flags this pattern.

A C++23 twist#

C++23 changed the category of one familiar expression. Under P2266, “Simpler implicit move”, a local variable or parameter named in a return statement is an xvalue rather than an lvalue. Earlier standards already moved from such variables when returning by value; C++23 goes further and changes the category itself. So this now compiles:

int&& forward_it(int&& x) { return x; }   // C++20: error, x is an lvalue

because x in the return statement is an xvalue, and an rvalue reference can bind to it.

It also changes what decltype(auto) deduces, since the parenthesized (x) in a return statement is now an xvalue too:

decltype(auto) g() { int x = 0; return (x); }
// C++20: g() returns int&   (an lvalue reference to a dead local)
// C++23: g() returns int&&  (an rvalue reference to a dead local)

Both versions return a dangling reference, which is the real lesson: never write return (x); in a function declared decltype(auto). GCC and Clang both implement the C++23 rule in -std=c++23 mode.

In one table#

CategoryHas identityResources may be takenTypical examplesdecltype((e))
lvalueyesnox, *p, a[i], "hi"T&
xvalueyesyesstd::move(x), S().m, a local in return x; (C++23)T&&
prvaluenoyes3, x + 1, S()T