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 64 template <class _DecayedFp> 65 struct __member_pointer_class_type {}; 66 67 template <class _Ret, class _ClassType> 68 struct __member_pointer_class_type<_Ret _ClassType::*> { 69 typedef _ClassType type; 70 }; 71 72 template <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> 77 using __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 81 template <class _Fp, class _A0, class _DecayFp = __decay_t<_Fp>, class _DecayA0 = __decay_t<_A0> > 82 using __enable_if_bullet2 _LIBCPP_NODEBUG = 83 __enable_if_t<is_member_function_pointer<_DecayFp>::value && __is_reference_wrapper<_DecayA0>::value>; 84 85 template <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> 90 using __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 95 template <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> 100 using __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 104 template <class _Fp, class _A0, class _DecayFp = __decay_t<_Fp>, class _DecayA0 = __decay_t<_A0> > 105 using __enable_if_bullet5 _LIBCPP_NODEBUG = 106 __enable_if_t<is_member_object_pointer<_DecayFp>::value && __is_reference_wrapper<_DecayA0>::value>; 107 108 template <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> 113 using __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 122 template <class... _Args> 123 __nat __invoke(_Args&&... __args); 124 125 // bullets 1, 2 and 3 126 127 // clang-format off 128 template <class _Fp, class _A0, class... _Args, class = __enable_if_bullet1<_Fp, _A0> > 129 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR 130 decltype((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 135 template <class _Fp, class _A0, class... _Args, class = __enable_if_bullet2<_Fp, _A0> > 136 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR 137 decltype((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 142 template <class _Fp, class _A0, class... _Args, class = __enable_if_bullet3<_Fp, _A0> > 143 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR 144 decltype(((*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 151 template <class _Fp, class _A0, class = __enable_if_bullet4<_Fp, _A0> > 152 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR 153 decltype(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 158 template <class _Fp, class _A0, class = __enable_if_bullet5<_Fp, _A0> > 159 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR 160 decltype(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 165 template <class _Fp, class _A0, class = __enable_if_bullet6<_Fp, _A0> > 166 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR 167 decltype((*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 174 template <class _Fp, class... _Args> 175 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR 176 decltype(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 183 template <class _Ret, class _Fp, class... _Args> 184 struct __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 }; 199 template <class _Fp, class... _Args> 200 using __is_invocable _LIBCPP_NODEBUG = __invokable_r<void, _Fp, _Args...>; 201 202 template <bool _IsInvokable, bool _IsCVVoid, class _Ret, class _Fp, class... _Args> 203 struct __nothrow_invokable_r_imp { 204 static const bool value = false; 205 }; 206 207 template <class _Ret, class _Fp, class... _Args> 208 struct __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 222 template <class _Ret, class _Fp, class... _Args> 223 struct __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 231 template <class _Ret, class _Fp, class... _Args> 232 using __nothrow_invokable_r _LIBCPP_NODEBUG = 233 __nothrow_invokable_r_imp<__invokable_r<_Ret, _Fp, _Args...>::value, is_void<_Ret>::value, _Ret, _Fp, _Args...>; 234 235 template <class _Fp, class... _Args> 236 using __nothrow_invokable _LIBCPP_NODEBUG = 237 __nothrow_invokable_r_imp<__is_invocable<_Fp, _Args...>::value, true, void, _Fp, _Args...>; 238 239 template <class _Ret, bool = is_void<_Ret>::value> 240 struct __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 247 template <class _Ret> 248 struct __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 255 template <class _Func, class... _Args> 256 inline const bool __is_invocable_v = __is_invocable<_Func, _Args...>::value; 257 258 template <class _Ret, class _Func, class... _Args> 259 inline const bool __is_invocable_r_v = __invokable_r<_Ret, _Func, _Args...>::value; 260 261 template <class _Func, class... _Args> 262 inline const bool __is_nothrow_invocable_v = __nothrow_invokable<_Func, _Args...>::value; 263 264 template <class _Func, class... _Args> 265 struct __invoke_result 266 : enable_if<__is_invocable_v<_Func, _Args...>, typename __invokable_r<void, _Func, _Args...>::_Result> {}; 267 268 template <class _Func, class... _Args> 269 using __invoke_result_t _LIBCPP_NODEBUG = typename __invoke_result<_Func, _Args...>::type; 270 271 template <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 280 template <class _Fn, class... _Args> 281 struct _LIBCPP_TEMPLATE_VIS _LIBCPP_NO_SPECIALIZATIONS is_invocable : bool_constant<__is_invocable_v<_Fn, _Args...>> {}; 282 283 template <class _Ret, class _Fn, class... _Args> 284 struct _LIBCPP_TEMPLATE_VIS _LIBCPP_NO_SPECIALIZATIONS is_invocable_r 285 : bool_constant<__is_invocable_r_v<_Ret, _Fn, _Args...>> {}; 286 287 template <class _Fn, class... _Args> 288 _LIBCPP_NO_SPECIALIZATIONS inline constexpr bool is_invocable_v = __is_invocable_v<_Fn, _Args...>; 289 290 template <class _Ret, class _Fn, class... _Args> 291 _LIBCPP_NO_SPECIALIZATIONS inline constexpr bool is_invocable_r_v = __is_invocable_r_v<_Ret, _Fn, _Args...>; 292 293 // is_nothrow_invocable 294 295 template <class _Fn, class... _Args> 296 struct _LIBCPP_TEMPLATE_VIS _LIBCPP_NO_SPECIALIZATIONS is_nothrow_invocable 297 : bool_constant<__nothrow_invokable<_Fn, _Args...>::value> {}; 298 299 template <class _Ret, class _Fn, class... _Args> 300 struct _LIBCPP_TEMPLATE_VIS _LIBCPP_NO_SPECIALIZATIONS is_nothrow_invocable_r 301 : bool_constant<__nothrow_invokable_r<_Ret, _Fn, _Args...>::value> {}; 302 303 template <class _Fn, class... _Args> 304 _LIBCPP_NO_SPECIALIZATIONS inline constexpr bool is_nothrow_invocable_v = is_nothrow_invocable<_Fn, _Args...>::value; 305 306 template <class _Ret, class _Fn, class... _Args> 307 _LIBCPP_NO_SPECIALIZATIONS inline constexpr bool is_nothrow_invocable_r_v = 308 is_nothrow_invocable_r<_Ret, _Fn, _Args...>::value; 309 310 template <class _Fn, class... _Args> 311 struct _LIBCPP_TEMPLATE_VIS _LIBCPP_NO_SPECIALIZATIONS invoke_result : __invoke_result<_Fn, _Args...> {}; 312 313 template <class _Fn, class... _Args> 314 using invoke_result_t = typename invoke_result<_Fn, _Args...>::type; 315 316 #endif // _LIBCPP_STD_VER >= 17 317 318 _LIBCPP_END_NAMESPACE_STD 319 320 #endif // _LIBCPP___TYPE_TRAITS_INVOKE_H 321