为什么std :: is_invocable不接受非类型模板参数

Cur*_*ous 3 c++ templates non-type c++17

我最近偶然发现了将要引入C++ 17标准的std :: is_invocable,我想知道为什么它需要用户为函数指针提供一个类型,而不是只提供函数指针本身,这可能是更方便,特别是因为非类型模板参数现在可以不受约束.

我的意思可以在下面的例子中解释

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;
}
Run Code Online (Sandbox Code Playgroud)

Bar*_*rry 6

我想知道为什么它需要一个用户为函数指针提供一个类型而不是只提供函数指针本身,这可能更方便

因为您始终具有要测试的可调用类型,但您并不总是将其作为常量表达式.当然,当你确实拥有你必须写出的价值decltype(foo)而不仅仅是foo,但这似乎是一个相当小的负担,并将覆盖相当大比例的用例.不确定是否值得增加复杂性,template <auto F, class... Args> is_invocable以便有时候,因为用户不必写decltype.