C++ 什么是左值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6397745/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is an lvalue?
提问by Armen Tsirunyan
Possible Duplicate:
What are rvalues, lvalues, xvalues, glvalues, and prvalues?
The C++ Standard, mostly in Chapter 5, entitled Expressions, defines which expressions are lvalues and which are rvalues. I have read that chapter, and I believe I can correctly distinguish between lvalues and rvalues.
C++ 标准,主要在第 5 章,题为表达式,定义了哪些表达式是左值,哪些是右值。我读过那一章,我相信我可以正确区分左值和右值。
However before I had read good C++ books and/or the standard, I used to think that an lvalue is something that can stand on the left side of an assignment, and an rvalue is something which cannot. Obviously there are numerous counterexamples to this naive definition. Some time later I thought that an lvalue is something which has an address, and an rvalue is something which doesn't. This too, seems to have counterexamples in the form of, say, some temporary objects, which, obviously, do have an address.
然而,在我阅读好的 C++ 书籍和/或标准之前,我曾经认为左值可以放在赋值的左侧,而右值则不能。显然,这个幼稚的定义有很多反例。一段时间后,我认为左值是有地址的东西,而右值是没有地址的东西。这似乎也有反例,例如一些临时对象,显然它们确实有地址。
A friend of mine asked me what is an lvalue and what is an rvalue. I told him approximately what it is, he demanded a more complete answer. I told him to go read the standard. He refused to molest his brains and said he was sure there must be some necessary and sufficient conditionfor something to be an lvalue.
我的一个朋友问我什么是左值,什么是右值。我告诉他大概是什么,他要求更完整的答案。我告诉他去读标准。他拒绝调戏自己的大脑,并说他确信某物成为左值必须有一些充要条件。
Is there?
在那儿?
For example, an lvalue is something that a non-const reference can bind to. But this one isn't really satisfactory. I am looking for something more obvious, something which is easy to explain, without resorting to considering each expression type...
例如,左值是非常量引用可以绑定到的东西。但是这个真的不是很满意。我正在寻找更明显的东西,易于解释的东西,而无需考虑每种表达类型......
I hope the question was clear.
我希望这个问题很清楚。
采纳答案by Puppy
Pretty simply, an rvalue is when the expression result will not survive past the end of said expression. An lvalue will. This basic principle is what enables move semantics and rvalue references- that you can modify them without issue, because you know that object's life is over.
很简单,右值是表达式结果在所述表达式结束后无法继续存在的时候。一个左值会。这个基本原则使移动语义和右值引用成为可能——你可以毫无问题地修改它们,因为你知道对象的生命已经结束。
回答by Dennis Zickefoose
I've found considering lvalues as "things with names" to be the most useful simplification. The expression ++x
is an lvalue, with the name being x
. By contrast, rvalue
are "things without names". x++
is an rvalue, because it yields some unnamed value.
我发现将左值视为“具有名称的事物”是最有用的简化。表达式++x
是一个左值,名称为x
。相比之下,rvalue
是“没有名字的东西”。 x++
是一个右值,因为它产生一些未命名的值。
回答by lovesh
An l-value is something that refers to a memory location. this is the simplest thing i could say.An r-value can be an l-value An r-value is just the result of an expression or a constant
l 值是指内存位置的东西。这是我能说的最简单的事情。r 值可以是 l 值 r 值只是表达式或常量的结果
回答by jemmanuel
may be you are just a little confused between modifiable lvalue and non-lvalues. rvalues can be lvalues or non-lvalues.the term modifiable lvalue is used after the introduction of const modifier(see wikipedia entry on values) in C++ every expression returns an lvalue,rvalue or no value.when an lvalue appears as a rvalue it is implicitly convertedto a rvalue(see IBM on value types)
可能你只是在可修改的左值和非左值之间有点困惑。右值可以是左值或非左值。在 C++ 中引入 const 修饰符后使用术语可修改左值(请参阅维基百科关于值的条目)每个表达式都返回左值、右值或无值。当左值作为右值出现时,它是隐式转换为右值(参见IBM 关于值类型)