如何从类型列表继承,然后调用继承成员列表中的成员

How to inherit from a list of types and then call a member on the list of inherited members?

本文关键字:成员 列表 继承 调用 然后 类型      更新时间:2023-10-16

我有一组具有以下结构的类:

class U
{
public:
    explicit U(int) { ... }
    U() {...}
    Init(int) {...}
};

我需要能够将这些类中的一个或多个组成一个类x。伪代码:

template<class TypeSequence>
class X that derives publicly from all the classes in TypeSequence
{
    X(int): all bases are initialized with the integer passed 
    {}
    //if the above constructor is impossible, then the following will do as well:
    X(int)
    {
        Call Init on all bases and pass the given int to them.
    }
};
我想我需要很多的mpl,但我不是很擅长。我要做的事情是可行的吗?一个代码示例就很好了。

我的错误:忘了说我不会使用c++ 11的特性。我正在寻找一个MPL解决方案。

好吧,Boost。MPL包含元函数inheritinherit_linearly,您可以将它们与for_each组合以获得第二个变体(带有init函数)。或者只使用boost::mpl::fold和自定义元函数:

struct Null_IntConstructor
{
  Null_IntConstructor(int) { }
};
struct InheritFrom_IntConstructor_Folder
{
  template<typename T1, typename T2>
  struct apply
  {
    struct type : T1, T2
    {
      type(int x) : T1(x), T2(x) { }
    };
  };
};
template<typename Bases>
struct InheritFrom_IntConstructor 
  : boost::mpl::fold<Bases, 
                     Null_IntConstructor,
                     InheritFrom_IntConstructor_Folder>::type
{
  InheritFrom_IntConstructor(int x) 
    : boost::mpl::fold<Bases, 
                       Null_IntConstructor,
                       InheritFrom_IntConstructor_Folder>::type(x) 
  { }
};

使用例子:

#include <iostream>
#include <boost/mpl/fold.hpp>
#include <boost/mpl/vector.hpp>
struct A
{
  A(int x) { std::cout << "A::A " << x << std::endl; }
};
struct B
{
  B(int x) { std::cout << "B::B " << x << std::endl; }
};
struct C
{
  C(int x) { std::cout << "C::C " << x << std::endl; }
};
int main()
{
  InheritFrom_IntConstructor< boost::mpl::vector<A, B, C> >(1);
}

元函数InheritFrom_IntConstructor可以泛化为接受任意类型作为构造函数参数,我不确定是否可以泛化为接受任意数量的参数。

像这样?

template <typename ...BaseClasses>
class Aggregator : public BaseClasses...
{
public:
    Aggregator(int i) : BaseClasses(i)...
    {}
};

使用例子:

Aggregator<U, V, W> a(10);
Aggregator<U, V> b(15);
Aggregator<W> c(20);

注意:它使用可变模板,所以需要c++ 11

我不使用Boost,我不确定我的解决方案在多大程度上更接近您需要的。但我还是把它贴出来了:

template<typename typeseq>
struct X : typeseq::head, X<typename typeseq::tail>
{
   typedef typename typeseq::head base;
   typedef X<typename typeseq::tail> recursebase;
   X(int i) : base(i), recursebase(i) {}
   void init(int i) 
   {
       base::init(i);
       recursebase::init(i);
   }
};
template<>
struct X<null_type> 
{
   X(int i) {}
   void init(int i)  { } 
};
然后,测试代码:
typedef typelist<S,typelist<U>> typeseq;
X<typeseq> x(10);
x.init(100);

在线演示:http://ideone.com/e6tuM

应该这样做:

template<class typeOne>
class X1 : public typeOne
{
    X(int b): typeOne(b)
    {}
};
template<class typeOne, class typeTwo>
class X2 : public typeOne, public typeTwo
{
    X(int b): typeOne(b), typeTwo(b)
    {}
};
template<class typeOne, class typeTwo, class TypeThree>
class X3 : public typeOne, public typeTwo, public typeThree
{
    X(int b): typeOne(b), typeTwo(b), typeThree(b)
    {}
};

或者,如果您愿意为每个对象浪费几个字节,您可以使用占位符,并且只使用较大的占位符。对于每个实例,每个未使用的基类型最多浪费一个字节。

template<int>
class PlaceHolder { PlaceHolder(int){} };
template<
         class typeOne, 
         class typeTwo=PlaceHolder<2>, 
         class TypeThree=PlaceHolder<3>,
         class TypeFour=PlaceHolder<4>,
         class TypeFive=PlaceHolder<5>
        >
class X : 
         public typeOne, 
         public typeTwo, 
         public typeThree,
         public typeFour,
         public typeFive
{
    X(int b)
    : typeOne(b), 
    typeTwo(b), 
    typeThree(b),
    typeFour(b),
    typeFive(b)
    {}
    X(const X& b)
    : typeOne(b), 
    typeTwo(b), 
    typeThree(b),
    typeFour(b),
    typeFive(b)
    {}
    X& operator=(const X& b) {
        typeOne::operator=(b);
        typeTwo::operator=(b);
        typeThree::operator=(b);
        typeFour::operator=(b);
        typeFive::operator=(b);}
        return *this;
    }
};