Boost logo

Boost :

From: Vesa Karvonen (vesa.karvonen_at_[hidden])
Date: 2001-06-30 15:37:55


----- Original Message -----
From: "David Abrahams" <david.abrahams_at_[hidden]>

> I am probably being dense. Why would one want this in lieu of
> #define ADD(x,y) ((x)+(y))
>
> ??

Because BOOST_PREPROCESSOR_ADD/SUB(X,Y) results in a single token. This means
that you can pass it as a parameter to other preprocessor metaprogramming
primitives. For example:

    BOOST_PREPROCESSOR_REPEAT
    ( BOOST_PREPROCESSOR_ADD
      ( 10
      , 20
      )
    , ...
    , ...
    , ...
    )

The above would not work with ADD(x,y).

BOOST_PREPROCESSOR_ADD() and BOOST_PREPROCESSOR_SUB() may be quite useful.
About a week ago I had a repetition problem where I thought I needed the
primitives (or at least SUB - IIRC), but I decided to leave the issue at the
time. Unfortunately I haven't yet had the time to take another look at the
problem I was trying to solve then.

> ----- Original Message -----
> From: "Vesa Karvonen" <vesa.karvonen_at_[hidden]>
> To: <boost_at_[hidden]>
> Sent: Saturday, June 30, 2001 3:46 PM
> Subject: [boost] PREPROCESSOR library review
>
>
> > Hi,
> >
> > A few hours ago, I invented a technique for implementing ADD(X,Y) and
> SUB(X,Y)
> > using linear amount of tokens. In other words, code like this does what
> you'd
> > expect:
> >
> > assert(BOOST_PREPROCESSOR_ADD(5,10) == 15);
> > assert(BOOST_PREPROCESSOR_SUB(10,5) == 5);
> >
> > The solution is a repetition primitive that repeatedly calls the specified
> > macro with the specified parameter (here without the prefix):
> >
> > #define REPEAT_CALL(N,M,P) REPEAT_CALL_DELAY(N,M,P)
> > #define REPEAT_CALL_DELAY(N,M,P) REPEAT_CALL##N(M,P)
> > #define REPEAT_CALL0(M,P)
                                ^ insert P above

> > #define REPEAT_CALL1(M,P) M(P)
> > #define REPEAT_CALL2(M,P) M(M(P))
> > #define REPEAT_CALL3(M,P) REPEAT_CALL2(M,M(P))
> > #define REPEAT_CALL4(M,P) REPEAT_CALL3(M,M(P))
> > #define REPEAT_CALL5(M,P) REPEAT_CALL4(M,M(P))
> > // ...
> >
> > In other words, this evaluates to something like:
> >
> > M(M(M(M(M(M(P))))))
> >
> > The ADD and SUB primitives are then implemented like this:
> >
> > #define BOOST_PREPROCESSOR_ADD(X,Y)\
> > BOOST_PREPROCESSOR_REPEAT_CALL(Y,BOOST_PREPROCESSOR_INC,X)
> >
> > #define BOOST_PREPROCESSOR_SUB(X,Y)\
> > BOOST_PREPROCESSOR_REPEAT_CALL(Y,BOOST_PREPROCESSOR_DEC,X)
> >
> > ...
> >
> > I have also made some updates, based on review comments, to the library
> > documentation.
> >
> > If you want, I can update the library files, so that you can test and try
> the
> > new ADD and SUB primitives.
> >
> > It is highly likely that other interesting and useful operations could be
> > implemented in the future.
> >
> > -Vesa
> >
> >
> >
> > Info: http://www.boost.org Unsubscribe:
> <mailto:boost-unsubscribe_at_[hidden]>
> >
> > Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
> >
> >
> >
>
>
> Info: http://www.boost.org Unsubscribe:
<mailto:boost-unsubscribe_at_[hidden]>
>
> Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
>
>
>


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk