Link: http://blog.csdn.net/csdnji/article/details/169200
The left value (lvalue) and right value (Rvalue) are two very basic concepts in programming, but they are also very easy to misunderstand. I have read many articles, I feel that I have not seen the articles that have thoroughly explained this question, so I am brave enough to give it a try. If the concept of the left and right values is not very clear, they will jump out as soon as they are blocked, so you will be bored, just like playing computer games, there are always a few mines to test your patience at a time, and it would be nice to sweep all mines at a time. :)
The left and right values are first derived from the compilation theory (thanks to programs of Lily ). In the C language, it indicates two values on both sides of the value assignment operator. The value on the left is the left value, and the value on the right is the right value. For example:
Int II = 5; // II is the left value, 5 is the right value int JJ = II; // JJ is the left value, and II is the right value
The above shows that the Left value can certainly be used as the right value, but the opposite is not true. The earliest difference between the left value and the right value is whether it can be changed. The left value can be changed, and the right value cannot be changed. [NOTE 1]
NOTE 1: In C ++, pig is discolored and no longer valid. The gongpig game is quite fun. I have been lucky for several times, but it is really dangerous. :)
As mentioned in many articles, in C ++, the left value can be located, that is, the address value, but the right value has no address. [NOTE 2]
NOTE 2: This is still not accurate. I generate a temporary right value STD: vector () in the program. Can you say it has no address? Is it a ghost or ghost without a body? It has an address, and it is also an absolute right value.
In modern c ++, the left and right values have basically lost their original meaning. For a left-value expression, you can use the specific name and reference (pointer or reference) to specify an object. The non-left value is the right value. Next definition:
The left value indicates that a specific name must be referenced in the program.
The right value indicates that no specific name in the program references this value.
It has nothing to do with whether they can be changed or whether there is an address in the stack or heap.
1. Left
In the following code:
int ii = 5;int const jj = ii;int a[5];a[0] = 100;*(a+3) = 200;int const& max( int const& a, int const& b ) //call by reference{ return a > b ? a : b;}int& fun(int& a) //call by reference{ a += 5; return a;}
II, JJ, a [0], * (a + 3), and return values of function max, such as Max (II, JJ ), [NOTE 3] The values of fun (ii) returned by the function are left values ., They are all values with Specific Reference names. II, JJ, a [0], * (a + 3), max (II, JJ), and fun (ii) are their names respectively.
NOTE 3: There is a blind spot that is not easy to identify. That is, someone will ask whether the value of max (8, 9) reaches the Left or Right value. The reference to const can be referenced to the right value, so Max (8, 9) seems to be the right value, but neither the left value nor the right value can be changed. To be consistent with the previous concept, I think it is the left value of a constant that cannot be changed.
The left value cannot be changed, that is, the left value modified by const. For example, the JJ and max (II, JJ) values above are the left values trapped by the const curse.
The left value that is not trapped by const can be changed. For example, the following code is true:
ii = 600;a[0] = 700;fun(ii) = 800; //OK!
Our eyes are fine, fun (ii) = 800; completely correct, because it is the left value that can be changed. So when we look at the source code of STL, we will understand why the return value of the overloaded operator [] Operator in STD: vector is written as a reference, because operator [] must return the left value.
2. Right Value
The value without a specific name is the right value. Let's take a look at the following code:
std::list();std::string(“It is a rvalue!”);int fun1() //call by value{ …}int* fun2() //call by reference{ …}
STD: List (), STD: string ("it is a rvalue !"), The return value of function fun1 is fun1 (). The return value of function fun2 () is the right value, and their values are not referenced by specific names. Maybe someone will be surprised that fun2 () is also the right value? Isn't the top max (a, B) the left value?
It is clear that the return value of function fun2 is pointer and pointer is call by value, while the return value of function max is reference and reference is call by reference. Therefore, the introduction of reference in C ++ is not only for convenience, but also a necessity. [NOTE 4]
Note 4: Clause 1 of more effective C ++ written by Scott Meyer specifically describes the differences between pointer and reference. It is well written and clearly identified.
Fun2 () is the right value, but * fun2 () is the left value, which is the same as * P that is often seen. So when you look at the C ++ library code, the Return Value of the operator * function is reference.
Of course, I also missed a right value, that is, the literal value (literal), for example, 5, 8.23, 'A', and so on are all right values.
When the right value first appears, the biggest feature is unchangeable. However, just like our moral standards, the times are different, and standards have changed. The previous three-outline five often had been thrown into the garbage of history.
C ++ has the right value that can be changed, and this feature is very useful. This is the temporary object generated by the user-defined class constructor. For example:
STD: vector (9), STD: deque (),...... Are the right values that can be changed. There are several lines of code on page page51 of article 7 in more than tional C ++ of herb Sutter:
// Example 7-2(b): The right way to shrink-to-fit a vector.vector<Customer> c( 10000 );
// ...now c.capacity() >= 10000...
// erase all but the first 10 elementsc.erase( c.begin()+10, c.end() );
// the following line does shrink c‘s
// internal buffer to fit (or close)vector<Customer>( c ).swap( c );// ...now c.capacity() == c.size(), or
// perhaps a little more than c.size()
After reading it several times, you will find that when the vector size increases to a certain extent and you do not need so much space, you will find a way to shrink it to the most appropriate size, however, other methods, such as calling the member function reserve (), cannot be used. In this case, the right value must be used to change this nature.
Vector <customer> (c). Swap (c); this line of code is a little eye-catching.
First, use the copy constructor to generate a temporary right value vector <customer> (c). the right value is exactly the right size, and then swap it with C [Note 5 ], c becomes the proper size, and at the end of the entire expression, the temporary right value destructor returns the memory space. Really elegant gentleman!
Note 5: the temporary right value changes.
If you do not understand it, you can read the book or directly view the source code of the library.
Why? I thought for a moment. I think this is the case. When we look at the data arrangement structure of a class, we will find that every data member of the class has a name, I think the compiler will generate a Name Reference to the right value of the temporary object that is not known externally during compilation, but when you need to change this temporary object, this name is used. For example:
Class Point {public: // purely for convenience, I publish data members. In reality, try not to use int x, y, z ;...... // Other member functions };
Now we can change the right value and use the anonymous reference name.
Point (). X = 6; // the right point () is changed (). y = 6; // agree to change the right value, but note that this right value is not the same as above.
Summary
The real difference between the left value and the right value is that the Left value indicates that a specific name reference exists, and the right value does not have a specific name reference. Of course, I still have some negligence. I hope you can remind me to correct my shortcomings.
Two days ago, I read a new article sent by Herb Sutter from the email (I subscribe to his new article email notification). One article is about the tuple data structure and there is nothing new. I have read it before, there is also a name is :( mostly) Private, address for the http://www.cuj.com/documents/s=8273/cujcexp2107sutter/ content itself is not deep, but after reading the article, found everywhere C ++ of the treasure clouds, and what is the sleeves of Qian Kun, dripping water, Tibet, and the sea have a more perceptual knowledge.
In the next article, I want to talk about how college graduates are looking for work in the IT industry from a different perspective. I want to give all readers some ideas, not just job seekers. I have already thought about the topic, and it is called "dress up as a tiger and eat a pig". But now I have something to do, so I may ask you to wait for a few days.
Please indicate the source for reprinting. Thank you!
Wu Tong wrote on 2003.6.20
Last modified: 2003.6.21
Left value right value ()