Should bit fields smaller than int be subject to holistic promotion?

Let's say I have the following struct :

 struct A { unsigned int a : 1; unsigned int b : 1; }; 

I'm interested in the type of expression a + b . While technically, bit fields are of a โ€œtypeโ€ with a size smaller than int , so integral progress may need to happen, and then the result is int , as if it were happening in gcc and clang.

But since it is impossible to extract the exact type of the bit field itself, and it will always be displayed as its "large" type (ie unsigned int in this case), should the integral advance correctly occur? Because we cannot talk about exact types and their sizes for bit fields, except that they are displayed as unsigned int , in which case integral progress should not occur.

(Once again, my question is that MSVC considers unsigned int be a type of such an expression)

+5
source share
1 answer

If we move on to the draft C ++: N4140 standard in section 5 , he says:

Many binary operators that expect arithmetic operands or an enumeration type cause conversions and produce results in a similar way. The goal is to give a generic type, which is also a type of result. This pattern is called regular arithmetic conversion, which are defined as follows:

and the following brand applies:

  • Otherwise, integral promotions (4.5) must be performed on both operands .61 Then to the advanced Operands:

and section 4.5, which says (my attention):

The value for the integer bit field (9.6) can be converted to a prvalue of type int if int can represent all the values โ€‹โ€‹of the bit field; otherwise, it can be converted to unsigned int if unsigned int can represent all the values โ€‹โ€‹of the bit field. If the bit field is furthermore, an entire stock does not apply to it. If a bit field has an enumerated type, it is treated like any other value of this type for promotion.

So gcc and clang are correct, a and b should be incremented to int.

+4
source

All Articles