什么标准条款要求进行左值到右值的转换?
鉴于:
int main() {
int x = 0;
int y = x; // <---
}
有人可以告诉我标准的哪个条款(首选 2003)要求将表达式 x
从 lvalue 转换为 rvalue对象y
的初始化?
(或者,如果我错了并且没有发生这样的转换,那么我也想了解这一点!)
Given:
int main() {
int x = 0;
int y = x; // <---
}
Could someone please tell me which clause of the standard (2003 preferred) mandates the conversion of the expression x
from lvalue to rvalue in the initialisation of the object y
?
(Or, if I'm mistaken and no such conversion takes place, then I'd like to learn that too!)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我发现将左值视为真实对象并将右值视为存储在对象中的值更容易(如果可能不是 100% 精确)。表达式 x 是一个左值表达式,它引用第一行中定义的对象 x ,但当用作对类型的赋值的右侧时,该类型不是用户定义的类型会读取实际值,这就是执行从左值到右值的转换的地方:读取对象的内容。
至于标准中规定转换的特定条款......好吧,我能想到的最接近的是 4.1 [conv.lvalue]/2 (左值到右值转换):
赋值右侧是右值的要求在 5.17 [expr.ass] 中是隐式的或缺失的,但事实就是如此,否则以下表达式将是错误的,因为 rhs 是右值并且没有右值到左值转换:
编辑:对于初始化,8.5 [dcl.init]/14,最后一个项目符号(指基本类型)状态(强调我的):
那里的值意味着示例中的左值表达式是读取(即转换为右值)。无论如何,前面提到赋值的段落可以应用在这里:如果初始化需要左值而不是右值,则表达式
int i = 0;< /code> 格式不正确。
I find it easier (if maybe not 100% precise) to think of lvalue-s as real objects and rvalue-s as the value stored in the object. The expression
x
is an lvalue expression that refers to the objectx
defined in the first line, but when used as the right hand side of an assignment to a type that is not a user defined type the actual value is read, and that is where the conversion from lvalue to rvalue is performed: reading the contents of the object.As to the specific clause in the standard that dictates that conversion... well, the closest that I can think is 4.1 [conv.lvalue]/2 (Lvalue to Rvalue conversion):
The requirement that the right hand side of the assignment is an rvalue is either implicit or missing from 5.17 [expr.ass], but that is the case or else the following expression would be an error since the rhs is an rvalue and there is no rvalue-to-lvalue conversion:
EDIT: For initialization, 8.5 [dcl.init]/14, last bullet (which refers to fundamental types) states (emphasis mine):
That value there means that the lvalue expression in your example is read (i.e. converted to an rvalue). At any rate the previous paragraph that referred to assignment could be applied here: if initialization required an lvalue rather than an rvalue, the expression
int i = 0;
would be ill-formed.我确实相信这在某种程度上是直观的(其他人已经说过 - 需要值,因此显然需要将对象指示符转换为包含的值其中)。我能想到的最好的办法,4p3:
请注意末尾的“当且仅当” - 其初始化器用作右值 >,因为初始化将其用作右值(转换的结果)。所以到 3.10p7
编辑:输入 4p3 的段落可以在 8.5p16 找到,最后一个项目符号:
另请注意下面的评论。
I do believe that this is intuitive to some degree (what others already said - the value is needed, so there is an obvious need to convert the object designator to the value contained therein). The best I could come up with, by 4p3:
Note the "if and only if" at the end - the initializer therefor is used as an rvalue, because the initialization uses it as an rvalue (result of the conversion). So by 3.10p7
EDIT: The paragraph for entering 4p3 can be found at 8.5p16, last bullet:
Also note the comments below.
这是您要找的吗:
§3.10/7
我认为当您编写
int y = x
时,它基本上复制了对象x
中包含的值,这是一个左值,但是 < em>value 本身是一个右值,因此上下文需要一个右值。§4.1/2 说,
也许这两句话可以解答你的疑惑。如果我的理解有误,请指正。我想学习新东西。
@Tomalak 的评论:
int &y = x
不会复制该值。它只是创建对象本身的别名。但正如我之前所说的,int y = x
,基本上是复制作为右值的值。因此,上下文需要一个右值,因为这里正在进行复制。Is this what you're looking for:
§3.10/7
And I think when you write
int y = x
, it basically copies the value contained in the objectx
which is a lvalue, but the value itself is an rvalue, hence the context expects an rvalue.§4.1/2 says,
Maybe these two quotations clarify your doubt. Correct me if my understanding is wrong. I would like to learn new things.
@Tomalak's comment:
Well
int &y = x
does NOT copy the value. It just creates an alias of the object itself. But as I previously saidint y = x
, basically copies the value which is an rvalue. Hence, the context expects an rvalue, as a copying is being done here.初始化程序具有以下语法:
在您的示例中,
x
是一个赋值表达式
,它遵循此语法产生式链:并且标识符“如果实体是左值”,则标识符“是左值”函数或变量”(5.1/4“主要表达式”)。
因此,在您的示例中,
=
右侧的表达式恰好是左值
的表达式。当然,它可以是右值
,但并非必须如此。并且没有强制的左值到右值的转换。不过,我不确定知道这一点有什么价值。
The initializer has the follwing grammar:
In your example,
x
is anassignment-expression
which follows this chain of grammar productions:And an identifier "is an lvalue if the entity is a function or variable" (5.1/4 "Primary expressions").
So in your example, the expression to the right of the
=
is an expression that happens to be anlvalue
. It could be anrvalue
of course, but it doesn't have to be. And there is no mandated lvalue-to-rvalue conversion.I'm not sure what the value in knowing this is, though.