Metaprogramming: inherit from each class in boost mpl :: vector

I want to inherit from a set of classes contained in boost mpl :: vector. Is it possible?

In particular, I want to expand testfor an arbitrary set of template parameters passed as mpl :: vector.

template<class T>
struct Slice
{
public:
  virtual void foo(T v) const = 0;
};

struct A{};
struct B{};

template <class T1, class T2>
struct test : public Slice<T1>, public Slice<T2>
{
  void foo(T1 a) const {std::cout<<"A"<<std::endl;}
  void foo(T2 b) const {std::cout<<"B"<<std::endl;}
};

If I know that there are only two parameters, I can simply write:

template <class mpl_vector_t >
struct test : public Slice<typename mpl::at<mpl_vector_t,mpl::int_<0> >::type >, 
          public Slice<typename mpl::at<mpl_vector_t,mpl::int_<1> >::type >
{
  typedef typename mpl::at<mpl_vector_t,mpl::int_<0> >::type T1;
  typedef typename mpl::at<mpl_vector_t,mpl::int_<1> >::type T2;

  void foo(T1 a) const {std::cout<<"A"<<std::endl;}
  void foo(T2 b) const {std::cout<<"B"<<std::endl;}
};

Is it possible to do this for an arbitrary mpl :: vector?

My test program looks like this:

int
main  (int ac, char **av)
{
  A a;
  B b;
  // test<A,B> t; //original
  test<mpl::vector<A,B> > t;  //mpl::vector with 2 elements
  Slice<A>* Sa = &t;
  Slice<B>* Sb = &t;
  Sa->foo(a);
  Sb->foo(b);
}
+5
source share
1 answer

Do you want to use mpl :: inherit_linearly

+5
source

All Articles