模板函数作为模板仿函数的模板参数

时间:2016-05-17 14:59:07

标签: c++ templates

我想做这样的事情,是否有人知道这是否可能?

template<typename T> using pLearn = T (*)(T, T, const HebbianConf<T> &);
template<typename T> using pNormal = T (*)(T, T);
template<typename T> using pDerivative = T (*)(T, T, T);

template <class Type, pLearn LearnCB, pNormal NormalCB, pDerivative DerivCB>
class TransfFunction {
public:
  static Type learn(Type a, Type b, const HebbianConf<Type> &setup) { return LearnCB<Type>(a, b, setup); };
  static Type normal(Type a, Type b) { return NormalCB<Type>(a, b); };
  static Type normal(Type a, Type b, Type c) { return DerivCB<Type>(a, b, c); };
};

错误:

In file included from /Functions.cpp:2:0:
/Functions.h:207:23: error: ‘pLearn’ is not a type
 template <class Type, pLearn LearnCB, pNormal NormalCB, pDerivative DerivCB>
                       ^
/Functions.h:207:39: error: ‘pNormal’ is not a type
 template <class Type, pLearn LearnCB, pNormal NormalCB, pDerivative DerivCB>
                                       ^
/Functions.h:207:57: error: ‘pDerivative’ is not a type
 template <class Type, pLearn LearnCB, pNormal NormalCB, pDerivative DerivCB>

1 个答案:

答案 0 :(得分:4)

错误说明了一切:

In file included from /Functions.cpp:2:0:
/Functions.h:207:23: error: ‘pLearn’ is not a type
 template <class Type, pLearn LearnCB, pNormal NormalCB, pDerivative DerivCB>
                       ^

pLearn不是类型 - pLearn是别名模板。模板非类型参数需要类型。您需要为其提供类型参数。其他两个相同:

template <class Type,
          pLearn<Type> LearnCB,
          pNormal<Type> NormalCB,
          pDerivative<Type> DerivCB>
class TransfFunction { ... };