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


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

查看所有标签

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

计算几何

计算几何

周培德 / 2008-7 / 69.00元

《计算几何:算法设计与分析(第3版)》系统地介绍了计算几何中的基本概念、求解诸多问题的算法及复杂性分析,概括了求解几何问题所特有的许多思想方法、几何结构与数据结构。全书共分11章,包括:预备知识,几何查找(检索),多边形,凸壳及其应用,Voronoi图、三角剖分及其应用,交与并及其应用,多边形的获取及相关问题,几何体的划分与等分、算法的运动规划、几何拓扑网络设计、随机几何算法与并行几何算法等。一起来看看 《计算几何》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

在线进制转换器
在线进制转换器

各进制数互转换器

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具