命名空间
变体
操作

std::is_invocable, std::is_invocable_r, std::is_nothrow_invocable, std::is_nothrow_invocable_r

来自 cppreference.cn
< cpp‎ | types
 
 
元编程库
类型特征
类型类别
(C++11)
(C++11)(DR*)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11) 
(C++11)
(C++11)
类型属性
(C++11)
(C++11)
(C++14)
(C++11)(在 C++26 中弃用)
(C++11)(直到 C++20*)
(C++11)(在 C++20 中弃用)
(C++11)
类型特征常量
元函数
(C++17)
支持的操作
关系和属性查询
(C++11)
(C++11)
(C++11)
is_invocableis_invocable_ris_nothrow_invocableis_nothrow_invocable_r
(C++17)(C++17)(C++17)(C++17)
类型修改
(C++11)(C++11)(C++11)
类型转换
(C++11)(在 C++23 中弃用)
(C++11)(在 C++23 中弃用)
(C++11)
(C++11)(直到 C++20*)(C++17)

(C++11)
(C++17)
编译时有理算术
编译时整数序列
 
定义于头文件 <type_traits>
template< class Fn, class... ArgTypes >
struct is_invocable;
(1) (自 C++17 起)
template< class R, class Fn, class... ArgTypes >
struct is_invocable_r;
(2) (自 C++17 起)
template< class Fn, class... ArgTypes >
struct is_nothrow_invocable;
(3) (自 C++17 起)
template< class R, class Fn, class... ArgTypes >
struct is_nothrow_invocable_r;
(4) (自 C++17 起)
1) 确定当 INVOKE(std::declval<Fn>(), std::declval<ArgTypes>()...) 被视为未求值操作数时是否良好构成。
2) 确定当 INVOKE<R>(std::declval<Fn>(), std::declval<ArgTypes>()...) 被视为未求值操作数时是否良好构成。
3) 确定当 INVOKE(std::declval<Fn>(), std::declval<ArgTypes>()...) 被视为未求值操作数时是否良好构成,并且已知不会抛出任何异常。
4) 确定当 INVOKE<R>(std::declval<Fn>(), std::declval<ArgTypes>()...) 被视为未求值操作数时是否良好构成,并且已知不会抛出任何异常。

如果 FnR 或参数包 ArgTypes 中的任何类型不是完整类型、(可能带 cv 限定的) void 或未知边界的数组,则行为未定义。

如果上述模板的实例化直接或间接地依赖于不完整类型,并且如果假设该类型已完成,则该实例化可能会产生不同的结果,则行为未定义。

如果程序为此页面上描述的任何模板添加特化,则行为未定义。

目录

[编辑] 辅助变量模板

定义于头文件 <type_traits>
template< class Fn, class... ArgTypes >

inline constexpr bool is_invocable_v =

    std::is_invocable<Fn, ArgTypes...>::value;
(1) (自 C++17 起)
template< class R, class Fn, class... ArgTypes >

inline constexpr bool is_invocable_r_v =

    std::is_invocable_r<R, Fn, ArgTypes...>::value;
(2) (自 C++17 起)
template< class Fn, class... ArgTypes >

inline constexpr bool is_nothrow_invocable_v =

    std::is_nothrow_invocable<Fn, ArgTypes...>::value;
(3) (自 C++17 起)
template< class R, class Fn, class... ArgTypes >

inline constexpr bool is_nothrow_invocable_r_v =

    std::is_nothrow_invocable_r<R, Fn, ArgTypes...>::value;
(4) (自 C++17 起)

继承自 std::integral_constant

成员常量

value
[静态]
true 如果(对于重载 (1)INVOKE(std::declval<Fn>(), std::declval<ArgTypes>()...) 被视为未求值操作数时良好构成,否则为 false
(公共静态成员常量)

成员函数

operator bool
将对象转换为 bool,返回 value
(公共成员函数)
operator()
(C++14)
返回 value
(公共成员函数)

成员类型

类型 定义
value_type bool
type std::integral_constant<bool, value>

[编辑] 注释

特性测试 Std 特性
__cpp_lib_is_invocable 201703L (C++17) std::is_invocable, std::invoke_result

[编辑] 示例

#include <type_traits>
 
auto func2(char) -> int (*)()
{
    return nullptr;
}
 
int main()
{
    static_assert(std::is_invocable_v<int()>);
    static_assert(not std::is_invocable_v<int(), int>);
    static_assert(std::is_invocable_r_v<int, int()>);
    static_assert(not std::is_invocable_r_v<int*, int()>);
    static_assert(std::is_invocable_r_v<void, void(int), int>);
    static_assert(not std::is_invocable_r_v<void, void(int), void>);
    static_assert(std::is_invocable_r_v<int(*)(), decltype(func2), char>);
    static_assert(not std::is_invocable_r_v<int(*)(), decltype(func2), void>);
}

[编辑] 参见

(C++17)(C++23)
使用给定参数调用任何 Callable 对象 并有可能指定返回类型(自 C++23 起)
(函数模板) [编辑]
(C++11)(在 C++20 中移除)(C++17)
推导使用一组参数调用可调用对象的结果类型
(类模板) [编辑]
(C++11)
获取模板类型参数的对象的引用,以用于未求值的上下文
(函数模板) [编辑]
指定可以使用给定的一组参数类型调用可调用类型
(概念) [编辑]