考虑模板类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
}
Run Code Online (Sandbox Code Playgroud)
PolicyTwo由于模板参数数量错误而无法正常工作.如果指定其他模板参数的类型,有没有办法PolicyTwo用作POLICY模板参数?
我正在使用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;
Run Code Online (Sandbox Code Playgroud)