【发布时间】:2015-04-13 15:21:53
【问题描述】:
我惊讶地发现以下代码在 MSVC 下编译、运行并产生预期的输出:
#include <iostream>
using namespace std;
struct Foo{
int _x;
Foo(int x): _x(x) {}
} //Note: no semi-colon after class definition.
//Makes this behave as a return type for the following function:
Foo_factory(int x)
{return Foo(x);}
int main (int argc, char* argv[])
{
Foo foo = Foo_factory(42);
cout << foo._x << endl; //Prints "42"
return 0;
}
看到 MinGW 无法编译并出现错误“新类型可能未在返回类型中定义”,我并不感到惊讶。这只是标准的另一个 Microsoft 例外,还是合法的 C++?
【问题讨论】:
-
您可以禁用语言扩展并找出答案 ;)
-
这在 C99 (demo) 中是允许的,但在 C++14 中是不允许的。
标签: c++ visual-c++