惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
博客园 - 司徒正美
宝玉的分享
宝玉的分享
阮一峰的网络日志
阮一峰的网络日志
The Cloudflare Blog
月光博客
月光博客
博客园 - 【当耐特】
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园 - 三生石上(FineUI控件)
博客园 - 聂微东
小众软件
小众软件
Hugging Face - Blog
Hugging Face - Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
Apple Machine Learning Research
Apple Machine Learning Research
V
V2EX
Jina AI
Jina AI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
罗磊的独立博客
雷峰网
雷峰网
博客园 - 叶小钗
量子位
IT之家
IT之家

博客园 - On the way

做产品16年,我有9条心得--百度贴吧前负责人 toB的产品经理和toc产品经理区别 Stack overflow 编译能通过,运行时出现Stack overflow VC2008操作Excel2007总结 C++中使用DOM写XML文档 VC++ MSXML创建XML文件以及对XML文档解析 MFC中的MainFrame Dlg,App,Doc,View的关系 于typedef的用法总结 C++/CLI与C#常用语法对比 讲讲volatile的作用 利用C++/CLI 封装Native C++ 提升.NET web game性能 规格单位换算 C/C++中宏使用总结 Working with XML in a Classic COM Application - On the way 位图和矢量图区别 WIKI扫盲手册 C#的显式接口和隐式接口 软件设计的七大原则 ASP.NET下MVC设计模式的实现
理解lvalue和rvalue
On the way · 2011-11-16 · via 博客园 - On the way

Lvalues and Rvalues

An object is a region of storage that can be examined and stored into. An lvalue is an expression that refers to such an object. An lvalue does not necessarily permit modification of the object it designates. For example, a const object is an lvalue that cannot be modified. The termmodifiable lvalue is used to emphasize that the lvalue allows the designated object to be changed as well as examined. The following object types are lvalues, but not modifiable lvalues:

  • An array type
  • An incomplete type
  • const-qualified type
  • An object is a structure or union type and one of its members has a const-qualified type

Because these lvalues are not modifiable, they cannot appear on the left side of an assignment statement.

The term rvalue refers to a data value that is stored at some address in memory. An rvalue is an expression that cannot have a value assigned to it. Both a literal constant and a variable can serve as an rvalue. When an lvalue appears in a context that requires an rvalue, the lvalue is implicitly converted to an rvalue. The reverse, however, is not true: an rvalue cannot be converted to an lvalue. Rvalues always have complete types or the void type.

C ISO C defines a function designator as an expression that has function type A function designator is distinct from an object type or an lvalue. It can be the name of a function or the result of dereferencing a function pointer. The C language also differentiates between its treatment of a function pointer and an object pointer.

C++ On the other hand, in C++, a function call that returns a reference is an lvalue. Otherwise, a function call is an rvalue expression. In C++, every expression produces an lvalue, an rvalue, or no value.

In both C and C++, certain operators require lvalues for some of their operands. The table below lists these operators and additional constraints on their usage. 

Operator Requirement
& (unary) Operand must be an lvalue.
++ -- Operand must be an lvalue. This applies to both prefix and postfix forms.
= += -= *= %= <<= >>= &= ^= |= Left operand must be an lvalue.

For example, all assignment operators evaluate their right operand and assign that value to their left operand. The left operand must be a modifiable lvalue or a reference to a modifiable object.

The address operator (&) requires an lvalue as an operand while the increment (++) and the decrement (--) operators require a modifiable lvalue as an operand. The following example shows expressions and their corresponding lvalues. 

Expression Lvalue
x = 42 x
*ptr = newvalue *ptr
a++ a
C++int& f() 
The function call to f()

Mac OS X C The remainder of this section is platform-specific and pertains to C only.

When compiled with the GNU C language extensions enabled, compound expressions, conditional expressions, and casts are allowed as lvalues, provided that their operands are lvalues. The use of this language extension is deprecated for C++ code.

A compound expression can be assigned if the last expression in the sequence is an lvalue. The following expressions are equivalent:

(x + 1, y) *= 42; x + 1, (y *=42);

The address operator can be applied to a compound expression, provided the last expression in the sequence is an lvalue. The following expressions are equivalent:

&(x + 1, y); x + 1, &y;

A conditional expression can be a valid lvalue if its type is not void and both of its branches for true and false are valid lvalues. Casts are valid lvalues if the operand is an lvalue. The primary restriction is that you cannot take the address of an lvalue cast.

---------------------------------------------------

今天看C++模板的资料,里面说到lvalue,rvalue的问题,这个问题以前也看到过,也查过相关资料,但是没有考虑得很深,只知道rvalue不能取地址,不能赋值等等一些规则。今天则突然有了更深层次的理解(也可以说是顿悟,耗时不过几秒钟),记录下来。

下面是我对这两个单词字面的意思的猜测:

  • lvalue估计来源于left value。 在赋值语句中lvalue = rvalue;位置处于左边。就是可以修改的值。
  • rvalue估计来源于right value。处于赋值语句右边,是只读的不可修改的值。

接下来是我所悟到内容的详细分析

  • lvalue是可以赋值的,说明它是一个变量,它在内存中一定存在,一定有地址。所以&lvalue是有效的,能取到在内存中的地址。

    访问lvalue一定会导致CPU访问存储器(相对较慢的操作)。

    lvalue的例子:

    1. int a;  
    2. a = 10;   
    3. int* p = &a;   
    4. &a = 0;   
  • rvalue是不可以赋值的,它不是一个变量,在内存中没有存在,没有地址。它要么是存在于CPU的寄存器中,要么是存在于指令中(立即数)。所以只要对rvalue取地址,那么就一定是错误的(编译器会抱怨的)。

    访问rvalue不会导致CPU访问存储器(对立即数和寄存器的访问很快)。

    rvalue的例子:

    1. int a;  
    2. a = 10;   
    3. 10 = a;   
    4.   
    5. int foo()  
    6. {  
    7.     return 0;  
    8. }  
    9. int b = foo();   
    10. int* p = &foo();   
    11. void bar(int& i)  
    12. {  
    13. }  
    14. bar(foo());   
  • 函数的返回值是rvalue,对于返回int, char 等这样最基本的类型,是通过CPU寄存器返回的,因此返回值没有地址是可以理解的。但是如果函数返回的是一个用户自定义类型的对象,肯定不可能通过寄存器来返回这个对象值的(寄存器大小数量都有限,对象的大小可以非常大),那究竟是怎样返回对象的呢?
    1. class UDT  
    2. {  
    3.   int data[100];  
    4. public:  
    5.   UDT()  
    6.   {  
    7.     printf("construct/n");  
    8.   }  
    9.   BBB& operator = (BBB& )  
    10.   {  
    11.     printf("operator =/n");  
    12.     return *this;  
    13.   }  
    14. };  
    15. UDT foo()  
    16. {  
    17.   return UDT();  
    18. }  
    19. void main()  
    20. {  
    21.   UDT obj = foo();  
    22. }  
    23.   
    24. construct  

    带着疑问,我查了查vc编译出来的代码,原来obj这个局部变量的地址被压入了堆栈,foo函数内部以堆栈上的obj地址作为this指针调用了UDT的构造函数。噢,难怪执行UDT obj = foo();这个语句只有调用了一次构造函数,而没有调用operator =,这都是因为函数返回值必须是rvalue这个规则所带来的好处,如果返回值是一个lvalue,那么这个语句一定会调用operator = 运算符。

http://blog.csdn.net/rogerhe/article/details/6410993