0

How can I define a nested #define into a macro with variadic arguments

#ifndef MY_PRINTF
#define MY_PRINTF(f_, ...) { \
#ifdef USE_WRITE_DEBUG_INFO \
    char buff[200]; \
    sprintf(buff, (f_), __VA_ARGS__); \
    WriteDebugInfo(buff); \
#else \
    printf((f_), __VA_ARGS__); \
#endif \
} 
#endif 

Visual studio complains about missing directives. I'd appreciate for any hint.

afp_2008
  • 1,940
  • 1
  • 19
  • 46

1 Answers1

2

You can't use preprocessor directives while defining a #define. This means that your #ifdef USE_WRITE_DEBUG_INFO won't work.


For this case, use a function rather than a macro:

#include <cstdarg>

void my_printf(const char* format, ...) {
#ifdef USE_WRITE_DEBUG_INFO
    char buff[200];

    va_list args;
    va_start(args, format);
    // Note: uses the snprintf variant rather than sprintf variant,
    // avoiding buffer overlows.
    vsnprintf(buff, 200, format, args);
    va_end(args);

    WriteDebugInfo(buff);
#else
    va_list args;
    va_start(args, format);
    vprintf(format, args);
    va_end(args);
#endif
}

In general, you'd have to bring the preprocessor directives outside of the #define:

#ifndef MY_PRINTF

#ifdef USE_WRITE_DEBUG_INFO
// do { ... } while (false) makes the macro behave mostly like a regular function call.
#define MY_PRINTF(f_, ...) do { \
        char buff[200]; \
        sprintf(buff, (f_), __VA_ARGS__); \
        WriteDebugInfo(buff); \
    } while (false)
#else
#define MY_PRINTF(f_, ...) printf((f_), __VA_ARGS__)
#endif

#endif 

Justin
  • 24,288
  • 12
  • 92
  • 142
  • 3
    You can even use variadic template: `template void my_printf(const char* format, Ts... args) { printf(format, args...); }`. – Jarod42 Jan 07 '21 at 01:36