加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 模板模板参数,模板参数数量错误

发布时间:2020-12-16 05:04:14 所属栏目:百科 来源:网络整理
导读:考虑模板类C,其中包含通过模板模板参数设置的策略和两个策略定义: templateclass T struct PolicyOne { };templateclass T,int U,int V struct PolicyTwo { };templateclass T,templateclass class POLICY struct C { POLICYT policy; };void f(){ Cint,Pol
考虑模板类C,其中包含通过模板模板参数设置的策略和两个策略定义:
template<class T> struct PolicyOne { };
template<class T,int U,int V> struct PolicyTwo { };
template<class T,template<class> class POLICY> struct C { POLICY<T> policy; };

void f()
{
    C<int,PolicyOne> mc1;
    C<int,PolicyTwo<1,2> > mc2; // doesn't work this way
}

由于模板参数数量错误,PolicyTwo无效.
如果指定其他模板参数的类型,是否可以使用PolicyTwo作为POLICY模板参数?

我正在使用C 03,因此别名声明不可用.
我知道this question,但我没有看到我的问题的解决方案.

解决方法

根据策略的使用方式,您可以使用继承代替别名模板进行管理:
template<int U,int V> struct PolicyTwoAdaptor {
  template<class T> struct type: PolicyTwo<T,U,V> { }; };
C<int,PolicyTwoAdaptor<1,2>::type> mc2;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读