Is bool safe in determining a bit field?

Possible duplicate:
C ++ bitfield assembly with bools

Is the safe use of the C ++ bool keyword guaranteed inside a bitfield definition?

Sort of:

 struct flags { bool a : 1; bool b : 1; } 
+5
source share
2 answers

Yes. In practice, you can use sizeof(bool) * CHAR_BIT as a guide to find out how many bits are available.

From C ++ 98 , § 9.6.3

The bit field must be an integer or enumerated type (3.9.1).

From C ++ 98, § 3.9.1.7

The types are bool, char, wchar_t, and signed and unsigned integer types are called integral types.

+5
source

From C ++ 03 9.6 “Bit-fields”:

The bit field must be an integral or enumerated type (3.9.1). this is an implementation - it is determined whether a simple (neither explicit signature nor unsigned) char, short, int or long bit field is signed or unsigned. The bool value can be successfully stored in a bit field of any nonzero size ....

If true or false is stored in a bool type bit field of any size (including a single-bit bit field), the original bool value and the bit field value are compared equal to ....

3.9.1 / 7 "Basic types" indicates that bool is an integral type.

+6
source

Source: https://habr.com/ru/post/1443365/


All Articles