I was surprised to discover that the following code compiles, runs, AND produces the expected output under 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;
}
I was less surprised to see MinGW fail to compile with the error "new types may not be defined in a return type". Is this just another Microsoft exception to the standard, or is this legal C++?
In N3797 (C++14) and N3485 (C++11), §8.3.5 [dcl.fct]/9 explicitly starts with:
Types shall not be defined in return or parameter types.
Thus, your code is invalid and GCC is correct to diagnose it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With