#defines with variable # arguments
Richard A. O'Keefe
ok at quintus.UUCP
Sun May 15 18:53:57 AEST 1988
In article <2886 at phoenix.Princeton.EDU>, lgy at pupthy2.PRINCETON.EDU (Larry Yaffe) writes:
> In article <2633 at tekcrl.TEK.COM> terryl at tekcrl.tek.com writes:
> >In article <2855 at phoenix.Princeton.EDU+ (lgy at pupthy2.PRINCETON.EDU) I wrote:
> >+ Are there any versions of cpp which allow one to define macros which
> >+accept variable numbers of arguments?> > I came across this kludge years ago, hope it helps:
> > The TRICK is to ADD an extra level of parentheses, using it like thus:
> > sprintf( (buffer,"<what-ever-string-you-want>",<varargs>) );
> >This makes cpp THINK there is only one argument to the macro; I know you
> >don't want to muck with the sources and just want to put in the #define
> A number of people suggested variants of the above.
Just a wild heretical suggestion, but there is *another* macro processor
which comes standard with UNIX. System V has it and BSD has it, and there
is a rather nice public domain version. I refer, of course, to m4.
What I suggest is a one-time conversion:
sprintf(S,F) -> sprintf0(S,F)
sprintf(S,F,A) -> sprintf1(S,F,A)
...
sprintf(S,F,A,B,C,D,E,F)-> sprintf6(S,F,A,B,C,D,E,F)
however many you need. This is easy enough to do with m4. Then you write
your cpp macros for each sprintfN, which is a bit of a pain, but means you
don't have to keep using m4.
It is precisely because m4 is there to fall back on when you need REAL
macro-power that cpp can be so weak.
More information about the Comp.unix.wizards
mailing list