3

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?

8
  • 2
    Typically, MSVS is almost always wrong. Clang 3.4.1 also compiles it successfully. Commented Sep 9, 2016 at 16:01
  • Why would you want the compiler to silently rewrite your code for you behind your back? You explicitly asked for a 15-bit field of type bool, which is impossible. How should it know that you actually wanted an 8-bit field of type bool, as opposed to creating a 15-bit field of type long, 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?) Commented Sep 9, 2016 at 16:05
  • @CodyGray If it's impossible, why does GCC on Linux let me do it? And the type shouldn't matter, they're just alignment pieces, no? Commented Sep 9, 2016 at 16:06
  • 2
    @Rakete1111 except in this case g++ also complains. coliru.stacked-crooked.com/a/c95cb2b1e77dbdb5 (its a warning, but still) Commented Sep 9, 2016 at 16:07
  • 1
    It is certainly not impossible if the compiler is allowed to make assumptions. Few things are. But it violates the principle of least surprise to have the compiler reinterpreting code that you've written behind your back. Commented Sep 9, 2016 at 16:07

1 Answer 1

4

Yes this is a bug in MSVS. That standard states in [class.bit]/1

[...]The value of the integral constant expression may be larger than the number of bits in the object representation (3.9) of the bit-field’s type; in such cases the extra bits are used as padding bits and do not participate in the value representation (3.9) of the bit-field.[...]

So the compiler should have added extra padding and only let you have a number of bits equal to CHAR_BIT * sizeof(bit_field_underlying_type).

Sign up to request clarification or add additional context in comments.

5 Comments

I wouldn't really call this a bug. It is MSVS not complying with the standard definitely, but the error message is clearly intended to be generated. So "non-standard behavior" is probably a better description.
@lcs I would say that not being standard conforming is a bug.
@SebastianLenartowicz It did catch this when gcc and clang did not ;)
@lcs: In /Za mode, non-conformance is a bug. In /Ze mode, it is merely an intentional deviation.
I've sent feedback to MSVC about this.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.