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.
> 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.
--
Ben.
I as well can't find any instance of __VA_ARGS__ in the C99 standard.
--
Mark
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);
--
char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa67f6aaa,0xaa9aa9f6,0x11f6},*p
=b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
2:{i++;if(i)break;else default:continue;if(0)case 1:putchar(a[i&15]);break;}}}
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).
--
Ben.
And yes, I didn't search carefully, sorry for noise.
--
Mark