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

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

概述这是这个场景:我想要一个主机类,它可以有一个可变数量的混合(不是太多的可变模板 – 参见例如 http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.103.144).但是,我也希望由主机类参数化mixins,以便它们可以引用它的公共类型(使用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 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所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1234203.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存