Do I need to #undef a local #define? Is there such a thing as a local define?

后端 未结 6 1158
刺人心
刺人心 2021-02-05 12:45

Sometimes to make things easier to write and read, I write some local #define macros within functions (for example, #define O_REAL Ogre::Real).

6条回答
  •  南旧
    南旧 (楼主)
    2021-02-05 13:09

    there is no such thing as a local define. Defines are always tentative evil ;-)

    For your example I would recommend typedef to make alias for variable names

    However, sometimes local defines are a nice thing for the programmer (but the are never for the maintainer). To make things a bit easier and a bit safer I always undef these things and I even guard their entry:

    #if defined(LOCAL_CROWBAR)
    #error Hurgh!
    #endif /* LOCAL_CROWBAR */
    #define LOCAL_CROWBAR
    
    ... use LOCAL_CROWBAR ...
    
    #undef LOCAL_CROWBAR
    

    Nevertheless, avoid those whenever possible!

提交回复
热议问题