I'm not sure how to phrase this question. I have a simple demo.
I thought this code would work but I get the compile errors below. Changing T to int (the commented out line) makes it work but now it no longer is generic. Why isn't this current version compiling? and how do I make this work?
#include <cstdio>
#include <functional>
template<class T>
void test(T t, std::function<void(T)> f) { f(t); }
//void test(T t, std::function<void(int)> f) { f(t); }
void PrintIt(int v){ printf("V=%d\n", v); };
int main(int argc, char *argv[]) {
test(5, PrintIt);
}
Results:
prog.cpp: In function 'int main(int, char**)':
prog.cpp:11:20: error: no matching function for call to 'test(int, void (&)(int))'
test(5, PrintIt);
^
prog.cpp:11:20: note: candidate is:
prog.cpp:4:6: note: template<class T> void test(T, std::function<void(T)>)
void test(T t, std::function<void(T)> f) { f(t); }
^
prog.cpp:4:6: note: template argument deduction/substitution failed:
prog.cpp:11:20: note: mismatched types 'std::function<void(T)>' and 'void (*)(int)'
test(5, PrintIt);