Left value right
Left value (LVALUE) and Right (RVALUE) are two very basic concepts in programming, but it is also very easy to misunderstood, read a lot of articles, self-feeling really tailored this question, there is no very thorough article. So self-discourse and try. If the concept of left value right is not very clear, they will jump like blocking the road, so that you will be annoying, just like playing computer games, there are always a few mines to test your tolerance, if once It's better to sweep all mines. :)
Left value (LVALUE) and Right (RVALUE) first come from compilation theory (thank you southern size PROGRAMS). In the C language, two values located on both sides of the assignment operator, the left side is called the left value, and the right value is called right. such as:
INT II = 5; // II is the left value, 5 is right value
INT jj = II; // jj is the left value, II is right value
The above shows that the left value can be used as the right value, but it is otherwise. The earliest difference between the left value and the right value is whether it can be changed. The left value can be changed, the right value cannot be changed. [Note 1]
Note 1: This is a pig sheep in C , which is no longer established. The arch pig game is still very fun, I really caught several times, but I really have a good risk. :)
In many articles, in C , the left value is more referring to the value, that is, the value of the address, and the right value has no address. [Note 2]
Note 2: This is still inaccurate, I generate a temporary right at :: vector (), can you say that it doesn't address it? Is it a ghost or ghost without a flesh? It is address, and it is also an absolute right value.
In modern C , now the left and right values have already lost their original meaning, and an object is specified for the left value expression, with the specific name and reference (Pointer or Reference). Non-left value is right. I will come to the next definition:
The left value must have a specific name to reference this value in the program.
Right value indicator There is no specific name in the program to reference this value.
Can they be changed if they can be changed, and there is no relationship in the stack or heap.
1. Left value
In the code below:
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 the return value of the function MAX, such as Max (II, JJ), [Note 3] The return value of Fun (II) is left value. They are all values for specific reference names. II, JJ, A [0], * (A 3), Max (II, JJ), Fun (II) are their names.
Note 3: There is a blind spot that is less than clear here. That is, someone will ask MAX (8, 9) to reach the left value or the right value, the C standard specified constant reference can be referenced to the right, so Max (8, 9) seems to be right, but regardless of It is the left value, or the right value, we can't try to change it. In order to be consistent with the previous concept, I think it is the left value, and the constant constant is not changed. If the left value can be changed, that is, the left value modified by Const, such as JJ, Max (II, JJ) that is trapped by the constant magic.
The left value that is not trapped by Const is of course changed, such as the following code is established:
II = 600;
a [0] = 700;
Fun (ii) = 800; // ok!
Our eyes have no problem, FUN (II) = 800; it is completely correct because it is the left value that can be changed. So we look at the STL source code, you will understand why the return value of the overload Operator [] operator in the std :: vector is written because Operator [] must return the left value.
2. Right value
The value without a specific name is right value. First 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 the function fun1 fun1 (), the return value of the function fun2 is all right values, and their values are not specific. Name to reference. Maybe someone will be strange, fun2 () is also right? Isn't the front MAX (A, b) not left value?
Please clearly, the return value of the function fun2 is Pointer, and Pointer is also call by value, and the return value of the function MAX is Reference, and Reference is call by reference. So introducing Reference in C is not just for convenience, it is also a must. [Note 4]
Note 4: The terms of the "More Effective C " written by Scott Meyer 1 specifically talked about the difference between Pointer and Reference, very well written, distinguished.
FUN2 () is right value, but * fun2 () is the left value, just like the * P that is often seen, so when you look at the C library code, you will find the function return value of the overload Operator * is REFERENCE.
Of course, I still miss a right value, that is, literal value, such as 5, 8.23, 'a', etc., etc., is right value.
When the right value is initially emerged, a maximum feature is not changeable. But just like our moral standards, the times are different, the standards have changed, and the previous three has already been thrown into the history of garbage.
There is a right value that can be changed in C , and this feature is also very useful. That is the temporary object generated by the user-defined class (Class) constructor. such as:
Std :: Vector (9), std :: deve (), ... is all right values that can be changed. The Page51 page of the "More Exceptional C " in Herb Sutter has such a few lines of code: // Example 7-2 (b): The Right Way to Shrink-to-Fit A Vector.
Vector
// ... now c.capacity ()> = 10000 ...
// Erase All But The First 10 Elements
C.RASE (C. Segin () 10, C.End ());
// The Following Line Does Shrink C's
// Internal Buffer To Fit (or Close)
Vector
// ... now c.capacity () == C.SIZE (), or OR
// perhaps a little more more c.size ()
Seriously see a few times, you will find that the size of the vector is increased to a certain extent, you don't need so many spaces, you will find a way to shrink it to the most suitable size, but use other ways, such as calling member functions RESERVE () can't be done, this time you must use the right value to change this nature.
Vector
First, use the replication constructor to generate a temporary right vector
Note 5: This time this temporary right value has changed.
If you still don't understand, you can look at the book, or look directly at the source code of the library.
As for why? I think about it, I think so, we read the data layout structure of class (Class), will find that each of its data members have a name, I want the compiler to generate an externally in the process of compiling. Unknown references to this temporary object right value, but when you need to change this temporary object, this name is used. such as:
Class Point
{
Public: // Purely for convenience, I am open to data members, try not to use this in reality
INT X, Y, Z;
... // Other Member Functions
}
We can now change the right value and use anonymous reference name.
Point (). X = 6; // change the right value
Point (). Y = 6; // agreed to change the right value, but not, this right is not the same.
to sum up
The real difference between the left value and the right value I think is these, the left value indicates a specific name reference, and the right value does not have a specific name reference. Of course, I will still have negligence. I hope everyone can remind me that I am not enough.
I saw a new article from Herb Sutter from the message (I subscribed to his new article email notification), one is telling the TUPLE data structure, there is nothing new, it seems to have seen it, there is also a name :( Mostly) Private, address is http://www.cuj.com/Documents/s=8273/20jcexp2107sutter/ The content itself is not deep, but after reading the article, I found that I can see it everywhere C wave, what will be called Sleeve Qiankun, a plot of sensual understanding of dripping sea. In the next article, I want to talk from the general perspective, from my own experience, how to find a job in the IT industry, I want all readers to have some thinking, not just job seekers. I have already thought about it, just call "playing the tiger to eat pigs", but now I have some other things to be busy, so you may let everyone wait a few days.
Please indicate the source, thank you!
Wu Tong wrote in 2003.6.20
Recently modified 2003.6.21