c++ 演员的结果是高价值吗?

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

内容简介:http://stackoverflow.com/questions/26508609/is-the-result-of-a-cast-an-rvalue

int a = 0;

那么(C)是标准C中的值?

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

#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);
}

编译器具有不同的结果:

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

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

应该是一个rvalue,但是Web编译器运行Visual Studio和Visual Studio有一个 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 正如Igor指出,这可以使用/ Za( see it live

)禁用.

我们可以看到,这应该是C标准草案5.4的显着类型转换(铸造符号)第1段的重要值(特别是prvalue)(强调我的):

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 ]

gccclang 都会导致r值,这是预期的结果.

除此之外,我建议在web编译器上使用 rextester ,因为rextester可以让您共享程序,并且还可以实时共享.

更新

Ben Voigt指出这个 bug report ,所以似乎Visual Studio实际上产生了一个左值.所以这不仅仅是 extension which allows temporary objects to be bound to non-const lvalue references 的一个例子.

因为dyp也指出了 gcc used to have a cast to lvalue extension .

更新2

Mgetz提交了 bug report ,答复是这是通过使用 /Zc:rvalueCast flag 修复的,标志的描述如下:

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.

这个标志在未来的版本中是否默认启用是不清楚的.

http://stackoverflow.com/questions/26508609/is-the-result-of-a-cast-an-rvalue


以上所述就是小编给大家介绍的《c++ 演员的结果是高价值吗?》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

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

互联网思想十讲

互联网思想十讲

段永朝 / 商务印书馆 / 2014-10 / 68.00

本书是作者为北京大学新闻与传播学院硕士生开设的《互联网前沿思想》课程的讲义。作者力图从技术、经济和社会的角度,在大尺度上观察互联网究竟根植于什么样的文化土壤。作者选择了复杂性、社会网络分析、公共空间这三个维度展开分析,为读者呈现出了脱胎于工业时代的互联网继承了哪些思想,并对哪些思想做出了彻底的颠覆。一起来看看 《互联网思想十讲》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具