C++ std::is_函数不将模板参数识别为函数
我正在将指向函数的指针传递到函数模板中:C++ std::is_函数不将模板参数识别为函数,c++,types,c++11,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. } intf(inta){返
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.
}
intf(inta){返回a+1;}
模板
无效使用(F){
静态_断言(std::is_function::value,“需要函数”);
}
int main(){
use(&f);//普通的f也不起作用。
}
但是模板参数F
未被is_function
识别为函数,静态断言失败。编译器错误消息指出F
是int(*)(int)
,它是指向函数的指针。为什么会这样?在这种情况下,如何识别函数或函数指针?F
是函数指针(无论您是传递F
还是传递&F
)。因此,请移除指针:
std::is_function<typename std::remove_pointer<F>::type>::value
std::is_函数::值
(讽刺的是,
std::is_function==false
-)我讨厌隐式转换:((讽刺的是,std::is_function==false
-)”-也许对于未来的标准来说,一个std::is_callable
可能是个好主意,因为std::is_function
甚至不适用于lambdas,而只是简单的函数(在现代C++中,每个可调用函数都是函数的时代已经过去了)“@ChristianRaustd::is_function
是主要的分类特征之一。是可调用的
或者类似的东西会有完全不同的用途。@LucDanton是的,当然。没有人认为std::is_function
有它的价值,并且完全适合std::is_array
(对于std::array
或std::is_class
都不应该返回true。但是,额外的std::is_callable
将是一个好主意,并且将与其他相当高层次的特性一致,比如(已经提出的AFAIK)std::是可交换的
,以及迫切需要的(尽管不建议使用AFAIK)std::是可散列的
。