Anonymous struct as a return type

我是研究僧i 提交于 2019-12-11 03:03:19

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!