Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
is_invocable.hpp
Go to the documentation of this file.
1// parasoft-begin-suppress AUTOSAR-A2_8_1-a-2 "False positive: also defines arene::base::is_invocable"
2// parasoft-begin-suppress AUTOSAR-A5_0_3-a "False positive: There is no pointer indirection in return types"
3
4// Copyright 2024, Toyota Motor Corporation
5//
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7
8#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_TYPE_TRAITS_IS_INVOCABLE_HPP_
9#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_TYPE_TRAITS_IS_INVOCABLE_HPP_
10
11// IWYU pragma: private, include "arene/base/type_traits.hpp"
12// IWYU pragma: friend "(arene/base(?!/tests)|stdlib/include/stdlib_detail)/.*"
13
14#include "arene/base/constraints/constraints.hpp"
15#include "arene/base/stdlib_choice/cstddef.hpp"
16#include "arene/base/stdlib_choice/declval.hpp"
17#include "arene/base/stdlib_choice/enable_if.hpp"
18#include "arene/base/stdlib_choice/integral_constant.hpp"
19#include "arene/base/stdlib_choice/is_convertible.hpp"
20#include "arene/base/stdlib_choice/is_same.hpp"
21#include "arene/base/stdlib_choice/is_void.hpp"
22#include "arene/base/stdlib_choice/remove_cv.hpp"
23#include "arene/base/stdlib_choice/remove_reference.hpp"
24#include "arene/base/type_list/type_list.hpp"
25#include "arene/base/type_traits/is_reference_wrapper.hpp"
26#include "arene/base/type_traits/remove_cvref.hpp"
27
28namespace arene {
29namespace base {
30
31namespace is_invocable_detail {
32
33/// @brief A sentinel for an invalid return type.
34// parasoft-begin-suppress AUTOSAR-A2_7_3 "False positive: documented"
35struct invalid_return {};
36// parasoft-end-suppress AUTOSAR-A2_7_3
37
38/// @brief Determine if a callable type is invocable with the specified argument types.
39/// @tparam Fn The callable type.
40/// @tparam Args The callable's parameter types.
41// parasoft-begin-suppress AUTOSAR-A2_7_3 "False positive: documented"
42template <typename Fn, typename... Args>
43class is_invocable_impl {
44 public:
45 /// @brief A declaration representing a successful invocation.
46 /// @tparam MemFuncPtr The member function pointer type.
47 /// @tparam Instance The qualified instance or reference type.
48 /// @tparam Params The parameter types.
49 /// @param dummy parameter used for overload resolution
50 /// @return Not implememted. The return type is the return type of the invocation
51 template <typename MemFuncPtr, typename Instance, typename... Params>
52 static auto try_invoke(std::nullptr_t dummy
53 ) noexcept(noexcept((std::declval<Instance>().*std::declval<MemFuncPtr>())(std::declval<Params>()...)))
54 -> decltype((std::declval<Instance>().*std::declval<MemFuncPtr>())(std::declval<Params>()...));
55
56 /// @brief A declaration representing a successful invocation.
57 /// @tparam MemPtr The member pointer type.
58 /// @tparam Instance The qualified instance or reference type.
59 /// @param dummy parameter used for overload resolution
60 /// @return Not implememted. The return type is the return type of the invocation
61 template <typename MemPtr, typename Instance>
62 static auto try_invoke(std::nullptr_t dummy) noexcept -> decltype((std::declval<Instance>().*std::declval<MemPtr>()));
63
64 /// @brief A declaration representing a successful invocation.
65 /// @tparam MemFuncPtr The member function pointer type.
66 /// @tparam Pointer The qualified pointer type.
67 /// @tparam Params The parameter types.
68 /// @param dummy parameter used for overload resolution
69 /// @return Not implememted. The return type is the return type of the invocation
70 template <typename MemFuncPtr, typename Pointer, typename... Params>
71 static auto try_invoke(std::nullptr_t dummy
72 ) noexcept(noexcept(((*std::declval<Pointer>()).*std::declval<MemFuncPtr>())(std::declval<Params>()...)))
73 -> decltype(((*std::declval<Pointer>()).*std::declval<MemFuncPtr>())(std::declval<Params>()...));
74
75 /// @brief A declaration representing a successful invocation.
76 /// @tparam MemPtr The member pointer type.
77 /// @tparam Pointer The qualified pointer type.
78 /// @param dummy parameter used for overload resolution
79 /// @return Not implememted. The return type is the return type of the invocation
80 template <typename MemPtr, typename Pointer>
81 static auto try_invoke(std::nullptr_t dummy) noexcept(noexcept((*std::declval<Pointer>()).*std::declval<MemPtr>()))
82 -> decltype(((*std::declval<Pointer>()).*std::declval<MemPtr>()));
83
84 /// @brief A declaration representing a successful invocation.
85 /// @tparam MemFuncPtr The member function pointer type.
86 /// @tparam RefWrap The qualified reference wrapper type.
87 /// @tparam Params The parameter types.
88 /// @param dummy parameter used for overload resolution
89 /// @return Not implememted. The return type is the return type of the invocation
90 template <
91 typename MemFuncPtr,
92 typename RefWrap,
93 typename... Params,
94 constraints<std::enable_if_t<is_reference_wrapper_v<remove_cvref_t<RefWrap>>>> = nullptr>
95 static auto try_invoke(std::nullptr_t dummy
96 ) noexcept(noexcept((std::declval<RefWrap>().get().*std::declval<MemFuncPtr>())(std::declval<Params>()...)))
97 -> decltype((std::declval<RefWrap>().get().*std::declval<MemFuncPtr>())(std::declval<Params>()...));
98
99 /// @brief A declaration representing a successful invocation.
100 /// @tparam MemPtr The member pointer type.
101 /// @tparam RefWrap The qualified reference wrapper type.
102 /// @param dummy parameter used for overload resolution
103 /// @return Not implememted. The return type is the return type of the invocation
104 template <
105 typename MemPtr,
106 typename RefWrap,
107 constraints<std::enable_if_t<is_reference_wrapper_v<remove_cvref_t<RefWrap>>>> = nullptr>
108 static auto try_invoke(std::nullptr_t dummy) noexcept
109 -> decltype((std::declval<RefWrap>().get().*std::declval<MemPtr>()));
110
111 /// @brief A declaration representing a successful invocation.
112 /// @tparam Callable The callable type.
113 /// @tparam Params The parameter types.
114 /// @param dummy parameter used for overload resolution
115 /// @return Not implememted. The return type is the return type of the invocation
116 template <typename Callable, typename... Params>
117 static auto try_invoke(std::nullptr_t dummy) noexcept(noexcept(std::declval<Callable>()(std::declval<Params>()...)))
118 -> decltype(std::declval<Callable>()(std::declval<Params>()...));
119
120 /// @brief A declaration representing an invalid invocation.
121 /// @param dummy parameter used for overload resolution
122 /// @return Not implememted. The return type is @c invalid_return
123 template <typename...>
124 static auto try_invoke(void* dummy) -> invalid_return;
125
126 /// @brief The result type of an invocation.
127 using result = decltype(is_invocable_impl::try_invoke<Fn, Args...>(nullptr));
128
129 // parasoft-begin-suppress AUTOSAR-M11_0_1-a-2 "False positive: this is not 'member data', it is a public property"
130 /// @brief Whether an invocation is noexcept.
131 static constexpr bool is_noexcept{noexcept(is_invocable_impl::try_invoke<Fn, Args...>(nullptr))};
132 // parasoft-end-suppress AUTOSAR-M11_0_1-a-2
133};
134// parasoft-end-suppress AUTOSAR-A2_7_3
135
136/// @brief Determine if a callable type is invocable with the specified return and argument types.
137/// @tparam RequireNoThrow Whether the invocable is expected to be no-throw.
138/// @tparam Ret The callable's return type.
139/// @tparam Fn The callable type.
140/// @tparam Args The callable's parameter types.
141// parasoft-begin-suppress AUTOSAR-A2_7_3 "False positive: documented"
142template <bool RequireNoThrow, typename Ret, typename Fn, typename... Args>
143struct is_invocable_r final {
144 /// @brief Provides details on the invocation
145 using invoke_detail = is_invocable_impl<Fn, Args...>;
146
147 /// @brief If the public trait requires @c noexcept
148 static constexpr bool noexcept_ok{!RequireNoThrow || invoke_detail::is_noexcept};
149
150 /// @brief If the expression is valid
151 static constexpr bool return_valid{!std::is_same<typename invoke_detail::result, invalid_return>::value};
152
153 /// @brief If the expression result is convertible to the specified return type
154 static constexpr bool return_ok{
155 std::is_void<Ret>::value || std::is_convertible<typename invoke_detail::result, Ret>::value
156 };
157
158 /// @brief The value of the type trait
159 static constexpr bool value{noexcept_ok && return_valid && return_ok};
160};
161// parasoft-end-suppress AUTOSAR-A2_7_3
162
163/// @brief Determine the return type of a functor invoked with the specified arguments
164/// @tparam Fn The functor type.
165/// @tparam Args The functor parameter types.
166///
167/// Primary template with no @c type member.
168///
169// parasoft-begin-suppress AUTOSAR-A2_7_3 "False positive: documented"
170template <typename Fn, typename Args, typename = constraints<>>
171class invoke_result {};
172// parasoft-end-suppress AUTOSAR-A2_7_3
173
174/// @brief Determine the return type of a functor invoked with the specified arguments
175/// @tparam Fn The functor type.
176/// @tparam Args The functor parameter types.
177///
178/// Specialization if @c is_invocable_v<Fn, Args...> is @c true
179///
180// parasoft-begin-suppress AUTOSAR-A2_7_3 "False positive: documented"
181template <typename Fn, typename... Args>
182class invoke_result<
183 Fn,
184 type_list<Args...>,
185 constraints<
186 std::enable_if_t<!std::is_same<invalid_return, typename is_invocable_impl<Fn, Args...>::result>::value>>> {
187 public:
188 /// @brief The type of the result of invoking @c Fn with @c Args...
189 using type = typename is_invocable_impl<Fn, Args...>::result;
190};
191// parasoft-end-suppress AUTOSAR-A2_7_3
192
193} // namespace is_invocable_detail
194
195/// @brief Determine if a functor type is invocable with the specified argument types.
196/// @tparam Fn The functor type.
197/// @tparam Args The functor parameter types.
198// parasoft-begin-suppress AUTOSAR-A2_7_3 "False positive: documented"
199template <typename Fn, typename... Args>
201 : public std::integral_constant<bool, is_invocable_detail::is_invocable_r<false, void, Fn, Args...>::value> {};
202// parasoft-end-suppress AUTOSAR-A2_7_3
203
204/// @brief Determine if a functor type is invocable with the specified argument types.
205/// @tparam Fn The functor type.
206/// @tparam Args The functor parameter types.
207template <typename Fn, typename... Args>
208extern constexpr bool is_invocable_v = is_invocable<Fn, Args...>::value;
209
210/// @brief Determine if a functor type is invocable with the specified return and argument types.
211/// @tparam Fn The functor type.
212/// @tparam Args The functor parameter types.
213// parasoft-begin-suppress AUTOSAR-A2_7_3 "False positive: documented"
214template <typename Ret, typename Fn, typename... Args>
217// parasoft-end-suppress AUTOSAR-A2_7_3
218
219/// @brief Determine if a functor type is invocable with the specified return and argument types.
220/// @tparam Ret The functor return type.
221/// @tparam Fn The functor type.
222/// @tparam Args The functor parameter types.
223template <typename Ret, typename Fn, typename... Args>
224extern constexpr bool is_invocable_r_v = is_invocable_r<Ret, Fn, Args...>::value;
225
226/// @brief Determine if a functor type is no-throw invocable with the specified argument types.
227/// @tparam Fn The functor type.
228/// @tparam Args The functor parameter types.
229// parasoft-begin-suppress AUTOSAR-A2_7_3 "False positive: documented"
230template <typename Fn, typename... Args>
233// parasoft-end-suppress AUTOSAR-A2_7_3
234
235/// @brief Determine if a functor type is no-throw invocable with the specified argument types.
236/// @tparam Fn The functor type.
237/// @tparam Args The functor parameter types.
238template <typename Fn, typename... Args>
239extern constexpr bool is_nothrow_invocable_v = is_nothrow_invocable<Fn, Args...>::value;
240
241/// @brief Determine if a functor type is no-throw invocable with the specified return and argument types.
242/// @tparam Ret The functor return type.
243/// @tparam Fn The functor type.
244/// @tparam Args The functor parameter types.
245// parasoft-begin-suppress AUTOSAR-A2_7_3 "False positive: documented"
246template <typename Ret, typename Fn, typename... Args>
249// parasoft-end-suppress AUTOSAR-A2_7_3
250
251/// @brief Determine if a functor type is no-throw invocable with the specified return and argument types.
252/// @tparam Ret The functor return type.
253/// @tparam Fn The functor type.
254/// @tparam Args The functor parameter types.
255template <typename Ret, typename Fn, typename... Args>
256extern constexpr bool is_nothrow_invocable_r_v = is_nothrow_invocable_r<Ret, Fn, Args...>::value;
257
258/// @brief Determines the return type of a functor invoked with the specified arguments
259/// @tparam Fn The functor type.
260/// @tparam Args The functor parameter types.
261// parasoft-begin-suppress AUTOSAR-A2_7_3 "False positive: documented"
262template <typename Fn, typename... Args>
264// parasoft-end-suppress AUTOSAR-A2_7_3
265
266/// @brief The return type of a functor invoked with the specified arguments
267/// @tparam Fn The functor type.
268/// @tparam Args The functor parameter types.
269// parasoft-begin-suppress AUTOSAR-A2_7_3 "False positive: documented"
270template <typename Fn, typename... Args>
271using invoke_result_t = typename invoke_result<Fn, Args...>::type;
272// parasoft-end-suppress AUTOSAR-A2_7_3
273
274} // namespace base
275} // namespace arene
276
277#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_TYPE_TRAITS_IS_INVOCABLE_HPP_
278 // parasoft-end-suppress AUTOSAR-A5_0_3-a "False positive: There is no pointer indirection in return types"
Determines the return type of a functor invoked with the specified arguments.
Definition is_invocable.hpp:263
Determine if a functor type is invocable with the specified return and argument types.
Definition is_invocable.hpp:216
Determine if a functor type is invocable with the specified argument types.
Definition is_invocable.hpp:201
Determine if a functor type is no-throw invocable with the specified return and argument types.
Definition is_invocable.hpp:248
Determine if a functor type is no-throw invocable with the specified argument types.
Definition is_invocable.hpp:232
Definition array_exceptions_disabled.cpp:11
constexpr bool is_invocable_v
Determine if a functor type is invocable with the specified argument types.
constexpr bool is_invocable_r_v
Determine if a functor type is invocable with the specified return and argument types.
constexpr bool is_nothrow_invocable_r_v
Determine if a functor type is no-throw invocable with the specified return and argument types.
constexpr bool is_nothrow_invocable_v
Determine if a functor type is no-throw invocable with the specified argument types.
Copyright 2026, Toyota Motor Corporation.
Definition array_exceptions_disabled.cpp:10