为什么我会收到此代码的匿名类型警告?

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

在编译我的项目时,我收到警告 在匿名联合中声明的匿名类型是扩展 [-Wnested-anon-types]。我的代码包含这个联合:

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

就网站上的其他答案而言,如果我从结构中省略 IEEE, 名称,我只会收到此警告。但目前该结构不应该是匿名类型?

因为标准是这么说的 ([class.union.anon]):

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

不过,警告的措辞可能需要一些工作。 Clang 允许(作为编译器扩展)未命名 嵌套结构和匿名联合 * 中的其他匿名联合,因此警告的作者似乎得到了只是有点懒,决定 "anonymous types" 是一个很好的包罗万象。

请注意,为您的 union 命名(因此工会不再是匿名的)会使警告消失。

*命名结构在匿名联合中仍然被禁止(并且没有匿名结构这样的东西)