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

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

内容简介: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++ 演员的结果是高价值吗?》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

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

四步创业法

四步创业法

[美] Steven Gary Blank / 七印部落 / 华中科技大学出版社 / 2012-8-1 / 48.00

《四步创业法》获李开复推荐,是精益创业理论的奠基之作。作者Steven Gary Blank博士是硅谷资深企业家,他一共创办了八家企业,并担任多家硅谷公司的董事和创业顾问。本书总结作者25年创业经验,提出全新的客户发展方法 (诣在弥补传统产品开发方法的缺陷) ,掀起了硅谷近年精益创业 的浪潮。七印部落正在翻译作者的博客和授课视频,欢迎访问微博:http://weibo.com/7seals ......一起来看看 《四步创业法》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具