模板参数数量错误的模板模板参数 [英] Template template parameter with wrong number of template parameters
问题描述
考虑具有通过模板模板参数和两个策略定义设置的策略的模板类 C:
Consider a template class C with a policy set via template template parameter and two policy definitions:
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
模板参数?
PolicyTwo
doesn't work because of wrong number of template arguments.
Is there a way to use PolicyTwo
as POLICY
template parameter if you specify the types for the additional template parameters?
我使用的是 C++03,所以别名声明不可用.我知道这个问题,但我在那里看不到解决我的问题的方法.
I'm using C++03, so alias declarations are not available. I'm aware of this question, but I don't see a solution to my problem there.
推荐答案
根据策略的使用方式,您或许可以使用继承代替别名模板进行管理:
Depending on how the policy is used, you may be able to manage with inheritance in place of alias templates:
template<int U, int V> struct PolicyTwoAdaptor {
template<class T> struct type: PolicyTwo<T, U, V> { }; };
C<int, PolicyTwoAdaptor<1, 2>::type> mc2;
这篇关于模板参数数量错误的模板模板参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!