Boost logo

Boost Users :

From: Joel FALCOU (joel.falcou_at_[hidden])
Date: 2008-05-23 16:43:10


So, I have to generate a few template class specialisation that look
like that :

template<size_t I,size_t O, size_t M> class foo;

template<> class foo<4,0,0> {};
template<> class foo<3,1,0> {};
template<> class foo<3,0,1> {};
template<> class foo<0,3,1> {};
template<> class foo<0,1,3> {};
...
template<> class foo<1,1,2> {};

and so on for all combination of I,O,M so that M+I+O = N
(N = 4 in this example).

So I decided to use BOOST_PP to generate those specialisation for
arbirary values of N and to test the way I can enumerate the sets
of value, I wrote the following code :

#define MAX_VAL 11
#define L1 (0, (1, (2, (3, (4, (5, (6, (7, (8, (9, (10, (11,
BOOST_PP_NIL))))))))))))

#define NOP(p)

#define ADD(p) BOOST_PP_ADD( BOOST_PP_TUPLE_ELEM(3,0,p),
BOOST_PP_ADD(BOOST_PP_TUPLE_ELEM(3,1,p),BOOST_PP_TUPLE_ELEM(3,2,p)))

#define PRT(p) cout << BOOST_PP_TUPLE_ELEM(3,0,p) << " " <<
BOOST_PP_TUPLE_ELEM(3,1,p) << " " << BOOST_PP_TUPLE_ELEM(3,2,p) << endl;

#define DO(r, p) BOOST_PP_IF(BOOST_PP_EQUAL(ADD(p),MAX_VAL),PRT,NOP)(p)

int main()
{
   BOOST_PP_LIST_FOR_EACH_PRODUCT(DO, 3, (L1, L1, L1))
   return 0;
}

that use cout to display the values triplet.
This works nice but it is somehow slow for larger value of MAX_VAL
and cumbersome to extand cause of the list.

Is there anyway I can produce the same code using a better PP code ?

Thanks in advance

-- 
Joel FALCOU
Research Engineer @ Institut d'Electronique Fondamentale
Université PARIS SUD XI
France

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