Is 'bool' a basic datatype in C++?
Solution 1:
bool is a fundamental datatype in C++. Converting true
to an integer type will yield 1, and converting false
will yield 0 (4.5/4 and 4.7/4). In C, until C99, there was no bool datatype, and people did stuff like
enum bool {
false, true
};
So did the Windows API. Starting with C99, we have _Bool
as a basic data type. Including stdbool.h
will typedef #define
that to bool
and provide the constants true
and false
. They didn't make bool a basic data-type (and thus a keyword) because of compatibility issues with existing code.
Solution 2:
Yes, bool is a built-in type.
WIN32 is C code, not C++, and C does not have a bool, so they provide their own typedef BOOL.
Solution 3:
C++ does lots of automatic casting for you - that is, if you have a variable of type bool
and pass it to something expecting an int
, it will make it into an int
for you - 0
for false
and 1
for true
.
I don't have my standard around to see if this is guaranteed, but every compiler I've used does this (so one can assume it will always work).
However, relying on this conversion is a bad idea. Code can stop compiling if a new method is added that overloads the int
signature, etc.
Solution 4:
yes, it was introduced in 1993.
for further reference: Boolean Datatype