Namespaces
Variants

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

From cppreference.net
Metaprogramming library
Type traits
Type categories
(C++11)
(C++11) ( DR* )
Type properties
(C++11)
(C++11)
(C++14)
(C++11) (deprecated in C++26)
(C++11) ( until C++20* )
(C++11) (deprecated in C++20)
(C++11)
Type trait constants
Metafunctions
(C++17)
Supported operations
Relationships and property queries
(C++11)
(C++11)
(C++11)
is_invocable is_invocable_r is_nothrow_invocable is_nothrow_invocable_r
(C++17) (C++17) (C++17) (C++17)
Type modifications
Type transformations
(C++11) (deprecated in C++23)
(C++11) (deprecated in C++23)
(C++11)
(C++11) ( until C++20* ) (C++17)

Compile-time rational arithmetic
Compile-time integer sequences
定义于头文件 <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 > ( ) ... ) 的表达式是否良构,且已知不会抛出任何异常。

如果 Fn, R 或参数包 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
[static]
若(对于重载 (1) INVOKE ( std:: declval < Fn > ( ) , std:: declval < ArgTypes > ( ) ... ) 作为未求值操作数时合法则为 true ,否则为 false
(公开静态成员常量)

成员函数

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

成员类型

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

注释

功能测试 标准 功能
__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)
以给定参数调用任意 可调用 对象 并可指定返回类型 (C++23 起)
(函数模板)
(C++11) (C++20 中移除) (C++17)
推导调用可调用对象及参数集的结果类型
(类模板)
(C++11)
在未求值语境中获取模板类型参数的引用
(函数模板)
规定可调用类型能以给定参数类型集进行调用
(概念)