c++ 是`f().a [0]`一个x值?

栏目: C++ · 发布时间: 7年前

内容简介:http://stackoverflow.com/questions/31206988/is-f-a0-an-xvalue
struct S{
    int a[3] = {1,2,3};
};

S&& f(){return S();}

&f().a;       //[Error] taking address of xvalue (rvalue reference)
&f().a[0];    //ok in GCC 5.1.0 and Clang 3.6.0

S s;
&static_cast<S&&>(s).a;     //[Error] taking address of xvalue (rvalue reference)
&static_cast<S&&>(s).a[0];  //ok in GCC 5.1.0 and Clang 3.6.0

5.7 An expression is an xvalue if it is:

(7.1) — the result of calling a function, whether implicitly or explicitly, whose return type is an rvalue reference to object type,

(7.2) — a cast to an rvalue reference to object type,

(7.3) — a class member access expression designating a non-static data member of non-reference type in which the object expression is an xvalue, or

(7.4) — a .* pointer-to-member expression in which the first operand is an xvalue and the second operand is a pointer to data member.

5.2.1 Subscripting

A postfix expression followed by an expression in square brackets is a postfix expression. One of the expressions

shall have the type “array of T ” or “pointer to T ” and the other shall have unscoped enumeration

or integral type. The result is of type “ T ”. The type “ T ” shall be a completely-defined object type. The expression E1[E2] is identical (by definition) to *((E1)+(E2))<<*t [ Note: see 5.3 and 5.7 for details of * and

+ and 8.3.4 for details of arrays. —end note ], except that in the case of an array operand, the result is an lvalue if that operand is an lvalue and an xvalue otherwise.

那么,是f().a [0] xvalue?

我认为f().a [0]应该是一个x值.

[EDIT1]

忽略& f().和& f().a [0];因为12.2 [class.temporary] p5.2

The lifetime of a temporary bound to the returned value in a function return statement (6.6.3) is not  extended; the temporary is destroyed at the end of the full-expression in the return statement

static_cast<S&&>(s).a是一个x值(7.2和7.3).

“除了在数组操作数的情况下,如果该操作数是一个左值,另一个值则是一个左值.

所以我认为static_cast<S&&>(s).a [0]应该是一个x值,但是

&安培;的static_cast< S&安培;&安培;&将(S).A [0]; //在GCC 5.1.0和Clang 3.6.0中

追问:

我错了吗?如果我错了,给我一个例子,下标数组会产生一个x值.

据我所知,你确实是正确的,这看起来是一个“错误”,虽然这是公平的,最近改变了 CWG defect 1213

这说:

Because the subscripting operation is defined as indirection through a pointer value, the result of a subscript operator applied to an xvalue array is an lvalue, not an xvalue. This could be surprising to some.

而这一改变的部分5.2.1 [expr.sub]如下:

A postfix expression followed by an expression in square brackets is a

postfix expression. One of the expressions shall have the type

“array of T” or“pointer to T” and the other shall have unscoped enumeration or integral type. The result is an lvalue of type

“T.” The type “T” shall be a completely-defined object type.62 The

expression E1[E2] is identical (by definition) to *((E1)+(E2)) [Note:

see 5.3 [expr.unary] and 5.7 [expr.add] for details of * and + and

8.3.4 [dcl.array] for details of arrays. —end note]

, except that in the case of an array operand, the result is an lvalue if that operand

is an lvalue and an xvalue otherwise.

那么确实是f()的结果a [0];而static_cast<S&&>(s).a [0]应为x值.

这个缺陷在2012年12月之前没有提出决议, clangs defect report support 列出了该缺陷报告的支持是未知的,所以很可能实施者还没有修复这个缺陷.

更新

提出cl ang bug report: Subscript operator applied to an temporary array results in an lvalue .

http://stackoverflow.com/questions/31206988/is-f-a0-an-xvalue


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

树莓派学习指南

树莓派学习指南

[英]Peter Membrey、[澳]David Hows / 张志博、孙峻文 / 人民邮电出版社 / 2014-4 / 49.00元

树莓派(Raspberry Pi)是一款基于Linux系统的、只有一张信用卡大小的卡片式计算机。由于功能强大、性能出色、价格便宜等特点,树莓派得到了计算机硬件爱好者以及教育界的欢迎,风靡一时。 《树莓派学习指南(基于Linux)》是学习在树莓派上基于Linux进行开发的一本实践指南。全书共3个部分11章,第一部分是前两章,讲述如何设置和运行图形用户界面(GUI)。第二部分是第3章到第7章,讲......一起来看看 《树莓派学习指南》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具