Boost logo

Boost Users :

From: Hicham Mouline (hicham_at_[hidden])
Date: 2008-08-15 11:27:26


hello,
relating to my earlier post, the code for the 2-seq and 3-seq cross product is:

#include <boost/mpl/pair.hpp>
#include <boost/mpl/placeholders.hpp>
#include <boost/mpl/fold.hpp>
#include <boost/mpl/vector/vector10.hpp>
#include <boost/mpl/copy.hpp>
#include <boost/mpl/transform.hpp>

namespace mpl = boost::mpl;
using namespace mpl::placeholders;

template <typename T>
struct pair_with : mpl::pair<T, _> {};

template <typename pair>
struct triple_with : mpl::vector3<typename pair::first, typename pair::second, _> {};

// public metafunction
template<typename Sequence1, typename Sequence2>
struct sequence2_cross_product :
  mpl::fold<
    Sequence1,
    mpl::vector0<>,
    mpl::copy<
     mpl::transform<
      Sequence2,
      pair_with<_2>
>,
     mpl::back_inserter<_1>
>
>
{};

template< typename SequencePair, typename Sequence>
struct seq_seqpair_product :
  mpl::fold<
    SequencePair,
    mpl::vector0<>,
    mpl::copy<
     mpl::transform<
      Sequence,
      triple_with<_2>
>,
     mpl::back_inserter<_1>
>
>
{};

// public metafunction
template<typename Sequence1,typename Sequence2,typename Sequence3>
struct sequence3_cross_product : seq_seqpair_product< typename sequence2_cross_product<Sequence1,Sequence2>::type, Sequence3>
{};

usage is like:

S1: any MPL sequence with T11, T12, T13...T1k types
S2: any MPL sequence with T21, T22, T23...T2m types
S3: any MPL sequence with T31, T32, T33...T3n types

typedef sequence2_cross_product<S1, S2>::type binary_product;
/* this will be a mpl::vector< pair<T11, T21>,
                                pair<T11, T22>,
                                ...
                                pair<T11, T2m>,
                                pair<T12, T21>,
                                pair<T12, T22>,
                                ...
                                pair<T1k, T2m> > */

// and

typedef sequence3_cross_product<S1, S2, S3>::type ternary_product;
/* this will be same mpl::vector, but with
  vector3<T11, T21, T31> ..... instead */

1) I'm trying to make these metafunctions general enough,
perhaps in the future i might use them for something
i'm not thinking about now....
i would like to let the caller (me), choose what tuple/template
to embed the resulting Tij, Tkl in...

. for the sequence2_cross_product metafunction, mpl::pair,
or mpl::vector2 or any class template that takes 2 type template
arguments, or i could restrict the user to something that
interacts well with mpl, is it "lambda expressions" or "metafct"
or "metafct class"?

. for the sequence3_cross_product metafunction, mpl::vector3 or
any class template that takes 3 type template args... same question

2) I have a class template RT (not metafct or metafct class),
that takes 3 template args, for each of these args, i have a
list of possible types (in an mpl sequence),
then i have a class R.

I would like to define

2.1. typedef boost::variant< R, RT<T11, T21, T31>, RT<T11, T21, T32>.... > R;

2.2. do an explicit template instantiation of all the RT<..,..,..>

Should i use Boost.PP for this, e.g.:

#include <boost/preprocessor/seq/for_each_product.hpp>
#include <boost/preprocessor/seq/enum.hpp>
#define Type1 (T11)(T12)(T13)...(T1k)
#define Type2 (T21)(T22)(T23)...(T2k)
#define Type3 (T31)(T32)(T33)...(T3k)
#define M(r, product) template class RT<BOOST_PP_SEQ_ENUM(product)>;
BOOST_PP_SEQ_FOR_EACH_PRODUCT(M, (Type1)(Type2)(Type3))
#undef Type1
#undef Type2
#undef Type3
#undef M

and what about variant<...>, how would i write it with PP?

comment, thoughsts are appreciated,

thanks,

regards,


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