2 //===----------------------------------------------------------------------===//
4 // The LLVM Compiler Infrastructure
6 // This file is dual licensed under the MIT and the University of Illinois Open
7 // Source Licenses. See LICENSE.TXT for details.
9 //===----------------------------------------------------------------------===//
11 #ifndef _LIBCPP_FUNCTIONAL_BASE
12 #define _LIBCPP_FUNCTIONAL_BASE
15 #include <type_traits>
20 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
21 #pragma GCC system_header
24 _LIBCPP_BEGIN_NAMESPACE_STD
26 template <class _Arg, class _Result>
27 struct _LIBCPP_TYPE_VIS_ONLY unary_function
29 typedef _Arg argument_type;
30 typedef _Result result_type;
33 template <class _Arg1, class _Arg2, class _Result>
34 struct _LIBCPP_TYPE_VIS_ONLY binary_function
36 typedef _Arg1 first_argument_type;
37 typedef _Arg2 second_argument_type;
38 typedef _Result result_type;
41 template <class _Tp> struct _LIBCPP_TYPE_VIS_ONLY hash;
44 struct __has_result_type
47 struct __two {char __lx; char __lxx;};
48 template <class _Up> static __two __test(...);
49 template <class _Up> static char __test(typename _Up::result_type* = 0);
51 static const bool value = sizeof(__test<_Tp>(0)) == 1;
54 #if _LIBCPP_STD_VER > 11
55 template <class _Tp = void>
59 struct _LIBCPP_TYPE_VIS_ONLY less : binary_function<_Tp, _Tp, bool>
61 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
62 bool operator()(const _Tp& __x, const _Tp& __y) const
66 #if _LIBCPP_STD_VER > 11
68 struct _LIBCPP_TYPE_VIS_ONLY less<void>
70 template <class _T1, class _T2>
71 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
72 auto operator()(_T1&& __t, _T2&& __u) const
73 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) < _VSTD::forward<_T2>(__u)))
74 -> decltype (_VSTD::forward<_T1>(__t) < _VSTD::forward<_T2>(__u))
75 { return _VSTD::forward<_T1>(__t) < _VSTD::forward<_T2>(__u); }
76 typedef void is_transparent;
83 struct __derives_from_unary_function
86 struct __two {char __lx; char __lxx;};
87 static __two __test(...);
88 template <class _Ap, class _Rp>
89 static unary_function<_Ap, _Rp>
90 __test(const volatile unary_function<_Ap, _Rp>*);
92 static const bool value = !is_same<decltype(__test((_Tp*)0)), __two>::value;
93 typedef decltype(__test((_Tp*)0)) type;
97 struct __derives_from_binary_function
100 struct __two {char __lx; char __lxx;};
101 static __two __test(...);
102 template <class _A1, class _A2, class _Rp>
103 static binary_function<_A1, _A2, _Rp>
104 __test(const volatile binary_function<_A1, _A2, _Rp>*);
106 static const bool value = !is_same<decltype(__test((_Tp*)0)), __two>::value;
107 typedef decltype(__test((_Tp*)0)) type;
110 template <class _Tp, bool = __derives_from_unary_function<_Tp>::value>
111 struct __maybe_derive_from_unary_function // bool is true
112 : public __derives_from_unary_function<_Tp>::type
117 struct __maybe_derive_from_unary_function<_Tp, false>
121 template <class _Tp, bool = __derives_from_binary_function<_Tp>::value>
122 struct __maybe_derive_from_binary_function // bool is true
123 : public __derives_from_binary_function<_Tp>::type
128 struct __maybe_derive_from_binary_function<_Tp, false>
132 template <class _Tp, bool = __has_result_type<_Tp>::value>
133 struct __weak_result_type_imp // bool is true
134 : public __maybe_derive_from_unary_function<_Tp>,
135 public __maybe_derive_from_binary_function<_Tp>
137 typedef typename _Tp::result_type result_type;
141 struct __weak_result_type_imp<_Tp, false>
142 : public __maybe_derive_from_unary_function<_Tp>,
143 public __maybe_derive_from_binary_function<_Tp>
148 struct __weak_result_type
149 : public __weak_result_type_imp<_Tp>
156 struct __weak_result_type<_Rp ()>
158 typedef _Rp result_type;
162 struct __weak_result_type<_Rp (&)()>
164 typedef _Rp result_type;
168 struct __weak_result_type<_Rp (*)()>
170 typedef _Rp result_type;
175 template <class _Rp, class _A1>
176 struct __weak_result_type<_Rp (_A1)>
177 : public unary_function<_A1, _Rp>
181 template <class _Rp, class _A1>
182 struct __weak_result_type<_Rp (&)(_A1)>
183 : public unary_function<_A1, _Rp>
187 template <class _Rp, class _A1>
188 struct __weak_result_type<_Rp (*)(_A1)>
189 : public unary_function<_A1, _Rp>
193 template <class _Rp, class _Cp>
194 struct __weak_result_type<_Rp (_Cp::*)()>
195 : public unary_function<_Cp*, _Rp>
199 template <class _Rp, class _Cp>
200 struct __weak_result_type<_Rp (_Cp::*)() const>
201 : public unary_function<const _Cp*, _Rp>
205 template <class _Rp, class _Cp>
206 struct __weak_result_type<_Rp (_Cp::*)() volatile>
207 : public unary_function<volatile _Cp*, _Rp>
211 template <class _Rp, class _Cp>
212 struct __weak_result_type<_Rp (_Cp::*)() const volatile>
213 : public unary_function<const volatile _Cp*, _Rp>
219 template <class _Rp, class _A1, class _A2>
220 struct __weak_result_type<_Rp (_A1, _A2)>
221 : public binary_function<_A1, _A2, _Rp>
225 template <class _Rp, class _A1, class _A2>
226 struct __weak_result_type<_Rp (*)(_A1, _A2)>
227 : public binary_function<_A1, _A2, _Rp>
231 template <class _Rp, class _A1, class _A2>
232 struct __weak_result_type<_Rp (&)(_A1, _A2)>
233 : public binary_function<_A1, _A2, _Rp>
237 template <class _Rp, class _Cp, class _A1>
238 struct __weak_result_type<_Rp (_Cp::*)(_A1)>
239 : public binary_function<_Cp*, _A1, _Rp>
243 template <class _Rp, class _Cp, class _A1>
244 struct __weak_result_type<_Rp (_Cp::*)(_A1) const>
245 : public binary_function<const _Cp*, _A1, _Rp>
249 template <class _Rp, class _Cp, class _A1>
250 struct __weak_result_type<_Rp (_Cp::*)(_A1) volatile>
251 : public binary_function<volatile _Cp*, _A1, _Rp>
255 template <class _Rp, class _Cp, class _A1>
256 struct __weak_result_type<_Rp (_Cp::*)(_A1) const volatile>
257 : public binary_function<const volatile _Cp*, _A1, _Rp>
262 #ifndef _LIBCPP_HAS_NO_VARIADICS
263 // 3 or more arguments
265 template <class _Rp, class _A1, class _A2, class _A3, class ..._A4>
266 struct __weak_result_type<_Rp (_A1, _A2, _A3, _A4...)>
268 typedef _Rp result_type;
271 template <class _Rp, class _A1, class _A2, class _A3, class ..._A4>
272 struct __weak_result_type<_Rp (&)(_A1, _A2, _A3, _A4...)>
274 typedef _Rp result_type;
277 template <class _Rp, class _A1, class _A2, class _A3, class ..._A4>
278 struct __weak_result_type<_Rp (*)(_A1, _A2, _A3, _A4...)>
280 typedef _Rp result_type;
283 template <class _Rp, class _Cp, class _A1, class _A2, class ..._A3>
284 struct __weak_result_type<_Rp (_Cp::*)(_A1, _A2, _A3...)>
286 typedef _Rp result_type;
289 template <class _Rp, class _Cp, class _A1, class _A2, class ..._A3>
290 struct __weak_result_type<_Rp (_Cp::*)(_A1, _A2, _A3...) const>
292 typedef _Rp result_type;
295 template <class _Rp, class _Cp, class _A1, class _A2, class ..._A3>
296 struct __weak_result_type<_Rp (_Cp::*)(_A1, _A2, _A3...) volatile>
298 typedef _Rp result_type;
301 template <class _Rp, class _Cp, class _A1, class _A2, class ..._A3>
302 struct __weak_result_type<_Rp (_Cp::*)(_A1, _A2, _A3...) const volatile>
304 typedef _Rp result_type;
307 #endif // _LIBCPP_HAS_NO_VARIADICS
311 #ifndef _LIBCPP_HAS_NO_VARIADICS
315 template <class _Fp, class _A0, class ..._Args,
317 inline _LIBCPP_INLINE_VISIBILITY
319 __invoke(_Fp&& __f, _A0&& __a0, _Args&& ...__args)
320 -> decltype((_VSTD::forward<_A0>(__a0).*__f)(_VSTD::forward<_Args>(__args)...))
322 return (_VSTD::forward<_A0>(__a0).*__f)(_VSTD::forward<_Args>(__args)...);
325 template <class _Fp, class _A0, class ..._Args,
327 inline _LIBCPP_INLINE_VISIBILITY
329 __invoke(_Fp&& __f, _A0&& __a0, _Args&& ...__args)
330 -> decltype(((*_VSTD::forward<_A0>(__a0)).*__f)(_VSTD::forward<_Args>(__args)...))
332 return ((*_VSTD::forward<_A0>(__a0)).*__f)(_VSTD::forward<_Args>(__args)...);
337 template <class _Fp, class _A0,
339 inline _LIBCPP_INLINE_VISIBILITY
341 __invoke(_Fp&& __f, _A0&& __a0)
342 -> decltype(_VSTD::forward<_A0>(__a0).*__f)
344 return _VSTD::forward<_A0>(__a0).*__f;
347 template <class _Fp, class _A0,
349 inline _LIBCPP_INLINE_VISIBILITY
351 __invoke(_Fp&& __f, _A0&& __a0)
352 -> decltype((*_VSTD::forward<_A0>(__a0)).*__f)
354 return (*_VSTD::forward<_A0>(__a0)).*__f;
359 template <class _Fp, class ..._Args>
360 inline _LIBCPP_INLINE_VISIBILITY
362 __invoke(_Fp&& __f, _Args&& ...__args)
363 -> decltype(_VSTD::forward<_Fp>(__f)(_VSTD::forward<_Args>(__args)...))
365 return _VSTD::forward<_Fp>(__f)(_VSTD::forward<_Args>(__args)...);
367 template <class _Tp, class ..._Args>
368 struct __invoke_return
370 typedef decltype(__invoke(_VSTD::declval<_Tp>(), _VSTD::declval<_Args>()...)) type;
373 #else // _LIBCPP_HAS_NO_VARIADICS
375 #include <__functional_base_03>
377 #endif // _LIBCPP_HAS_NO_VARIADICS
380 template <class _Ret>
381 struct __invoke_void_return_wrapper
383 #ifndef _LIBCPP_HAS_NO_VARIADICS
384 template <class ..._Args>
385 static _Ret __call(_Args&&... __args) {
386 return __invoke(_VSTD::forward<_Args>(__args)...);
390 static _Ret __call(_Fn __f) {
391 return __invoke(__f);
394 template <class _Fn, class _A0>
395 static _Ret __call(_Fn __f, _A0& __a0) {
396 return __invoke(__f, __a0);
399 template <class _Fn, class _A0, class _A1>
400 static _Ret __call(_Fn __f, _A0& __a0, _A1& __a1) {
401 return __invoke(__f, __a0, __a1);
404 template <class _Fn, class _A0, class _A1, class _A2>
405 static _Ret __call(_Fn __f, _A0& __a0, _A1& __a1, _A2& __a2){
406 return __invoke(__f, __a0, __a1, __a2);
412 struct __invoke_void_return_wrapper<void>
414 #ifndef _LIBCPP_HAS_NO_VARIADICS
415 template <class ..._Args>
416 static void __call(_Args&&... __args) {
417 __invoke(_VSTD::forward<_Args>(__args)...);
421 static void __call(_Fn __f) {
425 template <class _Fn, class _A0>
426 static void __call(_Fn __f, _A0& __a0) {
430 template <class _Fn, class _A0, class _A1>
431 static void __call(_Fn __f, _A0& __a0, _A1& __a1) {
432 __invoke(__f, __a0, __a1);
435 template <class _Fn, class _A0, class _A1, class _A2>
436 static void __call(_Fn __f, _A0& __a0, _A1& __a1, _A2& __a2) {
437 __invoke(__f, __a0, __a1, __a2);
443 class _LIBCPP_TYPE_VIS_ONLY reference_wrapper
444 : public __weak_result_type<_Tp>
453 // construct/copy/destroy
454 _LIBCPP_INLINE_VISIBILITY reference_wrapper(type& __f) _NOEXCEPT
455 : __f_(_VSTD::addressof(__f)) {}
456 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
457 private: reference_wrapper(type&&); public: // = delete; // do not bind to temps
461 _LIBCPP_INLINE_VISIBILITY operator type& () const _NOEXCEPT {return *__f_;}
462 _LIBCPP_INLINE_VISIBILITY type& get() const _NOEXCEPT {return *__f_;}
464 #ifndef _LIBCPP_HAS_NO_VARIADICS
466 template <class... _ArgTypes>
467 _LIBCPP_INLINE_VISIBILITY
468 typename __invoke_of<type&, _ArgTypes...>::type
469 operator() (_ArgTypes&&... __args) const {
470 return __invoke(get(), _VSTD::forward<_ArgTypes>(__args)...);
474 _LIBCPP_INLINE_VISIBILITY
475 typename __invoke_return<type>::type
476 operator() () const {
477 return __invoke(get());
481 _LIBCPP_INLINE_VISIBILITY
482 typename __invoke_return0<type, _A0>::type
483 operator() (_A0& __a0) const {
484 return __invoke(get(), __a0);
488 _LIBCPP_INLINE_VISIBILITY
489 typename __invoke_return0<type, _A0 const>::type
490 operator() (_A0 const& __a0) const {
491 return __invoke(get(), __a0);
494 template <class _A0, class _A1>
495 _LIBCPP_INLINE_VISIBILITY
496 typename __invoke_return1<type, _A0, _A1>::type
497 operator() (_A0& __a0, _A1& __a1) const {
498 return __invoke(get(), __a0, __a1);
501 template <class _A0, class _A1>
502 _LIBCPP_INLINE_VISIBILITY
503 typename __invoke_return1<type, _A0 const, _A1>::type
504 operator() (_A0 const& __a0, _A1& __a1) const {
505 return __invoke(get(), __a0, __a1);
508 template <class _A0, class _A1>
509 _LIBCPP_INLINE_VISIBILITY
510 typename __invoke_return1<type, _A0, _A1 const>::type
511 operator() (_A0& __a0, _A1 const& __a1) const {
512 return __invoke(get(), __a0, __a1);
515 template <class _A0, class _A1>
516 _LIBCPP_INLINE_VISIBILITY
517 typename __invoke_return1<type, _A0 const, _A1 const>::type
518 operator() (_A0 const& __a0, _A1 const& __a1) const {
519 return __invoke(get(), __a0, __a1);
522 template <class _A0, class _A1, class _A2>
523 _LIBCPP_INLINE_VISIBILITY
524 typename __invoke_return2<type, _A0, _A1, _A2>::type
525 operator() (_A0& __a0, _A1& __a1, _A2& __a2) const {
526 return __invoke(get(), __a0, __a1, __a2);
529 template <class _A0, class _A1, class _A2>
530 _LIBCPP_INLINE_VISIBILITY
531 typename __invoke_return2<type, _A0 const, _A1, _A2>::type
532 operator() (_A0 const& __a0, _A1& __a1, _A2& __a2) const {
533 return __invoke(get(), __a0, __a1, __a2);
536 template <class _A0, class _A1, class _A2>
537 _LIBCPP_INLINE_VISIBILITY
538 typename __invoke_return2<type, _A0, _A1 const, _A2>::type
539 operator() (_A0& __a0, _A1 const& __a1, _A2& __a2) const {
540 return __invoke(get(), __a0, __a1, __a2);
543 template <class _A0, class _A1, class _A2>
544 _LIBCPP_INLINE_VISIBILITY
545 typename __invoke_return2<type, _A0, _A1, _A2 const>::type
546 operator() (_A0& __a0, _A1& __a1, _A2 const& __a2) const {
547 return __invoke(get(), __a0, __a1, __a2);
550 template <class _A0, class _A1, class _A2>
551 _LIBCPP_INLINE_VISIBILITY
552 typename __invoke_return2<type, _A0 const, _A1 const, _A2>::type
553 operator() (_A0 const& __a0, _A1 const& __a1, _A2& __a2) const {
554 return __invoke(get(), __a0, __a1, __a2);
557 template <class _A0, class _A1, class _A2>
558 _LIBCPP_INLINE_VISIBILITY
559 typename __invoke_return2<type, _A0 const, _A1, _A2 const>::type
560 operator() (_A0 const& __a0, _A1& __a1, _A2 const& __a2) const {
561 return __invoke(get(), __a0, __a1, __a2);
564 template <class _A0, class _A1, class _A2>
565 _LIBCPP_INLINE_VISIBILITY
566 typename __invoke_return2<type, _A0, _A1 const, _A2 const>::type
567 operator() (_A0& __a0, _A1 const& __a1, _A2 const& __a2) const {
568 return __invoke(get(), __a0, __a1, __a2);
571 template <class _A0, class _A1, class _A2>
572 _LIBCPP_INLINE_VISIBILITY
573 typename __invoke_return2<type, _A0 const, _A1 const, _A2 const>::type
574 operator() (_A0 const& __a0, _A1 const& __a1, _A2 const& __a2) const {
575 return __invoke(get(), __a0, __a1, __a2);
577 #endif // _LIBCPP_HAS_NO_VARIADICS
580 template <class _Tp> struct __is_reference_wrapper_impl : public false_type {};
581 template <class _Tp> struct __is_reference_wrapper_impl<reference_wrapper<_Tp> > : public true_type {};
582 template <class _Tp> struct __is_reference_wrapper
583 : public __is_reference_wrapper_impl<typename remove_cv<_Tp>::type> {};
586 inline _LIBCPP_INLINE_VISIBILITY
587 reference_wrapper<_Tp>
588 ref(_Tp& __t) _NOEXCEPT
590 return reference_wrapper<_Tp>(__t);
594 inline _LIBCPP_INLINE_VISIBILITY
595 reference_wrapper<_Tp>
596 ref(reference_wrapper<_Tp> __t) _NOEXCEPT
598 return ref(__t.get());
602 inline _LIBCPP_INLINE_VISIBILITY
603 reference_wrapper<const _Tp>
604 cref(const _Tp& __t) _NOEXCEPT
606 return reference_wrapper<const _Tp>(__t);
610 inline _LIBCPP_INLINE_VISIBILITY
611 reference_wrapper<const _Tp>
612 cref(reference_wrapper<_Tp> __t) _NOEXCEPT
614 return cref(__t.get());
617 #ifndef _LIBCPP_HAS_NO_VARIADICS
618 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
619 #ifndef _LIBCPP_HAS_NO_DELETED_FUNCTIONS
621 template <class _Tp> void ref(const _Tp&&) = delete;
622 template <class _Tp> void cref(const _Tp&&) = delete;
624 #else // _LIBCPP_HAS_NO_DELETED_FUNCTIONS
626 template <class _Tp> void ref(const _Tp&&);// = delete;
627 template <class _Tp> void cref(const _Tp&&);// = delete;
629 #endif // _LIBCPP_HAS_NO_DELETED_FUNCTIONS
631 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
633 #endif // _LIBCPP_HAS_NO_VARIADICS
635 #if _LIBCPP_STD_VER > 11
636 template <class _Tp1, class _Tp2 = void>
637 struct __is_transparent
640 struct __two {char __lx; char __lxx;};
641 template <class _Up> static __two __test(...);
642 template <class _Up> static char __test(typename _Up::is_transparent* = 0);
644 static const bool value = sizeof(__test<_Tp1>(0)) == 1;
650 struct _LIBCPP_TYPE_VIS_ONLY allocator_arg_t { };
652 #if defined(_LIBCPP_HAS_NO_CONSTEXPR) || defined(_LIBCPP_BUILDING_MEMORY)
653 extern const allocator_arg_t allocator_arg;
655 constexpr allocator_arg_t allocator_arg = allocator_arg_t();
661 struct __has_allocator_type
664 struct __two {char __lx; char __lxx;};
665 template <class _Up> static __two __test(...);
666 template <class _Up> static char __test(typename _Up::allocator_type* = 0);
668 static const bool value = sizeof(__test<_Tp>(0)) == 1;
671 template <class _Tp, class _Alloc, bool = __has_allocator_type<_Tp>::value>
672 struct __uses_allocator
673 : public integral_constant<bool,
674 is_convertible<_Alloc, typename _Tp::allocator_type>::value>
678 template <class _Tp, class _Alloc>
679 struct __uses_allocator<_Tp, _Alloc, false>
684 template <class _Tp, class _Alloc>
685 struct _LIBCPP_TYPE_VIS_ONLY uses_allocator
686 : public __uses_allocator<_Tp, _Alloc>
690 #ifndef _LIBCPP_HAS_NO_VARIADICS
692 // allocator construction
694 template <class _Tp, class _Alloc, class ..._Args>
695 struct __uses_alloc_ctor_imp
697 static const bool __ua = uses_allocator<_Tp, _Alloc>::value;
698 static const bool __ic =
699 is_constructible<_Tp, allocator_arg_t, _Alloc, _Args...>::value;
700 static const int value = __ua ? 2 - __ic : 0;
703 template <class _Tp, class _Alloc, class ..._Args>
704 struct __uses_alloc_ctor
705 : integral_constant<int, __uses_alloc_ctor_imp<_Tp, _Alloc, _Args...>::value>
708 template <class _Tp, class _Allocator, class... _Args>
709 inline _LIBCPP_INLINE_VISIBILITY
710 void __user_alloc_construct_impl (integral_constant<int, 0>, _Tp *__storage, const _Allocator &, _Args &&... __args )
712 new (__storage) _Tp (_VSTD::forward<_Args>(__args)...);
715 template <class _Tp, class _Allocator, class... _Args>
716 inline _LIBCPP_INLINE_VISIBILITY
717 void __user_alloc_construct_impl (integral_constant<int, 1>, _Tp *__storage, const _Allocator &__a, _Args &&... __args )
719 new (__storage) _Tp (allocator_arg, __a, _VSTD::forward<_Args>(__args)...);
722 template <class _Tp, class _Allocator, class... _Args>
723 inline _LIBCPP_INLINE_VISIBILITY
724 void __user_alloc_construct_impl (integral_constant<int, 2>, _Tp *__storage, const _Allocator &__a, _Args &&... __args )
726 new (__storage) _Tp (_VSTD::forward<_Args>(__args)..., __a);
729 template <class _Tp, class _Allocator, class... _Args>
730 inline _LIBCPP_INLINE_VISIBILITY
731 void __user_alloc_construct (_Tp *__storage, const _Allocator &__a, _Args &&... __args)
733 __user_alloc_construct_impl(
734 __uses_alloc_ctor<_Tp, _Allocator>(),
735 __storage, __a, _VSTD::forward<_Args>(__args)...
738 #endif // _LIBCPP_HAS_NO_VARIADICS
740 _LIBCPP_END_NAMESPACE_STD
742 #endif // _LIBCPP_FUNCTIONAL_BASE