[comp.lang.c++] How SHOULD these macros work?

schweitz@lexvmc.iinus1.ibm.com ("Eric Schweitz") (02/15/91)

From: schweitz@lexvmc.iinus1.ibm.com
Date: 15 February 1991, 10:41:22 EST

I have a question concerning the preprocessor. Namely, what behavior
SHOULD be expected from the following:

#define one val1,val2
#define two(arg1,arg2)  arg1+arg2
....
   two (one)  // ?

Should this evaluate to:

a) val1+val2
  -or-
b) val1,val2+

Please excuse me if this is a neophyte question.
Also, is this behavior different of the same for C and C++?

Thanks,
Schweitz        schweitz@lexvmc.iinus1.ibm.com

gwu@nujoizey.tcs.com (George Wu) (02/19/91)

In article <9102151600.AA03343@ucbvax.Berkeley.EDU>,
schweitz@lexvmc.iinus1.ibm.com ("Eric Schweitz") writes:
|> 
|> I have a question concerning the preprocessor. Namely, what behavior
|> SHOULD be expected from the following:
|> 
|> #define one val1,val2
|> #define two(arg1,arg2)  arg1+arg2
|> ....
|>    two (one)  // ?
|> 
|> Should this evaluate to:
|> 
|> a) val1+val2
|>   -or-
|> b) val1,val2+

     My (Sun 4.1) cpp evaluates this as an error, and I think it's correct,
ie. your sample code isn't legal (whatever legal is for the cpp).  When
expanding macros, the cpp doesn't expand additional macros until the current
substitution is complete.  In other words, the cpp sees "two" and tries to
grab the two arguments within the parenthesis.  It finds only one there
because the cpp doesn't try to expand "one" until "two" is done.


|> Also, is this behavior different of the same for C and C++?

     For the most part, there are no C or C++ specific definitions of cpp
behavior.  The only *behavior* difference I know of is having the cpp strip
out C++ // comments the same way it removes C comments, and I consider this
implementation dependant.  There's also the addition of predefined macros,
such as __cplusplus and ANSI, but that's just use of existing cpp behavior.

							George

----
George J Wu, Software Engineer        | gwu@tcs.com or uunet!tcs!gwu
Teknekron Communications Systems, Inc.| (415) 649-3752
2121 Allston Way, Berkeley, CA, 94704 | Quit reading news.  Get back to work.