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

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

内容简介: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


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

查看所有标签

猜你喜欢:

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

Head First Rails

Head First Rails

David Griffiths / O'Reilly Media / 2008-12-30 / USD 49.99

Figure its about time that you hop on the Ruby on Rails bandwagon? You've heard that it'll increase your productivity exponentially, and allow you to created full fledged web applications with minimal......一起来看看 《Head First Rails》 这本书的介绍吧!

MD5 加密
MD5 加密

MD5 加密工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具