Topic: Preprocessor Behavior
Author: "Paul Mensonides" <leavings@attbi.com>
Date: Sat, 15 Dec 2001 08:51:50 CST Raw View
I have a question about preprocessor behavior:
--- example ---
void ab() { // test function
return;
}
#define CONCAT_1(A, B) A ## B
#define CONCAT_2(A, B) CONCAT_1(A, B)
#define DELAY(NAME) NAME
#define A1 a
#define B1 b
#define A2() a
#define B2() b
#define LHS (
#define RHS )
int main() {
DELAY(CONCAT_1)(A1, B1)();
DELAY(CONCAT_1) LHS A1, B1 RHS ();
CONCAT_1 ( a, b ) ();
DELAY(CONCAT_1)(A2(), B2())();
DELAY(CONCAT_2)(A1, B1)();
return 0;
}
--- end example ---
I would expect all of the above lines to expand to the same thing, i.e.
'ab()' five times.
I preprocessed this file with Comeau C++, Borland BCC55, VC++6, and VC++7;
here are my results:
Comeau -and- Borland both produced the following (for the macro expansion
section of the 'code' above):
A1B1();
CONCAT_1 ( a, b ) ();
ab();
a b();
ab();
Both versions of Visual C++ produced the following:
A1B1();
CONCAT_1 ( a, b ) ();
ab();
ab();
ab();
Which is correct? It wouldn't surprise me if they are all wrong. For
instance, why doesn't rescanning pick up CONCAT_1 produced by the second
line--it is exactly equivalent to the original line 3. Also, what is the
difference between line 1 and line 4 (i.e. function-like macro vs. no-arg
macro)?--yet in all four preprocessors it produces a different output. Why
does using CONCAT_2 make a difference in the last line since it simply
delegates its parameters to CONCAT_1?
This is what I expected:
// after 1st pass
CONCAT_1 ( a , b ) ();
CONCAT_1 ( a , b ) ();
ab();
CONCAT_1 ( a , b ) ();
CONCAT_2 ( a , b ) ();
// after 2nd pass
ab();
ab();
ab();
ab();
CONCAT_1(a, b) ();
// after 3rd pass
ab();
ab();
ab();
ab();
ab();
Where am I going wrong, or conversly where are these preprocessors going
wrong?
Thanks for the help,
Paul Mensonides
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]