我在 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/