考虑一个模板类 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;
关于c++ - 模板参数数量错误的模板模板参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15951683/