Search code examples
c++booleanbit-fields

Is bool safe in a bitfield definition?


Possible Duplicate:
C++ bitfield packing with bools

Is it guaranteed to be safe to use C++'s bool keyword inside a bitfield definition?

Something like:

struct flags {
    bool a : 1;
    bool b : 1;
}

Solution

  • Yes. In practice, you can use sizeof(bool) * CHAR_BIT as your guide to knowing how many bits are available.

    From C++98, § 9.6.3

    A bit-field shall have integral or enumeration type (3.9.1).

    From C++98, § 3.9.1.7

    Types bool, char, wchar_t, and the signed and unsigned integer types are collectively called integral types