Boost logo

Boost :

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


On 13-08-09 04:40 AM, Mathias Gaunard wrote:
> On 07/08/13 22:25, Eric Niebler wrote:
>> 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!
>
> Why not just use BOOST_PP_REPEAT otherwise? It's simple and it always
> works.
>
> Just replace ELEM by BOOST_PP_SEQ_ELEM(N, S0)

Isn't accessing the Nth element of a PP sequence O(N)? Seems to me that
what Paul posted would be more efficient.

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