Search code examples
c++macrospredefined-macro

How to achieve a c++ macros like this


#define idebug(...) \
  \#ifdef _DEBUG\
    printf(__VA_ARGS__);\
  \#endif\
#endif

It is difficult to describe the intention, which generally means that i predefine a macros idebug which to save some code. If _ DEBUG flag is predefined, then print the output. Or pretend nothing happened.

if we achieve it using a function ,it will look like this:

void idebug(...)
{
  #ifdef _DEBUG
    printf(...);
  #endif
}

Suppose there is a program

int main()                    
{                            
  int a = 10;         
  idebug("a:%d\n",a);      
}                           

when we are in the debugging phase, we want a output by complier:

int main()                    
{                            
  int a = 10;         
  printf("a:%d\n",a);      
} 

if we are in the release phase, we want a output by complier:

int main()                    
{                            
  int a = 10;         
} 

Solution

  • Do it the other way:

    #ifdef _DEBUG
    # define idebug(...) printf(__VA_ARGS__)
    #else
    # define idebug(...) ((void)0)
    #endif