一个联合内的不同匿名联合之间具有相同名称的字段

Fields with the same name across different anonymous unions inside one union

在一个联合内的不同匿名联合之间具有相同名称的字段是否合法?

union Foo
{
    union
    {
        int bar;
    };

    union
    {
        int bar;
    };
};

此代码无法通过 GCC 编译,但在 MSVC 中运行良好。

C++ 标准不允许这样做。任何编译此代码的编译器都是不符合规范的。

参见 10.4.1/1:

The names of the members of an anonymous union shall be distinct from the names of any other entity in the scope in which the anonymous union is declared.