Warning: This file is not a C or C++ file. It does not have highlighting.

1// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP___TYPE_TRAITS_INVOKE_H
11#define _LIBCPP___TYPE_TRAITS_INVOKE_H
12
13#include <__config>
14#include <__type_traits/conditional.h>
15#include <__type_traits/decay.h>
16#include <__type_traits/enable_if.h>
17#include <__type_traits/integral_constant.h>
18#include <__type_traits/is_base_of.h>
19#include <__type_traits/is_core_convertible.h>
20#include <__type_traits/is_member_pointer.h>
21#include <__type_traits/is_reference_wrapper.h>
22#include <__type_traits/is_same.h>
23#include <__type_traits/is_void.h>
24#include <__type_traits/nat.h>
25#include <__utility/declval.h>
26#include <__utility/forward.h>
27
28#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
29# pragma GCC system_header
30#endif
31
32// This file defines the following libc++-internal API (back-ported to C++03):
33//
34// template <class... Args>
35// decltype(auto) __invoke(Args&&... args) noexcept(noexcept(std::invoke(std::forward<Args>(args...)))) {
36// return std::invoke(std::forward<Args>(args)...);
37// }
38//
39// template <class Ret, class... Args>
40// Ret __invoke_r(Args&&... args) {
41// return std::invoke_r(std::forward<Args>(args)...);
42// }
43//
44// template <class Ret, class Func, class... Args>
45// inline const bool __is_invocable_r_v = is_invocable_r_v<Ret, Func, Args...>;
46//
47// template <class Func, class... Args>
48// struct __is_invocable : is_invocable<Func, Args...> {};
49//
50// template <class Func, class... Args>
51// inline const bool __is_invocable_v = is_invocable_v<Func, Args...>;
52//
53// template <class Func, class... Args>
54// inline const bool __is_nothrow_invocable_v = is_nothrow_invocable_v<Func, Args...>;
55//
56// template <class Func, class... Args>
57// struct __invoke_result : invoke_result {};
58//
59// template <class Func, class... Args>
60// using __invoke_result_t = invoke_result_t<Func, Args...>;
61
62_LIBCPP_BEGIN_NAMESPACE_STD
63
64template <class _DecayedFp>
65struct __member_pointer_class_type {};
66
67template <class _Ret, class _ClassType>
68struct __member_pointer_class_type<_Ret _ClassType::*> {
69 typedef _ClassType type;
70};
71
72template <class _Fp,
73 class _A0,
74 class _DecayFp = __decay_t<_Fp>,
75 class _DecayA0 = __decay_t<_A0>,
76 class _ClassT = typename __member_pointer_class_type<_DecayFp>::type>
77using __enable_if_bullet1 _LIBCPP_NODEBUG =
78 __enable_if_t<is_member_function_pointer<_DecayFp>::value &&
79 (is_same<_ClassT, _DecayA0>::value || is_base_of<_ClassT, _DecayA0>::value)>;
80
81template <class _Fp, class _A0, class _DecayFp = __decay_t<_Fp>, class _DecayA0 = __decay_t<_A0> >
82using __enable_if_bullet2 _LIBCPP_NODEBUG =
83 __enable_if_t<is_member_function_pointer<_DecayFp>::value && __is_reference_wrapper<_DecayA0>::value>;
84
85template <class _Fp,
86 class _A0,
87 class _DecayFp = __decay_t<_Fp>,
88 class _DecayA0 = __decay_t<_A0>,
89 class _ClassT = typename __member_pointer_class_type<_DecayFp>::type>
90using __enable_if_bullet3 _LIBCPP_NODEBUG =
91 __enable_if_t<is_member_function_pointer<_DecayFp>::value &&
92 !(is_same<_ClassT, _DecayA0>::value || is_base_of<_ClassT, _DecayA0>::value) &&
93 !__is_reference_wrapper<_DecayA0>::value>;
94
95template <class _Fp,
96 class _A0,
97 class _DecayFp = __decay_t<_Fp>,
98 class _DecayA0 = __decay_t<_A0>,
99 class _ClassT = typename __member_pointer_class_type<_DecayFp>::type>
100using __enable_if_bullet4 _LIBCPP_NODEBUG =
101 __enable_if_t<is_member_object_pointer<_DecayFp>::value &&
102 (is_same<_ClassT, _DecayA0>::value || is_base_of<_ClassT, _DecayA0>::value)>;
103
104template <class _Fp, class _A0, class _DecayFp = __decay_t<_Fp>, class _DecayA0 = __decay_t<_A0> >
105using __enable_if_bullet5 _LIBCPP_NODEBUG =
106 __enable_if_t<is_member_object_pointer<_DecayFp>::value && __is_reference_wrapper<_DecayA0>::value>;
107
108template <class _Fp,
109 class _A0,
110 class _DecayFp = __decay_t<_Fp>,
111 class _DecayA0 = __decay_t<_A0>,
112 class _ClassT = typename __member_pointer_class_type<_DecayFp>::type>
113using __enable_if_bullet6 _LIBCPP_NODEBUG =
114 __enable_if_t<is_member_object_pointer<_DecayFp>::value &&
115 !(is_same<_ClassT, _DecayA0>::value || is_base_of<_ClassT, _DecayA0>::value) &&
116 !__is_reference_wrapper<_DecayA0>::value>;
117
118// __invoke forward declarations
119
120// fall back - none of the bullets
121
122template <class... _Args>
123__nat __invoke(_Args&&... __args);
124
125// bullets 1, 2 and 3
126
127// clang-format off
128template <class _Fp, class _A0, class... _Args, class = __enable_if_bullet1<_Fp, _A0> >
129inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR
130decltype((std::declval<_A0>().*std::declval<_Fp>())(std::declval<_Args>()...))
131__invoke(_Fp&& __f, _A0&& __a0, _Args&&... __args)
132 _NOEXCEPT_(noexcept((static_cast<_A0&&>(__a0).*__f)(static_cast<_Args&&>(__args)...)))
133 { return (static_cast<_A0&&>(__a0).*__f)(static_cast<_Args&&>(__args)...); }
134
135template <class _Fp, class _A0, class... _Args, class = __enable_if_bullet2<_Fp, _A0> >
136inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR
137decltype((std::declval<_A0>().get().*std::declval<_Fp>())(std::declval<_Args>()...))
138__invoke(_Fp&& __f, _A0&& __a0, _Args&&... __args)
139 _NOEXCEPT_(noexcept((__a0.get().*__f)(static_cast<_Args&&>(__args)...)))
140 { return (__a0.get().*__f)(static_cast<_Args&&>(__args)...); }
141
142template <class _Fp, class _A0, class... _Args, class = __enable_if_bullet3<_Fp, _A0> >
143inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR
144decltype(((*std::declval<_A0>()).*std::declval<_Fp>())(std::declval<_Args>()...))
145__invoke(_Fp&& __f, _A0&& __a0, _Args&&... __args)
146 _NOEXCEPT_(noexcept(((*static_cast<_A0&&>(__a0)).*__f)(static_cast<_Args&&>(__args)...)))
147 { return ((*static_cast<_A0&&>(__a0)).*__f)(static_cast<_Args&&>(__args)...); }
148
149// bullets 4, 5 and 6
150
151template <class _Fp, class _A0, class = __enable_if_bullet4<_Fp, _A0> >
152inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR
153decltype(std::declval<_A0>().*std::declval<_Fp>())
154__invoke(_Fp&& __f, _A0&& __a0)
155 _NOEXCEPT_(noexcept(static_cast<_A0&&>(__a0).*__f))
156 { return static_cast<_A0&&>(__a0).*__f; }
157
158template <class _Fp, class _A0, class = __enable_if_bullet5<_Fp, _A0> >
159inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR
160decltype(std::declval<_A0>().get().*std::declval<_Fp>())
161__invoke(_Fp&& __f, _A0&& __a0)
162 _NOEXCEPT_(noexcept(__a0.get().*__f))
163 { return __a0.get().*__f; }
164
165template <class _Fp, class _A0, class = __enable_if_bullet6<_Fp, _A0> >
166inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR
167decltype((*std::declval<_A0>()).*std::declval<_Fp>())
168__invoke(_Fp&& __f, _A0&& __a0)
169 _NOEXCEPT_(noexcept((*static_cast<_A0&&>(__a0)).*__f))
170 { return (*static_cast<_A0&&>(__a0)).*__f; }
171
172// bullet 7
173
174template <class _Fp, class... _Args>
175inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR
176decltype(std::declval<_Fp>()(std::declval<_Args>()...))
177__invoke(_Fp&& __f, _Args&&... __args)
178 _NOEXCEPT_(noexcept(static_cast<_Fp&&>(__f)(static_cast<_Args&&>(__args)...)))
179 { return static_cast<_Fp&&>(__f)(static_cast<_Args&&>(__args)...); }
180// clang-format on
181
182// __invokable
183template <class _Ret, class _Fp, class... _Args>
184struct __invokable_r {
185 template <class _XFp, class... _XArgs>
186 static decltype(std::__invoke(std::declval<_XFp>(), std::declval<_XArgs>()...)) __try_call(int);
187 template <class _XFp, class... _XArgs>
188 static __nat __try_call(...);
189
190 // FIXME: Check that _Ret, _Fp, and _Args... are all complete types, cv void,
191 // or incomplete array types as required by the standard.
192 using _Result _LIBCPP_NODEBUG = decltype(__try_call<_Fp, _Args...>(0));
193
194 using type = __conditional_t<_IsNotSame<_Result, __nat>::value,
195 __conditional_t<is_void<_Ret>::value, true_type, __is_core_convertible<_Result, _Ret> >,
196 false_type>;
197 static const bool value = type::value;
198};
199template <class _Fp, class... _Args>
200using __is_invocable _LIBCPP_NODEBUG = __invokable_r<void, _Fp, _Args...>;
201
202template <bool _IsInvokable, bool _IsCVVoid, class _Ret, class _Fp, class... _Args>
203struct __nothrow_invokable_r_imp {
204 static const bool value = false;
205};
206
207template <class _Ret, class _Fp, class... _Args>
208struct __nothrow_invokable_r_imp<true, false, _Ret, _Fp, _Args...> {
209 typedef __nothrow_invokable_r_imp _ThisT;
210
211 template <class _Tp>
212 static void __test_noexcept(_Tp) _NOEXCEPT;
213
214#ifdef _LIBCPP_CXX03_LANG
215 static const bool value = false;
216#else
217 static const bool value =
218 noexcept(_ThisT::__test_noexcept<_Ret>(std::__invoke(std::declval<_Fp>(), std::declval<_Args>()...)));
219#endif
220};
221
222template <class _Ret, class _Fp, class... _Args>
223struct __nothrow_invokable_r_imp<true, true, _Ret, _Fp, _Args...> {
224#ifdef _LIBCPP_CXX03_LANG
225 static const bool value = false;
226#else
227 static const bool value = noexcept(std::__invoke(std::declval<_Fp>(), std::declval<_Args>()...));
228#endif
229};
230
231template <class _Ret, class _Fp, class... _Args>
232using __nothrow_invokable_r _LIBCPP_NODEBUG =
233 __nothrow_invokable_r_imp<__invokable_r<_Ret, _Fp, _Args...>::value, is_void<_Ret>::value, _Ret, _Fp, _Args...>;
234
235template <class _Fp, class... _Args>
236using __nothrow_invokable _LIBCPP_NODEBUG =
237 __nothrow_invokable_r_imp<__is_invocable<_Fp, _Args...>::value, true, void, _Fp, _Args...>;
238
239template <class _Ret, bool = is_void<_Ret>::value>
240struct __invoke_void_return_wrapper {
241 template <class... _Args>
242 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static _Ret __call(_Args&&... __args) {
243 return std::__invoke(std::forward<_Args>(__args)...);
244 }
245};
246
247template <class _Ret>
248struct __invoke_void_return_wrapper<_Ret, true> {
249 template <class... _Args>
250 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static void __call(_Args&&... __args) {
251 std::__invoke(std::forward<_Args>(__args)...);
252 }
253};
254
255template <class _Func, class... _Args>
256inline const bool __is_invocable_v = __is_invocable<_Func, _Args...>::value;
257
258template <class _Ret, class _Func, class... _Args>
259inline const bool __is_invocable_r_v = __invokable_r<_Ret, _Func, _Args...>::value;
260
261template <class _Func, class... _Args>
262inline const bool __is_nothrow_invocable_v = __nothrow_invokable<_Func, _Args...>::value;
263
264template <class _Func, class... _Args>
265struct __invoke_result
266 : enable_if<__is_invocable_v<_Func, _Args...>, typename __invokable_r<void, _Func, _Args...>::_Result> {};
267
268template <class _Func, class... _Args>
269using __invoke_result_t _LIBCPP_NODEBUG = typename __invoke_result<_Func, _Args...>::type;
270
271template <class _Ret, class... _Args>
272_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Ret __invoke_r(_Args&&... __args) {
273 return __invoke_void_return_wrapper<_Ret>::__call(std::forward<_Args>(__args)...);
274}
275
276#if _LIBCPP_STD_VER >= 17
277
278// is_invocable
279
280template <class _Fn, class... _Args>
281struct _LIBCPP_NO_SPECIALIZATIONS is_invocable : bool_constant<__is_invocable_v<_Fn, _Args...>> {};
282
283template <class _Ret, class _Fn, class... _Args>
284struct _LIBCPP_NO_SPECIALIZATIONS is_invocable_r : bool_constant<__is_invocable_r_v<_Ret, _Fn, _Args...>> {};
285
286template <class _Fn, class... _Args>
287_LIBCPP_NO_SPECIALIZATIONS inline constexpr bool is_invocable_v = __is_invocable_v<_Fn, _Args...>;
288
289template <class _Ret, class _Fn, class... _Args>
290_LIBCPP_NO_SPECIALIZATIONS inline constexpr bool is_invocable_r_v = __is_invocable_r_v<_Ret, _Fn, _Args...>;
291
292// is_nothrow_invocable
293
294template <class _Fn, class... _Args>
295struct _LIBCPP_NO_SPECIALIZATIONS is_nothrow_invocable : bool_constant<__nothrow_invokable<_Fn, _Args...>::value> {};
296
297template <class _Ret, class _Fn, class... _Args>
298struct _LIBCPP_NO_SPECIALIZATIONS is_nothrow_invocable_r
299 : bool_constant<__nothrow_invokable_r<_Ret, _Fn, _Args...>::value> {};
300
301template <class _Fn, class... _Args>
302_LIBCPP_NO_SPECIALIZATIONS inline constexpr bool is_nothrow_invocable_v = is_nothrow_invocable<_Fn, _Args...>::value;
303
304template <class _Ret, class _Fn, class... _Args>
305_LIBCPP_NO_SPECIALIZATIONS inline constexpr bool is_nothrow_invocable_r_v =
306 is_nothrow_invocable_r<_Ret, _Fn, _Args...>::value;
307
308template <class _Fn, class... _Args>
309struct _LIBCPP_NO_SPECIALIZATIONS invoke_result : __invoke_result<_Fn, _Args...> {};
310
311template <class _Fn, class... _Args>
312using invoke_result_t = typename invoke_result<_Fn, _Args...>::type;
313
314#endif // _LIBCPP_STD_VER >= 17
315
316_LIBCPP_END_NAMESPACE_STD
317
318#endif // _LIBCPP___TYPE_TRAITS_INVOKE_H
319

Warning: This file is not a C or C++ file. It does not have highlighting.

source code of libcxx/include/__type_traits/invoke.h