For some time now, two terms have been appearing on some blogs that I don't quite understand: Rvalue
and Lvalue
. Without being clear about the meaning of both terms, the content of certain examples becomes complicated to understand.
- What differences are there between a
Rvalue
and aLvalue
? - How can each be identified?
- What can and cannot be done with each of them?
There is a short and fairly clear explanation in this MSDN article . I translate (emphasis mine):
All C++ expressions are either an lvalue or an rvalue . An lvalue is an object that persists beyond an expression . You can think of an lvalue as an object that has a name. All variables, including hard-coded (const) variables, are lvalues .
An rvalue is a temporary value that does not persist beyond the expression in which it is used .
To better understand the difference between lvalue and rvalue , consider the following example:
Here
x
it is an lvalue because it persists beyond the expression that defines it. The expression 3+4 is an rvalue because it evaluates to a temporary value that doesn't persist beyond the expression.Here are some examples of incorrect uses of lvalues and rvalues :
TL;DR (not part of original): You can assign a value to an lvalue (if it's not
const
). Not to an rvalue , but you can use it as the value you assign to an lvalue .