Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is bool safe in a bitfield definition? [duplicate]

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;
}
like image 316
Eric Avatar asked Nov 01 '12 04:11

Eric


People also ask

Is a bool 1 bit?

Boolean variables are stored as 16-bit (2-byte) numbers, but they can only be True or False.

What is bitfield in c++?

Both C and C++ allow integer members to be stored into memory spaces smaller than the compiler would ordinarily allow. These space-saving structure members are called bit fields, and their width in bits can be explicitly declared.


1 Answers

From C++03 9.6 "Bit-fields":

A bit-field shall have integral or enumeration type (3.9.1). It is implementation-defined whether a plain (neither explicitly signed nor unsigned) char, short, int or long bit-field is signed or unsigned. A bool value can successfully be stored in a bit-field of any nonzero size. ...

If the value true or false is stored into a bit-field of type bool of any size (including a one bit bit-field), the original bool value and the value of the bit-field shall compare equal. ...

3.9.1/7 "Fundamental types" specifies that bool is an integral type.

like image 59
Michael Burr Avatar answered Oct 03 '22 10:10

Michael Burr