Anonymous C ++ Structures

I use the following union to simplify operations with byte, nibble, and bit:

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; }; 

It works well, but also generates this warning:

warning: ISO C ++ prohibits anonymous structures [-pedantic]

Good, good to know. But ... how to get this warning from my g ++ output? Is it possible to write something like this union without this problem?

+6
source share
1 answer

The gcc -fms-extensions compiler option will allow non-standard anonymous structures without warning.

(This allows the use of "Microsoft extensions")

You can also achieve the same effect in actual C ++ using this convention.

 union Byte { struct bit { 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; }; struct nibble { unsigned int _0: 4; unsigned int _1: 4; }; unsigned char byte; }; 

At the same time, your non-standard byte.nibble_0 becomes legal byte.nibble._0

+4
source

Source: https://habr.com/ru/post/943577/


All Articles