【问题标题】:meta-programming: inherit from every class in a boost mpl::vector元编程:继承自 boost mpl::vector 中的每个类
【发布时间】:2011-06-20 15:08:45
【问题描述】:

我希望从 boost mpl::vector 中包含的一组类继承。这可能吗?

具体来说,我希望为任意多个模板参数扩展test,作为 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;}
};

如果我知道只有两个参数,那么我可以简单地写:

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;}
};

是否可以对任意 mpl::vector 执行此操作?

我的测试程序如下所示:

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);
}

【问题讨论】:

  • 接口继承、单继承、多继承、promiscuous继承:)要是ATL的设计者有TMP/mpl就好了:_)
  • @sehe 很好,我将在我的词汇表中添加“混杂继承”:P

标签: c++ metaprogramming boost-mpl


【解决方案1】:

你想使用mpl::inherit_linearly

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-14
    • 1970-01-01
    • 2022-01-13
    • 1970-01-01
    相关资源
    最近更新 更多