Variable length arg lists for macros

Steve C. Simmons scs at itivax.UUCP
Tue Sep 6 10:30:20 AEST 1988


In article <1036 at cbnews.ATT.COM> lvc at cbnews.ATT.COM (Lawrence V. Cipriani) writes:
-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 ?
-> 
-> 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, cbnews!lvc lvc at cbnews.ATT.COM

A nice hack, but one additional word of warning -- the disappearing line
of code can cause some interesting and obscure bugs.  I had one program
that worked with the trace code but failed oddly without it.  The exact
form escapes me, but it was something like

	. . .
	if ( x == m )
		trace( printf( "the consequences\n) ; )
	froob( some function ) ;

This produced rather different programs depending on the definition of
'trace'!

-- 
Steve Simmons		...!umix!itivax!vax3!scs
Industrial Technology Institute, Ann Arbor, MI.
"You can't get here from here."



More information about the Comp.std.c mailing list