Variable length arg lists for macros

Lawrence V. Cipriani lvc at cbnews.ATT.COM
Thu Sep 1 22:19:44 AEST 1988


In article <438 at ucsvc.unimelb.edu.au>, u5565522 at ucsvc.unimelb.edu.au (David Clunie) writes:
> How do people feel about the idea of preprocessor macros with variable
> length argument lists ?
I'd like it, but its not essential.  I don't mind using double parenthesis
to fake out the preprocessor.

> 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).

Only clumsy for the uncoordinated :-)  You missed one more way:

	#ifdef TRACE
	#define trace(anything)	anything
	#else
	#define trace(anything)
	#endif

and you use it like this:

	c(i)
	{
		trace( if (i == 0) printf("boom\n");)
	}

The only restriction I can think of now is that you'll have to be careful
how the comma operator is used.

-- 
Larry Cipriani, AT&T Network Systems, Columbus OH, cbnews!lvc lvc at cbnews.ATT.COM



More information about the Comp.std.c mailing list