Boost logo

Boost :

From: Paul Mensonides (pmenso57_at_[hidden])
Date: 2003-06-26 15:55:02

> Can you elaborate some more on this. Do you mean that when a
> library macro calls some user-defined macro, which in turn
> calls the same library macro, then it will fail to evaluate?

Yes. Macros are not allowed to recurse. This means that something like

#define true false
#define false true

true // expands to "false", then expands to "true", then stops
false // expands to "true", then expands to "false", then stops

The problem, relative to library primitives that invoke user-defined
macros, is that a simple macro of the form WHILE(pred, op, state)
prohibits the use of WHILE when WHILE invokes the predicate and
operation macros that are supplied by the user. What the pp-lib does is
define a large set of WHILE macros (e.g.) all implemented similarly. It
then defines "WHILE" as an object-like macro that expands to the next
"available" (i.e. not disabled) WHILE macro. So the expression:

WHILE(pred, op, state)

Is actually the invocation of an object-like macro (WHILE) that expands
to some available WHILE_xxx function-like macro which is invoked against
the parameters.

> I have to admit that I don't grok the magic BOOST_PP uses to
> make recursion work. Could the same technique be used here
> without duplicating a lot of code from the library?

No. First, you'd have to define a large set of macros for several of
the constructs that call user-defined macros (256 in the pp-lib).
Second, you'd have to define a new library state for that construct (or
make it act like another which requires changing other parts of the
library). Third, you'd have to double the number of macros required to
do the automatic recursion on buggy preprocessors. Fourth, you'd have
to double number of implementation macros for EDG-based preprocessors to
make it perform at any kind of reasonable speed. That is about what it
would take to make "some" of the structures work. Some of the pp-lib's
algorithms don't allow recursion because of this explosion in the number
of macros.

In Chaos, OTOH, recursion is abstracted and everything that takes
user-defined macros/lambda expressions is reentrant (i.e. recursive).
Under the Chaos model, defining algorithms that operate like you mention
above is pretty easy. Each one would require about three or four
macros, each one would be reentrant, and each one would be overloaded on
lambda expression vs. user-defined macro.

Paul Mensonides

Boost list run by bdawes at, gregod at, cpdaniel at, john at