1
함수 포인터를 형식이 아닌 템플릿 인수로 사용하려고했지만 형식을 추론하지 못하는 이유를 이해하지 못하는 경우가 있습니다. 여기함수 포인터가 템플릿 인수로 형식 공제 실패
불행하게도 실패에 대한 GCC는 이유를 제공하지 않는 것 같다
template <class T, class U, class R>
R sum(T a, U b) { return a + b; }
template <class T, class R, R (*Func)(T, R)>
R reduce(T *in, R initial, int len) {
for (int i = 0; i < len; ++i)
initial = Func(in[i], initial);
return initial;
}
int main() {
double data[] = {1, 2, 3, 4, 5};
std::cout << "Sum: " << reduce<sum>(data, 0.0, 5) << "\n";
return 0;
}
예 : 대신
test.cpp: In function ‘int main()’:
test.cpp:15:64: error: no matching function for call to ‘reduce(double [5], double, int)’
test.cpp:15:64: note: candidate is:
test.cpp:7:3: note: template<class T, class R, R (* Func)(T, R)> R reduce(T*, R, int)
test.cpp:7:3: note: template argument deduction/substitution failed:
을, 모든 데이터 유형을 지정하면 작동 할 것입니다 :
std::cout << "Sum: " << reduce<double, double, sum>(data, 0.0, 5) << "\n";
무슨 일입니까?
오 ~의 이런, 난 항상에 대한 템플릿 함수의 부분 특수화를 허용하지 잊었다! 어쨌든, 함수의 매개 변수로 함수를 전달할 수는 없습니다 (그러나 템플릿에서 전달할 것입니다). 그래서 저는 전화를 전적으로 전문화해야합니다. – AkiRoss