How do I implement no-op macro (or template) in C++?

后端 未结 9 1947
谎友^
谎友^ 2020-12-01 11:53

How do I implement no-op macro in C++?

#include    

#ifdef NOOP       
    #define conditional_noop(x) what goes here?   
#else       
    #         


        
9条回答
  •  Happy的楠姐
    2020-12-01 12:43

    Like others have said, leave it blank.

    A trick you should use is to add (void)0 to the macro, forcing users to add a semicolon after it:

    #ifdef NOOP       
        #define conditional_noop(x) (void)0
    #else       
        #define conditional_noop(x) std::cout << (x); (void)0
    #endif  
    

    In C++, (void)0 does nothing. This article explains other not-as-good options, as well as the rationale behind them.

提交回复
热议问题