我在Visual C++ 17编译器上有以下代码:
#include "stdafx.h"
class Foo {};
Foo FuncBar()
{
return Foo();
}
int main()
{
Foo &myFoo = FuncBar();
}
通常情况下,将临时变量分配给左值应该是非法的。在这里,我返回了一个临时的Foo()并将其绑定到一个左值引用上。 然而,这段代码可以编译并且运行良好。为什么会允许这样做呢?
我在Visual C++ 17编译器上有以下代码:
#include "stdafx.h"
class Foo {};
Foo FuncBar()
{
return Foo();
}
int main()
{
Foo &myFoo = FuncBar();
}
这是一个微软C++扩展。您可以使用/Za标志使编译失败。