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?

Upvotes: 3

Views: 1179

Answers (2)

Jarod42
Jarod42

Reputation: 218268

You may do:

#define PRINTF(unused, ...) fprintf(stderr, __VA_ARGS__)
#define DEBUG(arg) PRINTF arg

Upvotes: 3

Ack - I just realized the answer is simple and I was one step away:

#define DEBUG(...) DEBUG2 __VA_ARGS__

Upvotes: 0

Related Questions