std::is_invocable, std::is_invocable_r, std::is_nothrow_invocable, std::is_nothrow_invocable_r
From cppreference.com
Defined in header <type_traits>
|
||
template <class Fn, class... ArgTypes> struct is_invocable; |
(1) | (since C++17) |
template <class R, class Fn, class... ArgTypes> struct is_invocable_r; |
(2) | (since C++17) |
template <class Fn, class... ArgTypes> struct is_nothrow_invocable; |
(3) | (since C++17) |
template <class R, class Fn, class... ArgTypes> struct is_nothrow_invocable_r; |
(4) | (since C++17) |
1) Determines whether
Fn
can be invoked with the arguments ArgTypes...
. Formally, determines whether INVOKE(declval<Fn>(), declval<ArgTypes>()...)
is well formed when treated as an unevaluated operand, where INVOKE
is the operation defined in Template:concept.2) Determines whether
Fn
can be invoked with the arguments ArgTypes...
to yield a result that is convertible to R
. Formally, determines whether INVOKE<R>(declval<Fn>(), declval<ArgTypes>()...)
is well formed when treated as an unevaluated operand, where INVOKE
is the operation defined in Template:concept3) Determines whether
Fn
is callable with the arguments ArgTypes...
(same as (1)), and that such call is known not to throw any exceptions.4) Determines whether
Fn
can be invoked with the arguments ArgTypes...
to yield a result that is convertible to R
(same as (2)), and that such call is known not to throw any exceptions.If Fn, R
or any type in the parameter pack ArgTypes
is not a complete type, (possibly cv-qualified) void, or an array of unknown bound, the behavior is undefined.
If an instantiation of a template above depends, directly or indirectly, on an incomplete type, and that instantiation could yield a different result if that type were hypothetically completed, the behavior is undefined.
Contents |
Helper variable templates
Defined in header <type_traits>
|
||
template <class Fn, class... ArgTypes> inline constexpr bool is_invocable_v = std::is_invocable<Fn, ArgTypes...>::value; |
(1) | (since 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) | (since C++17) |
template <class Fn, class... ArgTypes> inline constexpr bool is_nothrow_invocable_v = std::is_nothrow_invocable<Fn, ArgTypes...>::value; |
(3) | (since 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) | (since C++17) |
Inherited from std::integral_constant
Member constants
value [static] |
true if INVOKE<R>(declval<Fn>(), declval<ArgTypes>()...) is well formed when treated as an unevaluated operand , false otherwise (public static member constant) |
Member functions
operator bool |
converts the object to bool, returns value (public member function) |
operator() (C++14) |
returns value (public member function) |
Member types
Type | Definition |
value_type
|
bool |
type
|
std::integral_constant<bool, value> |
Possible implementation
Note: Possible implementation of INVOKE see Template:cpp/utility/functional/invoke
namespace detail { template <typename AlwaysVoid, typename, typename...> struct is_invocable : std::false_type {}; template <typename F, typename... Args> struct is_invocable<decltype(void(INVOKE(std::declval<F>(), std::declval<Args>()...))), F, Args...> : std::true_type {}; template <bool cond, typename F, typename... Args> struct is_nothrow_invocable : std::false_type {}; template <typename F, typename... Args> struct is_nothrow_invocable<true, F, Args...> : std::integral_constant<bool, noexcept(INVOKE(std::declval<F>(), std::declval<Args>()...))> {}; } // namespace detail template <typename F, typename... Args> struct is_invocable : detail::is_invocable<void, F, Args...> {}; template <typename R, typename F, typename... Args> struct is_invocable_r : std::integral_constant<bool, is_invocable<F, Args...>::value && std::is_convertible<invoke_result_t<F, Args...>, R>::value> {}; template <typename F, typename... Args> struct is_nothrow_invocable : detail::is_nothrow_invocable<is_invocable<F, Args...>::value, F, Args...> {}; template <typename R, typename F, typename... Args> struct is_nothrow_invocable_r : std::integral_constant<bool, is_nothrow_invocable<F, Args...>::value && std::is_convertible<invoke_result_t<F, Args...>, R>::value> {};
Examples
Run this code
#include <type_traits> #include <iostream> class ClassType {}; auto func1() -> void { } auto func2() -> int { return 0; } auto func3(int a) -> void { } auto func4(ClassType b) -> int { return -1; } auto func5(ClassType b) -> int (*)() { auto r = &func2; return r; } auto main() -> int { static_assert( std::is_invocable<void ()>::value ); static_assert( std::is_invocable<int ()>::value ); static_assert( std::is_invocable_r<int, int ()>::value ); static_assert( std::is_invocable_r<void, void (int), int>::value ); static_assert( std::is_invocable_r<int(*)(), decltype(func5), ClassType>::value ); return 0; }
See also
(C++17)(C++23) |
invokes any Callable object with given arguments and possibility to specify return type(since C++23) (function template) |
(C++11)(removed in C++20)(C++17) |
deduces the result type of invoking a callable object with a set of arguments (class template) |
(C++11) |
obtains a reference to an object of the template type argument for use in an unevaluated context (function template) |