我试图创建模板类,并且偏向于专门化其构造函数:
template <typename T>
struct LookupTableDesc {
T from;
T step;
int nSteps;
};
template <>
struct LookupTableDesc<void*> {};
using NoArgument = void*;
using LookupTableNoDimension = LookupTableDesc<void*>;
template <typename T, typename Arg1T, typename Arg2T>
class LookupTable {
std::vector<T> m_table;
using func_type = T(*)(Arg1T, Arg2T);
public:
LookupTable(func_type func, LookupTableDesc<Arg1T> desc1, LookupTableDesc<Arg2T> desc2);
const T* buffer() const { return m_table.data(); }
virtual ~LookupTable() {}
};
template <typename T, typename Arg1T, typename Arg2T>
LookupTable<T, Arg1T, Arg2T>::LookupTable(func_type func, LookupTableDesc<Arg1T> desc1, LookupTableDesc<Arg2T> desc2) {
Arg1T x1 = desc1.from;
for (int i = 0; i < desc1.nSteps; i++, x1 += desc1.step) {
Arg2T x2 = desc2.from;
for (int j = 0; j < desc2.nSteps; j++, x2 += desc2.step) {
m_table.push_back(func(x1, x2));
}
}
}
//template <typename T, typename ArgT>
//LookupTable<T, ArgT, NoArgument>::LookupTable(func_type func, LookupTableDesc<ArgT> desc, LookupTableNoDimension) {
// ArgT x = desc.from;
// for (int i = 0; i < desc.nSteps; i++, x += desc.step) {
// m_table.push_back(func(x, NoArgument()));
// }
//}
没有专业化的模板可以很好地编译,但是如果我取消评论专业化,则会出现以下错误:
error C2244: 'LookupTable<T,ArgT,NoArgument>::LookupTable': unable to match function definition to an existing declaration
note: see declaration of 'LookupTable<T,ArgT,NoArgument>::LookupTable'
note: definition
note: 'LookupTable<T,ArgT,NoArgument>::LookupTable(T (__cdecl *)(Arg1T,Arg2T),LookupTableDesc<Arg1T>,LookupTableNoDimension)'
note: existing declarations
note: 'LookupTable<T,Arg1T,Arg2T>::LookupTable(T (__cdecl *)(Arg1T,Arg2T),LookupTableDesc<Arg1T>,LookupTableDesc<Arg2T>)'
在线编译器在同一位置(专门化)会产生另一个错误:
error: expected constructor, destructor, or type conversion before '(' token
请解释出什么问题。