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无效. 我正在使用C 03,因此别名声明不可用. 解决方法
根据策略的使用方式,您可以使用继承代替别名模板进行管理:
template<int U,int V> struct PolicyTwoAdaptor { template<class T> struct type: PolicyTwo<T,U,V> { }; }; C<int,PolicyTwoAdaptor<1,2>::type> mc2; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |