我将函数指针传递给函数模板:
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.
}
但是 is_function
无法将模板参数 F
识别为函数,因此静态断言失败。编译器错误消息指出 F
是 int(*)(int)
,它是指向函数的指针。为什么会这样?在这种情况下,我如何识别函数或指向函数的指针?
最佳答案
F
是一个指向函数的指针(无论您传递的是 f
还是 &f
)。所以删除指针:
std::is_function<typename std::remove_pointer<F>::type>::value
(讽刺的是,std::is_function<std::function<FT>> == false
;-))
关于c++ - std::is_function 不将模板参数识别为函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16395168/