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

标签 c++ templates c++17 non-type

我最近偶然发现了 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/

相关文章:

c++ - 如何预先计算值数组?

c++ - 为什么我的并行 std::for_each 只使用 1 个线程?

c++ - 具有模板特化的 constexpr 数组成员 : inconsistent behavior cross compilers

c++ - 数字常量之前的预期非限定 ID

c++ - Windows XP 和 Windows 7 之间的 DCOM 互操作性

c++ - 为什么有些 STL 算法提供额外的 '_if' 函数而不是重载?

c++ - 钻石物体的定义

c++ - 能够返回不同类型的函数?

c++ - 强制为模板参数类实现函数?

jquery - Django:单击按钮加载另一个模板