Disappearing function call
Dale Worley
drw at cullvax.UUCP
Thu Oct 2 06:16:01 AEST 1986
What I want to do is to write a function call of a variable number of
arguments:
debug(x, y, z, ...)
that will generate no code when the symbol DEBUG is not defined, but
generate a call of some function (say, debug_()) when it is. If
debug() always had the same number of arguments, we could use
#ifdef DEBUG
#define debug(x, y, z) debug_(x, y, z)
#else
#define debug(x, y, z) /* null */
but macros aren't allowed to have a variable number of arguments.
The best solutions I've heard so far are:
1) We write
debug(x Z y Z z)
using "Z" instead of ",". Using the definitions
#ifdef DEBUG
#define debug(x) debug_(x)
#define Z ,
#else
#define debug(x) /* null */
#endif
works, since the "Z"s are turned into "," after the call of debug() is
expanded. This is ugly because you have to write "Z" instead of ",".
2) We write
debug(x, y, z))
Using the definitions
#ifdef DEBUG
#define debug (debug_
#else
#define debug debug1(
#define debug1(x) /* null */
This works when DEBUG is off by replacing the call to debug() to a
call to debug1() with one argument, which is then ignored. This ugly
because of the mismatched parentheses.
Is there a good way to do this?
Dale
More information about the Comp.lang.c
mailing list