非const引用绑定到临时,Visual Studio错误? [英] Non-const reference bound to temporary, Visual Studio bug?

查看:128
本文介绍了非const引用绑定到临时,Visual Studio错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到这个,而编译一些便携式代码在 gcc 。基本上,这个奇怪的代码编译在Visual studio,这真的只是让我的脑海:

I ran into this while compiling some portable code in gcc. Basically this strange code compiles in Visual studio which really just blows my mind:

class Zebra {int x;};
Zebra goo() {Zebra z; return z;}
void foo(Zebra &x)
{
    Zebra y;
    x = y;
    foo(goo());
}

Visual studio 这一个飞。 gcc 会将此捕获为编译错误。有趣的是,如果你输入typedef Zebra to int, VC ++ 会抱怨。相当矛盾的行为。

Visual studio lets this one fly. gcc will catch this as a compile error. Interestingly, If you typedef Zebra to int, VC++ will complain. Quite contradictory behavior. Thoughts?

推荐答案

这是Visual Studio的旧扩展,我在Microsoft网站上找到的唯一参考是这个错误报告: 临时对象可以绑定到非Const References ,其中有以下示例代码:

This is old extension to Visual Studio, the only reference I could find on the Microsoft site was this bug report: Temporary Objects Can be Bound to Non-Const References, which has the following example code:

struct A {};

A     f1();
void f2(A&);

int main()
{
    f2(f1()); // This line SHALL trigger an error, but it can be compiled without any     errors or warnings.
}

其中一个回应说明:


有一个4级警告(如果你传递/ W4到编译器,则启用4级警告)

There is a level 4 warning (level 4 warning are enabled if you pass /W4 to the compiler) for it

这篇博客文章: Visual C ++是这样的Liberal ,它涵盖了这个扩展,注意到: p>

This blog post: Visual C++ is so Liberal which covers this extension notes that:


使用禁用语言扩展(/ Za)会出现错误:

Using Disable Language Extensions (/Za) makes it an error:

这篇关于非const引用绑定到临时,Visual Studio错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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