Why and when to use __noop?

后端 未结 3 1774
再見小時候
再見小時候 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:20

    The __noop intrinsic specifies that a function should be ignored and the argument list be parsed but no code be generated for the arguments. It is intended for use in global debug functions that take a variable number of arguments.

    In your case the argument is an obviously side effect free expression that can be easily optimized out, so it doesn't matter.

    But if the argument expression has side effects or is so complex that the compiler can't prove that it terminates normally and has no side-effects then using __noop prevents the potentially expensive evaluation of that expression.

    The second benefit is that it behaves like a function call with a variable number of arguments syntactically. So substituting it for a function call doesn't affect the parsing of the program. With some other replacements (like the empty string), that might be a problem in some situations.

提交回复
热议问题