On 9/20/2014 9:35 AM, Colin Fowler wrote:
Hi all, I'm trying to use BOOST_PP to generate some code.
I can generate the sequence int x_0 to int x_4 using the code below.
#include
#define DECL1(z, n, text) text ## _ ## n = n; #define decl1(text, count) BOOST_PP_REPEAT(count, DECL1, text) decl1 (int x, 5) What I'd like to do is write a decl2 that can be used something like this:
decl2 (int x, 5, 2)
to generate int x_0_0 to int x_4_1
I've tried nesting BOOST_PP_REPEAT but so far I've been unable to produce the desired behaviour unless I hardcode one of the iteration counts.
I'd like to also eventually write a decl3(int x, 5, 3, 4) when I understand how a decl2 would work. Any help much appreciated!
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.