Why and when to use __noop?

后端 未结 3 1787
再見小時候
再見小時候 2021-02-18 17:06

I was reading about __noop and the MSDN example is

#if DEBUG
   #define PRINT   printf_s
#else
   #define PRINT   __noop
#endif

int main() {
   PRINT(\"\\nhello         


        
3条回答
  •  醉话见心
    2021-02-18 17:18

    #define PRINT
    extern int some_complicated_calculation();
    PRINT("%d\n", some_complicated_calculation());
    

    would call the function even though you don't want the result.

    Using __noop, the function won't be called.

    You could (assuming the compiler supports variadic macros) define PRINT to ignore the arguments; but then they won't be parsed at all, and may become invalid if you change the code around them without compiling the variant that defines PRINT to do something. Using __noop, the arguments are still parsed, so are more likely to remain valid.

提交回复
热议问题