|
Boost : |
From: Paul Mensonides (pmenso57_at_[hidden])
Date: 2003-01-01 05:28:54
----- Original Message -----
From: "Aleksey Gurtovoy" <agurtovoy_at_[hidden]>
Hi Aleksey,
> I am going to use our wonderful Preprocessor library to generate a
> metafunction that basically looks like this:
[...]
> I love everything about it except for the "(0, (1, (2, (3, (4,
> BOOST_PP_NIL)))))" part. I would like the above to become something along
> these lines:
>
> value = BOOST_PP_RANGE_FOLD_LEFT(
> AUX_MAX_ARITY_OP
> , -1
> , BOOST_PP_RANGE(0, 4)
> )
This one is certainly possible. However, "BOOST_PP_RANGE_FOLD_LEFT" would
just be a synonym for whatever folding macro operates on the type generated
by "BOOST_PP_RANGE." It would be to difficult to make a parametizable macro
that yields a sequence of numbers:
BOOST_PP_SEQ_FOLD_LEFT(
AUX_MAX_ARITY_OP
, -1
, BOOST_PP_SEQ_RANGE(0, 4)
)
Which is close to what you want. In fact, it is almost already there:
#include <boost/preprocessor/seq/fold_left.hpp>
#include <boost/preprocessor/seq/subseq.hpp>
#define NUMBERS \
(0)(1)(2)(3)(4)(5)(6)(7)(8)(9) \
(10)(11)(12)(13)(14)(15)(16)(17)(18)(19) \
/* ... */
#define RANGE(first, length) \
BOOST_PP_SEQ_SUBSEQ( NUMBERS, first, length ) \
/**/
BOOST_PP_SEQ_FOLD_LEFT(AUX_MAX_ARITY_OP, -1, RANGE(0, 5))
The only major difference is that BOOST_PP_SEQ_SUBSEQ takes first and length
operands, rather than first and last. Which, for the zero-case is almost
the same: first -> BOOST_PP_INC(last). It wouldn't be too difficult to
bang out a variant that goes from first -> last though.
> or, better yet,
>
> value = BOOST_PP_FOLD_LEFT(
> AUX_MAX_ARITY_OP
> , -1
> , BOOST_PP_RANGE(0, 4)
> )
>
> where 'BOOST_PP_FOLD_LEFT' is a generic algorithm that can be used on all
PP
> sequences.
>
> How hard would it be to have something like this?
With C99's variadic macros, fairly easy with lists vs. sequences. Without
them, impossible. The reason is simple, without variadics I have no way of
telling the difference between (a) and (a, b), etc.... With variadics I can
count the arguments.
> I suppose it's possible to generate the code I need using BOOST_PP_WHILE,
> but IMO that solution wouldn't be as conceptually nice and intuitive as
the
> above.
Paul Mensonides
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk