C和C++裏面的lvalue 和 rvalue的釋義

C和C++裏面的lvalue 和 rvalue的釋義

在看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的屬性,和左右根本沒有任何關係。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章