Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bool colon initialization

Tags:

c++

boolean

colon

While reading some C++ code, I saw and was confused by this little line in a class:

bool x:1;

In debug builds, I noticed that 'x' is initialized as 'false', but I can not find any documentation about that. Can anyone tell me what this syntax does?

like image 518
Nikola Nikolic Avatar asked Dec 26 '22 20:12

Nikola Nikolic


1 Answers

it's a bit field. read up on bit fields in your c++ textbook.

the initialization to false is independent of the declaration. whether it is guaranteed by your code depends on your code (not given).

the c++ standard gives the compiler some leeway for integer and enumeration bitfields of size 1: storing the value 1 in such a field, you may get out the value -1. happily this applies only to fields of size 1, and it does not apply to a field of type bool.

like image 195
Cheers and hth. - Alf Avatar answered Jan 08 '23 23:01

Cheers and hth. - Alf