When should BOOL and bool be used in C++ and why?
I think using bool is cleaner and more portable because it’s a built-in type. But BOOL is unavoidable when you interactive with legacy code/C code, or doing inter-op from .NET with C code/Windows API.
So my policy is:
Use bool inside C++.
Use BOOL when talk to outer world, e.g., export function in windows DLL.
Is there a definitive explanation of when to use one over the other?
Matthew Wilson discusses
BOOL,bool, and similar in section 13.4.2 of Imperfect C++. Mixing the two can be problematic, since they generally have different sizes (and so pointers and references aren’t interchangeable), and sinceboolisn’t guaranteed to have any particular size. Trying to use typedefs or conditional compilating to smooth over the differences betweenBOOLandboolor trying to allow for a single Boolean type to work in both C and C++ is even worse:This approach means that a function’s return type can differ depending on which language calls it; Wilson explains that he’s seen more than one bug in his own code and others’ that results from this. He concludes:
In short, he would agree with your approach.