Reputation: 2973
int x = 8;
int y = x ;
Here how a lvalue can be act as rvalue ? I know this is a silly question , but i just want to make my concepts clear on rvalue and lvalue .
Upvotes: 0
Views: 313
Reputation: 4713
An lvalue is any value that can appear on the left hand side of an assignment. Something that can be assigned to, like the variable x
in this case. Rvalues are things that have a value, like 8
or getting the value of x
. You can think of lvalues as a subset of rvalues, that is all lvalues can have their value queried and used as an rvalue.
Upvotes: 2
Reputation: 791421
Through an lvalue to rvalue conversion.
You can use an lvalue almost anywhere where an rvalue is required and an implicit lvalue to rvalue conversion will occur automatically.
Informally this conversion is "evaluating" or "taking the value of" the object that the lvalue refers to. This isn't strictly true in all cases; in unevaluated contexts, such as the operand of sizeof
, the value of the object won't be used.
ISO/IEC 14882:2011 4.1 [conv.lval]:
A glvalue (3.10) of a non-function, non-array type
T
can be converted to a prvalue. IfT
is an incomplete type, a program that necessitates this conversion is ill-formed. If the object to which the glvalue refers is not an object of typeT
and is not an object of a type derived fromT
, or if the object is uninitialized, a program that necessitates this conversion has undefined behavior. IfT
is a non-class type, the type of the prvalue is the cv-unqualified version ofT
. Otherwise, the type of the prvalue isT
.
Upvotes: 3