c++ - std::is_function 不将模板参数识别为函数

标签 c++ types c++11

我将函数指针传递给函数模板:

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 识别为函数,因此静态断言失败。编译器错误消息指出 Fint(*)(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/

相关文章:

typescript - 基于 TypeScript 中的现有对象创建 TypeSafe 对象文字

c++11 - 如何在 gcc 中启用 C++11?

c++11 foreach 语法和自定义迭代器

c++ - 将 Boost 添加到 Eclipse for C++ - 包括路径问题

c++ - If(), else if() c++中的替代方案(这是AI吗?)

c++ - 为什么匿名 union 不能包含具有非平凡构造函数/析构函数的成员?

haskell - `pure x = (\_ -> x)` 是什么意思?

c++ - 无法理解类型别名和常量

c++ - Qt Creator/C++ 调整绘图大小

c++ - 如何在Qt中用宏定义信号