作爲一個練習,我正在嘗試使用模板編寫數組實現,但函數指針作爲模板參數。每次數組獲得索引時都會調用該函數。C++模板參數推理
template<typename T, int size>
using Array_fnIndex = void (*)(int index);
template<typename T, int size, typename Array_fnIndex<T, size> fnIndex>
struct Array {
T data[size];
T& operator[](int index) {
fnIndex(index);
return data[index];
}
};
// example index function
template<typename T, int size>
void checkIndex(int index) {
assert(index < size);
}
int main() {
Array<int, 10, checkIndex<int, 10>> a; // this works
Array<int, 10, checkIndex<int, 11>> b; // this also works, not what I want
Array<int, 10, checkIndex> c; // this doesn't work, but what I want
return 0;
}
在main
函數的最後數組聲明是我想什麼,在哪裏checkIndex的模板參數數組匹配前面的模板參數。但是這不會編譯(使用Microsoft編譯器)。我得到以下錯誤:
error C2440: 'specialization': cannot convert from 'void (__cdecl *)(uint)' to 'void (__cdecl *)(uint)'
note: None of the functions with this name in scope match the target type
有什麼辦法來得到期望的結果,其中用於提供的函數模板參數也會從其他參數推斷?
您的實際代碼中是否需要'checkIndex'上的模板? – AndyG
另外,您無法在任何地方傳遞模板函數指針......它只是不存在。在你想傳遞模板函數的地方,你必須實例化它。這就是爲什麼Vittorio將你的函數變成結構體的原因。 – AndyG