Variadic debug macros

N

Noob

Hello,

I've been reading about variadic macros, and have a few questions.

http://en.wikipedia.org/wiki/Variadic_macro

I don't find the term "variadic" in ISO/IEC 9899:TC3. Did the standard
committee avoid that word for some reason? :)

Is the Wikipedia article correct that variadic macros are supported in
C99 but not in C89?

Is it possible in C89 to write a function-like macro wrapping a variadic
function which has no va_list flavor (a la vprintf)?

Do you agree that writing
#define MYDEBUG printf
is poor style?
(triggers a "statement with no effect" warning, when MYDEBUG is set to
NOP, among other problems.)

Regards.
 
B

Ben Bacarisse

Noob said:
I've been reading about variadic macros, and have a few questions.

http://en.wikipedia.org/wiki/Variadic_macro

I don't find the term "variadic" in ISO/IEC 9899:TC3. Did the standard
committee avoid that word for some reason? :)

Other may be able to say but there is no obvious need to name these
as special macros. In the list of changes they are described as
"macros with a variable number of arguments". Adding yet another
technical term would not make anything clearer.
Is the Wikipedia article correct that variadic macros are supported in
C99 but not in C89?
Yes.

Is it possible in C89 to write a function-like macro wrapping a variadic
function which has no va_list flavor (a la vprintf)?

No, but see below. I tend to find that using the v* variants works out
better in the long run for things like logging and debugging function.
Do you agree that writing
#define MYDEBUG printf
is poor style?
(triggers a "statement with no effect" warning, when MYDEBUG is set to
NOP, among other problems.)

Yuck. The old trick was to force the user to write extra parentheses
so that the macro had one argument:

#define MYDEBUG(x) printf x
...
MYDEBUG(("Got %d expected %d\n", c, x));

To disable debugging you throw everything away:

#define MYDEBUG(x)

to be left with an empty statement where a debug statement used to be.
 
M

Mark

Ben said:
Other may be able to say but there is no obvious need to name these
as special macros. In the list of changes they are described as
"macros with a variable number of arguments". Adding yet another
technical term would not make anything clearer.

I as well can't find any instance of __VA_ARGS__ in the C99 standard.
 
B

Ben Pfaff

Mark said:
I as well can't find any instance of __VA_ARGS__ in the C99 standard.

I guess you didn't search very carefully:

5 The identifier __VA_ARGS__ shall occur only in the
replacement-list of a function-like macro that uses the
ellipsis notation in the arguments.

....

2 An identifier __VA_ARGS__ that occurs in the replacement list
shall be treated as if it were a parameter, and the variable
arguments shall form the preprocessing tokens used to
replace it.

....

9 EXAMPLE 7 Finally, to show the variable argument list macro facilities:
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define showlist(...) puts(#__VA_ARGS__)
#define report(test, ...) ((test)?puts(#test):\
printf(__VA_ARGS__))
debug("Flag");
debug("X = %d\n", x);
showlist(The first, second, and third items.);
report(x>y, "x is %d but y is %d", x, y);
 
B

Ben Bacarisse

Mark said:
I as well can't find any instance of __VA_ARGS__ in the C99
standard.

You do see that I said nothing about __VA_ARGS__ yes?

Of course it is in C99. In the PDF I have, double underscore is
written with a space, presumably to make the two characters stand
out. It means you have to search for "_ _VA_ARGS_ _" (or some inn
string like VA_ARGS).
 
M

Mark

Ben said:
You do see that I said nothing about __VA_ARGS__ yes?
It was mentioned in the Wiki link posted by OP.

And yes, I didn't search carefully, sorry for noise.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top