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

查看所有标签

猜你喜欢:

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

jQuery 技术内幕

jQuery 技术内幕

高云 / 机械工业出版社 / 2014-1-1 / 99元

本书首先通过“总体架构”梳理了各个模块的分类、功能和依赖关系,让大家对jQuery的工作原理有大致的印象;进而通过“构造 jQuery 对象”章节分析了构造函数 jQuery() 的各种用法和内部构造过程;接着详细分析了底层支持模块的源码实现,包括:选择器 Sizzle、异步队列 Deferred、数据缓存 Data、队列 Queue、浏览器功能测试 Support;最后详细分析了功能模块的源码实......一起来看看 《jQuery 技术内幕》 这本书的介绍吧!

URL 编码/解码
URL 编码/解码

URL 编码/解码

SHA 加密
SHA 加密

SHA 加密工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具