我惊讶地发现以下代码在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;
}
Run Code Online (Sandbox Code Playgroud)
我没有惊讶地看到MinGW无法编译错误"可能没有在返回类型中定义新类型".这只是该标准的另一个Microsoft例外,还是这个合法的C++?
在N3797(C++ 14)和N3485(C++ 11)中,§8.3.5[dcl.fct]/9明确地以:
不应在返回或参数类型中定义类型.
因此,您的代码无效,GCC对于诊断它是正确的.
| 归档时间: |
|
| 查看次数: |
119 次 |
| 最近记录: |