Boost logo

Boost Users :

Subject: Re: [Boost-users] BOOT_PP_REPEAT simple nesting problem
From: Edward Diener (eldiener_at_[hidden])
Date: 2014-09-21 19:03:55


On 9/21/2014 2:21 PM, Colin Fowler wrote:
> On 21-09-2014 17:51, Edward Diener wrote:
> <SNIP>
>>
>> The BOOST_PP_REPEAT macro takes a single count, so you would need to
>> pass into the macro a single repetition count while preserving the
>> individual counts in some way for BOOST_PP_REPEAT. The only way you
>> could do this is by having the 'data' field of BOOST_PP_REPEAT be more
>> complicated, ie. by having the 'data' field include both your 'text'
>> and the individual count numbers. When you need a single input to
>> include complicated data you should look at how to work with Boost PP
>> arrays, lists, tuples, and/or seqs; all of which let you incorporate a
>> number of pieces of data in a single input.
>>
>> So given:
>>
>> decl2 ( int x, 5, 2 )
>>
>> and using a Boost PP tuple,
>>
>> you want to pass to initially pass to BOOST_PP_REPEAT something like
>> (10, DECL2, (text,(5,2))) and then you can work out how to generate
>> your output for each of the 10 repetitions of DECL2.
>>
>> You can recursively call BOOST_PP_REPEAT from within BOOST_PP_REPEAT
>> processing, and while it is advantageous to use the BOOST_PP_REPEAT_z
>> form it is not necessary.
>>
>> I hope this helps you to figure out how to do what you want.
>>
>>
>
> Hi Edward, that was very helpful, thank you! I had tried using tuples
> yesterday but couldn't get the syntax right. I've come up with the
> following that's very close to doing exactly what I want (I need to
> fiddle around the ordering of the output a bit).
>
> #include <boost/preprocessor.hpp>
> #define DECL2B(z,n,text)
> BOOST_PP_CAT(BOOST_PP_CAT(BOOST_PP_CAT(BOOST_PP_TUPLE_ELEM(2, 1, text),
> n), _), BOOST_PP_TUPLE_ELEM(2, 0, text));
> #define DECL2A(z,n,text) BOOST_PP_REPEAT(BOOST_PP_TUPLE_ELEM(2, 0,
> text), DECL2B, (n,BOOST_PP_TUPLE_ELEM(2,1,text)))
> #define decl2( text, x, y ) BOOST_PP_REPEAT(y, DECL2A, (x,text))
> decl2(int x_, 3, 2)
>
> I have to use BOOST_PP_CAT as for some reason ## still gives surrounding
> spaces after a BOOST_PP_TUPLE_ELEM. Can this be made more readable or am
> I doing things the correct way?
>
> Here's the current output btw: int x_0_0; int x_1_0; int x_2_0; int
> x_0_1; int x_1_1; int x_2_1;

You have got the idea. The reason that BOOST_PP_CAT is needed instead of
direct use of '##' is that '##" immediately attempts to concatenate,
before any expansion of its left and right elements, while BOOST_PP_CAT
concatenates only after full argument expansion.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net