C++: bool vs custom BOOL

Started by
17 comments, last by Shannon Barber 18 years, 8 months ago
i have a seperate header file with all the typedefs in it (*luckily*)

this way I stay compiler and platform independent concerning datatype size

is there some sort of compiler option to make sure bool is 32 bit aligned?


tztz how can one make something that important implementation specific *gr*
http://www.8ung.at/basiror/theironcross.html
Advertisement
Why is it so important that bool be 32-bit aligned? Efficiency isn't really a good reason (in most cases). Micro-optimizing using typedefs like that is a relic of the past when it comes to C/C++ (unless you're targeting some kind of ancient platform).
Quote:Original post by Basiror
i have a seperate header file with all the typedefs in it

this way I stay compiler and platform independent concerning datatype size

is there some sort of compiler option to make sure bool is 32 bit aligned?


tztz how can one make something that important implementation specific *gr*


That's *why* its implementation specific, so that it's the appropriate size and format on all platforms and processors.

The compiler will do its best to make the data aligned on platforms that have an abundance of memory. Check the project settings.
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Quote:Original post by Basiror
is there some sort of compiler option to make sure bool is 32 bit aligned?


There usually is a #pragma or -- for gcc an __attribute__ that controls that, check your documentation.

Quote:tztz how can one make something that important implementation specific


Because it's precisely up to the compiler implementers to know what is the best solution for the platform they are writing a compiler for. You wouldn't want to enforce in the language specifications that a given data type must be XYZ-aligned, particularly if that's is impossible on platform ABC due to hardware particularities.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Windows API uses BOOL because programs written in C need to be able to interface with it. There is absolutely no reason to use BOOL in your C++ programs.
There's also no way to get the built-in comparison operators to return BOOL -- they all use bool. And any overloaded comparison operator shouldn't return BOOL because that violates the principal of least astonishment. ( Every standard comparison returns bool, so yours should too. )
the compiler didn t complain so i assume it did the conversion at compilation time
http://www.8ung.at/basiror/theironcross.html
Quote:Original post by Shannon Barber
In C++ you should always use the built-in bool (and true & false) if that's what you mean.


The one place to be careful is std::vector<bool>
Scroll down to the What About bool? section of this Guru of the Week article for a good explanation.
With any luck they'll fix it in C++0x.
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara

This topic is closed to new replies.

Advertisement