什么标准子句强制这种左值到右值转换? [英] What standard clause mandates this lvalue-to-rvalue conversion?

查看:228
本文介绍了什么标准子句强制这种左值到右值转换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定:

  int main(){
int x = 0;
int y = x; //< ---
}

有人可以告诉我标准(2003年推荐)要求在对象初始化过程中将表达式 x lvalue 转换为 rvalue code> y ?



(或者,如果我误以为没有这样的转换, )

解决方案

我觉得更容易(如果可能不是100%精确)认为lvalue-对象和rvalue-s作为存储在对象中的值。表达式 x 是一个左值表达式,引用第一行中定义的对象 x ,但当用作对于不是用户定义类型的类型的赋值的右侧,读取实际的,并且执行从lvalue到rvalue的转换:读取对象的内容。 / p>

对于标准中规定转换的特定子句...好吧,我可以认为最接近的是4.1 [conv.lvalue] / 2(Lvalue to Rvalue转换):


由左值表示的对象中包含的值是右值结果。


< blockquote>

要求赋值的右边是一个右值是隐含的或从5.17 [expr.ass]中缺失,但是这是case或者下面的表达式是一个错误,因为rhs是一个右值,没有rvalue-to-lvalue转换:

  int x = 5; 

编辑:对于初始化,8.5 [dcl.init] / 14,最后一个项目符号类型)状态(强调我):



  • 否则,被初始化的对象的初始值是可能已转换)初始值表达式的。 [...]

    这意味着lvalue您的示例中的表达式是读取(即转换为右值)。无论如何,前面提到赋值的段落可以在这里应用:如果初始化需要一个值,而不是一个值,表达式 int i = 0; 会形成错误。


    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!)

    解决方案

    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 object x 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 value contained in the object indicated by the lvalue is the rvalue result.

    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:

    int x = 5;
    

    EDIT: For initialization, 8.5 [dcl.init]/14, last bullet (which refers to fundamental types) states (emphasis mine):

    • Otherwise, the initial value of the object being initialized is the (possibly converted) value of the initializer expression. [...]

    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.

    这篇关于什么标准子句强制这种左值到右值转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆