Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Types and Language Concepts

Value categories: lvalue, prvalue, xvalue

Concept page on C++ value categories: lvalues, prvalues, xvalues, glvalues, how to tell them apart, decltype rules and their role in move semantics.

Every C++ expression has a type and a value category. The type says what kind of value it is (int, std::string); the category says what kind of expression it is and how it may be used: can you take its address, may you bind a non-const reference to it, may its resources be stolen? Overload resolution, reference binding, decltype and move semantics all depend on the category, not just the type.

Since C++17 there are three primary categories. An lvalue has identity and is not about to expire (a named variable, *p, a[i], a string literal). A prvalue ("pure rvalue") is a temporary result with no identity that initializes something (42, a + b, a call returning by value). An xvalue ("expiring value") has identity but may be moved from (std::move(x), a call returning T&&). Two groups combine them: glvalue = lvalue or xvalue, rvalue = prvalue or xvalue.

C++
int x = 5;
x;              // lvalue
x + 1;          // prvalue
std::move(x);   // xvalue

Try it

C++ Seeing the category with decltype

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

C++ Categories drive overload selection

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

How to tell them apart

  • Can you take the address with &? If yes, it is an lvalue (or at least a glvalue). Prvalues have no address: &(x + 1) is an error.
  • Does it name something? Variables, parameters, members of named objects, dereferenced pointers, subscripts and function names are lvalues.
  • Is it a function call? Returning T gives a prvalue, T& an lvalue, T&& an xvalue.
  • Is it a cast? static_cast<T&&>(e) is an xvalue (that is all std::move does); a cast to T& is an lvalue; a cast to a non-reference type is a prvalue.
  • Is it a literal? Most literals are prvalues; string literals are lvalues; this is a prvalue.
  • Member access: obj.m is an lvalue if obj is an lvalue, an xvalue if obj is an xvalue, and ptr->m is always an lvalue.

Usage notes

  • A name is always an lvalue, even if its type is an rvalue reference. Inside a function taking T&& arg, use std::move(arg) or std::forward<T>(arg) to pass it on as an rvalue. See Forwarding references.
  • Reference binding: non-const lvalue references bind only to lvalues; const lvalue references bind to anything; rvalue references bind to rvalues (prvalues and xvalues), not to lvalues.
  • Temporary materialization (C++17): a prvalue is not an object; it only becomes a temporary object when needed, for example when bound to a reference or when a member is accessed. That is why returning by value from a function with a prvalue involves no copy or move (see Copy elision).
  • decltype(x) vs decltype((x)): the first gives the declared type of the name x, the second treats it as an expression and returns T& for an lvalue, T&& for an xvalue, and T for a prvalue.
  • std::move does not move. It casts to an xvalue so that an overload taking T&& is selected. The move happens (or not) in that overload. Applying it to a const object yields const T&&, which usually picks the copy constructor.
  • Ref-qualified member functions (& and && after the parameter list) overload on the category of *this, as Gadget::name shows above.
  • Pre-C++17 differences: the old model had prvalues as true temporaries and copy elision as an optional optimization.