是铸造的结果是一个右值吗? [英] Is the result of a cast an rvalue?

查看:114
本文介绍了是铸造的结果是一个右值吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  int a = 0; 
(int)a

不同的编译器对此代码显示不同的结果:

  #include< iostream> 
using namespace std;

void f(int& x)
{
cout<< l value< endl;
}

void f(int& x)
{
cout< r值< endl;
}

int main()
{
int a = 0;
f((int)a);
}

具有不同结果的编译器:


$ b b

1) http://cpp.sh/2r6



2) http://webcompiler.cloudapp.net/

解决方案

应该是右值,但 webcompiler 正在运行Visual Studio,Visual Studio有 允许临时对象绑定到非const常量引用的扩展 在这种情况下,它会生成一个左值的错误/扩展如Igor指出,可以使用 / Za 观看直播 ) 。



我们可以看到它应该是C ++标准草案中的右值特别是prvalue 5.4 显示类型转换(cast符号)段落


表达式(T)cast-expression的结果是类型T.如果T是一个左值引用类型或右值
对函数类型和x值的引用,如果T是对值类型的右值引用
; [注意:如果T是
a是cv限定的非类类型,当确定cv限定符的类型时,cv限定符被忽略
,否则结果是一个prvalue。产生prvalue;见3.10。 -end
note]


两者 gcc clang 导致<



另外,我建议使用

因为rextester允许您共享您的程序,所以您可以通过 webcompiler B>更新



Ben Voigt指出此错误报告,所以看起来Visual Studio实际上产生了一个值。因此,这不是简单的扩展,它允许临时对象绑定到非常量值引用。 p>

由于dyp指出 gcc曾经




$ b 更新2

> Mgetz提交了
错误报告,回复是这是通过使用 / Zc:rvalueCast标志,标志的描述如下:


当指定/ Zc:rvalueCast选项时,编译器正确地
标识一个右值引用类型作为转换的结果操作
按照C ++ 11标准。当选项不是
时,编译器的行为与Visual Studio 2012中的相同。
默认情况下,/ Zc:rvalueCast关闭。为了符合并消除使用cast的
错误,我们建议您使用/ Zc:rvalueCast。


不清楚此标记在未来版本中是否默认启用。


Let

int a = 0;

Then is (int)a an rvalue in standard C++?

Different compilers show different results for this code:

#include <iostream>
using namespace std;

void f(int& x)
{
    cout << "l value" << endl;
}

void f(int&& x)
{
    cout << "r value" << endl;
}

int main()
{
    int a = 0;
    f((int)a);
}

compilers with different results:

1) http://cpp.sh/2r6

2) http://webcompiler.cloudapp.net/

解决方案

The should be an rvalue but webcompiler is running Visual Studio and Visual Studio has an extension which allows temporary objects to be bound to non-const lvalue references. a bug/extension that casues it to generate an lvalue in this case As Igor points out above this can be disabled using /Za (see it live).

We can see that it should be an rvalue(specifically a prvalue) from the draft C++ standard section 5.4 Explicit type conversion (cast notation) paragraph 1 which says (emphasis mine):

The result of the expression (T) cast-expression is of type T. The result is an lvalue if T is an lvalue reference type or an rvalue reference to function type and an xvalue if T is an rvalue reference to object type; otherwise the result is a prvalue.[ Note: if T is a non-class type that is cv-qualified, the cv-qualifiers are ignored when determining the type of the resulting prvalue; see 3.10. —end note ]

Both gcc and clang result in rvalue which is the expected result.

As an aside, I would recommend using rextester over webcompiler since rextester allows you to share your program and also has live sharing.

Update

Ben Voigt point out this bug report and so it seems that Visual Studio actually produces an lvalue. So this is not simply a case of the extension which allows temporary objects to be bound to non-const lvalue references.

As dyp points out gcc used to have a cast to lvalue extension as well.

Update 2

Mgetz filed a bug report, the response was that this is fixed by using the /Zc:rvalueCast flag, the description of the flag is as follows:

When the /Zc:rvalueCast option is specified, the compiler correctly identifies an rvalue reference type as the result of a cast operation in accordance with the C++11 standard. When the option is not specified, the compiler behavior is the same as in Visual Studio 2012. By default, /Zc:rvalueCast is off. For conformance and to eliminate errors in the use of casts, we recommend that you use /Zc:rvalueCast.

It is unclear whether this flag will be enabled by default in future versions.

这篇关于是铸造的结果是一个右值吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆