加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – decltype((A {}.int_member))的正确结果是什么?

发布时间:2020-12-16 05:29:37 所属栏目:百科 来源:网络整理
导读:给定类型A的定义: struct A { int i; }; 根据规范[expr.ref](我用n4618): (if E2 is non-reference,) …If E1 is an lvalue,then E1.E2 is an lvalue; otherwise E1.E2 is an xvalue … 显然A {}.我是x值; 也给了[dcl.type.simple]: (for decltype(e) ,)
给定类型A的定义:
struct A { int i; };

根据规范[expr.ref](我用n4618):

(if E2 is non-reference,) …If E1 is an lvalue,then E1.E2 is an lvalue; otherwise E1.E2 is an xvalue

显然A {}.我是x值;
也给了[dcl.type.simple]:

(for decltype(e),) — … if e is an unparenthesized id-expression or an unparenthesized class member access…
otherwise,if e is an xvalue,decltype(e) is T&&,where T is the type of e

因此,decltype((A {}.i))将产生int&&

然而,我尝试过GCC5.1和Clang3.9,它们产生int,而vs2015u3产生int&&哪个是对的?

解决方法

INT&安培;&安培;是正确的.

你在[expr.ref]中引用的措辞在几年前改变了cwg 616,并没有被实施立即采用;看到我的答案here.基本上,编译器不得不同时采用DR 616和临时表达式的文档,或者它们会破坏一个代码,其中我们绑定对对象的成员引用的对象的终生扩展是必需的.在旧的实现模式中,只有prvalue可以指定终身延长是可行的对象(尽管约翰内斯指出,但是在N3918之前,这是一个模糊的措辞,所以…).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读