user2231
user2231

Reputation: 21

Example of expression evaluating to an lvalue

Is there any expression that can be evaluated to give an lvalue? I believe expressions yield an rvalue which can be assigned to an lvalue.

Upvotes: 2

Views: 255

Answers (4)

Andrew White
Andrew White

Reputation: 53496

Gabe is correct, but more specifically, any expression that results in a reference is an lvalue. But that's not all, pointer deferences and array access are both expressions that result in an lvalue.

Upvotes: 2

Tony Delroy
Tony Delroy

Reputation: 106086

Yet another example:

(which ? x : y) = 10;

Upvotes: 1

Eugen Constantin Dinca
Eugen Constantin Dinca

Reputation: 9130

Another example would be a function that returns a reference, i.e. std::vector's index operator (reference operator[]( size_type n) so you can do something like this:

std::vector< Z > vector;
vector[ y ] = z;

Upvotes: 1

Gabe
Gabe

Reputation: 86718

If you have x[y] = z;, x[y] is the lvalue and z is the rvalue. Clearly x[y] is an expression, and it can be evaluated. Therefore it can be evaluated to give an lvalue.

Upvotes: 7

Related Questions