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

后端 未结 2 783
借酒劲吻你
借酒劲吻你 2021-01-01 08:10

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
#defin         


        
2条回答
  •  不知归路
    2021-01-01 08:48

    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  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.

提交回复
热议问题