Let's inspect a simple definition:
template <typename T>
struct TD;
bool is_odd(const int x) {
return x % 2 != 0;
}
template<typename T>
void foo(T arg){
TD<T> type_displayer;
}
int main() {
foo(is_odd);
}
When you run this code, you get an error. An error saying that:
'TD<bool (*)(int)>
type_displayer' has incomplete type
That's my favourite 'hack' to inspect the deduced type (T
from foo
s argument). You can clearly see how I passed a function to foo
in main
, but the deduced type was <bool (*)(int)>
, which is a pointer to a function that returns bool
and takes an int
as argument.
That's all it is to passing a function as an argument to a template function.
For references on function pointers, see this question and this tutorial.