c++ - #define X X 在 C 中的作用是什么?

标签 c++ c llvm llvm-clang

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/

相关文章:

c++ - Boost Shared Memory - 指针的返回 vector

c++ - 自动转换错误

java - 以编程方式或通过命令行分析 SSL 证书

python - 从 python 调用 c 函数是行不通的

xcode - 使用#pragma 抑制 Xcode 中的 “Instance method not found” 警告

windows - 您可以将 OCaml 项目本地编译为 Windows 库吗?

c++ - 限定依赖类型的 typedef

c++ - 如何grep 'vector'而不是 'std::vector'

c - 关于岗位增量

linux - 从 LLVM 位代码创建与位置无关的目标文件