C++ - lvalue vs rvalue vs xvalue vs prvalue vs glvalue
lvalue and rvalue
lvalue | rvalue |
---|---|
may appear on the left or right hand side of an assignment | only on the right hand side of an assignment |
refers to a memory location; can take the address of it via the & operator |
cannot get the address |
For example: a
, b
and c
are lvalues, while a * b
is an rvalue:
int a = 1; // a is a lvalue
int b = 2; // b is a lvalue
a = b; // ok
b = a; // ok
a = a * b; // ok
int c = a * b; // ok
a * b = 42; // error, rvalue cannot be on the left hand side of assignment
xvalue, glvalue, prvalue
An xvalue (an "eXpiring" value) refers to an object, usually near the end of its lifetime (so that its resources may be moved, for example). An xvalue is the result of certain kinds of expressions involving rvalue references. (e.g. The result of calling a function whose return type is an rvalue reference is an xvalue.)
glvalue, prvalue are rescoping lvalue and rvalue because of the intruducion of xvalue. Basically glvalue expands lvalue to include xvalue, and prvalue shinks rvalue to exclude xvalue.
- glvalue ("generalized" lvalue) = either lvalue or xvalue.
- rvalue = either prvalue ("pure" rvalues) or xvalue
To visualize the relationships:
|lvalue | xvalue | prvalue |
|<-- glvalue --> |
| <--- rvalue ---> |