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


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

查看所有标签

猜你喜欢:

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

小圈子·大社交

小圈子·大社交

Paul Adams / 王志慧 / 人民邮电出版社 / 2013-1 / 29.00元

网络正在脱离以内容为核心构建的方式,转向以人为核心重新构建。这样深远的变革将影响我们制定商业策略、设计以及营销和广告的方式。 本书作者先后在谷歌和Facebook供职,对于社交网络有深入的研究和丰富的实战经验。他以学术界和工业界最新的调查研究为基础,阐述了人们如何通过社交圈子相互联系的规律,探讨了理念和品牌信息如何通过社交网络传播开来的过程。书中介绍了许多实际的例子,通过这些鲜活的实例,你将......一起来看看 《小圈子·大社交》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

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

在线图片转Base64编码工具

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具