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.
int x = 5; x; // lvalue x + 1; // prvalue std::move(x); // xvalue
Try it
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.
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
Tgives a prvalue,T&an lvalue,T&&an xvalue. - Is it a cast?
static_cast<T&&>(e)is an xvalue (that is allstd::movedoes); a cast toT&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;
thisis a prvalue. - Member access:
obj.mis an lvalue ifobjis an lvalue, an xvalue ifobjis an xvalue, andptr->mis 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, usestd::move(arg)orstd::forward<T>(arg)to pass it on as an rvalue. See Forwarding references. - Reference binding: non-
constlvalue references bind only to lvalues;constlvalue 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)vsdecltype((x)): the first gives the declared type of the namex, the second treats it as an expression and returnsT&for an lvalue,T&&for an xvalue, andTfor a prvalue.std::movedoes not move. It casts to an xvalue so that an overload takingT&&is selected. The move happens (or not) in that overload. Applying it to aconstobject yieldsconst T&&, which usually picks the copy constructor.- Ref-qualified member functions (
&and&&after the parameter list) overload on the category of*this, asGadget::nameshows above. - Pre-C++17 differences: the old model had prvalues as true temporaries and copy elision as an optional optimization.