为什么 C99 的 bool 是宏而不是 typedef?
Why is C99's bool a macro rather than a typedef?
为什么C99引入的boolean类型支持使用预处理器而不是语言自带的工具?具体来说,为什么我们有:
#define bool _Bool
#define true 1
#define false 0
在 <stdbool.h>
而不是:
typedef _Bool bool;
enum { false = 0, true = 1 };
我想枚举可以看作是一种品味问题。但是 - 为什么不使用 typedef?
来自 C11 规范的第 7.18/3 节:
The remaining three macros are suitable for use in #if
preprocessing directives.
规范列出 true
、false
和 __bool_true_false_are_defined
。
规范还继续声明(在 7.18/4 中)bool
、true
和 false
宏可能未被程序定义。
关于取消定义它们的最后一部分(我猜)是因为 C99 发布时的许多遗留代码使用了它们自己的布尔类型和值的定义和变体。所以它不会使现有代码无效。
所以它们是宏,所以它们可以在预处理器条件下使用,所以它们可以被程序取消定义。
为什么C99引入的boolean类型支持使用预处理器而不是语言自带的工具?具体来说,为什么我们有:
#define bool _Bool
#define true 1
#define false 0
在 <stdbool.h>
而不是:
typedef _Bool bool;
enum { false = 0, true = 1 };
我想枚举可以看作是一种品味问题。但是 - 为什么不使用 typedef?
来自 C11 规范的第 7.18/3 节:
The remaining three macros are suitable for use in
#if
preprocessing directives.
规范列出 true
、false
和 __bool_true_false_are_defined
。
规范还继续声明(在 7.18/4 中)bool
、true
和 false
宏可能未被程序定义。
关于取消定义它们的最后一部分(我猜)是因为 C99 发布时的许多遗留代码使用了它们自己的布尔类型和值的定义和变体。所以它不会使现有代码无效。 所以它们是宏,所以它们可以在预处理器条件下使用,所以它们可以被程序取消定义。