为什么主编译器对stdint.h使用typedef,而对stdbool.h使用#define?

Jas*_*n S 12 c typedef include language-lawyer c-preprocessor

我只是注意到gcc和clang似乎都对stdint.h使用typedef,而对stdbool.h使用#define.

例如:clang的stdint.h

 #ifdef __INT8_TYPE__
 #ifndef __int8_t_defined  /* glibc sys/types.h also defines int8_t*/
 typedef __INT8_TYPE__ int8_t;
 #endif /* __int8_t_defined */
 typedef __UINT8_TYPE__ uint8_t;
 # define __int_least8_t int8_t
 # define __uint_least8_t uint8_t
 #endif /* __INT8_TYPE__ */
Run Code Online (Sandbox Code Playgroud)

clang的stdbool.h

#ifndef __cplusplus
#define bool _Bool
#define true 1
#define false 0
#elif defined(__GNUC__) && !defined(__STRICT_ANSI__)
/* Define _Bool, bool, false, true as a GNU extension. */
#define _Bool bool
#define bool  bool
#define false false
#define true  true
#endif
Run Code Online (Sandbox Code Playgroud)

为什么不呢typedef _Bool bool;

(gcc stdint.hstdbool.h)

zwo*_*wol 20

stdbool.h定义bool为宏,因为C标准(第7.18节)表示bool应定义为宏,并将etc stdint.h定义intN_t为typedef,因为C标准(7.20节)表示intN_t等应定义为typedef.

好吧,为什么C标准说这些东西?我无法肯定地告诉你,但有一条线索在第7.18节第4段:

尽管有7.1.3的规定,程序可能会取消定义,然后重新定义宏bool,true和false.

如果bool是一个typedef,true而且false,我不知道,enum常量,他们不能允许你这样做,因为没有办法撤消那种声明.

好吧,为什么C委员会想要允许你这样做?这更是投机性的,但可能是出于同样的原因,他们补充stdbool.h_Bool替代制造bool,truefalse关键字,因为它们在C++:他们希望保持与定义的旧程序的兼容性bool,truefalse自己,即使这些程序使用三阶包含stdbool.h...的聚会标题

没有这样的向后兼容性问题适用于定义的类型stdint.h; 一些系统提供(一些)作为扩展,但它们总是typedef.