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开发者_Go百科. 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.
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.
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.
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
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)
精彩评论