模板参数中模板参数个数错误
Template template parameter with wrong number of template parameters
考虑一个模板类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,所以别名声明是不可用的。我知道这个问题,但我没有看到解决我的问题的办法。
根据策略的使用方式,您可以使用继承来代替别名模板进行管理:
template<int U, int V> struct PolicyTwoAdaptor {
template<class T> struct type: PolicyTwo<T, U, V> { }; };
C<int, PolicyTwoAdaptor<1, 2>::type> mc2;
我看不出有谁可以用你目前的机制来解决这个问题,但是你可以逆转它的工作方式,它应该可以很好地编译(甚至可以通过删除类模板参数来降低复杂性):
template <typename T> struct PolicyBase { typedef T value_type; };
template<class T> struct PolicyOne : public PolicyBase<T> { };
template<class T, int U, int V> struct PolicyTwo : public PolicyBase<T> { };
template<class POLICY> struct C { POLICY policy; typedef typename POLICY::value_type T; };
void f()
{
C<PolicyOne<int> > mc1;
C<PolicyTwo<int, 1, 2> > mc2; // doesn't work this way
}
基本思想是将类型模板参数移出策略用户,并为其提供完全实例化的策略。然后,策略通过typedef(如果需要)向策略用户提供其模板类型。
相关文章:
- C++声明模板参数阴影模板参数错误
- 为什么 CRTP 模板C++给出无效参数错误?
- (C/C++)fscanf_s从txt文件以字符形式读取数组时缺少整数参数错误
- 参数错误可能与类型不匹配有关?
- printf 和 strftime 的参数错误无效
- 如何修复"ctypes"。参数错误:参数 2:<键入"异常.类型错误">:RaspberryPi 中的错误类型"错误
- 如何修复"没有重载函数需要 2 个参数"错误C++
- 术语不计算为函数采用 1 个参数错误?
- 从带有 getline() 的文件读入一行上有多个信息得到无效参数错误
- 为什么我不能像使用 std::string::size_type 那样使用 QList::size_type?(模板参数错误)
- cudaFreeHost() 无效参数错误
- 精神语法不会编译:函数模板参数错误?
- C 功能具有参考参数错误的迭代器错误.寻求解释
- 使用getDefaultCommConfig使用无效的参数错误
- C++ 可变参数模板和模板模板参数:错误:模板参数列表中参数 1 处的类型/值不匹配
- 使用SWIG生成的Python库时,向量分配器参数错误
- 错误C2664:无法转换参数错误
- C 作为参数错误的功能
- 命令行参数错误
- 增强Python参数错误