g – Mixins,可变模板和C中的CRTP

这是这个场景:我想要一个主机类,它可以有一个可变数量的混合(不是太多的可变模板 – 参见例如 http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.103.144).但是,我也希望由主机类参数化mixins,以便它们可以引用它的公共类型(使用CRTP成语).
当尝试混合这两个问题时出现问题 – 正确的语法对我来说不清楚.
例如,以下代码无法使用g 4.4.1进行编译:
template <template<class> class... Mixins>
class Host : public Mixins<Host<Mixins>>... {
  public:
    template <class... Args>
    Host(Args&&... args) : Mixins<Host>(std::forward<Args>(args))... {}
};

template <class Host> struct Mix1 {};

template <class Host> struct Mix2 {};

typedef Host<Mix1,Mix2> TopHost;
TopHost *th = new TopHost(Mix1<TopHost>(),Mix2<TopHost>());

错误

tst.cpp: In constructor ‘Host<Mixins>::Host(Args&& ...) [with Args = Mix1<Host<Mix1,Mix2> >,Mix2<Host<Mix1,Mixins = Mix1,Mix2]’:

tst.cpp:33:   instantiated from here

tst.cpp:18: error: type ‘Mix1<Host<Mix1,Mix2> >’ is not a direct base of ‘Host<Mix1,Mix2>’

tst.cpp:18: error: type ‘Mix2<Host<Mix1,Mix2>’

有没有人有成功的经验混合可变模板与CRTP?

解决方法

以下似乎有效.我在继承的mixin类中添加了Mixins …,这些类扩展了参数包的位置.在主机模板的主体之外,必须指定Host的所有模板参数,因此Mixins …用于此目的.在身体内部,只要主机足够无需拼出所有的模板参数.一种简短的手.
#include <utility>

template <template<class> class... Mixins>
class Host : public Mixins<Host<Mixins...>>...
{
  public:
    Host(Mixins<Host>&&... args) : Mixins<Host>(std::forward<Mixins<Host>>(args))... {}
};

template <class Host> struct Mix1 {};
template <class Host> struct Mix2 {};

int main (void)
{
  typedef Host<Mix1,Mix2> TopHost;
  delete new TopHost(Mix1<TopHost>(),Mix2<TopHost>());
}

相关文章

本程序的编译和运行环境如下(如果有运行方面的问题欢迎在评...
水了一学期的院选修,万万没想到期末考试还有比较硬核的编程...
补充一下,先前文章末尾给出的下载链接的完整代码含有部分C&...
思路如标题所说采用模N取余法,难点是这个除法过程如何实现。...
本篇博客有更新!!!更新后效果图如下: 文章末尾的完整代码...
刚开始学习模块化程序设计时,估计大家都被形参和实参搞迷糊...