Simple ways to disable parts of code

后端 未结 14 1140
误落风尘
误落风尘 2021-01-31 08:59

This isn\'t a typical question to solve a specific problem, it\'s rather a brain exercise, but I wonder if anyone has a solution.

In development we often need to disable

14条回答
  •  清歌不尽
    2021-01-31 09:23

    Synchronously switching on/off chunks of code scattered across the program is sometimes a need too.

    Inspired by this earlier post by Graham I cooked up something like this:

    void doNothing(){}
    #define DO_IF(flag, code) flag ? code : doNothing();
    

    This can be for instance used as follows:

    DO_IF(collectStats, recordStats());
    DO_IF(collectStats, store(pullStat()));
    

    An that is even better:

    #define DO_IF(flag,code) if( flag ) { code; }
    

提交回复
热议问题