Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does clang's stdbool.h contain #define false false

After being pointed there by a compiler error, I noticed clang's stdbool.h file includes (among other things) the following lines:

#define bool  bool #define false false #define true  true 

They're contained in an #ifdef block that enforces __cplusplus indirectly, hence the c++ tag even though stdbool.h is a C header.

What's the need for those defines? I imagine they're required for some preprocessor-related reason but I'd be interested to know what part of the standard or which technical reason makes it so clang has to include those.

like image 620
anthonyvd Avatar asked Jun 16 '15 18:06

anthonyvd


People also ask

Why we use #include Stdbool H?

Using the system header file stdbool. h allows you to use bool as a Boolean data type. true evaluates to 1 and false evaluates to 0 . bool is just a nice spelling for the data type _Bool .

What is include Stdbool h in C?

The header stdbool. h in the C Standard Library for the C programming language contains four macros for a Boolean data type. This header was introduced in C99. The macros as defined in the ISO C standard are : bool which expands to _Bool.

Should I use Stdbool H?

h was added to allow users the obvious name. That way, if your code didn't have a home-brewed bool , you could use the built in one. So do indeed use stdbool. h if you aren't bound to some existing home-brewed bool .


1 Answers

stdbool.h is a C header, not a C++ header. It is not usually found in C++ programs because true and false are already keywords in C++.

Consequently, if a C++ program includes stdbool.h it is a fairly clear indication that it is a ported-over C program (e.g. a C program that is being compiled as C++). In this case, G++ supports stdbool.h in C++ mode as a GNU extension, per the comments from the GCC stdbool.h:

/* Supporting <stdbool.h> in C++ is a GCC extension.  */ #define _Bool        bool #define bool        bool #define false        false #define true        true  ...  /* Signal that all the definitions are present.  */ #define __bool_true_false_are_defined        1 

Clang, likewise, supports stdbool.h in C++ for compatibility with G++. The values are intentionally defined here to match the built-in C++ type rather than the traditional C99 definitions. They are defined as macros presumably to provide some compatibility with the C99 standard, which requires:

The header shall define the following macros: bool, true, false, __bool_true_false_are_defined.

An application may undefine and then possibly redefine the macros bool, true, and false.

like image 61
nneonneo Avatar answered Sep 20 '22 15:09

nneonneo