在C预处理器中为其自身定义一些内容 [英] Defining something to itself in C preprocessor
问题描述
我碰到这些行:
#define bool bool
#define false false
#define true true
我想我不需要说很多 wtf?,但是需要明确的是:为自己定义东西的意义是什么?
I don't think I need to say more than "wtf?", but just to be clear: What is the point of defining something to itself?
这些行来自 c铛stdbool.h
推荐答案
C和C ++标准明确允许(并要求没有无限扩展)
The C and C++ standards explicitly allow that (and requires that there is no infinite expansion)
BTW,类似于函数的递归(或自引用)宏甚至更有用:
BTW, function-like recursive (or self-refential) macros are even more useful:
#define puts(X) (nblines++,puts(X))
(内部 puts
是对标准 puts
函数的调用;宏重载了此类进一步的调用按县ng nblines
)
(the inner puts
is a call to the standard puts
function; the macro "overloads" such further calls by counting nblines
)
您的定义可能有用,例如以及诸如 #ifdef true
之类的后续构造,这不可能是简单的 #define true
警告>,因为这会擦除 true
的所有后续使用,因此它必须恰好是 #define true true
Your define could be useful, e.g. with later constructs like #ifdef true
, and it can't be a simple because that would "erase" every further use of #define true
true
, so it has to be exactly#define true true
.
这篇关于在C预处理器中为其自身定义一些内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!