1

The following macro works:

#define DEBUG(msg, ...) printf(msg, __VA_ARGS__)

But when I add my own function, it says error: '__VA_ARGS__' was not declared in this scope. My code:

void Debug(const char* msg, ...) {
    printf(msg, __VA_ARGS__);
}

#define DEBUG(msg, ...) Debug(msg, __VA_ARGS__)

Is there any way to do so?

NutCracker
  • 11,485
  • 4
  • 44
  • 68
Sajib
  • 404
  • 3
  • 15
  • 2
    `__VA_ARGS__` is a variadic macro *only* thing. There are plenty of tutorials and examples for variadic functions online if you want to make one. – Some programmer dude Apr 07 '21 at 07:29

2 Answers2

2

Variadic parameter pack is your friend in this case:

template< typename ... Args >
void Debug( const char * msg, Args ... args ) {
    printf( msg, args ... );
}
NutCracker
  • 11,485
  • 4
  • 44
  • 68
2

__VA_ARGS__ simply does not exist outside of a variadic macro. For what you are attempting, use vprintf() instead of printf(), eg:

void Debug(const char* msg, ...) {
    va_list args;
    va_start(args, msg);
    vprintf(msg, args);
    va_end(args);
}

#define DEBUG(msg, ...) Debug(msg, __VA_ARGS__)
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770