Is there a fixed-width bool type in standard C++?
Asked Answered
E

1

6

As far as I could find, the width of the bool type is implementation-defined. But are there any fixed-width boolean types, or should I stick to, for e.g., a uint8_t to represent a fixed-width bool?

[EDIT] I made this python script that auto-generates a C++ class which can hold the variables I want to be able to send between a micro controller and my computer. The way it works is that it also keeps two arrays holding a pointer to each one of these variables and the sizeof each one of them. This gives me the necessary information to easily serialize and deserialize each one of these variables. For this to work however the sizeof, endianness, etc of the variable types have to be the same on both sides since I'm using the same generated code on both sides. I don't know if this will be a problem yet, but I don't expect it to be. I have already worked with this (32bit ARM) chip before and haven't had problems sending integer and float types in the past. However it will be a few days until I'm back and can try booleans out on the chip. This might be a bigger issue later, since this code might be reused on other chips later.

So my question is. Is there a fixed width bool type defined in the standard libraries or should I just use a uint8_t to represent the boolean?

Ens answered 25/12, 2015 at 0:17 Comment(18)
A bool type is fixed at true and false. How can 2 states have varying width?Stereochemistry
Are you talking about packing one or more bools in an integral type?Stereochemistry
Do you refer to size in the memory? If so it is compiler dependent for bool. In 99.9% it takes up 1 bit as it represents true or false, on or off. However, the other 7 are stuffed, can't be used. Did that answer it for you?Cathar
Are you talking about 8-bit vs 16-bit vs 32-bit vs 64-bit integers to represent a single bool?Stereochemistry
@Cathar how did you reach that conclusion of 99.9% ?Amin
I am currently writing some code that communicates between a microcrontroller and a computer. I was just wondering if sizeof(bool) would give me the same result on both. Apparently not necessary.Ens
Depends on the minimal accessible size for the microcontroller. If the microcontroller can only operate on 16-bit quantities or larger, then the size of a bool will be 16-bits. Some microcontrollers can access a bit as the minimum size.Stereochemistry
@Amin I made it up based on my own experience (100%) then I subtracted .1% before Jon Skeet or some other monster comes and corrects me. I could always claim I took that into consideration! (I am SMRT!) Jokes aside: That number is made up and represents that it is most commonly stored as 1 byte, where only 1 bit is used. Reference Disclaimer: I do not claim Jon Skeet to be an actual monster.Cathar
There is only one datatype explicitly for boolean values, and it doesn't have the property you want. Simple answer: pick a fixed-width type and use it as a bool.Paternity
@Emz: Typically bool is 8 bits, with all but the low-order bit being set to 0.Exarch
It would help if you could explain why you need a boolean type of some particular width. What purpose would such a type serve that isn't already served by bool? ("Having a fixed width" is not an answer.)Exarch
@KeithThompson That is what I said? Except zero initialization of the other bits, because there is no guarantee of that. Then I would have to add another 99.9% case.Cathar
If using a different type to simulate bool, be aware that you'll need to manually produce boolean conversions, e.g. x & FLAG needs to become !!(x & FLAG)Amin
sizeof any types in C and C++ are not necessarily the same across different architecturesClupeid
@LưuVĩnhPhúc except char and friends, these are always 1 byte. If you need the same number of bits, that's uint32_t and friends.Bulrush
@KeithThompson I made this python script which autogenerates a C++ class which can hold a the variables I want to be able to send between the micro controller and computer. The way it works is that I also keep two arrays holding a pointer to each one of these variables and the sizeof each one of them. This gives me the necessary information to easily serialize and deserialize each one of these variables. For this to work however the sizeof, endianness, etc of the variable types have to be the same on both sides.Ens
That information should be in the question, not just in a comment. With that additional background, I don't think this is a duplicate. You're asking how to transfer boolean values from one system to another, where the two systems may not use the same representation.Exarch
@KeithThompson As you recommended, I added the background information to the question.Ens
D
4

There is not. Just use uint8_t if you need to be sure of the size. Any integer type can easily be treated as boolean in C-related languages. See https://mcmap.net/q/151475/-is-sizeof-bool-defined-in-the-c-language-standard for a lengthy discussion of how bool's size is not guaranteed by the standard to be any specific value.

Drumbeat answered 26/12, 2015 at 5:53 Comment(4)
of course there's no implicit conversion to bool when doing this so you have to take care. For example uint8_t x = 0x500 & 0x100; will be 0, but a bool would have been trueAmin
@Amin true, but it's easy to fix: uint8_t x = 0x500 & 0x100 != 0. You just need to be careful.Heidiheidie
@MarkRansom ITYM (0x500 & 0x100) != 0 ;)Amin
A better solution may be to check the size of bool with a static assert. As exemplified by the above comments and this answer using uint8_t instead of bool comes with the risk of subtle errors.News

© 2022 - 2024 — McMap. All rights reserved.