Variable length arg lists for macros
Paul Gluckauf Haahr
haahr at phoenix.Princeton.EDU
Thu Sep 8 08:07:25 AEST 1988
> How do people feel about the idea of preprocessor macros with variable
> length argument lists ?
>
> However this can not be done with macro calls. Wouldn't it be nice to be
> able to do something like ...
>
>
> #ifdef TRACE
> #define tracef(s,...) printf(s,...)
> #else
> #define tracef(s,...) /* nothing */
> #endif
>
> At present, this CANNOT BE DONE, without nesting parentheses (clumsy) or
> using a call to an empty function (inefficient, unless you have inline
> integration in an optimizing compiler).
well, if your trace function has no more arguments than printf,
you can always do
#ifdef TRACE
#define tracef printf
#else
#define tracef 1 ? 0 :
#endif
note the complete absence of parenthese. if you want arguments,
the syntax gets ugly, but can work. for example,
#ifdef TRACE
#define trace(n) (debug > n) ? 0 : printf
#else
#define trace(n) 1 ? 0 :
#endif
usage is then
trace(3)("n = %d\n", n)
making that print to stderr probably requires an additional function
call, but only when tracing is enabled.
[ this trick is not my own. peter honeyman (now umix!honey) showed me
a variant in 1984 that is in the hdb uucp sources, used to implement
logging for uucico -x<number> ]
paul haahr
princeton!haahr haahr at princeton.edu
More information about the Comp.std.c
mailing list