Is it necessary?
Nope, it's optional.
What's the purpose of it?
Here's what the standard says in §9.6.2, C++11 (draft N3337, emphasis mine):
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 it's a hint to the compiler that summing up all the members of the struct
leads to 2 octects and thus is done hoping the compiler would make the struct
2 octects long. However, as per the standard there's no such requirement. Here's the excerpt from the previous point, §9.6.1:
extra bits are used as padding bits and do not participate in the value representation of the bit-field. 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.
Hence the standard does not guarantee any further than this regarding the size or alignment of a struct
/class
using bit-fields.
struct
2 octects long, which should be done by most compilers already. – Aniseikonia