#define XX在C中有什么影响?

Moh*_*nia 17 c c++ llvm llvm-clang

在LLVM项目的源代码中stdbool.h,它显示为:

/* 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
Run Code Online (Sandbox Code Playgroud)

在最后4行中有三行from #define X X.为什么要这么做?它有什么不同?难道这个强制编译器不会替换,比方说,truetrue

Mar*_*ott 19

我能想到的唯一原因是,预处理器语句就像

#ifdef bool
// do some stuff or define bool
#endif
Run Code Online (Sandbox Code Playgroud)

在其他c文件中包括事后将正常工作,而不是试图以另一种方式重新定义bool

#define bool int
Run Code Online (Sandbox Code Playgroud)

这将干扰第一个定义


alk*_*alk 7

#define X X
Run Code Online (Sandbox Code Playgroud)

具有"预处理器条件" *的效果:

#ifdef X
Run Code Online (Sandbox Code Playgroud)

是"真实的" "成功的".*


*更新

  • @vsz在这种情况下,它将重新定义C++的bool关键字以扩展为空. (3认同)