Defining something to itself in C preprocessor

前端 未结 3 1795
-上瘾入骨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:08

    It allows the user code to conditionally compile based on whether those macros are or aren't defined:

    #if defined(bool)
        /*...*/
    #else
        /*...*/
    #endif
    

    It basically saves you from having to pollute the global namespace with yet another name (like HAVE_BOOL), provided that the implementation lets its users know that iff it provides a bool, it will also provide a macro with the same name that expands to it (or the implementation may simply use this internally for its own preprocessor conditionals).

提交回复
热议问题