Defining something to itself in C preprocessor

前端 未结 3 1798
-上瘾入骨i
-上瘾入骨i 2021-01-13 18:53

I ran into these lines:

#define bool  bool
#define false false
#define true  true

I don\'t think I need to say more than \"wtf?\", but just

3条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-13 18:57

    The C and C++ standards explicitly allow that (and requires that there is no infinite expansion)

    BTW, function-like recursive (or self-refential) macros are even more useful:

    #define puts(X) (nblines++,puts(X))
    

    (the inner puts is a call to the standard puts function; the macro "overloads" such further calls by counting nblines)

    Your define could be useful, e.g. with later constructs like #ifdef true, and it can't be a simple #define true because that would "erase" every further use of true, so it has to be exactly#define true true.

提交回复
热议问题