如何从类型列表继承,然后调用继承成员列表中的成员
How to inherit from a list of types and then call a member on the list of inherited members?
我有一组具有以下结构的类:
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包含元函数inherit
和inherit_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;
}
};
相关文章:
- C++ 双向链表:使用矩阵类成员创建和填充列表
- 模板化检查是否存在带有参数列表的类成员函数?
- 如何将类成员方法的参数列表自动填充写入可变参数?
- 为什么我需要在成员发起器列表中重复基类的模板参数?
- 如何将成员函数作为参数传递并在派生对象上执行方法列表
- 扩展初始值设定项列表和未初始化的成员
- 在成员初始值设定项列表中分配unique_ptr
- 如何在构造器的成员初始值设定项列表中调用两个函数?
- 将初始化器列表/聚合初始化转发到 std::array 成员
- 使用成员初始值设定项列表时没有匹配的函数调用错误
- 类成员函数参数列表是否可以依赖于模板参数?
- 传递可变参数时在成员初始值设定项列表中强制转换
- 解释了构造函数成员初始化列表
- C/C++intellisense 0.26.1不显示结构成员列表
- 如何从 IBM i 文件中获取成员列表
- 类成员列表的C++模板构造
- C++列表:复制成员列表中的本地列表
- 不同类的成员列表实现不起作用
- 如何从类型列表继承,然后调用继承成员列表中的成员
- 类构造函数:不允许类型不完整(在成员列表中)-VS C++w/QT