c++ - 非常量引用绑定(bind)到临时的 Visual Studio 错误?

标签 c++ visual-studio temporary-objects

我在 gcc 中编译一些可移植代码时遇到了这个问题。基本上这个奇怪的代码在 Visual Studio 中编译,这真的让我大吃一惊:

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

Visual Studio 让这个飞起来。 gcc 会将此作为编译错误捕获。有趣的是,如果你将 Zebra 类型定义为 int,VC++ 会报错。相当矛盾的行为。想法?

最佳答案

这是 Visual Studio 的旧扩展,我可以在 Microsoft 网站上找到的唯一引用是这个错误报告:Temporary Objects Can be Bound to Non-Const References ,其示例代码如下:

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.
}

回复说明之一:

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

这篇博文:Visual C++ is so Liberal其中涵盖了此扩展说明:

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

关于c++ - 非常量引用绑定(bind)到临时的 Visual Studio 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16380966/

相关文章:

c++ - 调试信息的 clang llvm 中间表示

css - CSS 中字体开头的 @ 字符

c++ - 如何使用自然语法实现线程安全的容器?

c++ - 遗传算法和神经网络 : taking address of temporary [-fpermissive]

c++ - 我想将我的计算内容放在辅助线程中,同时将主循环保留在主线程中

c++ - Eclipse C++ 控制台打印命令 linux

c++ - 随着 std::byte 的标准化,我们什么时候使用 void* 什么时候使用 byte*?

visual-studio - Visual Studio 2010总是认为项目已经过时,但什么都没有改变

c# - 断点在 Visual Studio 2017 中的 xamarin uwp 中不起作用

c++ - pIter != cont.end() 在 for 循环中的性能