问题
The following code compiles fine with vc++ 19.00.23506 (flags: /Wall /WX /Za
) and with vc++ 19.10.25109.0 (flags: /Wall /WX /Za /permissive-
, this can be checked at http://webcompiler.cloudapp.net), but doesn't compile with clang 3.8.0 and g++ 6.3.0 (flags: -std=c++11 -Wall -Wextra -Werror -pedantic-errors
). Is it a bug in vc++ and does the standard prohibit such constructions?
struct
{
}
foo()
{
return {};
}
int main()
{
}
回答1:
MSVC appears wrong:
[dcl.fct]/9 Types shall not be defined in return or parameter types...
回答2:
You can return anonymous type, but you have to define it inside the function:
auto foo()
{
struct {} s;
return s;
}
来源:https://stackoverflow.com/questions/42888031/anonymous-struct-as-a-return-type