Article View: comp.std.c
Article #5343Re: Variable length arg lists for macros
From: Paul Gluckauf Haahr
Date: Wed, 07 Sep 1988 22:07
Date: Wed, 07 Sep 1988 22:07
48 lines
1793 bytes
1793 bytes
> 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@princeton.edu
Technical Details
Message-ID:
<3604@phoenix.Princeton.EDU>Path:
arc1-ca.pugleaf.net!utzoo!attcan!uunet!husc6!uwvax!rutgers!njin!princeton!phoenix!haahrReferences:
<438@ucsvc.unimelb.edu.au>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!attcan!uunet!husc6!uwvax!rutgers!njin!princeton!phoenix!haahr From: haahr@phoenix.Princeton.EDU (Paul Gluckauf Haahr) Newsgroups: comp.std.c Subject: Re: Variable length arg lists for macros Summary: not a general solution, but ... Message-ID: <3604@phoenix.Princeton.EDU> Date: 7 Sep 88 22:07:25 GMT References: <438@ucsvc.unimelb.edu.au> Reply-To: haahr@princeton.edu (Paul Gluckauf Haahr) Organization: Princeton University, Princeton NJ Lines: 48