Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing extra parentheses from C++ macro

I have a bunch of source code that uses double-parentheses for a bunch of macro calls where the 2nd arg and forward is a varargs for a print statement.

   DEBUG((1,"here is a debug"))
   DEBUG((1,"here is %d and %d",42,43))

etc..

I want to write a macro that can print args 2-..., but I can't figure out how to remove the extra parentheses so I can access the args.

I.e., this obviously does not work:

#define DEBUG(ignore,...) fprintf(stderr,__VA_ARGS__)

And the following attempt to be sneaky also fails (with 'DEBUG2 not defined'):

#define DEBUG2(ignore,...) fprintf(stderr,__VA_ARGS__)
#define DEBUG(...) DEBUG2

Without editing all the macro calls to remove parens, how can I define a macro that will do this?

like image 335
David Ljung Madison Stellar Avatar asked Mar 03 '26 18:03

David Ljung Madison Stellar


1 Answers

You may do:

#define PRINTF(unused, ...) fprintf(stderr, __VA_ARGS__)
#define DEBUG(arg) PRINTF arg
like image 193
Jarod42 Avatar answered Mar 06 '26 07:03

Jarod42