Boost logo

Boost :

From: Paul Mensonides (pmenso57_at_[hidden])
Date: 2002-06-25 23:17:24

> Hi,
> I'd like to be able to assign a macro a new value from within another macro
> definition. Actually, come to think about it, the ability to issue
> "escaped" # pre-processor cmds from within a macro definition would be
> remarkably useful.
> Something like:
> #define frog "my"
> #define APPEND_STRING( arg1, append )
> \#redefine ###arg1 arg1 append
> APPEND_STRING( frog, " dog" )
> would (magically) expand to:
> #redefine frog "my" " dog"

How about this....

If we had a #redef directive and the ability to *evaluate* a macro in the
definition of a macro--rather than the lazy evaluation that we have now, I think
that would a much more general solution:

#define frog "my"

#define APPEND_STRING( arg1, append ) arg1 append

// ...

#redef frog @APPEND_STRING(frog, " dog")
         // ^-- force evaluation at the time of definition

I would *love* to have this feature. It would make it possible to do arithmetic
with the preprocessor in an intuitive way:

#define X 10
#define MULTIPLY(a, b) a * b

#redef X @MULTIPLY(X, 2)

The preprocessor already knows how to do the arithmetic--it does it in #if/#elif

> 1. This is very rough and sketchy and I take no account for the order and
> timing of macro expansion so don't pick it apart on that basis, its *just*
> an outline of a pre-processor feature that I have wanted for mm.. around 10
> years.

Unfortunately, many people seem to think that token-mode (as opposed to
syntax-mode) processing is evil. Sometimes people take that argument to far and
ignore the benefits that the preprocessor *can* provide. The main arguments
against the preprocessor are that it doesn't respect syntax, type, and scope.
Personally, I think the preprocessor is useful specifically *because* it doesn't
respect syntax, type, and scope. This is not always a good thing, but there are
times that it is.

> 2. Obviously, there is no "#redefine" but a little bit of shuffling around
> with #undefs and a temporary define would do the same thing.
> The command #redefine would be rather useful - header files are *littered*
> with #undef blah, #define blah's.
> Just some outlandish ideas :-)

Not outlandish to me. :)

Paul Mensonides

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