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

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

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

查看所有标签

猜你喜欢:

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

离散数学及其应用

离散数学及其应用

Kenneth H.Rosen / 机械工业出版社 / 2012-11 / 99.00元

本书是介绍离散数学理论和方法的经典教材,已经成为采用率最高的离散数学教材,被美国众多名校用作教材,获得了极大的成功。中文版也已被国内大学广泛采用为教材。作者参考使用教师和学生的反馈,并结合自身对教育的洞察,对第7版做了大量的改进,使其成为更有效的教学工具。. 本书可作为1至2个学期的离散数学课入门教材,适用于数学,计算机科学。计算机工程.信息技术等专业的学生。 本书特点 实例:书中有8......一起来看看 《离散数学及其应用》 这本书的介绍吧!

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

URL 编码/解码

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具