9

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

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 解决方案。

4

4 回答 4

6

嗯,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可以概括为接受任意类型作为构造函数参数,我不确定是否可以概括为接受任意数量的参数。

于 2011-09-08T18:17:41.350 回答
4

像这样的东西?

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。

于 2011-09-08T18:09:22.983 回答
2

我没有使用 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

于 2011-09-08T18:25:28.287 回答
1

像这样的东西应该工作:

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;
    }
};
于 2011-09-08T18:00:24.863 回答