Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Is it legal to have fields with the same name across different anonymous unions inside one union?

union Foo
{
    union
    {
        int bar;
    };

    union
    {
        int bar;
    };
};

This code fails to compile by GCC but works fine in MSVC.

like image 604
FrozenHeart Avatar asked Nov 14 '18 22:11

FrozenHeart


Video Answer


1 Answers

This is not allowed by C++ standard. Any compiler which compiles this code is non-conformant.

See 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.

like image 184
SergeyA Avatar answered Sep 20 '22 11:09

SergeyA