Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

meta-programming: inherit from every class in a boost mpl::vector

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

Specifically, I wish to extend test for arbitrary many template parameters, passed as a 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 there are only two parameters then 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 is possible to do this for an arbitrary mpl::vector?

My test program looks like so:

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);
}
like image 593
Tom Avatar asked Jun 20 '11 15:06

Tom


1 Answers

You want to use mpl::inherit_linearly

like image 93
Joel Falcou Avatar answered Sep 28 '22 05:09

Joel Falcou