Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::is_function does not recognize template argument as function

Tags:

c++

types

c++11

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?

like image 839
Juraj Blaho Avatar asked May 06 '13 08:05

Juraj Blaho


1 Answers

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

like image 155
Konrad Rudolph Avatar answered Oct 20 '22 16:10

Konrad Rudolph