gpt4 book ai didi

c++ - 为什么 clang 的 stdbool.h 包含 #define false false

转载 作者:IT老高 更新时间:2023-10-28 13:21:57 26 4
gpt4 key购买 nike

在被编译器错误指出后,我注意到 clang 的 stdbool.h文件包括(除其他外)以下几行:

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

它们包含在 #ifdef block 中,该 block 间接强制执行 __cplusplus,因此即使 stdbool.h 是 C标题。

这些定义有什么需要?我想它们是某些与预处理器相关的原因所必需的,但我很想知道标准的哪一部分或哪个技术原因使它如此铿锵必须包括这些。

最佳答案

stdbool.h 是 C 头文件,而不是 C++ 头文件。它通常不会出现在 C++ 程序中,因为 truefalse 在 C++ 中已经是关键字。

因此,如果 C++ 程序包含 stdbool.h,则相当清楚地表明它是移植的 C 程序(例如,正在编译为 C++ 的 C 程序)。在这种情况下,G++ 支持 C++ 模式下的 stdbool.h 作为 GNU 扩展,根据来自 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 standard 的兼容性。 ,这需要:

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.

关于c++ - 为什么 clang 的 stdbool.h 包含 #define false false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30875069/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com