Boost logo

Boost Users :

From: Paul Mensonides (yg-boost-users_at_[hidden])
Date: 2003-05-06 21:42:34


Tanton Gibbs wrote:
> This makes sense. So, replacement is done after substitution on the
> RHS,
> not before macro argument substitution, correct?
>
> In other words, with
> #define EMPTY()
> #define MACRO( x ) x
>
> MACRO( EMPTY() )
> would have EMPTY() substituted for the formal macro parameter x and
> the RHS
> value x. Then, a macros are executed on the RHS changing EMPTY() into
> nothing (literally).

No. Replacement and rescanning are done on each argument to a function-like
macro before they are inserted into the replacement list (unless they are next
to the token-pasting operator or the stringizing operator). However, this
replacement and rescanning on the parameters happens *after* the arguments are
delineated as "actual parameters." So, the replacement list is rescanned
*after* the arguments have already been expanded. Take this example:

#define X 1, 2

#define A(x) B(x)
#define B(a, b) a + b

A(X) // 1 + 2

This is perfectly legal and correct. The argument X is delineated as a single
parameter to A. This single parameter is expanded and rescanned _prior_ to
insertion into the replacement list of A. The expanded result of the parameter
is inserted into the replacement list of A, yielding:

B(1, 2)

Then the replacement list of A is rescanned for more macros to replace--which
causes B(1, 2) to be invoked. Note that some preprocessors do not do this
expansion order correctly--notably VC++, so consider the above to be
illustrative of what is *supposed* to happen.

Make sense?

Regards,
Paul Mensonides


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net