c++ - 模板参数数量错误的模板模板参数

标签 c++ templates

考虑一个模板类 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/

相关文章:

c++ - c++中不同类型的数组

c++ - Qt : How to send keyevent to system?

c++ - 如何设置默认的 NetBeans 项目设置?

c++ - 可变参数模板必须具有非空参数包的可调用特化才能格式正确吗?

c++ - 模板类中模板函数的困难

c++ - 如何修复已定义的类模板?

c++ - 如何设置此数组的大小 C++

c++ - 使用声明的类或枚举

c++ - QJsonValueRef 与 QJsonValue

c++ - 确定模板函数的返回类型