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


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

查看所有标签

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

腾讯方法

腾讯方法

潘东燕、王晓明 / 机械工业出版社 / 2014-12-11 / 39.00

这是国内第一本深度讲述腾讯产品研发与团队转型的书。本书介绍了腾讯三个不同生命周期的产品的开发过程,包括如何踏足新领域开发新产品;如何救活一个即将半路夭折的产品;如何让一个老产品持续盈利。本书呈现了互联网产品开发时会遇到普遍问题和解决方法,涉及大企业如何内部创业,并迅速组建新的项目团队;如何实现跨部门的合作;在面临新团队和紧急开发任务时如何提高团队沟通效率;在产品研发方面,如何定位产品、如何敏捷开发......一起来看看 《腾讯方法》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具