本文介绍了f()。a [0]是一个xvalue吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  



查询:



解决方案

据我所知确实正确,这看起来是错误 ,尽管为了公平起见,最近使用表示:

,而第5.2.1节[expr.sub]更改如下:

所以 f()。a [0]; 和 static_cast< ; S&>(s).a [0] 应该是xvalues。



此缺陷直到2012年12月和将该缺陷报告的支持列为未知,因此实现者很可能尚未修复此缺陷。



更新



提起叮当声。


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

So, is f().a[0] an xvalue?

I think f().a[0] should be an xvalue.


[Edit1]

Ignoring &f().a; and &f().a[0]; because 12.2[class.temporary]p5.2

static_cast<S&&>(s).a is an xvalue(7.2 and 7.3).

" except that in the case of an array operand, the result is an lvalue if that operand is an lvalue and an xvalue otherwise."

So I think static_cast<S&&>(s).a[0] should be an xvalue, but

&static_cast<S&&>(s).a[0]; //ok in GCC 5.1.0 and Clang 3.6.0

Questing:

Am I wrong? If I am wrong, show me an example that subscripting an array results an xvalue.

解决方案

As far as I can tell you are indeed correct, this looks a "bug", although to be fair this changed recently with CWG defect 1213 which says:

and this changed section 5.2.1 [expr.sub] as follows:

So indeed the result of f().a[0]; and static_cast<S&&>(s).a[0] should be xvalues.

This defect did not have a proposed resolution until December 2012 and clangs defect report support lists the support of that defect report as unknown so most likely the implementers have not gotten to fixing this defect yet.

Update

Filed a clang bug report: Subscript operator applied to an temporary array results in an lvalue.

这篇关于f()。a [0]是一个xvalue吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-13 15:50