Question

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 .

Was it helpful?

Solution

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. If T 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 type T and is not an object of a type derived from T, or if the object is uninitialized, a program that necessitates this conversion has undefined behavior. If T is a non-class type, the type of the prvalue is the cv-unqualified version of T. Otherwise, the type of the prvalue is T.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top