11

次のユニオンを使用して、バイト、ニブル、およびビット操作を簡素化します。

union Byte
{
  struct {
    unsigned int bit_0: 1;
    unsigned int bit_1: 1;
    unsigned int bit_2: 1;
    unsigned int bit_3: 1;
    unsigned int bit_4: 1;
    unsigned int bit_5: 1;
    unsigned int bit_6: 1;
    unsigned int bit_7: 1;
  };

  struct {
    unsigned int nibble_0: 4;
    unsigned int nibble_1: 4;
  };

  unsigned char byte;
};

うまく機能しますが、次の警告も生成されます。

警告: ISO C++ は無名構造体を禁止しています [-pedantic]

わかりました。しかし...私のg ++​​出力からこの警告を取得するにはどうすればよいですか? この問題なしでこのユニオンのようなものを書く可能性はありますか?

4

1 に答える 1

11

gcc コンパイラ オプション-fms-extensionsは、非標準の匿名構造体を警告なしで許可します。

(そのオプションは、 「Microsoft 拡張機能」と見なされるものを有効にします)

この規則を使用して、有効な C++でも同じ効果を得ることができます。

union Byte
{
  struct bits_type {
    unsigned int _0: 1;
    unsigned int _1: 1;
    unsigned int _2: 1;
    unsigned int _3: 1;
    unsigned int _4: 1;
    unsigned int _5: 1;
    unsigned int _6: 1;
    unsigned int _7: 1;
  } bit;
  struct nibbles_type {
    unsigned int _0: 4;
    unsigned int _1: 4;
  } nibble;
  unsigned char byte;
};

これにより、あなたの非標準byte.nibble_0は合法になりますbyte.nibble._0

于 2013-04-24T21:38:40.960 に答える