我将函数指针传递给函数模板:
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.
}
Run Code Online (Sandbox Code Playgroud)
但模板参数F不被识别is_function为函数,静态断言失败.编译器错误消息说,F就是int(*)(int)这是一个函数指针.为什么它表现得那样?在这种情况下,如何识别功能或指针?
Kon*_*lph 15
F是一个函数指针(无论您是否通过f或&f).所以删除指针:
std::is_function<typename std::remove_pointer<F>::type>::value
Run Code Online (Sandbox Code Playgroud)
(具有讽刺意味的是std::is_function<std::function<FT>> == false;-))
| 归档时间: |
|
| 查看次数: |
1193 次 |
| 最近记录: |