Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can an unnamed struct not be used as a trailing return type?

struct { int a, b; } f(int x, int y) // OK
{
    return { x, y };
}

auto g(int x, int y) -> struct { int a, b; } // error C2332
{
    return { x, y };
}

int main()
{
    auto n = f(1, 2).a; // OK
}

My compiler is VC++ 2013 RC.

Why is g wrong while f is OK?

Is this a bug of VC++?

like image 429
xmllmx Avatar asked Oct 04 '13 10:10

xmllmx


1 Answers

Actually, in C++, it's illegal to define a type in a parameter or return type, named or not. See C++11[diff.decl]:

Change: In C++, types may not be defined in return or parameter types. In C, these type definitions are allowed

So the actual problem is the first case being accepted, not the second one being rejected.

like image 141
Angew is no longer proud of SO Avatar answered Sep 30 '22 13:09

Angew is no longer proud of SO