I'm wondering whether there are differences between bit fields in C and C++. I just looked up the current standards and the following is confusing me:
C11 6.7.2.1 11 wrote:An implementation may allocate any addressable storage unit large enough to hold a bitfield. If enough space remains, a bit-field that immediately follows another bit-field in a structure shall be packed into adjacent bits of the same unit. If insufficient space remains, whether a bit-field that does not fit is put into the next unit or overlaps adjacent units is implementation-defined. The order of allocation of bit-fields within a unit (high-order to low-order or low-order to high-order) is implementation-defined. The alignment of the addressable storage unit is unspecified.
This is just what has been concluded in this thread. (Or does "order" in this context only mean LSB first / MSB first?) However,
C++11 9.6 2 wrote:A declaration for a bit-field that omits the identifier declares an unnamed bit-field. Unnamed bit-fields are not members and cannot be initialized. [ Note: An unnamed bit-field is useful for padding to conform to externally-imposed layouts. —end note ] As a special case, an unnamed bit-field with a width of zero specifies alignment of the next bit-field at an allocation unit boundary. Only when declaring an unnamed bit-field may the value of the constant-expression be equal to zero.
So in C++, bit fields should be "suitable" for externally-imposed layouts (such as bits in a GDT)? In the preceding paragraph it sounds like "implementation-defined" is used synonymously for "machine dependent":
C++11 9.6 1 wrote:Allocation of bit-fields within a class object is implementation-defined. Alignment of bit-fields is implementation-defined. Bit-fields are packed into some addressable allocation unit. [ Note: Bit-fields straddle allocation units on some machines and not on others. Bit-fields are assigned right-to-left on some machines, left-to-right on others. —end note ]
I guess otherwise the existence of "padding" fields would not make sense at all, if they could just be shifted to whatever place...
EDIT: Wow, my post must be really evil, since it's my 666. post here