[comp.lang.c++] #define

dgorton@jupiter.newcastle.edu.au (David Mark Gorton) (05/30/91)

Is there a simple way to have #defines with a variable length parameter list?

Example of Problem:
-------------------
I wish to do something like the following in a "debug.h" file

#define nl << "\n"
#define endl << "\n" << flush
	(are these defined somewhere ?? - I have seen them in some manuals)

#define DEBUG_PRINT(a) if (debug) cout << a endl
#define DEBUG_PRINT2(a, b) if (debug) cout << a << b endl
#define DEBUG_PRINT3(a, b, c) if (debug) cout << a << b << c endl
#define DEBUG_PRINT4(a, b, c, d) if (debug) cout << a << b << c << d endl
etc. 

with one (or perhaps two) #define(s) if possible. Can it be achieved with the
# and ## preprocessor operators for example? 

I know about #ifdef so please dont write pages on the example used for 
simplicity above. I am also aware of variable length parameter lists, va_arg() 
etc - but wish to avoid them if a simpler solution is possible.

Thanks in advance,
Dave 

---------------------------------------------------------------------------
Dave Gorton.     Department of Computer Science,
                 University of Newcastle,
                 Newcastle, 2308, Australia.       .-_|\
Telephone   :    (049) 216 034                    /     \
                                                  \.--._/
Internet    :    dgorton@cs.newcastle.edu.au           v
---------------------------------------------------------------------------

davidm@uunet.UU.NET (David S. Masterson) (05/31/91)

>>>>> On 29 May 91 23:52:08 GMT, dgorton@jupiter.newcastle.edu.au (David Mark
>>>>> Gorton) said:

David> Example of Problem:
David> -------------------
David> I wish to do something like the following in a "debug.h" file

David> #define DEBUG_PRINT(a) if (debug) cout << a endl
David> #define DEBUG_PRINT2(a, b) if (debug) cout << a << b endl
David> #define DEBUG_PRINT3(a, b, c) if (debug) cout << a << b << c endl
David> etc. 

How about:

#define DEBUG_PRINT(a) if (debug) cout << a << endl
				// note ----^^

then use it as follows:

	DEBUG_PRINT(a);
	DEBUG_PRINT(a << b);
	DEBUG_PRINT("a = " << a);
	DEBUG_PRINT(a << b << c);	// etc.

David> I know about #ifdef so please dont write pages on the example used for
David> simplicity above. I am also aware of variable length parameter lists,
David> va_arg() etc - but wish to avoid them if a simpler solution is
David> possible.

If you're going to try to get fancy, then you'll probably want to build a
function and use va_arg().   The above example has been more than sufficient
for me, especially after hooking it up with the Tracer object defined in E&S
(chapter 3, I think).
--
====================================================================
David Masterson					Consilium, Inc.
(415) 691-6311					640 Clyde Ct.
uunet!cimshop!davidm				Mtn. View, CA  94043
====================================================================
"If someone thinks they know what I said, then I didn't say it!"

kvt@druwy.ATT.COM (TranKV) (05/31/91)

In article <dgorton.675561128@jupiter.newcastle.edu.au> dgorton@jupiter.newcastle.edu.au (David Mark Gorton) writes:
>Is there a simple way to have #defines with a variable length parameter list?

Yes. And you had it without knowing it:

>#define DEBUG_PRINT(a) if (debug) cout << a endl

With the above, you can do:

    DEBUG_PRINT(a);
    DEBUG_PRINT(a << b);
    DEBUG_PRINT(a << b << c);
    DEBUG_PRINT(a << b << c << ...);

I think you can do this because cpp doesn't recognize '<<' as special
token. It treats the whole thing in parens as a single parameter.
BTW, at least in cfront 2.X, endl is predefined as '\n'. 

Kim Tran
AT&T Bell Labs
kvt@druwy.att.com