26

Possible Duplicate:
What are rvalues, lvalues, xvalues, glvalues, and prvalues?

The C++ Standard, mostly in Chapter 5, entitled Expressions, defines which expressions are lvalues and which are rvalues. I have read that chapter, and I believe I can correctly distinguish between lvalues and rvalues.

However before I had read good C++ books and/or the standard, I used to think that an lvalue is something that can stand on the left side of an assignment, and an rvalue is something which cannot. Obviously there are numerous counterexamples to this naive definition. Some time later I thought that an lvalue is something which has an address, and an rvalue is something which doesn't. This too, seems to have counterexamples in the form of, say, some temporary objects, which, obviously, do have an address.

A friend of mine asked me what is an lvalue and what is an rvalue. I told him approximately what it is, he demanded a more complete answer. I told him to go read the standard. He refused to molest his brains and said he was sure there must be some necessary and sufficient condition for something to be an lvalue.

Is there?

For example, an lvalue is something that a non-const reference can bind to. But this one isn't really satisfactory. I am looking for something more obvious, something which is easy to explain, without resorting to considering each expression type...

I hope the question was clear.

9

4 Answers 4

16

Pretty simply, an rvalue is when the expression result will not survive past the end of said expression. An lvalue will. This basic principle is what enables move semantics and rvalue references- that you can modify them without issue, because you know that object's life is over.

Sign up to request clarification or add additional context in comments.

3 Comments

This is not true - you can have an lvalue that designates a temporary object, and an rvalue that designates an object of static storage duration, (among other possibilities)
@M.M, could you please provide examples of these two cases?
@user51462 for the first one, do *this in a member function called on a temporary object. For the second one, std::move(x) if x is a static object
13

I've found considering lvalues as "things with names" to be the most useful simplification. The expression ++x is an lvalue, with the name being x. By contrast, rvalue are "things without names". x++ is an rvalue, because it yields some unnamed value.

8 Comments

+1 for actually reading and understanding my question. But int& f() {int* p = new int; return *p;} What's the name of f()? :)
@Armen: 0x830D2CFF, or whatever address new int happens to yield. I never said you needed to have access to the name :-P
@Dennis: In this case can't we think of a temporaty object as one that has a name(bacause it has an address)?
stackoverflow.com/questions/6162226/what-rvalues-have-names (rvalues can have names (many have), and lvalues can miss a name (many do miss)).
@Dennis, f() is an rvalue, but not a temporary. In fact it can be argued that every temporary has an address. Every temporary is an object, and an object is a region of storage. But still there are no temporaries in C++ that have a name (but not every nameless object is a temporary!).
|
0

An l-value is something that refers to a memory location. this is the simplest thing i could say.An r-value can be an l-value An r-value is just the result of an expression or a constant

1 Comment

"An r-value can be an l-value" No; an expression is either an rvalue or an lvalue (in C++98); an lvalue is implicitly convertible to an rvalue.
0

may be you are just a little confused between modifiable lvalue and non-lvalues. rvalues can be lvalues or non-lvalues.the term modifiable lvalue is used after the introduction of const modifier(see wikipedia entry on values) in C++ every expression returns an lvalue,rvalue or no value.when an lvalue appears as a rvalue it is implicitly convertedto a rvalue(see IBM on value types)

2 Comments

No, I don't think I am confused between these notions
The const qualifier is not required to demonstrate a non-modifiable lvalue: int x[3]; here, x is a non-modifiable lvalue. "rvalues can be lvalues or non-lvalues" This is incorrect: no rvalue is an lvalue. Every expression is either an lvalue or an rvalue. There is an implicit conversion that permits an lvalue to be converted to an rvalue.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.