The following code generates MSVS Compiler Error C2034 in Visual Studio 2008:
struct TestStruct { unsigned short var1 : 7; unsigned short : 9; bool var2 : 1; bool : 15; // C2034 }; error C2034: 'TestStruct::<alignment member>': type of bit field too small for number of bits
However, the following code compiles successfully, which seems kind of silly, because I'd think the compiler could have just done this automatically:
struct TestStruct { unsigned short var1 : 7; unsigned short : 9; bool var2 : 1; bool : 7; bool : 8; }; However, both code snippets compile on my Linux GCC compiler. Is one compiler more correct than the other, according to the C++ Standard? If so, which, and why?
bool, which is impossible. How should it know that you actually wanted an 8-bit field of typebool, as opposed to creating a 15-bit field of typelong, or whatever? (GCC and ICC warn about this. Clang might, too, if I knew the right switch. So is your question just, why does MSVC treat this as an error?)