You should use bool
unless you need to interoperate with code that uses BOOL
, because bool
is a real Boolean type and BOOL
isn't. What do I mean "real Boolean type"? I mean that code like this does what you expect it to:
#define FLAG_A 0x00000001
#define FLAG_B 0x00000002
...
#define FLAG_F 0x00000020
struct S
{
// ...
unsigned int flags;
};
void doSomething(S* sList, bool withF)
{
for (S* s = sList; s; s = s->next)
{
if ((bool)(s->flags & FLAG_F) != withF)
continue;
// actually do something
}
}
because (bool)(s->flags & FLAG_F)
can be relied upon to evaluate to either 0 or 1. If that were a BOOL
instead of a bool
in the cast, it wouldn't work, because withF
evaluates to 0 or 1, and (BOOL)(s->flags & FLAG_F)
evaluates to 0 or the numeric value of FLAG_F, which in this case is not 1.
This example is contrived, yeah, but real bugs of this type can and do happen all too often in old code that doesn't use the C99/C++ genuine boolean types.