Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should BOOL and bool be used in C++?

Tags:

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?

like image 928
zhaorufei Avatar asked May 06 '09 15:05

zhaorufei


People also ask

What is difference between bool and bool?

The values for a bool are true and false , whereas for BOOL you can use any int value, though TRUE and FALSE macros are defined in the windef. h header.

Does C use bool or boolean?

C does not have boolean data types, and normally uses integers for boolean testing. Zero is used to represent false, and One is used to represent true. For interpretation, Zero is interpreted as false and anything non-zero is interpreted as true.

What is bool used for in C?

In C, Boolean is a data type that contains two types of values, i.e., 0 and 1. Basically, the bool type value represents two types of behavior, either true or false. Here, '0' represents false value, while '1' represents true value. In C Boolean, '0' is stored as 0, and another integer is stored as 1.

When did bool add C?

C99, the version of C released in 1999/2000, introduced a boolean type. To use it, however, you need to import a header file, so I'm not sure we can technically call it “native”. Anyway, we do have a bool type.


2 Answers

If BOOL is some sort of integral type, and it always is, and BOOL is defined so that it works right, the standard conversions will automatically get it right. You can't quite use them interchangeably, but you can get close.

Use BOOL at the interface, where you have to talk to the Win32 API or whatever. Use bool everywhere else.

like image 171
David Thornley Avatar answered Oct 13 '22 15:10

David Thornley


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 since bool isn't guaranteed to have any particular size. Trying to use typedefs or conditional compilating to smooth over the differences between BOOL and bool or trying to allow for a single Boolean type to work in both C and C++ is even worse:

#if defined(__cplusplus) || \
    defined(bool) /* for C compilation with C99 bool (macro) */
 typedef bool   bool_t;
#else
 typedef BOOL   bool_t;
#endif /* __cplusplus */

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:

The solution to this imperfection is, as it so often is, abstinence. I never use bool for anything that can possibly be accessed across multiple link units—dynamic/static libraries, supplied object files—which basically means not in functions or classes that appear outside of header files. The practical answer, such as it is, is to use a pseudo-Boolean type, which is the size of int.

In short, he would agree with your approach.

like image 32
Josh Kelley Avatar answered Oct 13 '22 15:10

Josh Kelley