Topic: Preprocessor substitution


Author: mfinney@inmind.com
Date: 1996/05/03
Raw View
I have the following test code...

   #define abc 1,2,3

   #define firstInList(x,y,z)    x
   #define secondInList(x,y,z)   y
   #define thirdInList(x,y,z)    z

   #define first(x)   firstInList(x)
   #define second(x)  secondInList(x)
   #define third(x)   thirdInList(x)

   x = first(abc)
   y = second(abc)
   z = third(abc)

and, using the IBM VisualAge C++ compiler (both the OS/2 3.0 version
and the NT 3.5 version), when I run it through the preprocessor I get...

   x = 1
   y = 2
   z = 3

which is exactly what I expected.  However, when I use the Microsoft
Visual C++ NT compiler, version 4.0, I get...

   x = 1,2,3
   y =
   z =

which is not what I expected.

Is what I expected correct?  I have reread the April '95 draft standard
and it seems to agree with what I expect.  In particular, it states that
parameters to a macro are FIRST bound to the replacement values and
THEN the replacement values are scanned for further macro substitutions
and then used for replacement.  And, LAST the result of the macro
substitution is then rescanned for further replacement.

That would seem to me to imply the following steps...

   x = first(abc)
   x = first(<1,2,3>)

where <...> means the string of tokens is treated as a
single unit for the purposes of substitution

   x = firstInList(1,2,3)
   x = 1

So, is my understanding correct and the Microsoft compiler incorrect
or is the IBM compiler incorrect.  If the latter, then how should the
draft standard be interpreted?

Thanks.



[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]