开发者

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 ele开发者_如何学Cments
  Slice<A>* Sa = &t;
  Slice<B>* Sb = &t;
  Sa->foo(a);
  Sb->foo(b);
}


You want to use mpl::inherit_linearly

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜