Defining something to itself in C preprocessor

前端 未结 3 1835
-上瘾入骨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 19:05

    It is called self referential Macros.

    According to gcv reference :

    A self-referential macro is one whose name appears in its definition. Recall that all macro definitions are rescanned for more macros to replace. If the self-reference were considered a use of the macro, it would produce an infinitely large expansion. To prevent this, the self-reference is not considered a macro call. It is passed into the preprocessor output unchanged.

    Reference example :

    One common, useful use of self-reference is to create a macro which expands to itself. If you write

    #define EPERM EPERM

    then the macro EPERM expands to EPERM. Effectively, it is left alone by the preprocessor whenever it’s used in running text. You can tell that it’s a macro with ‘#ifdef’. You might do this if you want to define numeric constants with an enum, but have ‘#ifdef’ be true for each constant.

提交回复
热议问题