传统上它是“=”运算符的左侧。 然而,随着时间的推移,“左值”/“右值”的含义发生了变化。 C++ 添加了术语“不可修改的左值”,它是任何不能分配给的左值:用“const”限定的数组和变量是两个例子。 在 C 中,您不能分配给任何右值(见下文)。 同样,在 C++ 中,您不能分配给不属于某些用户定义的类类型的右值。
It's traditionally the left side of the "=" operator. However, with time, meaning of "lvalue"/"rvalue" changed. C++ added the term of a "non-modifiable lvalue" which is any lvalue that cannot assigned to: arrays and variables that are qualified with "const" are two examples. In C, you cannot assign to any rvalue (see below). Likewise, in C++, you cannot assign to rvalues that are not of some user defined class type.
You can say an "lvalue" is an expression that names an object which persists over time and occupies some location of storage. Whether or not you can assign to that expression is not important for that classification. A reference, in particular, is also an lvalue, because it has a name that persists over time. All the following are lvalues, because they all refer to named objects. Also note that a const does not have any effect on the lvalue-ness.
int a; lvalue: a;
lvalue: ++a;
int a[2]; lvalue: a;
int &ra = a; lvalue: ra;
int *pa = &a; lvalue: *pa;
The term "rvalue" is used for things like literals and enumerator values and for temporaries that do not enjoy the fun of having a long life and are destroyed right away at the end of a full expression. For rvalues, not the aspect of persistence is important, but the value-aspect. Functions in C++ are lvalues, because they are persistent and they have an address, even though they are not objects. I've left them out in the above overview of lvalues, because it's easier to grasp lvalues when first only taking objects into account. All the following are rvalues:
enum { FOO, BAR }; rvalue: BAR;
int a[2]; rvalue: (a+1);
rvalue: 42;
int a; rvalue: a++; // refering to a temporary
struct mystruct { }; mystruct f() { return mystruct(); } rvalue: f();
Incidentally, often you have an lvalue, but an operator needs an rvalue. For example the binary builtin "+" operator adds two values. An lvalue expression first and for all specifies a location where a value first has to be read out. So when you add two variables, an "lvalue to rvalue" conversion takes place. The Standard says that the value contained in an lvalue expression is its rvalue result:
int a = 0, b = 1;
int c = a + b; // read values out of the lvalues of a and b.
Other operators do not take rvalue, but lvalues. They don't read a value. An example is the address-of operator, &. You cannot take the address of an rvalue expressions. Some rvalues are not even objects: They do not occupy any storage. Examples are again, literals (10, 3.3, ...) and enumerator values.
How is that scary stuff useful?
Well it has several advantages to have the distinction of lvalue and rvalue
Allowing the compiler to omit taking storage for rvalues and using registers/readonly memory for scalar values
Flagging expressions as elusive: rvalues will not live long
Allows efficient copy semantics for the compiler internally and in c++1x also exposed to the programmer (see move semantics and rvalue references): We can steal away resources from rvalues that are going to be destroyed anyway.
Allows to build rules upon that property
rvalues are not allowed to be generated from a yet uninitialized objects where an lvalues refers to. But lvalues may refer to uninitialized objects just fine
rvalues can never be polymorphic. Their static type must also be their dynamic type: Simplifies rules for the typeid operator.
another way to determine whether an expression is an lvalue is to ask "can I take its address?". If you can, it's an lvalue. If you can't, it's an rvalue. For example, &obj , &*ptr , &ptr[index] , and &++x are all valid (even though some of those expressions are silly), while &1729 , &(x + y) , &std::string("meow") , and &x++ are all invalid. Why does this work? The address-of operator requires that its "operand shall be an lvalue" (C++03 5.3.1/2). Why does it require that? Taking the address of a persistent object is fine, but taking the address of a temporary would be extremely dangerous, because temporaries evaporate quickly.
The "L" in lvalue is usually described as standing for "location". An lvalue specifies the location of something; as another answerer pointed out, lvalues can typically have their address taken. This is why numeric literals and non-reference function return values are not lvalues.
The L used to stand for "left" until const was introduced into C++. Const lvalues cannot appear on the left hand side of an assignment.
An lvalue is an expression (with an object type other than void) that potentially designates an object [...]
It is as simple as that.
The Wikipedia definition on the day of writing this answer was almost as good
[...] a value that points to a storage location, potentially allowing new values to be assigned or value that provide accessible memory address of a variable where data can be written, it is referred as location value.
Examples of lvalue expressions? Given
int a, b[1], *c;
we can have the following lvalue expressions: a, b and c each of which definitely designate an object. b[0] designates an object as well, hence it is an lvalue expression. What about the potentiality then? *c is always an lvalue expression, but it does not designate an object if c does not hold a pointer to a valid object. b[1]could semantically designate an object but it doesn't because it accesses the array out of bounds. Both of them are lvalue expressions.
When an lvalue expression that does not actually designate an object is evaluated, the behaviour is undefined.
An lvalue expression can be more complex than that, for example:
64) The name lvalue comes originally from the assignment expression E1 = E2, in which the left operand E1 is required to be a (modifiable) lvalue. It is perhaps better considered as representing an object locator value. [...]
Notice that E1 = E2 is not required to compile for E1 to be an lvalue in ISO C. 6.3.2.1p1 continues:
A modifiable lvalue is an lvalue that does not have array type, does not have an incomplete type, does not have a const-qualified type, and if it is a structure or union, does not have any member (including, recursively, any member or element of all contained aggregates or unions) with a const-qualified type.
It used to be case in languages before C (for example B) that all locator values could appear on the left-hand side of the assignment, hence the naming. This is no longer the case in C since arrays could never have been assigned to, and ISO C added const.
P.S. The same footnote explains the related term rvalue like this:
[...] What is sometimes called rvalue is in this International Standard described as the value of an expression. [...]
在 C++ 中,返回一个值的函数调用 引用是一个左值。 否则,一个 函数调用是一个右值表达式。 在 C++ 中,每个表达式都会生成一个 左值、右值或无值。
某些运算符需要左值 他们的一些操作数。 桌子 下面列出了这些运算符和 对它们的使用有额外的限制。
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.
例如,所有赋值运算符 评估他们的正确操作数并 将该值分配给他们的左边 操作数。 左操作数必须是 可修改的左值或对 a 的引用 可修改的对象。
From this article. Since the OP was a bit lazy in asking his question (although some people disagree, see comments), I'll be lazy as well and simply paste the entire relevant part here, probably breaking some copyright laws.
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 term modifiable 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
A 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.
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.
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.
发布评论
评论(8)
左值是可以分配给以下对象的值:
它是“左值”或“左手值”的缩写,基本上就是
左侧的值>=
符号,即您分配的值。作为非左值(即仅右值)的示例:
该代码不起作用,因为
printf()
(返回int< 的函数) /code>) 不能是左值,只能是右值。
An lvalue is a value that can be assigned to:
It's short for "left value" or "lefthand value" and it's basically just the value on the left of the
=
sign, i.e. the value you assign something to.As an example of what is not an lvalue (i.e rvalue only):
That code doesn't work because
printf()
(a function that returns anint
) cannot be an lvalue, only an rvalue.传统上它是“=”运算符的左侧。 然而,随着时间的推移,“左值”/“右值”的含义发生了变化。 C++ 添加了术语“不可修改的左值”,它是任何不能分配给的左值:用“const”限定的数组和变量是两个例子。 在 C 中,您不能分配给任何右值(见下文)。 同样,在 C++ 中,您不能分配给不属于某些用户定义的类类型的右值。
您可以说“左值”是一个表达式,它命名一个随时间持续存在并占用某些存储位置的对象。 是否可以分配给该表达式对于该分类并不重要。 特别是,引用也是左值,因为它的名称会随着时间的推移而持续存在。 以下所有内容都是左值,因为它们都引用命名对象。 另请注意,
const
对左值没有任何影响。术语“右值”用于诸如文字和枚举值之类的东西,以及那些不享受长寿命的乐趣并在完整表达式结束时立即被销毁的临时值。 对于右值来说,重要的不是持久性方面,而是值方面。 C++ 中的函数是左值,因为它们是持久的并且有地址,即使它们不是对象。 我在上面的左值概述中将它们排除在外,因为首先仅考虑对象时更容易掌握左值。 以下所有内容都是右值:
顺便说一句,通常您有一个左值,但运算符需要一个右值。 例如,二进制内置“+”运算符将两个值相加。 左值表达式首先指定必须首先读出值的位置。 因此,当您添加两个变量时,就会发生“左值到右值”的转换。 标准规定左值表达式中包含的值是其右值结果:
其他运算符不采用右值,而是采用左值。 他们不读取值。 一个例子是取址运算符
&
。 您不能获取右值表达式的地址。 有些右值甚至不是对象:它们不占用任何存储空间。 例如,文字 (10, 3.3, ...) 和枚举值。那些可怕的东西有什么用?
区分左值和右值有几个优点
...还有更多,我感觉...
It's traditionally the left side of the "=" operator. However, with time, meaning of "lvalue"/"rvalue" changed. C++ added the term of a "non-modifiable lvalue" which is any lvalue that cannot assigned to: arrays and variables that are qualified with "const" are two examples. In C, you cannot assign to any rvalue (see below). Likewise, in C++, you cannot assign to rvalues that are not of some user defined class type.
You can say an "lvalue" is an expression that names an object which persists over time and occupies some location of storage. Whether or not you can assign to that expression is not important for that classification. A reference, in particular, is also an lvalue, because it has a name that persists over time. All the following are lvalues, because they all refer to named objects. Also note that a
const
does not have any effect on the lvalue-ness.The term "rvalue" is used for things like literals and enumerator values and for temporaries that do not enjoy the fun of having a long life and are destroyed right away at the end of a full expression. For rvalues, not the aspect of persistence is important, but the value-aspect. Functions in C++ are lvalues, because they are persistent and they have an address, even though they are not objects. I've left them out in the above overview of lvalues, because it's easier to grasp lvalues when first only taking objects into account. All the following are rvalues:
Incidentally, often you have an lvalue, but an operator needs an rvalue. For example the binary builtin "+" operator adds two values. An lvalue expression first and for all specifies a location where a value first has to be read out. So when you add two variables, an "lvalue to rvalue" conversion takes place. The Standard says that the value contained in an lvalue expression is its rvalue result:
Other operators do not take rvalue, but lvalues. They don't read a value. An example is the address-of operator,
&
. You cannot take the address of an rvalue expressions. Some rvalues are not even objects: They do not occupy any storage. Examples are again, literals (10, 3.3, ...) and enumerator values.How is that scary stuff useful?
Well it has several advantages to have the distinction of lvalue and rvalue
... There is more to it, i feel it ...
出现在作业左侧的东西,即可以分配给的东西。
请注意,在 C++ 中,函数调用可能是左值 if:
then:
Something that appears on the left hand side of an assignment i.e. something that can be assigned to.
Note that in C++ a function call may be an lvalue if:
then:
我所知道的最好的解释之一可以在 这篇关于 RValue 引用的文章。
One of the best explanations I know of can be found in this article on RValue references.
左值中的“L”通常被描述为代表“位置”。 左值指定某物的位置; 正如另一位回答者指出的那样,左值通常可以获取其地址。 这就是为什么数字文字和非引用函数返回值不是左值。
在 const 被引入 C++ 之前,L 曾经代表“左”。 Const 左值不能出现在赋值的左侧。
The "L" in lvalue is usually described as standing for "location". An lvalue specifies the location of something; as another answerer pointed out, lvalues can typically have their address taken. This is why numeric literals and non-reference function return values are not lvalues.
The L used to stand for "left" until const was introduced into C++. Const lvalues cannot appear on the left hand side of an assignment.
对于 C 编程语言 C11 草案 n1570 6.3.2.1p1 简单地说:
就这么简单。
撰写此答案之日的维基百科定义几乎同样好
左值表达式的示例? 假设
我们可以有以下左值表达式:
a
、b
和c
,其中每个明确指定一个对象。b[0]
也指定一个对象,因此它是一个左值表达式。 那么潜力呢?*c
始终是左值表达式,但如果c
不包含指向有效对象的指针,则它不会指定对象。b[1]
可以在语义上指定一个对象,但事实并非如此,因为它访问数组时越界了。 它们都是左值表达式。当计算实际上并不指定对象的左值表达式时,行为是未定义。
左值表达式可以比这更复杂,例如:
如果它可以编译,那么它就是左值表达式。
基本上,您可以在 C 中应用
&
(地址运算符)的所有内容都是左值,函数除外,因为函数在 C 中不是对象。那么 L 代表什么呢?
C11 n1570 脚注 64:
请注意,将
E1
编译为 ISO C 中的左值不需要E1 = E2
。6.3.2.1p1 继续:在 C 之前的语言(例如 B)中,所有定位器值都可能出现在赋值的左侧,因此命名。 在 C 中不再是这种情况,因为永远无法对数组进行赋值,并且 ISO C 添加了
const
。PS 相同的脚注解释了相关术语rvalue,如下所示:
For the C programming language C11 draft n1570 6.3.2.1p1 puts it succinctly:
It is as simple as that.
The Wikipedia definition on the day of writing this answer was almost as good
Examples of lvalue expressions? Given
we can have the following lvalue expressions:
a
,b
andc
each of which definitely designate an object.b[0]
designates an object as well, hence it is an lvalue expression. What about the potentiality then?*c
is always an lvalue expression, but it does not designate an object ifc
does not hold a pointer to a valid object.b[1]
could semantically designate an object but it doesn't because it accesses the array out of bounds. Both of them are lvalue expressions.When an lvalue expression that does not actually designate an object is evaluated, the behaviour is undefined.
An lvalue expression can be more complex than that, for example:
if it compiles, it is an lvalue expression.
Basically everything that you can apply the
&
(address-of operator) to in C is an lvalue, except functions, since functions are not objects in C.So what does the L then stand for?
C11 n1570 Footnote 64:
Notice that
E1 = E2
is not required to compile forE1
to be an lvalue in ISO C. 6.3.2.1p1 continues:It used to be case in languages before C (for example B) that all locator values could appear on the left-hand side of the assignment, hence the naming. This is no longer the case in C since arrays could never have been assigned to, and ISO C added
const
.P.S. The same footnote explains the related term rvalue like this:
一个绝对不是左值的简单例子:
A simple example of what is definitly not an lvalue:
从这个 文章。 由于OP有点懒惰地提出他的问题(尽管有些人不同意,请参阅评论),我也会很懒,只是将整个相关部分粘贴在这里,可能会违反一些版权法。
From this article. Since the OP was a bit lazy in asking his question (although some people disagree, see comments), I'll be lazy as well and simply paste the entire relevant part here, probably breaking some copyright laws.