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

后端 未结 9 1931
谎友^
谎友^ 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条回答
  • 2020-12-01 12:52

    As mentioned before - nothing.
    Also, there is a misprint in your code.
    it should be #else not #elif. if it is #elif it is to be followed by the new condition

    #include <iostream>   
    
    #ifdef NOOP       
        #define conditional_noop(x) do {} while(0)
    #else       
        #define conditional_noop(x) std::cout << (x)   
    #endif  
    

    Have fun coding! EDIT: added the [do] construct for robustness as suggested in another answer.

    0 讨论(0)
  • 2020-12-01 12:53
     #ifdef NOOP       
         #define conditional_noop(x)   
     #elif  
    

    nothing!

    0 讨论(0)
  • 2020-12-01 12:57

    As this is a macro, you should also consider a case like

    if (other_cond)
        conditional_noop(123);
    

    to be on the safe side, you can give an empty statement like

    #define conditional_noop(X) {}
    

    for older C sometimes you need to define the empty statment this way (should also get optimized away):

    #define conditional_noop(X) do {} while(0)
    
    0 讨论(0)
提交回复
热议问题