Boost logo

Boost :

From: Vesa Karvonen (vesa.karvonen_at_[hidden])
Date: 2001-06-30 14:46:45


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)
  #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


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