当尝试混合这两个问题时出现问题 – 正确的语法对我来说不清楚.
例如,以下代码无法使用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 heretst.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>());}总结
以上是内存溢出为你收集整理的g – Mixins,可变模板和C中的CRTP全部内容,希望文章能够帮你解决g – Mixins,可变模板和C中的CRTP所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)