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.
An lvalue (locator value) represents an object that occupies some identifiable location in memory (i.e. has an address). rvalues are defined by exclusion. Every expression is either an lvalue or an rvalue, so, an rvalue is an expression that does not represent an object occupying some identifiable location in memory.
An lvalue is an expression that yields an object reference, such as a variable name, an array subscript reference, a dereferenced pointer, or a function call that returns a reference. An lvalue always has a defined region of storage, so you can take its address.
An lvalue refers to an object that persists beyond a single expression. An rvalue is a temporary value that does not persist beyond the expression that uses it.
“l-value” refers to a memory location that identifies an object. “r-value” refers to the data value that is stored at some address in memory. References in C++ are nothing but the alternative to the already existing variable. They are declared using the '&' before the name of the variable.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With