class composition

hello, There is an example listed in "C++ template metaprogramming" to generate a class with its members from an mpl sequence: template <typename T> struct wrap { typedef T value; }; typedef mpl::vector< type1, type2, type3 > member_types; mpl::inherit_linearly< member_types, mpl::inherit< wrap<_2>, _1 > >::type generated; but then, the generated class did not look like this: class C { type1 t1; type2 t2; type3 t3; }; Can one do that with BOOST_PP and sequences from preprocessor libs instead? regards,

Hicham Mouline a écrit :
hello, There is an example listed in "C++ template metaprogramming" to generate a class with its members from an mpl sequence:
template <typename T> struct wrap { typedef T value; };
typedef mpl::vector< type1, type2, type3 > member_types; mpl::inherit_linearly< member_types, mpl::inherit< wrap<_2>, _1 > >::type generated;
but then, the generated class did not look like this:
class C { type1 t1; type2 t2; type3 t3; };
Can one do that with BOOST_PP and sequences from preprocessor libs instead?
Yes, something like : #define MEMBER(z,n,text) BOOST_PP_CAT(text,n) BOOST_PP_CAT(t,n); class C { BOOST_PP_ENUM_(3,MEMBER,type) }; A more generic version can be done if you pass a tuple of symbol as ENUM data -- ___________________________________________ Joel Falcou - Assistant Professor PARALL Team - LRI - Universite Paris Sud XI Tel : (+33)1 69 15 66 35

AMDG Hicham Mouline wrote:
There is an example listed in "C++ template metaprogramming" to generate a class with its members from an mpl sequence:
template <typename T> struct wrap { typedef T value; };
typedef mpl::vector< type1, type2, type3 > member_types; mpl::inherit_linearly< member_types, mpl::inherit< wrap<_2>, _1 > >::type generated;
but then, the generated class did not look like this:
class C { type1 t1; type2 t2; type3 t3; };
Can one do that with BOOST_PP and sequences from preprocessor libs instead?
You can--or you can just use fusion.
boost::fusion::as_vector

-----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Steven Watanabe Sent: 13 January 2009 18:20 To: boost-users@lists.boost.org Subject: Re: [Boost-users] class composition
AMDG
You can--or you can just use fusion.
boost::fusion::as_vector
::type v;
boost::fusion::at_c<0>(v); // object of type type1 boost::fusion::at_c<1>(v); // object of type type2
You mean I would have to literally actually write, if for e.g. I have 20 types, class C { .... boost::fusion::at_c<0>(v); boost::fusion::at_c<1>(v); ... boost::fusion::at_c<19>(v); } I like the PP version better as I don't need to change anything if later I add 1 more type to the vector. Am I understanding you right?
In Christ, Steven Watanabe

Hicham Mouline a écrit :
You mean I would have to literally actually write, if for e.g. I have 20 types,
class C { .... boost::fusion::at_c<0>(v); boost::fusion::at_c<1>(v); ... boost::fusion::at_c<19>(v); }
No You instantiate the fusion object, then for accessing the ith member, you use at_c -- ___________________________________________ Joel Falcou - Assistant Professor PARALL Team - LRI - Universite Paris Sud XI Tel : (+33)1 69 15 66 35
participants (3)
-
Hicham Mouline
-
Joel Falcou
-
Steven Watanabe