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

后端 未结 9 1965
谎友^
谎友^ 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:30

    #ifdef NOOP
        static inline void conditional_noop(int x) { }
    #else 
        static inline void conditional_noop(int x) { std::cout << x; }
    #endif
    

    Using inline function void enables type checking, even when NOOP isn't defined. So when NOOP isn't defined, you still won't be able to pass a struct to that function, or an undefined variable. This will eventually prevent you from getting compiler errors when you turn the NOOP flag on.

提交回复
热议问题