Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 991
nitronoid Avatar asked Oct 16 '25 08:10

nitronoid


1 Answers

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)

like image 176
AndyG Avatar answered Oct 18 '25 22:10

AndyG



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!