请在这里澄清左值和右值的概念
Please clarify the concept of lvalue and rvalue here
我有下面给出的程序
#include<stdio.h>
int main()
{
int i=5;
(++(++i));
}
此程序在 C++ 中编译良好,但在 C 中编译失败。我也无法真正理解。但我尝试阅读和搜索,发现这是因为预增量运算符 returns 在 c 中的右值和在 c++ 中的左值。
如果我将 (++(++i))
更改为 (++(i++))
,那么在 C 和 C++ 中编译都会失败,因为 post-increment 总是 returns 右值。
即使阅读了一些内容,我也不清楚左值和右值在这里的确切含义。谁能通俗地给我解释一下这些是什么?
右值本质上是原始值或操作的临时结果,不应对其进行操作。因此,不允许对这些进行预增量或 post 增量操作。
左值是指存储的对象、函数或原语的传统值,可以对其进行操作或调用。
在你的第一行:int i=5; // i is an lvalue, 5 is an rvalue
因此,++(i++)
正在转换为 ++6
,这实际上是编译器所抱怨的。
顺便说一句,这里已经回答了这个问题:What are rvalues, lvalues, xvalues, glvalues, and prvalues?
"An lvalue (locator value) represents an object that occupies some identifiable location in memory (i.e. has an address).
rvalues are defined by exclusion, by saying that every expression is either an lvalue or an rvalue. Therefore, from the above definition of lvalue, an rvalue is an expression that does not represent an object occupying some identifiable location in memory."
参考:http://eli.thegreenplace.net/2011/12/15/understanding-lvalues-and-rvalues-in-c-and-c
根据这个定义,
post-增量 i++
将不再起作用,因为返回的表达式不再位于 i
中,因为它已递增。
同时 ++i
返回的表达式 returns 对递增变量的引用 i
在 c 中,后缀或前缀 ++
运算符要求操作数是可修改的左值。两个运算符都执行左值转换,因此对象不再是左值。
C++ 还要求前缀 ++
运算符的操作数是可修改的左值,但前缀 ++
运算符的结果是左值。后缀 ++
运算符不是这种情况。
因此 (++(++i));
编译为第二个操作获得左值,但 (++(i++))
没有。
"lvalue" and "rvalue" are so named because of where each
of them can appear in an assignment operation. An
lvalue
can appear on the left side of an assignment operator,
whereas an rvalue can appear on the right side.
As an example:
int a;
a = 3;
In the second line, "a" is the lvalue, and "3" is the rvalue.
in this example:
int a, b;
a = 4;
b = a;
In the third line of that example, "b" is the lvalue, and "a" is
the rvalue, whereas it was the lvalue in line 2. This
illustrates an important point: An lvalue can also be an
rvalue, but an rvalue can never be an lvalue.
Another definition of lvalue is "a place where a value can
be stored."
我有下面给出的程序
#include<stdio.h>
int main()
{
int i=5;
(++(++i));
}
此程序在 C++ 中编译良好,但在 C 中编译失败。我也无法真正理解。但我尝试阅读和搜索,发现这是因为预增量运算符 returns 在 c 中的右值和在 c++ 中的左值。
如果我将 (++(++i))
更改为 (++(i++))
,那么在 C 和 C++ 中编译都会失败,因为 post-increment 总是 returns 右值。
即使阅读了一些内容,我也不清楚左值和右值在这里的确切含义。谁能通俗地给我解释一下这些是什么?
右值本质上是原始值或操作的临时结果,不应对其进行操作。因此,不允许对这些进行预增量或 post 增量操作。
左值是指存储的对象、函数或原语的传统值,可以对其进行操作或调用。
在你的第一行:int i=5; // i is an lvalue, 5 is an rvalue
因此,++(i++)
正在转换为 ++6
,这实际上是编译器所抱怨的。
顺便说一句,这里已经回答了这个问题:What are rvalues, lvalues, xvalues, glvalues, and prvalues?
"An lvalue (locator value) represents an object that occupies some identifiable location in memory (i.e. has an address).
rvalues are defined by exclusion, by saying that every expression is either an lvalue or an rvalue. Therefore, from the above definition of lvalue, an rvalue is an expression that does not represent an object occupying some identifiable location in memory."
参考:http://eli.thegreenplace.net/2011/12/15/understanding-lvalues-and-rvalues-in-c-and-c
根据这个定义,
post-增量 i++
将不再起作用,因为返回的表达式不再位于 i
中,因为它已递增。
同时 ++i
返回的表达式 returns 对递增变量的引用 i
在 c 中,后缀或前缀 ++
运算符要求操作数是可修改的左值。两个运算符都执行左值转换,因此对象不再是左值。
C++ 还要求前缀 ++
运算符的操作数是可修改的左值,但前缀 ++
运算符的结果是左值。后缀 ++
运算符不是这种情况。
因此 (++(++i));
编译为第二个操作获得左值,但 (++(i++))
没有。
"lvalue" and "rvalue" are so named because of where each
of them can appear in an assignment operation. An
lvalue
can appear on the left side of an assignment operator,
whereas an rvalue can appear on the right side.
As an example:
int a;
a = 3;
In the second line, "a" is the lvalue, and "3" is the rvalue.
in this example:
int a, b;
a = 4;
b = a;
In the third line of that example, "b" is the lvalue, and "a" is
the rvalue, whereas it was the lvalue in line 2. This
illustrates an important point: An lvalue can also be an
rvalue, but an rvalue can never be an lvalue.
Another definition of lvalue is "a place where a value can
be stored."