Why do I get the anonymous type warning for this code?

2.2k Views Asked by At

When compiling my project I get the warning anonymous types declared in an anonymous union are an extension [-Wnested-anon-types]. My code contains this union:

union
{
    uint32_t m_bits;  // All bits
    struct
    {
      uint32_t significand : 23;
      uint32_t exponent : 8;
      uint32_t sign : 1;
    } IEEE;
  };

As far as other answers on the site have said, I would only expect this warning if I omitted the IEEE, name from the struct. But currently the struct should not be an anonymous type?

1

There are 1 best solutions below

6
On BEST ANSWER

Because the standard says so ([class.union.anon]):

Note: Nested types, anonymous unions, and functions cannot be declared within an anonymous union.

The wording of the warning could use some work, though. Clang allows (as a compiler extension) unnamed nested structs and additional anonymous unions within an anonymous union*, so it seems the author of the warning got just a little lazy and decided "anonymous types" was a good catch-all.

Note that giving your union a name (thus the union is no longer anonymous) makes the warning disappear.

*Named structs are still disallowed within anonymous unions (and there's no such thing as an anonymous struct)