为什么c的stdbool.h包含#define false false [英] Why does clang's stdbool.h contain #define false false
问题描述
在被编译器错误指出后,我注意到clang的 stdbool.h
文件包括(其中包括)以下几行:
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
它们包含在#ifdef
块中,该块间接地强制执行__cplusplus
,因此即使stdbool.h
是C标头,也可以使用c ++标记.
They're contained in an #ifdef
block that enforces __cplusplus
indirectly, hence the c++ tag even though stdbool.h
is a C header.
这些定义有什么需要?我想出于某些与预处理器相关的原因而需要它们,但我想知道标准的哪一部分或哪个技术原因使之成为标准,因此clang必须包括这些内容.
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.
推荐答案
stdbool.h
是C头文件,而不是C ++头文件.通常在C ++程序中找不到它,因为true
和false
在C ++中已经是关键字.
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++.
因此,如果C ++程序包含stdbool.h
,则很明显地表明它是移植的C程序(例如,被编译为C ++的C程序).在这种情况下,根据中的注释,G ++在C ++模式下支持stdbool.h
作为GNU扩展. GCC stdbool.h
:
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在C ++中支持stdbool.h
以与G ++兼容.
在此故意定义这些值以匹配内置的C ++类型,而不是传统的C99定义.它们被定义为宏,以提供与 C99标准的某些兼容性,该标准要求:
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:
标头应定义以下宏:
bool
,true
,false
,__bool_true_false_are_defined
.
应用程序可能会取消定义,然后可能会重新定义宏bool,true和false.
An application may undefine and then possibly redefine the macros bool, true, and false.
这篇关于为什么c的stdbool.h包含#define false false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!