Boost logo

Boost :

Subject: Re: [boost] [preprocessor] nested for_each not working like expected
From: Eric Niebler (eniebler_at_[hidden])
Date: 2013-08-07 16:25:48


On 13-08-07 12:58 PM, paul Fultz wrote:
> As others have stated, `BOOST_PP_SEQ_FOR_EACH` is not reentrant. One simple
> workaround is to use deferred expressions, like this:
>
> #define BOOST_PP_SEQ_FOR_EACH_R_ID() BOOST_PP_SEQ_FOR_EACH_R
> #define DEFER(x) x BOOST_PP_EMPTY()
>
> #define S0 (0)(1)(2)(3)
> #define S1 (5)(6)(7)(8)
> #define M4(R, DATA, ELEM) (DATA,ELEM)
> #define M2(R, DATA, ELEM) DEFER(BOOST_PP_SEQ_FOR_EACH_R_ID)()(R, M4, ELEM, S1);
> BOOST_PP_EXPAND(BOOST_PP_SEQ_FOR_EACH_R(1, M2, ~, S0))
>
> If more depths are needed, just add more `BOOST_PP_EXPAND` macros. Also, you
> can reuse the recursion state so you could write `M2` like this:
>
> #define M2(R, DATA, ELEM) DEFER(BOOST_PP_SEQ_FOR_EACH_R_ID)()(1, M4, ELEM, S1);
>
> or like this, if you don't know the original recursion state:
>
> #define M2(R, DATA, ELEM) DEFER(BOOST_PP_SEQ_FOR_EACH_R_ID)()(BOOST_PP_DEC(R), M4, ELEM, S1);

I won't pretend to understand how this works, but it does. Thanks!

-- 
Eric Niebler
Boost.org

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