【发布时间】:2017-10-31 16:51:00
【问题描述】:
我试图理解并阅读右值不能转换为左值。下面的就好了
int x = 1; // x is an lvalue
int y = 2; // y is an lvalue
int z = x + y; // the "+" needs rvalues, so x and y are converted to rvalues
//(from lvalues) and an rvalue is returned
如果我这样做了
x = 5;
y = 6;
x 和 y 不是转换为左值(从右值)吗?
编辑:What are rvalues, lvalues, xvalues, glvalues, and prvalues? 中的问题几乎没有提及转换,也没有示例可以解释。
【问题讨论】:
-
不,它们是从右值分配给的。
-
在
x + y中,x和y表示的对象被复制到函数int operator+(int, int)的实际参数中。 “x 和 y 转换为右值”不正确 -
@Caleth 我遇到这个的地方是eli.thegreenplace.net/2011/12/15/…
-
x和y不要更改。 表达式x始终是int类型的左值表达式。在右值上下文中使用x的值