【发布时间】:2011-11-13 05:22:50
【问题描述】:
我有一组具有以下结构的类:
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 解决方案。
【问题讨论】:
-
+1 好问题。顺便说一下,你听说过mixin吗?也许,它可以在这里应用,或者它的变体?
-
我以前用Boost.MPL 做过这个,但我手头没有代码,现在也没有时间写一个完整的答案。如果到那时还没有人回答,我今晚会重新讨论这个问题。作为提示,我记得制作了一个特殊的 mixin,它接受两个
boost::mpl::vector<>迭代器作为模板参数。 -
U 和 TypeSequence 有什么关系?
-
@Nawaz:我认为 mixin 不适用。我不需要有关 U 类中派生类的信息。我需要类似于 mpl::inherit_linearly 的东西,我只是不知道语法。我也不知道如何初始化基础或在它们上调用 Init
-
肯定需要C++0x的可变参数模板特性。
标签: c++ templates boost metaprogramming boost-mpl