在看GCC的文檔的時候,看到一個詞lvalue,查了金山詞霸其釋義為 lvalue [計] 左值。因為的確在介紹編譯原理的課程中聽過這個詞,大致知道其意思就沒有多想。但是看完GCC文檔的這個篇幅,都無法明白全篇在說什么。問題還是出在了lvalue這個詞的“左值”是什么意思的理解上了。再找M-W字典,卻告知沒有這個詞。于是google了一把,的確很多地方都稱其為左值,我仍然不得要領。在一個百科網站About Site上找到該詞的準確釋義,摘貼如下:
Definition: C and C++ have the notion of lvalues and rvalues associated with variables and constants. The rvalue is the data value of the variable, that is, what information it contains. The "r" in rvalue can be thought of as "read" value. A variable also has an associated lvalue. The "l" in lvalue can be though of as location, meaning that a variable has a location that data or information can be put into. This is contrasted with a constant. A constant has some data value, that is an rvalue. But, it cannot be written to. It does not have an lvalue.
Another view of these terms is that objects with an rvalue, namely a variable or a constant can appear on the right hand side of a statement. They have some data value that can be manipulated. Only objects with an lvalue, such as variable, can appear on the left hand side of a statement. An object must be addressable to store a value.
Here are two examples.
int x;
x = 5; // This is fine, 5 is an rvalue, x can be an lvalue.
5 = x; // This is illegal. A literal constant such as 5 is not
// addressable. It cannot be a lvalue.
這段就說的很明白 lvalue中的l其實指的表示該值的存儲地址屬性,而另外一個相對的詞rvalue值中的r指得是read的屬性,和左右根本沒有任何關系。
C和C++里面的lvalue 和 rvalue的釋義
更新時間: 2008-04-17 14:07:42來源: 粵嵌教育瀏覽量:1001