I am passing a pointer to function into a function template:
int f(int a) { return a+1; }
template<typename F>
void use(F f) {
static_assert(std::is_function<F>::value, "Function required");
}
int main() {
use(&f); // Plain f does not work either.
}
But the template argument F is not recognized by is_function to be a function and the static assertion fails. Compiler error message says that F is int(*)(int) which is a pointer to function. Why does it behave like that? How can I recognize the function or pointer to function in this case?
F is a pointer to function (regardless of whether you pass f or &f). So remove the pointer:
std::is_function<typename std::remove_pointer<F>::type>::value
(Ironically, std::is_function<std::function<FT>> == false ;-))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With