在 source code of stdbool.h
在 LLVM 项目中,它显示为:
/* Don't define bool, true, and false in C++, except as a GNU extension. */
#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
在最后 4 行中,有 3 行 from #define X X
。为什么要这么做?它有什么区别?这不会强制编译器将 true
替换为 true
吗?
最佳答案
我能想到的唯一原因是,预处理器语句像
#ifdef bool
// do some stuff or define bool
#endif
在其他 c 文件中包含之后将正常工作,而不是尝试以另一种方式重新定义 bool,例如
#define bool int
这会干扰第一个定义
关于c++ - #define X X 在 C 中的作用是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18557840/