我最近偶然发现了 std::is_invocable这将被引入 C++17 标准,我想知道为什么它需要用户为函数指针提供类型,而不是只提供函数指针本身,这可能更方便,特别是因为非类型模板参数现在可以不受约束。
我的意思可以在下面的例子中解释
void hello_world() {
cout << "Hello world" << endl;
}
int main() {
cout << std::is_invocable_v<decltype(hello_world)> << endl;
// as opposed to being able to do
// cout << std::is_invocable_v<hello_world> << endl;
return 0;
}
最佳答案
I am wondering why it needs a user to provide a type for a function pointer as opposed to just providing the function pointer itself which might be more convenient
因为您总是拥有要测试的可调用对象的类型,但您并不总是拥有它的值作为常量表达式。当然,当你确实有值时,你必须写出 decltype(foo)
而不仅仅是 foo
,但这似乎是一个相当小的负担,并且会涵盖相当大比例的用例。不确定拥有 template <auto F, class... Args> is_invocable
是否值得增加复杂性只是这样,有时,您作为用户不必写 decltype
.
关于c++ - 为什么 std::is_invocable 不接受非类型模板参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42812829/