C++ #定义X在C中有什么作用?

C++ #定义X在C中有什么作用?,c++,c,llvm,llvm-clang,C++,C,Llvm,Llvm Clang,在In 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, f

在In 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行中,有三行from
#define X X
。你为什么要那样做?这有什么区别?这会不会迫使编译器用
true
替换,比如说
true

#define X X
具有“预处理器条件”*:

是“真”还是“成功”*



*更新

如果真、假等现在都是宏,则会有所不同。所以像这样编码

#if defined(true)
    ...
#else
    ...
#endif

会受到影响。

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

#ifdef bool
// do some stuff or define bool
#endif
在其他c文件中,include-aftern将正常工作,而不是以另一种方式重新定义bool,如

#define bool int

这会干扰第一个定义

你的意思是
#ifdef true
@MilesRout
#ifdef true
#if defined(true)
是一样的。这是我的观点,不过我是指我写的。@MilesRout Shorter,是的。更清楚,不一定<代码>#ifdef不太通用<代码>定义的X可以与其他表达式组合。我个人认为如果!与
\ifndef
@MilesRout
相比,defined X
不太可能被误读,如果defined比
\ifdef
更具优势,它允许您添加
\elif
案例,但我想说预处理器指令“正确”会让您投反对票。有些人对准确性要求很高。@john:我知道这一点,所以我把它放在引号里。然而,在预处理上下文中,它是一个布尔表达式,不是吗?你有没有其他措辞的想法?好的,我认为编辑应该用准确的措辞来满足所有人的期望。为什么有人会选择它而不仅仅是
#define X
?@vsz在这种情况下,它会重新定义C++的bool关键字,使其扩展为零。
#define bool int