Juraj Blaho
Juraj Blaho

Reputation: 13461

std::is_function does not recognize template argument as function

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?

Upvotes: 12

Views: 1624

Answers (1)

Konrad Rudolph
Konrad Rudolph

Reputation: 545865

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 ;-))

Upvotes: 15

Related Questions