1// -*- C++ -*-
2//===------------------------ functional ----------------------------------===//
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_FUNCTIONAL
11#define _LIBCPP_FUNCTIONAL
12
13/*
14    functional synopsis
15
16namespace std
17{
18
19template <class Arg, class Result>
20struct unary_function
21{
22    typedef Arg    argument_type;
23    typedef Result result_type;
24};
25
26template <class Arg1, class Arg2, class Result>
27struct binary_function
28{
29    typedef Arg1   first_argument_type;
30    typedef Arg2   second_argument_type;
31    typedef Result result_type;
32};
33
34template <class T>
35class reference_wrapper
36    : public unary_function<T1, R> // if wrapping a unary functor
37    : public binary_function<T1, T2, R> // if wraping a binary functor
38{
39public:
40    // types
41    typedef T type;
42    typedef see below result_type; // Not always defined
43
44    // construct/copy/destroy
45    template<class U>
46      reference_wrapper(U&&);
47    reference_wrapper(const reference_wrapper<T>& x) noexcept;
48
49    // assignment
50    reference_wrapper& operator=(const reference_wrapper<T>& x) noexcept;
51
52    // access
53    operator T& () const noexcept;
54    T& get() const noexcept;
55
56    // invoke
57    template <class... ArgTypes>
58      typename result_of<T&(ArgTypes&&...)>::type
59          operator() (ArgTypes&&...) const;
60};
61
62template <class T>
63  reference_wrapper(T&) -> reference_wrapper<T>;
64
65template <class T> reference_wrapper<T> ref(T& t) noexcept;
66template <class T> void ref(const T&& t) = delete;
67template <class T> reference_wrapper<T> ref(reference_wrapper<T>t) noexcept;
68
69template <class T> reference_wrapper<const T> cref(const T& t) noexcept;
70template <class T> void cref(const T&& t) = delete;
71template <class T> reference_wrapper<const T> cref(reference_wrapper<T> t) noexcept;
72
73template <class T> struct unwrap_reference;                                       // since C++20
74template <class T> struct unwrap_ref_decay : unwrap_reference<decay_t<T>> { };    // since C++20
75template <class T> using unwrap_reference_t = typename unwrap_reference<T>::type; // since C++20
76template <class T> using unwrap_ref_decay_t = typename unwrap_ref_decay<T>::type; // since C++20
77
78template <class T> // <class T=void> in C++14
79struct plus : binary_function<T, T, T>
80{
81    T operator()(const T& x, const T& y) const;
82};
83
84template <class T> // <class T=void> in C++14
85struct minus : binary_function<T, T, T>
86{
87    T operator()(const T& x, const T& y) const;
88};
89
90template <class T> // <class T=void> in C++14
91struct multiplies : binary_function<T, T, T>
92{
93    T operator()(const T& x, const T& y) const;
94};
95
96template <class T> // <class T=void> in C++14
97struct divides : binary_function<T, T, T>
98{
99    T operator()(const T& x, const T& y) const;
100};
101
102template <class T> // <class T=void> in C++14
103struct modulus : binary_function<T, T, T>
104{
105    T operator()(const T& x, const T& y) const;
106};
107
108template <class T> // <class T=void> in C++14
109struct negate : unary_function<T, T>
110{
111    T operator()(const T& x) const;
112};
113
114template <class T> // <class T=void> in C++14
115struct equal_to : binary_function<T, T, bool>
116{
117    bool operator()(const T& x, const T& y) const;
118};
119
120template <class T> // <class T=void> in C++14
121struct not_equal_to : binary_function<T, T, bool>
122{
123    bool operator()(const T& x, const T& y) const;
124};
125
126template <class T> // <class T=void> in C++14
127struct greater : binary_function<T, T, bool>
128{
129    bool operator()(const T& x, const T& y) const;
130};
131
132template <class T> // <class T=void> in C++14
133struct less : binary_function<T, T, bool>
134{
135    bool operator()(const T& x, const T& y) const;
136};
137
138template <class T> // <class T=void> in C++14
139struct greater_equal : binary_function<T, T, bool>
140{
141    bool operator()(const T& x, const T& y) const;
142};
143
144template <class T> // <class T=void> in C++14
145struct less_equal : binary_function<T, T, bool>
146{
147    bool operator()(const T& x, const T& y) const;
148};
149
150template <class T> // <class T=void> in C++14
151struct logical_and : binary_function<T, T, bool>
152{
153    bool operator()(const T& x, const T& y) const;
154};
155
156template <class T> // <class T=void> in C++14
157struct logical_or : binary_function<T, T, bool>
158{
159    bool operator()(const T& x, const T& y) const;
160};
161
162template <class T> // <class T=void> in C++14
163struct logical_not : unary_function<T, bool>
164{
165    bool operator()(const T& x) const;
166};
167
168template <class T> // <class T=void> in C++14
169struct bit_and : binary_function<T, T, T>
170{
171    T operator()(const T& x, const T& y) const;
172};
173
174template <class T> // <class T=void> in C++14
175struct bit_or : binary_function<T, T, T>
176{
177    T operator()(const T& x, const T& y) const;
178};
179
180template <class T> // <class T=void> in C++14
181struct bit_xor : binary_function<T, T, T>
182{
183    T operator()(const T& x, const T& y) const;
184};
185
186template <class T=void> // C++14
187struct bit_not : unary_function<T, T>
188{
189    T operator()(const T& x) const;
190};
191
192struct identity; // C++20
193
194template <class Predicate>
195class unary_negate // deprecated in C++17, removed in C++20
196    : public unary_function<typename Predicate::argument_type, bool>
197{
198public:
199    explicit unary_negate(const Predicate& pred);
200    bool operator()(const typename Predicate::argument_type& x) const;
201};
202
203template <class Predicate> // deprecated in C++17, removed in C++20
204unary_negate<Predicate> not1(const Predicate& pred);
205
206template <class Predicate>
207class binary_negate // deprecated in C++17, removed in C++20
208    : public binary_function<typename Predicate::first_argument_type,
209                             typename Predicate::second_argument_type,
210                             bool>
211{
212public:
213    explicit binary_negate(const Predicate& pred);
214    bool operator()(const typename Predicate::first_argument_type& x,
215                    const typename Predicate::second_argument_type& y) const;
216};
217
218template <class Predicate> // deprecated in C++17, removed in C++20
219binary_negate<Predicate> not2(const Predicate& pred);
220
221template <class F>
222constexpr unspecified not_fn(F&& f); // C++17, constexpr in C++20
223
224template<class T> struct is_bind_expression;
225template<class T> struct is_placeholder;
226
227    // See C++14 20.9.9, Function object binders
228template <class T> inline constexpr bool is_bind_expression_v
229  = is_bind_expression<T>::value; // C++17
230template <class T> inline constexpr int is_placeholder_v
231  = is_placeholder<T>::value; // C++17
232
233
234template<class Fn, class... BoundArgs>
235  constexpr unspecified bind(Fn&&, BoundArgs&&...);  // constexpr in C++20
236template<class R, class Fn, class... BoundArgs>
237  constexpr unspecified bind(Fn&&, BoundArgs&&...);  // constexpr in C++20
238
239template<class F, class... Args>
240 constexpr // constexpr in C++20
241 invoke_result_t<F, Args...> invoke(F&& f, Args&&... args) // C++17
242    noexcept(is_nothrow_invocable_v<F, Args...>);
243
244namespace placeholders {
245  // M is the implementation-defined number of placeholders
246  extern unspecified _1;
247  extern unspecified _2;
248  .
249  .
250  .
251  extern unspecified _Mp;
252}
253
254template <class Operation>
255class binder1st     // deprecated in C++11, removed in C++17
256    : public unary_function<typename Operation::second_argument_type,
257                            typename Operation::result_type>
258{
259protected:
260    Operation                               op;
261    typename Operation::first_argument_type value;
262public:
263    binder1st(const Operation& x, const typename Operation::first_argument_type y);
264    typename Operation::result_type operator()(      typename Operation::second_argument_type& x) const;
265    typename Operation::result_type operator()(const typename Operation::second_argument_type& x) const;
266};
267
268template <class Operation, class T>
269binder1st<Operation> bind1st(const Operation& op, const T& x);  // deprecated in C++11, removed in C++17
270
271template <class Operation>
272class binder2nd     // deprecated in C++11, removed in C++17
273    : public unary_function<typename Operation::first_argument_type,
274                            typename Operation::result_type>
275{
276protected:
277    Operation                                op;
278    typename Operation::second_argument_type value;
279public:
280    binder2nd(const Operation& x, const typename Operation::second_argument_type y);
281    typename Operation::result_type operator()(      typename Operation::first_argument_type& x) const;
282    typename Operation::result_type operator()(const typename Operation::first_argument_type& x) const;
283};
284
285template <class Operation, class T>
286binder2nd<Operation> bind2nd(const Operation& op, const T& x);  // deprecated in C++11, removed in C++17
287
288template <class Arg, class Result>      // deprecated in C++11, removed in C++17
289class pointer_to_unary_function : public unary_function<Arg, Result>
290{
291public:
292    explicit pointer_to_unary_function(Result (*f)(Arg));
293    Result operator()(Arg x) const;
294};
295
296template <class Arg, class Result>
297pointer_to_unary_function<Arg,Result> ptr_fun(Result (*f)(Arg));      // deprecated in C++11, removed in C++17
298
299template <class Arg1, class Arg2, class Result>      // deprecated in C++11, removed in C++17
300class pointer_to_binary_function : public binary_function<Arg1, Arg2, Result>
301{
302public:
303    explicit pointer_to_binary_function(Result (*f)(Arg1, Arg2));
304    Result operator()(Arg1 x, Arg2 y) const;
305};
306
307template <class Arg1, class Arg2, class Result>
308pointer_to_binary_function<Arg1,Arg2,Result> ptr_fun(Result (*f)(Arg1,Arg2));      // deprecated in C++11, removed in C++17
309
310template<class S, class T>      // deprecated in C++11, removed in C++17
311class mem_fun_t : public unary_function<T*, S>
312{
313public:
314    explicit mem_fun_t(S (T::*p)());
315    S operator()(T* p) const;
316};
317
318template<class S, class T, class A>
319class mem_fun1_t : public binary_function<T*, A, S>      // deprecated in C++11, removed in C++17
320{
321public:
322    explicit mem_fun1_t(S (T::*p)(A));
323    S operator()(T* p, A x) const;
324};
325
326template<class S, class T>          mem_fun_t<S,T>    mem_fun(S (T::*f)());      // deprecated in C++11, removed in C++17
327template<class S, class T, class A> mem_fun1_t<S,T,A> mem_fun(S (T::*f)(A));     // deprecated in C++11, removed in C++17
328
329template<class S, class T>
330class mem_fun_ref_t : public unary_function<T, S>      // deprecated in C++11, removed in C++17
331{
332public:
333    explicit mem_fun_ref_t(S (T::*p)());
334    S operator()(T& p) const;
335};
336
337template<class S, class T, class A>
338class mem_fun1_ref_t : public binary_function<T, A, S>      // deprecated in C++11, removed in C++17
339{
340public:
341    explicit mem_fun1_ref_t(S (T::*p)(A));
342    S operator()(T& p, A x) const;
343};
344
345template<class S, class T>          mem_fun_ref_t<S,T>    mem_fun_ref(S (T::*f)());      // deprecated in C++11, removed in C++17
346template<class S, class T, class A> mem_fun1_ref_t<S,T,A> mem_fun_ref(S (T::*f)(A));     // deprecated in C++11, removed in C++17
347
348template <class S, class T>
349class const_mem_fun_t : public unary_function<const T*, S>      // deprecated in C++11, removed in C++17
350{
351public:
352    explicit const_mem_fun_t(S (T::*p)() const);
353    S operator()(const T* p) const;
354};
355
356template <class S, class T, class A>
357class const_mem_fun1_t : public binary_function<const T*, A, S>      // deprecated in C++11, removed in C++17
358{
359public:
360    explicit const_mem_fun1_t(S (T::*p)(A) const);
361    S operator()(const T* p, A x) const;
362};
363
364template <class S, class T>          const_mem_fun_t<S,T>    mem_fun(S (T::*f)() const);      // deprecated in C++11, removed in C++17
365template <class S, class T, class A> const_mem_fun1_t<S,T,A> mem_fun(S (T::*f)(A) const);     // deprecated in C++11, removed in C++17
366
367template <class S, class T>
368class const_mem_fun_ref_t : public unary_function<T, S>      // deprecated in C++11, removed in C++17
369{
370public:
371    explicit const_mem_fun_ref_t(S (T::*p)() const);
372    S operator()(const T& p) const;
373};
374
375template <class S, class T, class A>
376class const_mem_fun1_ref_t : public binary_function<T, A, S>      // deprecated in C++11, removed in C++17
377{
378public:
379    explicit const_mem_fun1_ref_t(S (T::*p)(A) const);
380    S operator()(const T& p, A x) const;
381};
382
383template <class S, class T>          const_mem_fun_ref_t<S,T>    mem_fun_ref(S (T::*f)() const);   // deprecated in C++11, removed in C++17
384template <class S, class T, class A> const_mem_fun1_ref_t<S,T,A> mem_fun_ref(S (T::*f)(A) const);  // deprecated in C++11, removed in C++17
385
386template<class R, class T>
387constexpr unspecified mem_fn(R T::*); // constexpr in C++20
388
389class bad_function_call
390    : public exception
391{
392};
393
394template<class> class function; // undefined
395
396template<class R, class... ArgTypes>
397class function<R(ArgTypes...)>
398  : public unary_function<T1, R>      // iff sizeof...(ArgTypes) == 1 and
399                                      // ArgTypes contains T1
400  : public binary_function<T1, T2, R> // iff sizeof...(ArgTypes) == 2 and
401                                      // ArgTypes contains T1 and T2
402{
403public:
404    typedef R result_type;
405
406    // construct/copy/destroy:
407    function() noexcept;
408    function(nullptr_t) noexcept;
409    function(const function&);
410    function(function&&) noexcept;
411    template<class F>
412      function(F);
413    template<Allocator Alloc>
414      function(allocator_arg_t, const Alloc&) noexcept;            // removed in C++17
415    template<Allocator Alloc>
416      function(allocator_arg_t, const Alloc&, nullptr_t) noexcept; // removed in C++17
417    template<Allocator Alloc>
418      function(allocator_arg_t, const Alloc&, const function&);    // removed in C++17
419    template<Allocator Alloc>
420      function(allocator_arg_t, const Alloc&, function&&);         // removed in C++17
421    template<class F, Allocator Alloc>
422      function(allocator_arg_t, const Alloc&, F);                  // removed in C++17
423
424    function& operator=(const function&);
425    function& operator=(function&&) noexcept;
426    function& operator=(nullptr_t) noexcept;
427    template<class F>
428      function& operator=(F&&);
429    template<class F>
430      function& operator=(reference_wrapper<F>) noexcept;
431
432    ~function();
433
434    // function modifiers:
435    void swap(function&) noexcept;
436    template<class F, class Alloc>
437      void assign(F&&, const Alloc&);                 // Removed in C++17
438
439    // function capacity:
440    explicit operator bool() const noexcept;
441
442    // function invocation:
443    R operator()(ArgTypes...) const;
444
445    // function target access:
446    const std::type_info& target_type() const noexcept;
447    template <typename T>       T* target() noexcept;
448    template <typename T> const T* target() const noexcept;
449};
450
451// Deduction guides
452template<class R, class ...Args>
453function(R(*)(Args...)) -> function<R(Args...)>; // since C++17
454
455template<class F>
456function(F) -> function<see-below>; // since C++17
457
458// Null pointer comparisons:
459template <class R, class ... ArgTypes>
460  bool operator==(const function<R(ArgTypes...)>&, nullptr_t) noexcept;
461
462template <class R, class ... ArgTypes>
463  bool operator==(nullptr_t, const function<R(ArgTypes...)>&) noexcept;
464
465template <class R, class ... ArgTypes>
466  bool operator!=(const function<R(ArgTypes...)>&, nullptr_t) noexcept;
467
468template <class  R, class ... ArgTypes>
469  bool operator!=(nullptr_t, const function<R(ArgTypes...)>&) noexcept;
470
471// specialized algorithms:
472template <class  R, class ... ArgTypes>
473  void swap(function<R(ArgTypes...)>&, function<R(ArgTypes...)>&) noexcept;
474
475template <class T> struct hash;
476
477template <> struct hash<bool>;
478template <> struct hash<char>;
479template <> struct hash<signed char>;
480template <> struct hash<unsigned char>;
481template <> struct hash<char8_t>; // since C++20
482template <> struct hash<char16_t>;
483template <> struct hash<char32_t>;
484template <> struct hash<wchar_t>;
485template <> struct hash<short>;
486template <> struct hash<unsigned short>;
487template <> struct hash<int>;
488template <> struct hash<unsigned int>;
489template <> struct hash<long>;
490template <> struct hash<long long>;
491template <> struct hash<unsigned long>;
492template <> struct hash<unsigned long long>;
493
494template <> struct hash<float>;
495template <> struct hash<double>;
496template <> struct hash<long double>;
497
498template<class T> struct hash<T*>;
499template <> struct hash<nullptr_t>;  // C++17
500
501}  // std
502
503POLICY:  For non-variadic implementations, the number of arguments is limited
504         to 3.  It is hoped that the need for non-variadic implementations
505         will be minimal.
506
507*/
508
509#include <__config>
510#include <__debug>
511#include <__functional_base>
512#include <__functional/search.h>
513#include <concepts>
514#include <exception>
515#include <memory>
516#include <tuple>
517#include <type_traits>
518#include <typeinfo>
519#include <utility>
520#include <version>
521
522#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
523#pragma GCC system_header
524#endif
525
526_LIBCPP_BEGIN_NAMESPACE_STD
527
528#if _LIBCPP_STD_VER > 11
529template <class _Tp = void>
530#else
531template <class _Tp>
532#endif
533struct _LIBCPP_TEMPLATE_VIS plus : binary_function<_Tp, _Tp, _Tp>
534{
535    typedef _Tp __result_type;  // used by valarray
536    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
537    _Tp operator()(const _Tp& __x, const _Tp& __y) const
538        {return __x + __y;}
539};
540
541#if _LIBCPP_STD_VER > 11
542template <>
543struct _LIBCPP_TEMPLATE_VIS plus<void>
544{
545    template <class _T1, class _T2>
546    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
547    auto operator()(_T1&& __t, _T2&& __u) const
548    _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u)))
549    -> decltype        (_VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u))
550        { return        _VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u); }
551    typedef void is_transparent;
552};
553#endif
554
555
556#if _LIBCPP_STD_VER > 11
557template <class _Tp = void>
558#else
559template <class _Tp>
560#endif
561struct _LIBCPP_TEMPLATE_VIS minus : binary_function<_Tp, _Tp, _Tp>
562{
563    typedef _Tp __result_type;  // used by valarray
564    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
565    _Tp operator()(const _Tp& __x, const _Tp& __y) const
566        {return __x - __y;}
567};
568
569#if _LIBCPP_STD_VER > 11
570template <>
571struct _LIBCPP_TEMPLATE_VIS minus<void>
572{
573    template <class _T1, class _T2>
574    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
575    auto operator()(_T1&& __t, _T2&& __u) const
576    _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) - _VSTD::forward<_T2>(__u)))
577    -> decltype        (_VSTD::forward<_T1>(__t) - _VSTD::forward<_T2>(__u))
578        { return        _VSTD::forward<_T1>(__t) - _VSTD::forward<_T2>(__u); }
579    typedef void is_transparent;
580};
581#endif
582
583
584#if _LIBCPP_STD_VER > 11
585template <class _Tp = void>
586#else
587template <class _Tp>
588#endif
589struct _LIBCPP_TEMPLATE_VIS multiplies : binary_function<_Tp, _Tp, _Tp>
590{
591    typedef _Tp __result_type;  // used by valarray
592    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
593    _Tp operator()(const _Tp& __x, const _Tp& __y) const
594        {return __x * __y;}
595};
596
597#if _LIBCPP_STD_VER > 11
598template <>
599struct _LIBCPP_TEMPLATE_VIS multiplies<void>
600{
601    template <class _T1, class _T2>
602    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
603    auto operator()(_T1&& __t, _T2&& __u) const
604    _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) * _VSTD::forward<_T2>(__u)))
605    -> decltype        (_VSTD::forward<_T1>(__t) * _VSTD::forward<_T2>(__u))
606        { return        _VSTD::forward<_T1>(__t) * _VSTD::forward<_T2>(__u); }
607    typedef void is_transparent;
608};
609#endif
610
611
612#if _LIBCPP_STD_VER > 11
613template <class _Tp = void>
614#else
615template <class _Tp>
616#endif
617struct _LIBCPP_TEMPLATE_VIS divides : binary_function<_Tp, _Tp, _Tp>
618{
619    typedef _Tp __result_type;  // used by valarray
620    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
621    _Tp operator()(const _Tp& __x, const _Tp& __y) const
622        {return __x / __y;}
623};
624
625#if _LIBCPP_STD_VER > 11
626template <>
627struct _LIBCPP_TEMPLATE_VIS divides<void>
628{
629    template <class _T1, class _T2>
630    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
631    auto operator()(_T1&& __t, _T2&& __u) const
632    _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) / _VSTD::forward<_T2>(__u)))
633    -> decltype        (_VSTD::forward<_T1>(__t) / _VSTD::forward<_T2>(__u))
634        { return        _VSTD::forward<_T1>(__t) / _VSTD::forward<_T2>(__u); }
635    typedef void is_transparent;
636};
637#endif
638
639
640#if _LIBCPP_STD_VER > 11
641template <class _Tp = void>
642#else
643template <class _Tp>
644#endif
645struct _LIBCPP_TEMPLATE_VIS modulus : binary_function<_Tp, _Tp, _Tp>
646{
647    typedef _Tp __result_type;  // used by valarray
648    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
649    _Tp operator()(const _Tp& __x, const _Tp& __y) const
650        {return __x % __y;}
651};
652
653#if _LIBCPP_STD_VER > 11
654template <>
655struct _LIBCPP_TEMPLATE_VIS modulus<void>
656{
657    template <class _T1, class _T2>
658    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
659    auto operator()(_T1&& __t, _T2&& __u) const
660    _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) % _VSTD::forward<_T2>(__u)))
661    -> decltype        (_VSTD::forward<_T1>(__t) % _VSTD::forward<_T2>(__u))
662        { return        _VSTD::forward<_T1>(__t) % _VSTD::forward<_T2>(__u); }
663    typedef void is_transparent;
664};
665#endif
666
667
668#if _LIBCPP_STD_VER > 11
669template <class _Tp = void>
670#else
671template <class _Tp>
672#endif
673struct _LIBCPP_TEMPLATE_VIS negate : unary_function<_Tp, _Tp>
674{
675    typedef _Tp __result_type;  // used by valarray
676    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
677    _Tp operator()(const _Tp& __x) const
678        {return -__x;}
679};
680
681#if _LIBCPP_STD_VER > 11
682template <>
683struct _LIBCPP_TEMPLATE_VIS negate<void>
684{
685    template <class _Tp>
686    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
687    auto operator()(_Tp&& __x) const
688    _NOEXCEPT_(noexcept(- _VSTD::forward<_Tp>(__x)))
689    -> decltype        (- _VSTD::forward<_Tp>(__x))
690        { return        - _VSTD::forward<_Tp>(__x); }
691    typedef void is_transparent;
692};
693#endif
694
695
696#if _LIBCPP_STD_VER > 11
697template <class _Tp = void>
698#else
699template <class _Tp>
700#endif
701struct _LIBCPP_TEMPLATE_VIS equal_to : binary_function<_Tp, _Tp, bool>
702{
703    typedef bool __result_type;  // used by valarray
704    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
705    bool operator()(const _Tp& __x, const _Tp& __y) const
706        {return __x == __y;}
707};
708
709#if _LIBCPP_STD_VER > 11
710template <>
711struct _LIBCPP_TEMPLATE_VIS equal_to<void>
712{
713    template <class _T1, class _T2>
714    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
715    auto operator()(_T1&& __t, _T2&& __u) const
716    _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) == _VSTD::forward<_T2>(__u)))
717    -> decltype        (_VSTD::forward<_T1>(__t) == _VSTD::forward<_T2>(__u))
718        { return        _VSTD::forward<_T1>(__t) == _VSTD::forward<_T2>(__u); }
719    typedef void is_transparent;
720};
721#endif
722
723
724#if _LIBCPP_STD_VER > 11
725template <class _Tp = void>
726#else
727template <class _Tp>
728#endif
729struct _LIBCPP_TEMPLATE_VIS not_equal_to : binary_function<_Tp, _Tp, bool>
730{
731    typedef bool __result_type;  // used by valarray
732    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
733    bool operator()(const _Tp& __x, const _Tp& __y) const
734        {return __x != __y;}
735};
736
737#if _LIBCPP_STD_VER > 11
738template <>
739struct _LIBCPP_TEMPLATE_VIS not_equal_to<void>
740{
741    template <class _T1, class _T2>
742    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
743    auto operator()(_T1&& __t, _T2&& __u) const
744    _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) != _VSTD::forward<_T2>(__u)))
745    -> decltype        (_VSTD::forward<_T1>(__t) != _VSTD::forward<_T2>(__u))
746        { return        _VSTD::forward<_T1>(__t) != _VSTD::forward<_T2>(__u); }
747    typedef void is_transparent;
748};
749#endif
750
751
752#if _LIBCPP_STD_VER > 11
753template <class _Tp = void>
754#else
755template <class _Tp>
756#endif
757struct _LIBCPP_TEMPLATE_VIS greater : binary_function<_Tp, _Tp, bool>
758{
759    typedef bool __result_type;  // used by valarray
760    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
761    bool operator()(const _Tp& __x, const _Tp& __y) const
762        {return __x > __y;}
763};
764
765#if _LIBCPP_STD_VER > 11
766template <>
767struct _LIBCPP_TEMPLATE_VIS greater<void>
768{
769    template <class _T1, class _T2>
770    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
771    auto operator()(_T1&& __t, _T2&& __u) const
772    _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) > _VSTD::forward<_T2>(__u)))
773    -> decltype        (_VSTD::forward<_T1>(__t) > _VSTD::forward<_T2>(__u))
774        { return        _VSTD::forward<_T1>(__t) > _VSTD::forward<_T2>(__u); }
775    typedef void is_transparent;
776};
777#endif
778
779
780// less in <__functional_base>
781
782#if _LIBCPP_STD_VER > 11
783template <class _Tp = void>
784#else
785template <class _Tp>
786#endif
787struct _LIBCPP_TEMPLATE_VIS greater_equal : binary_function<_Tp, _Tp, bool>
788{
789    typedef bool __result_type;  // used by valarray
790    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
791    bool operator()(const _Tp& __x, const _Tp& __y) const
792        {return __x >= __y;}
793};
794
795#if _LIBCPP_STD_VER > 11
796template <>
797struct _LIBCPP_TEMPLATE_VIS greater_equal<void>
798{
799    template <class _T1, class _T2>
800    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
801    auto operator()(_T1&& __t, _T2&& __u) const
802    _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) >= _VSTD::forward<_T2>(__u)))
803    -> decltype        (_VSTD::forward<_T1>(__t) >= _VSTD::forward<_T2>(__u))
804        { return        _VSTD::forward<_T1>(__t) >= _VSTD::forward<_T2>(__u); }
805    typedef void is_transparent;
806};
807#endif
808
809
810#if _LIBCPP_STD_VER > 11
811template <class _Tp = void>
812#else
813template <class _Tp>
814#endif
815struct _LIBCPP_TEMPLATE_VIS less_equal : binary_function<_Tp, _Tp, bool>
816{
817    typedef bool __result_type;  // used by valarray
818    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
819    bool operator()(const _Tp& __x, const _Tp& __y) const
820        {return __x <= __y;}
821};
822
823#if _LIBCPP_STD_VER > 11
824template <>
825struct _LIBCPP_TEMPLATE_VIS less_equal<void>
826{
827    template <class _T1, class _T2>
828    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
829    auto operator()(_T1&& __t, _T2&& __u) const
830    _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) <= _VSTD::forward<_T2>(__u)))
831    -> decltype        (_VSTD::forward<_T1>(__t) <= _VSTD::forward<_T2>(__u))
832        { return        _VSTD::forward<_T1>(__t) <= _VSTD::forward<_T2>(__u); }
833    typedef void is_transparent;
834};
835#endif
836
837
838#if _LIBCPP_STD_VER > 11
839template <class _Tp = void>
840#else
841template <class _Tp>
842#endif
843struct _LIBCPP_TEMPLATE_VIS logical_and : binary_function<_Tp, _Tp, bool>
844{
845    typedef bool __result_type;  // used by valarray
846    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
847    bool operator()(const _Tp& __x, const _Tp& __y) const
848        {return __x && __y;}
849};
850
851#if _LIBCPP_STD_VER > 11
852template <>
853struct _LIBCPP_TEMPLATE_VIS logical_and<void>
854{
855    template <class _T1, class _T2>
856    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
857    auto operator()(_T1&& __t, _T2&& __u) const
858    _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) && _VSTD::forward<_T2>(__u)))
859    -> decltype        (_VSTD::forward<_T1>(__t) && _VSTD::forward<_T2>(__u))
860        { return        _VSTD::forward<_T1>(__t) && _VSTD::forward<_T2>(__u); }
861    typedef void is_transparent;
862};
863#endif
864
865
866#if _LIBCPP_STD_VER > 11
867template <class _Tp = void>
868#else
869template <class _Tp>
870#endif
871struct _LIBCPP_TEMPLATE_VIS logical_or : binary_function<_Tp, _Tp, bool>
872{
873    typedef bool __result_type;  // used by valarray
874    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
875    bool operator()(const _Tp& __x, const _Tp& __y) const
876        {return __x || __y;}
877};
878
879#if _LIBCPP_STD_VER > 11
880template <>
881struct _LIBCPP_TEMPLATE_VIS logical_or<void>
882{
883    template <class _T1, class _T2>
884    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
885    auto operator()(_T1&& __t, _T2&& __u) const
886    _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) || _VSTD::forward<_T2>(__u)))
887    -> decltype        (_VSTD::forward<_T1>(__t) || _VSTD::forward<_T2>(__u))
888        { return        _VSTD::forward<_T1>(__t) || _VSTD::forward<_T2>(__u); }
889    typedef void is_transparent;
890};
891#endif
892
893
894#if _LIBCPP_STD_VER > 11
895template <class _Tp = void>
896#else
897template <class _Tp>
898#endif
899struct _LIBCPP_TEMPLATE_VIS logical_not : unary_function<_Tp, bool>
900{
901    typedef bool __result_type;  // used by valarray
902    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
903    bool operator()(const _Tp& __x) const
904        {return !__x;}
905};
906
907#if _LIBCPP_STD_VER > 11
908template <>
909struct _LIBCPP_TEMPLATE_VIS logical_not<void>
910{
911    template <class _Tp>
912    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
913    auto operator()(_Tp&& __x) const
914    _NOEXCEPT_(noexcept(!_VSTD::forward<_Tp>(__x)))
915    -> decltype        (!_VSTD::forward<_Tp>(__x))
916        { return        !_VSTD::forward<_Tp>(__x); }
917    typedef void is_transparent;
918};
919#endif
920
921
922#if _LIBCPP_STD_VER > 11
923template <class _Tp = void>
924#else
925template <class _Tp>
926#endif
927struct _LIBCPP_TEMPLATE_VIS bit_and : binary_function<_Tp, _Tp, _Tp>
928{
929    typedef _Tp __result_type;  // used by valarray
930    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
931    _Tp operator()(const _Tp& __x, const _Tp& __y) const
932        {return __x & __y;}
933};
934
935#if _LIBCPP_STD_VER > 11
936template <>
937struct _LIBCPP_TEMPLATE_VIS bit_and<void>
938{
939    template <class _T1, class _T2>
940    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
941    auto operator()(_T1&& __t, _T2&& __u) const
942    _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) & _VSTD::forward<_T2>(__u)))
943    -> decltype        (_VSTD::forward<_T1>(__t) & _VSTD::forward<_T2>(__u))
944        { return        _VSTD::forward<_T1>(__t) & _VSTD::forward<_T2>(__u); }
945    typedef void is_transparent;
946};
947#endif
948
949
950#if _LIBCPP_STD_VER > 11
951template <class _Tp = void>
952#else
953template <class _Tp>
954#endif
955struct _LIBCPP_TEMPLATE_VIS bit_or : binary_function<_Tp, _Tp, _Tp>
956{
957    typedef _Tp __result_type;  // used by valarray
958    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
959    _Tp operator()(const _Tp& __x, const _Tp& __y) const
960        {return __x | __y;}
961};
962
963#if _LIBCPP_STD_VER > 11
964template <>
965struct _LIBCPP_TEMPLATE_VIS bit_or<void>
966{
967    template <class _T1, class _T2>
968    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
969    auto operator()(_T1&& __t, _T2&& __u) const
970    _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) | _VSTD::forward<_T2>(__u)))
971    -> decltype        (_VSTD::forward<_T1>(__t) | _VSTD::forward<_T2>(__u))
972        { return        _VSTD::forward<_T1>(__t) | _VSTD::forward<_T2>(__u); }
973    typedef void is_transparent;
974};
975#endif
976
977
978#if _LIBCPP_STD_VER > 11
979template <class _Tp = void>
980#else
981template <class _Tp>
982#endif
983struct _LIBCPP_TEMPLATE_VIS bit_xor : binary_function<_Tp, _Tp, _Tp>
984{
985    typedef _Tp __result_type;  // used by valarray
986    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
987    _Tp operator()(const _Tp& __x, const _Tp& __y) const
988        {return __x ^ __y;}
989};
990
991#if _LIBCPP_STD_VER > 11
992template <>
993struct _LIBCPP_TEMPLATE_VIS bit_xor<void>
994{
995    template <class _T1, class _T2>
996    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
997    auto operator()(_T1&& __t, _T2&& __u) const
998    _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) ^ _VSTD::forward<_T2>(__u)))
999    -> decltype        (_VSTD::forward<_T1>(__t) ^ _VSTD::forward<_T2>(__u))
1000        { return        _VSTD::forward<_T1>(__t) ^ _VSTD::forward<_T2>(__u); }
1001    typedef void is_transparent;
1002};
1003#endif
1004
1005
1006#if _LIBCPP_STD_VER > 11
1007template <class _Tp = void>
1008struct _LIBCPP_TEMPLATE_VIS bit_not : unary_function<_Tp, _Tp>
1009{
1010    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1011    _Tp operator()(const _Tp& __x) const
1012        {return ~__x;}
1013};
1014
1015template <>
1016struct _LIBCPP_TEMPLATE_VIS bit_not<void>
1017{
1018    template <class _Tp>
1019    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1020    auto operator()(_Tp&& __x) const
1021    _NOEXCEPT_(noexcept(~_VSTD::forward<_Tp>(__x)))
1022    -> decltype        (~_VSTD::forward<_Tp>(__x))
1023        { return        ~_VSTD::forward<_Tp>(__x); }
1024    typedef void is_transparent;
1025};
1026#endif
1027
1028#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_NEGATORS)
1029template <class _Predicate>
1030class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX17 unary_negate
1031    : public unary_function<typename _Predicate::argument_type, bool>
1032{
1033    _Predicate __pred_;
1034public:
1035    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1036    explicit unary_negate(const _Predicate& __pred)
1037        : __pred_(__pred) {}
1038    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1039    bool operator()(const typename _Predicate::argument_type& __x) const
1040        {return !__pred_(__x);}
1041};
1042
1043template <class _Predicate>
1044_LIBCPP_DEPRECATED_IN_CXX17 inline _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1045unary_negate<_Predicate>
1046not1(const _Predicate& __pred) {return unary_negate<_Predicate>(__pred);}
1047
1048template <class _Predicate>
1049class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX17 binary_negate
1050    : public binary_function<typename _Predicate::first_argument_type,
1051                             typename _Predicate::second_argument_type,
1052                             bool>
1053{
1054    _Predicate __pred_;
1055public:
1056    _LIBCPP_INLINE_VISIBILITY explicit _LIBCPP_CONSTEXPR_AFTER_CXX11
1057    binary_negate(const _Predicate& __pred) : __pred_(__pred) {}
1058
1059    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1060    bool operator()(const typename _Predicate::first_argument_type& __x,
1061                    const typename _Predicate::second_argument_type& __y) const
1062        {return !__pred_(__x, __y);}
1063};
1064
1065template <class _Predicate>
1066_LIBCPP_DEPRECATED_IN_CXX17 inline _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1067binary_negate<_Predicate>
1068not2(const _Predicate& __pred) {return binary_negate<_Predicate>(__pred);}
1069#endif // _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_NEGATORS)
1070
1071#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_BINDERS)
1072template <class __Operation>
1073class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 binder1st
1074    : public unary_function<typename __Operation::second_argument_type,
1075                            typename __Operation::result_type>
1076{
1077protected:
1078    __Operation                               op;
1079    typename __Operation::first_argument_type value;
1080public:
1081    _LIBCPP_INLINE_VISIBILITY binder1st(const __Operation& __x,
1082                               const typename __Operation::first_argument_type __y)
1083        : op(__x), value(__y) {}
1084    _LIBCPP_INLINE_VISIBILITY typename __Operation::result_type operator()
1085        (typename __Operation::second_argument_type& __x) const
1086            {return op(value, __x);}
1087    _LIBCPP_INLINE_VISIBILITY typename __Operation::result_type operator()
1088        (const typename __Operation::second_argument_type& __x) const
1089            {return op(value, __x);}
1090};
1091
1092template <class __Operation, class _Tp>
1093_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
1094binder1st<__Operation>
1095bind1st(const __Operation& __op, const _Tp& __x)
1096    {return binder1st<__Operation>(__op, __x);}
1097
1098template <class __Operation>
1099class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 binder2nd
1100    : public unary_function<typename __Operation::first_argument_type,
1101                            typename __Operation::result_type>
1102{
1103protected:
1104    __Operation                                op;
1105    typename __Operation::second_argument_type value;
1106public:
1107    _LIBCPP_INLINE_VISIBILITY
1108    binder2nd(const __Operation& __x, const typename __Operation::second_argument_type __y)
1109        : op(__x), value(__y) {}
1110    _LIBCPP_INLINE_VISIBILITY typename __Operation::result_type operator()
1111        (      typename __Operation::first_argument_type& __x) const
1112            {return op(__x, value);}
1113    _LIBCPP_INLINE_VISIBILITY typename __Operation::result_type operator()
1114        (const typename __Operation::first_argument_type& __x) const
1115            {return op(__x, value);}
1116};
1117
1118template <class __Operation, class _Tp>
1119_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
1120binder2nd<__Operation>
1121bind2nd(const __Operation& __op, const _Tp& __x)
1122    {return binder2nd<__Operation>(__op, __x);}
1123
1124template <class _Arg, class _Result>
1125class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 pointer_to_unary_function
1126    : public unary_function<_Arg, _Result>
1127{
1128    _Result (*__f_)(_Arg);
1129public:
1130    _LIBCPP_INLINE_VISIBILITY explicit pointer_to_unary_function(_Result (*__f)(_Arg))
1131        : __f_(__f) {}
1132    _LIBCPP_INLINE_VISIBILITY _Result operator()(_Arg __x) const
1133        {return __f_(__x);}
1134};
1135
1136template <class _Arg, class _Result>
1137_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
1138pointer_to_unary_function<_Arg,_Result>
1139ptr_fun(_Result (*__f)(_Arg))
1140    {return pointer_to_unary_function<_Arg,_Result>(__f);}
1141
1142template <class _Arg1, class _Arg2, class _Result>
1143class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 pointer_to_binary_function
1144    : public binary_function<_Arg1, _Arg2, _Result>
1145{
1146    _Result (*__f_)(_Arg1, _Arg2);
1147public:
1148    _LIBCPP_INLINE_VISIBILITY explicit pointer_to_binary_function(_Result (*__f)(_Arg1, _Arg2))
1149        : __f_(__f) {}
1150    _LIBCPP_INLINE_VISIBILITY _Result operator()(_Arg1 __x, _Arg2 __y) const
1151        {return __f_(__x, __y);}
1152};
1153
1154template <class _Arg1, class _Arg2, class _Result>
1155_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
1156pointer_to_binary_function<_Arg1,_Arg2,_Result>
1157ptr_fun(_Result (*__f)(_Arg1,_Arg2))
1158    {return pointer_to_binary_function<_Arg1,_Arg2,_Result>(__f);}
1159
1160template<class _Sp, class _Tp>
1161class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 mem_fun_t
1162    : public unary_function<_Tp*, _Sp>
1163{
1164    _Sp (_Tp::*__p_)();
1165public:
1166    _LIBCPP_INLINE_VISIBILITY explicit mem_fun_t(_Sp (_Tp::*__p)())
1167        : __p_(__p) {}
1168    _LIBCPP_INLINE_VISIBILITY _Sp operator()(_Tp* __p) const
1169        {return (__p->*__p_)();}
1170};
1171
1172template<class _Sp, class _Tp, class _Ap>
1173class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 mem_fun1_t
1174    : public binary_function<_Tp*, _Ap, _Sp>
1175{
1176    _Sp (_Tp::*__p_)(_Ap);
1177public:
1178    _LIBCPP_INLINE_VISIBILITY explicit mem_fun1_t(_Sp (_Tp::*__p)(_Ap))
1179        : __p_(__p) {}
1180    _LIBCPP_INLINE_VISIBILITY _Sp operator()(_Tp* __p, _Ap __x) const
1181        {return (__p->*__p_)(__x);}
1182};
1183
1184template<class _Sp, class _Tp>
1185_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
1186mem_fun_t<_Sp,_Tp>
1187mem_fun(_Sp (_Tp::*__f)())
1188    {return mem_fun_t<_Sp,_Tp>(__f);}
1189
1190template<class _Sp, class _Tp, class _Ap>
1191_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
1192mem_fun1_t<_Sp,_Tp,_Ap>
1193mem_fun(_Sp (_Tp::*__f)(_Ap))
1194    {return mem_fun1_t<_Sp,_Tp,_Ap>(__f);}
1195
1196template<class _Sp, class _Tp>
1197class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 mem_fun_ref_t
1198    : public unary_function<_Tp, _Sp>
1199{
1200    _Sp (_Tp::*__p_)();
1201public:
1202    _LIBCPP_INLINE_VISIBILITY explicit mem_fun_ref_t(_Sp (_Tp::*__p)())
1203        : __p_(__p) {}
1204    _LIBCPP_INLINE_VISIBILITY _Sp operator()(_Tp& __p) const
1205        {return (__p.*__p_)();}
1206};
1207
1208template<class _Sp, class _Tp, class _Ap>
1209class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 mem_fun1_ref_t
1210    : public binary_function<_Tp, _Ap, _Sp>
1211{
1212    _Sp (_Tp::*__p_)(_Ap);
1213public:
1214    _LIBCPP_INLINE_VISIBILITY explicit mem_fun1_ref_t(_Sp (_Tp::*__p)(_Ap))
1215        : __p_(__p) {}
1216    _LIBCPP_INLINE_VISIBILITY _Sp operator()(_Tp& __p, _Ap __x) const
1217        {return (__p.*__p_)(__x);}
1218};
1219
1220template<class _Sp, class _Tp>
1221_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
1222mem_fun_ref_t<_Sp,_Tp>
1223mem_fun_ref(_Sp (_Tp::*__f)())
1224    {return mem_fun_ref_t<_Sp,_Tp>(__f);}
1225
1226template<class _Sp, class _Tp, class _Ap>
1227_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
1228mem_fun1_ref_t<_Sp,_Tp,_Ap>
1229mem_fun_ref(_Sp (_Tp::*__f)(_Ap))
1230    {return mem_fun1_ref_t<_Sp,_Tp,_Ap>(__f);}
1231
1232template <class _Sp, class _Tp>
1233class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 const_mem_fun_t
1234    : public unary_function<const _Tp*, _Sp>
1235{
1236    _Sp (_Tp::*__p_)() const;
1237public:
1238    _LIBCPP_INLINE_VISIBILITY explicit const_mem_fun_t(_Sp (_Tp::*__p)() const)
1239        : __p_(__p) {}
1240    _LIBCPP_INLINE_VISIBILITY _Sp operator()(const _Tp* __p) const
1241        {return (__p->*__p_)();}
1242};
1243
1244template <class _Sp, class _Tp, class _Ap>
1245class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 const_mem_fun1_t
1246    : public binary_function<const _Tp*, _Ap, _Sp>
1247{
1248    _Sp (_Tp::*__p_)(_Ap) const;
1249public:
1250    _LIBCPP_INLINE_VISIBILITY explicit const_mem_fun1_t(_Sp (_Tp::*__p)(_Ap) const)
1251        : __p_(__p) {}
1252    _LIBCPP_INLINE_VISIBILITY _Sp operator()(const _Tp* __p, _Ap __x) const
1253        {return (__p->*__p_)(__x);}
1254};
1255
1256template <class _Sp, class _Tp>
1257_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
1258const_mem_fun_t<_Sp,_Tp>
1259mem_fun(_Sp (_Tp::*__f)() const)
1260    {return const_mem_fun_t<_Sp,_Tp>(__f);}
1261
1262template <class _Sp, class _Tp, class _Ap>
1263_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
1264const_mem_fun1_t<_Sp,_Tp,_Ap>
1265mem_fun(_Sp (_Tp::*__f)(_Ap) const)
1266    {return const_mem_fun1_t<_Sp,_Tp,_Ap>(__f);}
1267
1268template <class _Sp, class _Tp>
1269class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 const_mem_fun_ref_t
1270    : public unary_function<_Tp, _Sp>
1271{
1272    _Sp (_Tp::*__p_)() const;
1273public:
1274    _LIBCPP_INLINE_VISIBILITY explicit const_mem_fun_ref_t(_Sp (_Tp::*__p)() const)
1275        : __p_(__p) {}
1276    _LIBCPP_INLINE_VISIBILITY _Sp operator()(const _Tp& __p) const
1277        {return (__p.*__p_)();}
1278};
1279
1280template <class _Sp, class _Tp, class _Ap>
1281class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 const_mem_fun1_ref_t
1282    : public binary_function<_Tp, _Ap, _Sp>
1283{
1284    _Sp (_Tp::*__p_)(_Ap) const;
1285public:
1286    _LIBCPP_INLINE_VISIBILITY explicit const_mem_fun1_ref_t(_Sp (_Tp::*__p)(_Ap) const)
1287        : __p_(__p) {}
1288    _LIBCPP_INLINE_VISIBILITY _Sp operator()(const _Tp& __p, _Ap __x) const
1289        {return (__p.*__p_)(__x);}
1290};
1291
1292template <class _Sp, class _Tp>
1293_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
1294const_mem_fun_ref_t<_Sp,_Tp>
1295mem_fun_ref(_Sp (_Tp::*__f)() const)
1296    {return const_mem_fun_ref_t<_Sp,_Tp>(__f);}
1297
1298template <class _Sp, class _Tp, class _Ap>
1299_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
1300const_mem_fun1_ref_t<_Sp,_Tp,_Ap>
1301mem_fun_ref(_Sp (_Tp::*__f)(_Ap) const)
1302    {return const_mem_fun1_ref_t<_Sp,_Tp,_Ap>(__f);}
1303#endif
1304
1305////////////////////////////////////////////////////////////////////////////////
1306//                                MEMFUN
1307//==============================================================================
1308
1309template <class _Tp>
1310class __mem_fn
1311    : public __weak_result_type<_Tp>
1312{
1313public:
1314    // types
1315    typedef _Tp type;
1316private:
1317    type __f_;
1318
1319public:
1320    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1321    __mem_fn(type __f) _NOEXCEPT : __f_(__f) {}
1322
1323#ifndef _LIBCPP_CXX03_LANG
1324    // invoke
1325    template <class... _ArgTypes>
1326    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1327    typename __invoke_return<type, _ArgTypes...>::type
1328    operator() (_ArgTypes&&... __args) const {
1329        return _VSTD::__invoke(__f_, _VSTD::forward<_ArgTypes>(__args)...);
1330    }
1331#else
1332
1333    template <class _A0>
1334    _LIBCPP_INLINE_VISIBILITY
1335    typename __invoke_return0<type, _A0>::type
1336    operator() (_A0& __a0) const {
1337        return _VSTD::__invoke(__f_, __a0);
1338    }
1339
1340    template <class _A0>
1341    _LIBCPP_INLINE_VISIBILITY
1342    typename __invoke_return0<type, _A0 const>::type
1343    operator() (_A0 const& __a0) const {
1344        return _VSTD::__invoke(__f_, __a0);
1345    }
1346
1347    template <class _A0, class _A1>
1348    _LIBCPP_INLINE_VISIBILITY
1349    typename __invoke_return1<type, _A0, _A1>::type
1350    operator() (_A0& __a0, _A1& __a1) const {
1351        return _VSTD::__invoke(__f_, __a0, __a1);
1352    }
1353
1354    template <class _A0, class _A1>
1355    _LIBCPP_INLINE_VISIBILITY
1356    typename __invoke_return1<type, _A0 const, _A1>::type
1357    operator() (_A0 const& __a0, _A1& __a1) const {
1358        return _VSTD::__invoke(__f_, __a0, __a1);
1359    }
1360
1361    template <class _A0, class _A1>
1362    _LIBCPP_INLINE_VISIBILITY
1363    typename __invoke_return1<type, _A0, _A1 const>::type
1364    operator() (_A0& __a0, _A1 const& __a1) const {
1365        return _VSTD::__invoke(__f_, __a0, __a1);
1366    }
1367
1368    template <class _A0, class _A1>
1369    _LIBCPP_INLINE_VISIBILITY
1370    typename __invoke_return1<type, _A0 const, _A1 const>::type
1371    operator() (_A0 const& __a0, _A1 const& __a1) const {
1372        return _VSTD::__invoke(__f_, __a0, __a1);
1373    }
1374
1375    template <class _A0, class _A1, class _A2>
1376    _LIBCPP_INLINE_VISIBILITY
1377    typename __invoke_return2<type, _A0, _A1, _A2>::type
1378    operator() (_A0& __a0, _A1& __a1, _A2& __a2) const {
1379        return _VSTD::__invoke(__f_, __a0, __a1, __a2);
1380    }
1381
1382    template <class _A0, class _A1, class _A2>
1383    _LIBCPP_INLINE_VISIBILITY
1384    typename __invoke_return2<type, _A0 const, _A1, _A2>::type
1385    operator() (_A0 const& __a0, _A1& __a1, _A2& __a2) const {
1386        return _VSTD::__invoke(__f_, __a0, __a1, __a2);
1387    }
1388
1389    template <class _A0, class _A1, class _A2>
1390    _LIBCPP_INLINE_VISIBILITY
1391    typename __invoke_return2<type, _A0, _A1 const, _A2>::type
1392    operator() (_A0& __a0, _A1 const& __a1, _A2& __a2) const {
1393        return _VSTD::__invoke(__f_, __a0, __a1, __a2);
1394    }
1395
1396    template <class _A0, class _A1, class _A2>
1397    _LIBCPP_INLINE_VISIBILITY
1398    typename __invoke_return2<type, _A0, _A1, _A2 const>::type
1399    operator() (_A0& __a0, _A1& __a1, _A2 const& __a2) const {
1400        return _VSTD::__invoke(__f_, __a0, __a1, __a2);
1401    }
1402
1403    template <class _A0, class _A1, class _A2>
1404    _LIBCPP_INLINE_VISIBILITY
1405    typename __invoke_return2<type, _A0 const, _A1 const, _A2>::type
1406    operator() (_A0 const& __a0, _A1 const& __a1, _A2& __a2) const {
1407        return _VSTD::__invoke(__f_, __a0, __a1, __a2);
1408    }
1409
1410    template <class _A0, class _A1, class _A2>
1411    _LIBCPP_INLINE_VISIBILITY
1412    typename __invoke_return2<type, _A0 const, _A1, _A2 const>::type
1413    operator() (_A0 const& __a0, _A1& __a1, _A2 const& __a2) const {
1414        return _VSTD::__invoke(__f_, __a0, __a1, __a2);
1415    }
1416
1417    template <class _A0, class _A1, class _A2>
1418    _LIBCPP_INLINE_VISIBILITY
1419    typename __invoke_return2<type, _A0, _A1 const, _A2 const>::type
1420    operator() (_A0& __a0, _A1 const& __a1, _A2 const& __a2) const {
1421        return _VSTD::__invoke(__f_, __a0, __a1, __a2);
1422    }
1423
1424    template <class _A0, class _A1, class _A2>
1425    _LIBCPP_INLINE_VISIBILITY
1426    typename __invoke_return2<type, _A0 const, _A1 const, _A2 const>::type
1427    operator() (_A0 const& __a0, _A1 const& __a1, _A2 const& __a2) const {
1428        return _VSTD::__invoke(__f_, __a0, __a1, __a2);
1429    }
1430#endif
1431};
1432
1433template<class _Rp, class _Tp>
1434inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1435__mem_fn<_Rp _Tp::*>
1436mem_fn(_Rp _Tp::* __pm) _NOEXCEPT
1437{
1438    return __mem_fn<_Rp _Tp::*>(__pm);
1439}
1440
1441////////////////////////////////////////////////////////////////////////////////
1442//                                FUNCTION
1443//==============================================================================
1444
1445// bad_function_call
1446
1447class _LIBCPP_EXCEPTION_ABI bad_function_call
1448    : public exception
1449{
1450#ifdef _LIBCPP_ABI_BAD_FUNCTION_CALL_KEY_FUNCTION
1451public:
1452    virtual ~bad_function_call() _NOEXCEPT;
1453
1454    virtual const char* what() const _NOEXCEPT;
1455#endif
1456};
1457
1458_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
1459void __throw_bad_function_call()
1460{
1461#ifndef _LIBCPP_NO_EXCEPTIONS
1462    throw bad_function_call();
1463#else
1464    _VSTD::abort();
1465#endif
1466}
1467
1468#if defined(_LIBCPP_CXX03_LANG) && !defined(_LIBCPP_DISABLE_DEPRECATION_WARNINGS) && __has_attribute(deprecated)
1469#   define _LIBCPP_DEPRECATED_CXX03_FUNCTION \
1470        __attribute__((deprecated("Using std::function in C++03 is not supported anymore. Please upgrade to C++11 or later, or use a different type")))
1471#else
1472#   define _LIBCPP_DEPRECATED_CXX03_FUNCTION /* nothing */
1473#endif
1474
1475template<class _Fp> class _LIBCPP_DEPRECATED_CXX03_FUNCTION _LIBCPP_TEMPLATE_VIS function; // undefined
1476
1477namespace __function
1478{
1479
1480template<class _Rp>
1481struct __maybe_derive_from_unary_function
1482{
1483};
1484
1485template<class _Rp, class _A1>
1486struct __maybe_derive_from_unary_function<_Rp(_A1)>
1487    : public unary_function<_A1, _Rp>
1488{
1489};
1490
1491template<class _Rp>
1492struct __maybe_derive_from_binary_function
1493{
1494};
1495
1496template<class _Rp, class _A1, class _A2>
1497struct __maybe_derive_from_binary_function<_Rp(_A1, _A2)>
1498    : public binary_function<_A1, _A2, _Rp>
1499{
1500};
1501
1502template <class _Fp>
1503_LIBCPP_INLINE_VISIBILITY
1504bool __not_null(_Fp const&) { return true; }
1505
1506template <class _Fp>
1507_LIBCPP_INLINE_VISIBILITY
1508bool __not_null(_Fp* __ptr) { return __ptr; }
1509
1510template <class _Ret, class _Class>
1511_LIBCPP_INLINE_VISIBILITY
1512bool __not_null(_Ret _Class::*__ptr) { return __ptr; }
1513
1514template <class _Fp>
1515_LIBCPP_INLINE_VISIBILITY
1516bool __not_null(function<_Fp> const& __f) { return !!__f; }
1517
1518#ifdef _LIBCPP_HAS_EXTENSION_BLOCKS
1519template <class _Rp, class ..._Args>
1520_LIBCPP_INLINE_VISIBILITY
1521bool __not_null(_Rp (^__p)(_Args...)) { return __p; }
1522#endif
1523
1524} // namespace __function
1525
1526#ifndef _LIBCPP_CXX03_LANG
1527
1528namespace __function {
1529
1530// __alloc_func holds a functor and an allocator.
1531
1532template <class _Fp, class _Ap, class _FB> class __alloc_func;
1533template <class _Fp, class _FB>
1534class __default_alloc_func;
1535
1536template <class _Fp, class _Ap, class _Rp, class... _ArgTypes>
1537class __alloc_func<_Fp, _Ap, _Rp(_ArgTypes...)>
1538{
1539    __compressed_pair<_Fp, _Ap> __f_;
1540
1541  public:
1542    typedef _LIBCPP_NODEBUG_TYPE _Fp _Target;
1543    typedef _LIBCPP_NODEBUG_TYPE _Ap _Alloc;
1544
1545    _LIBCPP_INLINE_VISIBILITY
1546    const _Target& __target() const { return __f_.first(); }
1547
1548    // WIN32 APIs may define __allocator, so use __get_allocator instead.
1549    _LIBCPP_INLINE_VISIBILITY
1550    const _Alloc& __get_allocator() const { return __f_.second(); }
1551
1552    _LIBCPP_INLINE_VISIBILITY
1553    explicit __alloc_func(_Target&& __f)
1554        : __f_(piecewise_construct, _VSTD::forward_as_tuple(_VSTD::move(__f)),
1555               _VSTD::forward_as_tuple())
1556    {
1557    }
1558
1559    _LIBCPP_INLINE_VISIBILITY
1560    explicit __alloc_func(const _Target& __f, const _Alloc& __a)
1561        : __f_(piecewise_construct, _VSTD::forward_as_tuple(__f),
1562               _VSTD::forward_as_tuple(__a))
1563    {
1564    }
1565
1566    _LIBCPP_INLINE_VISIBILITY
1567    explicit __alloc_func(const _Target& __f, _Alloc&& __a)
1568        : __f_(piecewise_construct, _VSTD::forward_as_tuple(__f),
1569               _VSTD::forward_as_tuple(_VSTD::move(__a)))
1570    {
1571    }
1572
1573    _LIBCPP_INLINE_VISIBILITY
1574    explicit __alloc_func(_Target&& __f, _Alloc&& __a)
1575        : __f_(piecewise_construct, _VSTD::forward_as_tuple(_VSTD::move(__f)),
1576               _VSTD::forward_as_tuple(_VSTD::move(__a)))
1577    {
1578    }
1579
1580    _LIBCPP_INLINE_VISIBILITY
1581    _Rp operator()(_ArgTypes&&... __arg)
1582    {
1583        typedef __invoke_void_return_wrapper<_Rp> _Invoker;
1584        return _Invoker::__call(__f_.first(),
1585                                _VSTD::forward<_ArgTypes>(__arg)...);
1586    }
1587
1588    _LIBCPP_INLINE_VISIBILITY
1589    __alloc_func* __clone() const
1590    {
1591        typedef allocator_traits<_Alloc> __alloc_traits;
1592        typedef
1593            typename __rebind_alloc_helper<__alloc_traits, __alloc_func>::type
1594                _AA;
1595        _AA __a(__f_.second());
1596        typedef __allocator_destructor<_AA> _Dp;
1597        unique_ptr<__alloc_func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1598        ::new ((void*)__hold.get()) __alloc_func(__f_.first(), _Alloc(__a));
1599        return __hold.release();
1600    }
1601
1602    _LIBCPP_INLINE_VISIBILITY
1603    void destroy() _NOEXCEPT { __f_.~__compressed_pair<_Target, _Alloc>(); }
1604
1605    static void __destroy_and_delete(__alloc_func* __f) {
1606      typedef allocator_traits<_Alloc> __alloc_traits;
1607      typedef typename __rebind_alloc_helper<__alloc_traits, __alloc_func>::type
1608          _FunAlloc;
1609      _FunAlloc __a(__f->__get_allocator());
1610      __f->destroy();
1611      __a.deallocate(__f, 1);
1612    }
1613};
1614
1615template <class _Fp, class _Rp, class... _ArgTypes>
1616class __default_alloc_func<_Fp, _Rp(_ArgTypes...)> {
1617  _Fp __f_;
1618
1619public:
1620  typedef _LIBCPP_NODEBUG_TYPE _Fp _Target;
1621
1622  _LIBCPP_INLINE_VISIBILITY
1623  const _Target& __target() const { return __f_; }
1624
1625  _LIBCPP_INLINE_VISIBILITY
1626  explicit __default_alloc_func(_Target&& __f) : __f_(_VSTD::move(__f)) {}
1627
1628  _LIBCPP_INLINE_VISIBILITY
1629  explicit __default_alloc_func(const _Target& __f) : __f_(__f) {}
1630
1631  _LIBCPP_INLINE_VISIBILITY
1632  _Rp operator()(_ArgTypes&&... __arg) {
1633    typedef __invoke_void_return_wrapper<_Rp> _Invoker;
1634    return _Invoker::__call(__f_, _VSTD::forward<_ArgTypes>(__arg)...);
1635  }
1636
1637  _LIBCPP_INLINE_VISIBILITY
1638  __default_alloc_func* __clone() const {
1639      __builtin_new_allocator::__holder_t __hold =
1640        __builtin_new_allocator::__allocate_type<__default_alloc_func>(1);
1641    __default_alloc_func* __res =
1642        ::new ((void*)__hold.get()) __default_alloc_func(__f_);
1643    (void)__hold.release();
1644    return __res;
1645  }
1646
1647  _LIBCPP_INLINE_VISIBILITY
1648  void destroy() _NOEXCEPT { __f_.~_Target(); }
1649
1650  static void __destroy_and_delete(__default_alloc_func* __f) {
1651    __f->destroy();
1652      __builtin_new_allocator::__deallocate_type<__default_alloc_func>(__f, 1);
1653  }
1654};
1655
1656// __base provides an abstract interface for copyable functors.
1657
1658template<class _Fp> class _LIBCPP_TEMPLATE_VIS __base;
1659
1660template<class _Rp, class ..._ArgTypes>
1661class __base<_Rp(_ArgTypes...)>
1662{
1663    __base(const __base&);
1664    __base& operator=(const __base&);
1665public:
1666    _LIBCPP_INLINE_VISIBILITY __base() {}
1667    _LIBCPP_INLINE_VISIBILITY virtual ~__base() {}
1668    virtual __base* __clone() const = 0;
1669    virtual void __clone(__base*) const = 0;
1670    virtual void destroy() _NOEXCEPT = 0;
1671    virtual void destroy_deallocate() _NOEXCEPT = 0;
1672    virtual _Rp operator()(_ArgTypes&& ...) = 0;
1673#ifndef _LIBCPP_NO_RTTI
1674    virtual const void* target(const type_info&) const _NOEXCEPT = 0;
1675    virtual const std::type_info& target_type() const _NOEXCEPT = 0;
1676#endif // _LIBCPP_NO_RTTI
1677};
1678
1679// __func implements __base for a given functor type.
1680
1681template<class _FD, class _Alloc, class _FB> class __func;
1682
1683template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1684class __func<_Fp, _Alloc, _Rp(_ArgTypes...)>
1685    : public  __base<_Rp(_ArgTypes...)>
1686{
1687    __alloc_func<_Fp, _Alloc, _Rp(_ArgTypes...)> __f_;
1688public:
1689    _LIBCPP_INLINE_VISIBILITY
1690    explicit __func(_Fp&& __f)
1691        : __f_(_VSTD::move(__f)) {}
1692
1693    _LIBCPP_INLINE_VISIBILITY
1694    explicit __func(const _Fp& __f, const _Alloc& __a)
1695        : __f_(__f, __a) {}
1696
1697    _LIBCPP_INLINE_VISIBILITY
1698    explicit __func(const _Fp& __f, _Alloc&& __a)
1699        : __f_(__f, _VSTD::move(__a)) {}
1700
1701    _LIBCPP_INLINE_VISIBILITY
1702    explicit __func(_Fp&& __f, _Alloc&& __a)
1703        : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
1704
1705    virtual __base<_Rp(_ArgTypes...)>* __clone() const;
1706    virtual void __clone(__base<_Rp(_ArgTypes...)>*) const;
1707    virtual void destroy() _NOEXCEPT;
1708    virtual void destroy_deallocate() _NOEXCEPT;
1709    virtual _Rp operator()(_ArgTypes&&... __arg);
1710#ifndef _LIBCPP_NO_RTTI
1711    virtual const void* target(const type_info&) const _NOEXCEPT;
1712    virtual const std::type_info& target_type() const _NOEXCEPT;
1713#endif // _LIBCPP_NO_RTTI
1714};
1715
1716template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1717__base<_Rp(_ArgTypes...)>*
1718__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__clone() const
1719{
1720    typedef allocator_traits<_Alloc> __alloc_traits;
1721    typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
1722    _Ap __a(__f_.__get_allocator());
1723    typedef __allocator_destructor<_Ap> _Dp;
1724    unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1725    ::new ((void*)__hold.get()) __func(__f_.__target(), _Alloc(__a));
1726    return __hold.release();
1727}
1728
1729template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1730void
1731__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__clone(__base<_Rp(_ArgTypes...)>* __p) const
1732{
1733    ::new ((void*)__p) __func(__f_.__target(), __f_.__get_allocator());
1734}
1735
1736template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1737void
1738__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy() _NOEXCEPT
1739{
1740    __f_.destroy();
1741}
1742
1743template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1744void
1745__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy_deallocate() _NOEXCEPT
1746{
1747    typedef allocator_traits<_Alloc> __alloc_traits;
1748    typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
1749    _Ap __a(__f_.__get_allocator());
1750    __f_.destroy();
1751    __a.deallocate(this, 1);
1752}
1753
1754template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1755_Rp
1756__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::operator()(_ArgTypes&& ... __arg)
1757{
1758    return __f_(_VSTD::forward<_ArgTypes>(__arg)...);
1759}
1760
1761#ifndef _LIBCPP_NO_RTTI
1762
1763template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1764const void*
1765__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::target(const type_info& __ti) const _NOEXCEPT
1766{
1767    if (__ti == typeid(_Fp))
1768        return &__f_.__target();
1769    return nullptr;
1770}
1771
1772template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1773const std::type_info&
1774__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::target_type() const _NOEXCEPT
1775{
1776    return typeid(_Fp);
1777}
1778
1779#endif // _LIBCPP_NO_RTTI
1780
1781// __value_func creates a value-type from a __func.
1782
1783template <class _Fp> class __value_func;
1784
1785template <class _Rp, class... _ArgTypes> class __value_func<_Rp(_ArgTypes...)>
1786{
1787    typename aligned_storage<3 * sizeof(void*)>::type __buf_;
1788
1789    typedef __base<_Rp(_ArgTypes...)> __func;
1790    __func* __f_;
1791
1792    _LIBCPP_NO_CFI static __func* __as_base(void* p)
1793    {
1794        return reinterpret_cast<__func*>(p);
1795    }
1796
1797  public:
1798    _LIBCPP_INLINE_VISIBILITY
1799    __value_func() _NOEXCEPT : __f_(nullptr) {}
1800
1801    template <class _Fp, class _Alloc>
1802    _LIBCPP_INLINE_VISIBILITY __value_func(_Fp&& __f, const _Alloc& __a)
1803        : __f_(nullptr)
1804    {
1805        typedef allocator_traits<_Alloc> __alloc_traits;
1806        typedef __function::__func<_Fp, _Alloc, _Rp(_ArgTypes...)> _Fun;
1807        typedef typename __rebind_alloc_helper<__alloc_traits, _Fun>::type
1808            _FunAlloc;
1809
1810        if (__function::__not_null(__f))
1811        {
1812            _FunAlloc __af(__a);
1813            if (sizeof(_Fun) <= sizeof(__buf_) &&
1814                is_nothrow_copy_constructible<_Fp>::value &&
1815                is_nothrow_copy_constructible<_FunAlloc>::value)
1816            {
1817                __f_ =
1818                    ::new ((void*)&__buf_) _Fun(_VSTD::move(__f), _Alloc(__af));
1819            }
1820            else
1821            {
1822                typedef __allocator_destructor<_FunAlloc> _Dp;
1823                unique_ptr<__func, _Dp> __hold(__af.allocate(1), _Dp(__af, 1));
1824                ::new ((void*)__hold.get()) _Fun(_VSTD::move(__f), _Alloc(__a));
1825                __f_ = __hold.release();
1826            }
1827        }
1828    }
1829
1830    template <class _Fp,
1831        class = typename enable_if<!is_same<typename decay<_Fp>::type, __value_func>::value>::type>
1832    _LIBCPP_INLINE_VISIBILITY explicit __value_func(_Fp&& __f)
1833        : __value_func(_VSTD::forward<_Fp>(__f), allocator<_Fp>()) {}
1834
1835    _LIBCPP_INLINE_VISIBILITY
1836    __value_func(const __value_func& __f)
1837    {
1838        if (__f.__f_ == nullptr)
1839            __f_ = nullptr;
1840        else if ((void*)__f.__f_ == &__f.__buf_)
1841        {
1842            __f_ = __as_base(&__buf_);
1843            __f.__f_->__clone(__f_);
1844        }
1845        else
1846            __f_ = __f.__f_->__clone();
1847    }
1848
1849    _LIBCPP_INLINE_VISIBILITY
1850    __value_func(__value_func&& __f) _NOEXCEPT
1851    {
1852        if (__f.__f_ == nullptr)
1853            __f_ = nullptr;
1854        else if ((void*)__f.__f_ == &__f.__buf_)
1855        {
1856            __f_ = __as_base(&__buf_);
1857            __f.__f_->__clone(__f_);
1858        }
1859        else
1860        {
1861            __f_ = __f.__f_;
1862            __f.__f_ = nullptr;
1863        }
1864    }
1865
1866    _LIBCPP_INLINE_VISIBILITY
1867    ~__value_func()
1868    {
1869        if ((void*)__f_ == &__buf_)
1870            __f_->destroy();
1871        else if (__f_)
1872            __f_->destroy_deallocate();
1873    }
1874
1875    _LIBCPP_INLINE_VISIBILITY
1876    __value_func& operator=(__value_func&& __f)
1877    {
1878        *this = nullptr;
1879        if (__f.__f_ == nullptr)
1880            __f_ = nullptr;
1881        else if ((void*)__f.__f_ == &__f.__buf_)
1882        {
1883            __f_ = __as_base(&__buf_);
1884            __f.__f_->__clone(__f_);
1885        }
1886        else
1887        {
1888            __f_ = __f.__f_;
1889            __f.__f_ = nullptr;
1890        }
1891        return *this;
1892    }
1893
1894    _LIBCPP_INLINE_VISIBILITY
1895    __value_func& operator=(nullptr_t)
1896    {
1897        __func* __f = __f_;
1898        __f_ = nullptr;
1899        if ((void*)__f == &__buf_)
1900            __f->destroy();
1901        else if (__f)
1902            __f->destroy_deallocate();
1903        return *this;
1904    }
1905
1906    _LIBCPP_INLINE_VISIBILITY
1907    _Rp operator()(_ArgTypes&&... __args) const
1908    {
1909        if (__f_ == nullptr)
1910            __throw_bad_function_call();
1911        return (*__f_)(_VSTD::forward<_ArgTypes>(__args)...);
1912    }
1913
1914    _LIBCPP_INLINE_VISIBILITY
1915    void swap(__value_func& __f) _NOEXCEPT
1916    {
1917        if (&__f == this)
1918            return;
1919        if ((void*)__f_ == &__buf_ && (void*)__f.__f_ == &__f.__buf_)
1920        {
1921            typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1922            __func* __t = __as_base(&__tempbuf);
1923            __f_->__clone(__t);
1924            __f_->destroy();
1925            __f_ = nullptr;
1926            __f.__f_->__clone(__as_base(&__buf_));
1927            __f.__f_->destroy();
1928            __f.__f_ = nullptr;
1929            __f_ = __as_base(&__buf_);
1930            __t->__clone(__as_base(&__f.__buf_));
1931            __t->destroy();
1932            __f.__f_ = __as_base(&__f.__buf_);
1933        }
1934        else if ((void*)__f_ == &__buf_)
1935        {
1936            __f_->__clone(__as_base(&__f.__buf_));
1937            __f_->destroy();
1938            __f_ = __f.__f_;
1939            __f.__f_ = __as_base(&__f.__buf_);
1940        }
1941        else if ((void*)__f.__f_ == &__f.__buf_)
1942        {
1943            __f.__f_->__clone(__as_base(&__buf_));
1944            __f.__f_->destroy();
1945            __f.__f_ = __f_;
1946            __f_ = __as_base(&__buf_);
1947        }
1948        else
1949            _VSTD::swap(__f_, __f.__f_);
1950    }
1951
1952    _LIBCPP_INLINE_VISIBILITY
1953    _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT { return __f_ != nullptr; }
1954
1955#ifndef _LIBCPP_NO_RTTI
1956    _LIBCPP_INLINE_VISIBILITY
1957    const std::type_info& target_type() const _NOEXCEPT
1958    {
1959        if (__f_ == nullptr)
1960            return typeid(void);
1961        return __f_->target_type();
1962    }
1963
1964    template <typename _Tp>
1965    _LIBCPP_INLINE_VISIBILITY const _Tp* target() const _NOEXCEPT
1966    {
1967        if (__f_ == nullptr)
1968            return nullptr;
1969        return (const _Tp*)__f_->target(typeid(_Tp));
1970    }
1971#endif // _LIBCPP_NO_RTTI
1972};
1973
1974// Storage for a functor object, to be used with __policy to manage copy and
1975// destruction.
1976union __policy_storage
1977{
1978    mutable char __small[sizeof(void*) * 2];
1979    void* __large;
1980};
1981
1982// True if _Fun can safely be held in __policy_storage.__small.
1983template <typename _Fun>
1984struct __use_small_storage
1985    : public integral_constant<
1986          bool, sizeof(_Fun) <= sizeof(__policy_storage) &&
1987                    _LIBCPP_ALIGNOF(_Fun) <= _LIBCPP_ALIGNOF(__policy_storage) &&
1988                    is_trivially_copy_constructible<_Fun>::value &&
1989                    is_trivially_destructible<_Fun>::value> {};
1990
1991// Policy contains information about how to copy, destroy, and move the
1992// underlying functor. You can think of it as a vtable of sorts.
1993struct __policy
1994{
1995    // Used to copy or destroy __large values. null for trivial objects.
1996    void* (*const __clone)(const void*);
1997    void (*const __destroy)(void*);
1998
1999    // True if this is the null policy (no value).
2000    const bool __is_null;
2001
2002    // The target type. May be null if RTTI is disabled.
2003    const std::type_info* const __type_info;
2004
2005    // Returns a pointer to a static policy object suitable for the functor
2006    // type.
2007    template <typename _Fun>
2008    _LIBCPP_INLINE_VISIBILITY static const __policy* __create()
2009    {
2010        return __choose_policy<_Fun>(__use_small_storage<_Fun>());
2011    }
2012
2013    _LIBCPP_INLINE_VISIBILITY
2014    static const __policy* __create_empty()
2015    {
2016        static const _LIBCPP_CONSTEXPR __policy __policy_ = {nullptr, nullptr,
2017                                                             true,
2018#ifndef _LIBCPP_NO_RTTI
2019                                                             &typeid(void)
2020#else
2021                                                             nullptr
2022#endif
2023        };
2024        return &__policy_;
2025    }
2026
2027  private:
2028    template <typename _Fun> static void* __large_clone(const void* __s)
2029    {
2030        const _Fun* __f = static_cast<const _Fun*>(__s);
2031        return __f->__clone();
2032    }
2033
2034    template <typename _Fun>
2035    static void __large_destroy(void* __s) {
2036      _Fun::__destroy_and_delete(static_cast<_Fun*>(__s));
2037    }
2038
2039    template <typename _Fun>
2040    _LIBCPP_INLINE_VISIBILITY static const __policy*
2041    __choose_policy(/* is_small = */ false_type) {
2042      static const _LIBCPP_CONSTEXPR __policy __policy_ = {
2043          &__large_clone<_Fun>, &__large_destroy<_Fun>, false,
2044#ifndef _LIBCPP_NO_RTTI
2045          &typeid(typename _Fun::_Target)
2046#else
2047          nullptr
2048#endif
2049      };
2050        return &__policy_;
2051    }
2052
2053    template <typename _Fun>
2054    _LIBCPP_INLINE_VISIBILITY static const __policy*
2055        __choose_policy(/* is_small = */ true_type)
2056    {
2057        static const _LIBCPP_CONSTEXPR __policy __policy_ = {
2058            nullptr, nullptr, false,
2059#ifndef _LIBCPP_NO_RTTI
2060            &typeid(typename _Fun::_Target)
2061#else
2062            nullptr
2063#endif
2064        };
2065        return &__policy_;
2066    }
2067};
2068
2069// Used to choose between perfect forwarding or pass-by-value. Pass-by-value is
2070// faster for types that can be passed in registers.
2071template <typename _Tp>
2072using __fast_forward =
2073    typename conditional<is_scalar<_Tp>::value, _Tp, _Tp&&>::type;
2074
2075// __policy_invoker calls an instance of __alloc_func held in __policy_storage.
2076
2077template <class _Fp> struct __policy_invoker;
2078
2079template <class _Rp, class... _ArgTypes>
2080struct __policy_invoker<_Rp(_ArgTypes...)>
2081{
2082    typedef _Rp (*__Call)(const __policy_storage*,
2083                          __fast_forward<_ArgTypes>...);
2084
2085    __Call __call_;
2086
2087    // Creates an invoker that throws bad_function_call.
2088    _LIBCPP_INLINE_VISIBILITY
2089    __policy_invoker() : __call_(&__call_empty) {}
2090
2091    // Creates an invoker that calls the given instance of __func.
2092    template <typename _Fun>
2093    _LIBCPP_INLINE_VISIBILITY static __policy_invoker __create()
2094    {
2095        return __policy_invoker(&__call_impl<_Fun>);
2096    }
2097
2098  private:
2099    _LIBCPP_INLINE_VISIBILITY
2100    explicit __policy_invoker(__Call __c) : __call_(__c) {}
2101
2102    static _Rp __call_empty(const __policy_storage*,
2103                            __fast_forward<_ArgTypes>...)
2104    {
2105        __throw_bad_function_call();
2106    }
2107
2108    template <typename _Fun>
2109    static _Rp __call_impl(const __policy_storage* __buf,
2110                           __fast_forward<_ArgTypes>... __args)
2111    {
2112        _Fun* __f = reinterpret_cast<_Fun*>(__use_small_storage<_Fun>::value
2113                                                ? &__buf->__small
2114                                                : __buf->__large);
2115        return (*__f)(_VSTD::forward<_ArgTypes>(__args)...);
2116    }
2117};
2118
2119// __policy_func uses a __policy and __policy_invoker to create a type-erased,
2120// copyable functor.
2121
2122template <class _Fp> class __policy_func;
2123
2124template <class _Rp, class... _ArgTypes> class __policy_func<_Rp(_ArgTypes...)>
2125{
2126    // Inline storage for small objects.
2127    __policy_storage __buf_;
2128
2129    // Calls the value stored in __buf_. This could technically be part of
2130    // policy, but storing it here eliminates a level of indirection inside
2131    // operator().
2132    typedef __function::__policy_invoker<_Rp(_ArgTypes...)> __invoker;
2133    __invoker __invoker_;
2134
2135    // The policy that describes how to move / copy / destroy __buf_. Never
2136    // null, even if the function is empty.
2137    const __policy* __policy_;
2138
2139  public:
2140    _LIBCPP_INLINE_VISIBILITY
2141    __policy_func() : __policy_(__policy::__create_empty()) {}
2142
2143    template <class _Fp, class _Alloc>
2144    _LIBCPP_INLINE_VISIBILITY __policy_func(_Fp&& __f, const _Alloc& __a)
2145        : __policy_(__policy::__create_empty())
2146    {
2147        typedef __alloc_func<_Fp, _Alloc, _Rp(_ArgTypes...)> _Fun;
2148        typedef allocator_traits<_Alloc> __alloc_traits;
2149        typedef typename __rebind_alloc_helper<__alloc_traits, _Fun>::type
2150            _FunAlloc;
2151
2152        if (__function::__not_null(__f))
2153        {
2154            __invoker_ = __invoker::template __create<_Fun>();
2155            __policy_ = __policy::__create<_Fun>();
2156
2157            _FunAlloc __af(__a);
2158            if (__use_small_storage<_Fun>())
2159            {
2160                ::new ((void*)&__buf_.__small)
2161                    _Fun(_VSTD::move(__f), _Alloc(__af));
2162            }
2163            else
2164            {
2165                typedef __allocator_destructor<_FunAlloc> _Dp;
2166                unique_ptr<_Fun, _Dp> __hold(__af.allocate(1), _Dp(__af, 1));
2167                ::new ((void*)__hold.get())
2168                    _Fun(_VSTD::move(__f), _Alloc(__af));
2169                __buf_.__large = __hold.release();
2170            }
2171        }
2172    }
2173
2174    template <class _Fp, class = typename enable_if<!is_same<typename decay<_Fp>::type, __policy_func>::value>::type>
2175    _LIBCPP_INLINE_VISIBILITY explicit __policy_func(_Fp&& __f)
2176        : __policy_(__policy::__create_empty()) {
2177      typedef __default_alloc_func<_Fp, _Rp(_ArgTypes...)> _Fun;
2178
2179      if (__function::__not_null(__f)) {
2180        __invoker_ = __invoker::template __create<_Fun>();
2181        __policy_ = __policy::__create<_Fun>();
2182        if (__use_small_storage<_Fun>()) {
2183          ::new ((void*)&__buf_.__small) _Fun(_VSTD::move(__f));
2184        } else {
2185          __builtin_new_allocator::__holder_t __hold =
2186              __builtin_new_allocator::__allocate_type<_Fun>(1);
2187          __buf_.__large = ::new ((void*)__hold.get()) _Fun(_VSTD::move(__f));
2188          (void)__hold.release();
2189        }
2190      }
2191    }
2192
2193    _LIBCPP_INLINE_VISIBILITY
2194    __policy_func(const __policy_func& __f)
2195        : __buf_(__f.__buf_), __invoker_(__f.__invoker_),
2196          __policy_(__f.__policy_)
2197    {
2198        if (__policy_->__clone)
2199            __buf_.__large = __policy_->__clone(__f.__buf_.__large);
2200    }
2201
2202    _LIBCPP_INLINE_VISIBILITY
2203    __policy_func(__policy_func&& __f)
2204        : __buf_(__f.__buf_), __invoker_(__f.__invoker_),
2205          __policy_(__f.__policy_)
2206    {
2207        if (__policy_->__destroy)
2208        {
2209            __f.__policy_ = __policy::__create_empty();
2210            __f.__invoker_ = __invoker();
2211        }
2212    }
2213
2214    _LIBCPP_INLINE_VISIBILITY
2215    ~__policy_func()
2216    {
2217        if (__policy_->__destroy)
2218            __policy_->__destroy(__buf_.__large);
2219    }
2220
2221    _LIBCPP_INLINE_VISIBILITY
2222    __policy_func& operator=(__policy_func&& __f)
2223    {
2224        *this = nullptr;
2225        __buf_ = __f.__buf_;
2226        __invoker_ = __f.__invoker_;
2227        __policy_ = __f.__policy_;
2228        __f.__policy_ = __policy::__create_empty();
2229        __f.__invoker_ = __invoker();
2230        return *this;
2231    }
2232
2233    _LIBCPP_INLINE_VISIBILITY
2234    __policy_func& operator=(nullptr_t)
2235    {
2236        const __policy* __p = __policy_;
2237        __policy_ = __policy::__create_empty();
2238        __invoker_ = __invoker();
2239        if (__p->__destroy)
2240            __p->__destroy(__buf_.__large);
2241        return *this;
2242    }
2243
2244    _LIBCPP_INLINE_VISIBILITY
2245    _Rp operator()(_ArgTypes&&... __args) const
2246    {
2247        return __invoker_.__call_(_VSTD::addressof(__buf_),
2248                                  _VSTD::forward<_ArgTypes>(__args)...);
2249    }
2250
2251    _LIBCPP_INLINE_VISIBILITY
2252    void swap(__policy_func& __f)
2253    {
2254        _VSTD::swap(__invoker_, __f.__invoker_);
2255        _VSTD::swap(__policy_, __f.__policy_);
2256        _VSTD::swap(__buf_, __f.__buf_);
2257    }
2258
2259    _LIBCPP_INLINE_VISIBILITY
2260    explicit operator bool() const _NOEXCEPT
2261    {
2262        return !__policy_->__is_null;
2263    }
2264
2265#ifndef _LIBCPP_NO_RTTI
2266    _LIBCPP_INLINE_VISIBILITY
2267    const std::type_info& target_type() const _NOEXCEPT
2268    {
2269        return *__policy_->__type_info;
2270    }
2271
2272    template <typename _Tp>
2273    _LIBCPP_INLINE_VISIBILITY const _Tp* target() const _NOEXCEPT
2274    {
2275        if (__policy_->__is_null || typeid(_Tp) != *__policy_->__type_info)
2276            return nullptr;
2277        if (__policy_->__clone) // Out of line storage.
2278            return reinterpret_cast<const _Tp*>(__buf_.__large);
2279        else
2280            return reinterpret_cast<const _Tp*>(&__buf_.__small);
2281    }
2282#endif // _LIBCPP_NO_RTTI
2283};
2284
2285#if defined(_LIBCPP_HAS_BLOCKS_RUNTIME) && !defined(_LIBCPP_HAS_OBJC_ARC)
2286
2287extern "C" void *_Block_copy(const void *);
2288extern "C" void _Block_release(const void *);
2289
2290template<class _Rp1, class ..._ArgTypes1, class _Alloc, class _Rp, class ..._ArgTypes>
2291class __func<_Rp1(^)(_ArgTypes1...), _Alloc, _Rp(_ArgTypes...)>
2292    : public  __base<_Rp(_ArgTypes...)>
2293{
2294    typedef _Rp1(^__block_type)(_ArgTypes1...);
2295    __block_type __f_;
2296
2297public:
2298    _LIBCPP_INLINE_VISIBILITY
2299    explicit __func(__block_type const& __f)
2300        : __f_(reinterpret_cast<__block_type>(__f ? _Block_copy(__f) : nullptr))
2301    { }
2302
2303    // [TODO] add && to save on a retain
2304
2305    _LIBCPP_INLINE_VISIBILITY
2306    explicit __func(__block_type __f, const _Alloc& /* unused */)
2307        : __f_(reinterpret_cast<__block_type>(__f ? _Block_copy(__f) : nullptr))
2308    { }
2309
2310    virtual __base<_Rp(_ArgTypes...)>* __clone() const {
2311        _LIBCPP_ASSERT(false,
2312            "Block pointers are just pointers, so they should always fit into "
2313            "std::function's small buffer optimization. This function should "
2314            "never be invoked.");
2315        return nullptr;
2316    }
2317
2318    virtual void __clone(__base<_Rp(_ArgTypes...)>* __p) const {
2319        ::new ((void*)__p) __func(__f_);
2320    }
2321
2322    virtual void destroy() _NOEXCEPT {
2323        if (__f_)
2324            _Block_release(__f_);
2325        __f_ = 0;
2326    }
2327
2328    virtual void destroy_deallocate() _NOEXCEPT {
2329        _LIBCPP_ASSERT(false,
2330            "Block pointers are just pointers, so they should always fit into "
2331            "std::function's small buffer optimization. This function should "
2332            "never be invoked.");
2333    }
2334
2335    virtual _Rp operator()(_ArgTypes&& ... __arg) {
2336        return _VSTD::__invoke(__f_, _VSTD::forward<_ArgTypes>(__arg)...);
2337    }
2338
2339#ifndef _LIBCPP_NO_RTTI
2340    virtual const void* target(type_info const& __ti) const _NOEXCEPT {
2341        if (__ti == typeid(__func::__block_type))
2342            return &__f_;
2343        return (const void*)nullptr;
2344    }
2345
2346    virtual const std::type_info& target_type() const _NOEXCEPT {
2347        return typeid(__func::__block_type);
2348    }
2349#endif // _LIBCPP_NO_RTTI
2350};
2351
2352#endif // _LIBCPP_HAS_EXTENSION_BLOCKS && !_LIBCPP_HAS_OBJC_ARC
2353
2354}  // __function
2355
2356template<class _Rp, class ..._ArgTypes>
2357class _LIBCPP_TEMPLATE_VIS function<_Rp(_ArgTypes...)>
2358    : public __function::__maybe_derive_from_unary_function<_Rp(_ArgTypes...)>,
2359      public __function::__maybe_derive_from_binary_function<_Rp(_ArgTypes...)>
2360{
2361#ifndef _LIBCPP_ABI_OPTIMIZED_FUNCTION
2362    typedef __function::__value_func<_Rp(_ArgTypes...)> __func;
2363#else
2364    typedef __function::__policy_func<_Rp(_ArgTypes...)> __func;
2365#endif
2366
2367    __func __f_;
2368
2369    template <class _Fp, bool = _And<
2370        _IsNotSame<__uncvref_t<_Fp>, function>,
2371        __invokable<_Fp, _ArgTypes...>
2372    >::value>
2373    struct __callable;
2374    template <class _Fp>
2375        struct __callable<_Fp, true>
2376        {
2377            static const bool value = is_void<_Rp>::value ||
2378                __is_core_convertible<typename __invoke_of<_Fp, _ArgTypes...>::type,
2379                                      _Rp>::value;
2380        };
2381    template <class _Fp>
2382        struct __callable<_Fp, false>
2383        {
2384            static const bool value = false;
2385        };
2386
2387  template <class _Fp>
2388  using _EnableIfLValueCallable = typename enable_if<__callable<_Fp&>::value>::type;
2389public:
2390    typedef _Rp result_type;
2391
2392    // construct/copy/destroy:
2393    _LIBCPP_INLINE_VISIBILITY
2394    function() _NOEXCEPT { }
2395    _LIBCPP_INLINE_VISIBILITY
2396    function(nullptr_t) _NOEXCEPT {}
2397    function(const function&);
2398    function(function&&) _NOEXCEPT;
2399    template<class _Fp, class = _EnableIfLValueCallable<_Fp>>
2400    function(_Fp);
2401
2402#if _LIBCPP_STD_VER <= 14
2403    template<class _Alloc>
2404      _LIBCPP_INLINE_VISIBILITY
2405      function(allocator_arg_t, const _Alloc&) _NOEXCEPT {}
2406    template<class _Alloc>
2407      _LIBCPP_INLINE_VISIBILITY
2408      function(allocator_arg_t, const _Alloc&, nullptr_t) _NOEXCEPT {}
2409    template<class _Alloc>
2410      function(allocator_arg_t, const _Alloc&, const function&);
2411    template<class _Alloc>
2412      function(allocator_arg_t, const _Alloc&, function&&);
2413    template<class _Fp, class _Alloc, class = _EnableIfLValueCallable<_Fp>>
2414      function(allocator_arg_t, const _Alloc& __a, _Fp __f);
2415#endif
2416
2417    function& operator=(const function&);
2418    function& operator=(function&&) _NOEXCEPT;
2419    function& operator=(nullptr_t) _NOEXCEPT;
2420    template<class _Fp, class = _EnableIfLValueCallable<typename decay<_Fp>::type>>
2421    function& operator=(_Fp&&);
2422
2423    ~function();
2424
2425    // function modifiers:
2426    void swap(function&) _NOEXCEPT;
2427
2428#if _LIBCPP_STD_VER <= 14
2429    template<class _Fp, class _Alloc>
2430      _LIBCPP_INLINE_VISIBILITY
2431      void assign(_Fp&& __f, const _Alloc& __a)
2432        {function(allocator_arg, __a, _VSTD::forward<_Fp>(__f)).swap(*this);}
2433#endif
2434
2435    // function capacity:
2436    _LIBCPP_INLINE_VISIBILITY
2437    _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
2438      return static_cast<bool>(__f_);
2439    }
2440
2441    // deleted overloads close possible hole in the type system
2442    template<class _R2, class... _ArgTypes2>
2443      bool operator==(const function<_R2(_ArgTypes2...)>&) const = delete;
2444    template<class _R2, class... _ArgTypes2>
2445      bool operator!=(const function<_R2(_ArgTypes2...)>&) const = delete;
2446public:
2447    // function invocation:
2448    _Rp operator()(_ArgTypes...) const;
2449
2450#ifndef _LIBCPP_NO_RTTI
2451    // function target access:
2452    const std::type_info& target_type() const _NOEXCEPT;
2453    template <typename _Tp> _Tp* target() _NOEXCEPT;
2454    template <typename _Tp> const _Tp* target() const _NOEXCEPT;
2455#endif // _LIBCPP_NO_RTTI
2456};
2457
2458#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
2459template<class _Rp, class ..._Ap>
2460function(_Rp(*)(_Ap...)) -> function<_Rp(_Ap...)>;
2461
2462template<class _Fp>
2463struct __strip_signature;
2464
2465template<class _Rp, class _Gp, class ..._Ap>
2466struct __strip_signature<_Rp (_Gp::*) (_Ap...)> { using type = _Rp(_Ap...); };
2467template<class _Rp, class _Gp, class ..._Ap>
2468struct __strip_signature<_Rp (_Gp::*) (_Ap...) const> { using type = _Rp(_Ap...); };
2469template<class _Rp, class _Gp, class ..._Ap>
2470struct __strip_signature<_Rp (_Gp::*) (_Ap...) volatile> { using type = _Rp(_Ap...); };
2471template<class _Rp, class _Gp, class ..._Ap>
2472struct __strip_signature<_Rp (_Gp::*) (_Ap...) const volatile> { using type = _Rp(_Ap...); };
2473
2474template<class _Rp, class _Gp, class ..._Ap>
2475struct __strip_signature<_Rp (_Gp::*) (_Ap...) &> { using type = _Rp(_Ap...); };
2476template<class _Rp, class _Gp, class ..._Ap>
2477struct __strip_signature<_Rp (_Gp::*) (_Ap...) const &> { using type = _Rp(_Ap...); };
2478template<class _Rp, class _Gp, class ..._Ap>
2479struct __strip_signature<_Rp (_Gp::*) (_Ap...) volatile &> { using type = _Rp(_Ap...); };
2480template<class _Rp, class _Gp, class ..._Ap>
2481struct __strip_signature<_Rp (_Gp::*) (_Ap...) const volatile &> { using type = _Rp(_Ap...); };
2482
2483template<class _Rp, class _Gp, class ..._Ap>
2484struct __strip_signature<_Rp (_Gp::*) (_Ap...) noexcept> { using type = _Rp(_Ap...); };
2485template<class _Rp, class _Gp, class ..._Ap>
2486struct __strip_signature<_Rp (_Gp::*) (_Ap...) const noexcept> { using type = _Rp(_Ap...); };
2487template<class _Rp, class _Gp, class ..._Ap>
2488struct __strip_signature<_Rp (_Gp::*) (_Ap...) volatile noexcept> { using type = _Rp(_Ap...); };
2489template<class _Rp, class _Gp, class ..._Ap>
2490struct __strip_signature<_Rp (_Gp::*) (_Ap...) const volatile noexcept> { using type = _Rp(_Ap...); };
2491
2492template<class _Rp, class _Gp, class ..._Ap>
2493struct __strip_signature<_Rp (_Gp::*) (_Ap...) & noexcept> { using type = _Rp(_Ap...); };
2494template<class _Rp, class _Gp, class ..._Ap>
2495struct __strip_signature<_Rp (_Gp::*) (_Ap...) const & noexcept> { using type = _Rp(_Ap...); };
2496template<class _Rp, class _Gp, class ..._Ap>
2497struct __strip_signature<_Rp (_Gp::*) (_Ap...) volatile & noexcept> { using type = _Rp(_Ap...); };
2498template<class _Rp, class _Gp, class ..._Ap>
2499struct __strip_signature<_Rp (_Gp::*) (_Ap...) const volatile & noexcept> { using type = _Rp(_Ap...); };
2500
2501template<class _Fp, class _Stripped = typename __strip_signature<decltype(&_Fp::operator())>::type>
2502function(_Fp) -> function<_Stripped>;
2503#endif // !_LIBCPP_HAS_NO_DEDUCTION_GUIDES
2504
2505template<class _Rp, class ..._ArgTypes>
2506function<_Rp(_ArgTypes...)>::function(const function& __f) : __f_(__f.__f_) {}
2507
2508#if _LIBCPP_STD_VER <= 14
2509template<class _Rp, class ..._ArgTypes>
2510template <class _Alloc>
2511function<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc&,
2512                                     const function& __f) : __f_(__f.__f_) {}
2513#endif
2514
2515template <class _Rp, class... _ArgTypes>
2516function<_Rp(_ArgTypes...)>::function(function&& __f) _NOEXCEPT
2517    : __f_(_VSTD::move(__f.__f_)) {}
2518
2519#if _LIBCPP_STD_VER <= 14
2520template<class _Rp, class ..._ArgTypes>
2521template <class _Alloc>
2522function<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc&,
2523                                      function&& __f)
2524    : __f_(_VSTD::move(__f.__f_)) {}
2525#endif
2526
2527template <class _Rp, class... _ArgTypes>
2528template <class _Fp, class>
2529function<_Rp(_ArgTypes...)>::function(_Fp __f) : __f_(_VSTD::move(__f)) {}
2530
2531#if _LIBCPP_STD_VER <= 14
2532template <class _Rp, class... _ArgTypes>
2533template <class _Fp, class _Alloc, class>
2534function<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc& __a,
2535                                      _Fp __f)
2536    : __f_(_VSTD::move(__f), __a) {}
2537#endif
2538
2539template<class _Rp, class ..._ArgTypes>
2540function<_Rp(_ArgTypes...)>&
2541function<_Rp(_ArgTypes...)>::operator=(const function& __f)
2542{
2543    function(__f).swap(*this);
2544    return *this;
2545}
2546
2547template<class _Rp, class ..._ArgTypes>
2548function<_Rp(_ArgTypes...)>&
2549function<_Rp(_ArgTypes...)>::operator=(function&& __f) _NOEXCEPT
2550{
2551    __f_ = _VSTD::move(__f.__f_);
2552    return *this;
2553}
2554
2555template<class _Rp, class ..._ArgTypes>
2556function<_Rp(_ArgTypes...)>&
2557function<_Rp(_ArgTypes...)>::operator=(nullptr_t) _NOEXCEPT
2558{
2559    __f_ = nullptr;
2560    return *this;
2561}
2562
2563template<class _Rp, class ..._ArgTypes>
2564template <class _Fp, class>
2565function<_Rp(_ArgTypes...)>&
2566function<_Rp(_ArgTypes...)>::operator=(_Fp&& __f)
2567{
2568    function(_VSTD::forward<_Fp>(__f)).swap(*this);
2569    return *this;
2570}
2571
2572template<class _Rp, class ..._ArgTypes>
2573function<_Rp(_ArgTypes...)>::~function() {}
2574
2575template<class _Rp, class ..._ArgTypes>
2576void
2577function<_Rp(_ArgTypes...)>::swap(function& __f) _NOEXCEPT
2578{
2579    __f_.swap(__f.__f_);
2580}
2581
2582template<class _Rp, class ..._ArgTypes>
2583_Rp
2584function<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __arg) const
2585{
2586    return __f_(_VSTD::forward<_ArgTypes>(__arg)...);
2587}
2588
2589#ifndef _LIBCPP_NO_RTTI
2590
2591template<class _Rp, class ..._ArgTypes>
2592const std::type_info&
2593function<_Rp(_ArgTypes...)>::target_type() const _NOEXCEPT
2594{
2595    return __f_.target_type();
2596}
2597
2598template<class _Rp, class ..._ArgTypes>
2599template <typename _Tp>
2600_Tp*
2601function<_Rp(_ArgTypes...)>::target() _NOEXCEPT
2602{
2603    return (_Tp*)(__f_.template target<_Tp>());
2604}
2605
2606template<class _Rp, class ..._ArgTypes>
2607template <typename _Tp>
2608const _Tp*
2609function<_Rp(_ArgTypes...)>::target() const _NOEXCEPT
2610{
2611    return __f_.template target<_Tp>();
2612}
2613
2614#endif // _LIBCPP_NO_RTTI
2615
2616template <class _Rp, class... _ArgTypes>
2617inline _LIBCPP_INLINE_VISIBILITY
2618bool
2619operator==(const function<_Rp(_ArgTypes...)>& __f, nullptr_t) _NOEXCEPT {return !__f;}
2620
2621template <class _Rp, class... _ArgTypes>
2622inline _LIBCPP_INLINE_VISIBILITY
2623bool
2624operator==(nullptr_t, const function<_Rp(_ArgTypes...)>& __f) _NOEXCEPT {return !__f;}
2625
2626template <class _Rp, class... _ArgTypes>
2627inline _LIBCPP_INLINE_VISIBILITY
2628bool
2629operator!=(const function<_Rp(_ArgTypes...)>& __f, nullptr_t) _NOEXCEPT {return (bool)__f;}
2630
2631template <class _Rp, class... _ArgTypes>
2632inline _LIBCPP_INLINE_VISIBILITY
2633bool
2634operator!=(nullptr_t, const function<_Rp(_ArgTypes...)>& __f) _NOEXCEPT {return (bool)__f;}
2635
2636template <class _Rp, class... _ArgTypes>
2637inline _LIBCPP_INLINE_VISIBILITY
2638void
2639swap(function<_Rp(_ArgTypes...)>& __x, function<_Rp(_ArgTypes...)>& __y) _NOEXCEPT
2640{return __x.swap(__y);}
2641
2642#else // _LIBCPP_CXX03_LANG
2643
2644#include <__functional_03>
2645
2646#endif
2647
2648////////////////////////////////////////////////////////////////////////////////
2649//                                  BIND
2650//==============================================================================
2651
2652template<class _Tp> struct __is_bind_expression : public false_type {};
2653template<class _Tp> struct _LIBCPP_TEMPLATE_VIS is_bind_expression
2654    : public __is_bind_expression<typename remove_cv<_Tp>::type> {};
2655
2656#if _LIBCPP_STD_VER > 14
2657template <class _Tp>
2658_LIBCPP_INLINE_VAR constexpr size_t is_bind_expression_v = is_bind_expression<_Tp>::value;
2659#endif
2660
2661template<class _Tp> struct __is_placeholder : public integral_constant<int, 0> {};
2662template<class _Tp> struct _LIBCPP_TEMPLATE_VIS is_placeholder
2663    : public __is_placeholder<typename remove_cv<_Tp>::type> {};
2664
2665#if _LIBCPP_STD_VER > 14
2666template <class _Tp>
2667_LIBCPP_INLINE_VAR constexpr size_t is_placeholder_v = is_placeholder<_Tp>::value;
2668#endif
2669
2670namespace placeholders
2671{
2672
2673template <int _Np> struct __ph {};
2674
2675#if defined(_LIBCPP_CXX03_LANG) || defined(_LIBCPP_BUILDING_LIBRARY)
2676_LIBCPP_FUNC_VIS extern const __ph<1>   _1;
2677_LIBCPP_FUNC_VIS extern const __ph<2>   _2;
2678_LIBCPP_FUNC_VIS extern const __ph<3>   _3;
2679_LIBCPP_FUNC_VIS extern const __ph<4>   _4;
2680_LIBCPP_FUNC_VIS extern const __ph<5>   _5;
2681_LIBCPP_FUNC_VIS extern const __ph<6>   _6;
2682_LIBCPP_FUNC_VIS extern const __ph<7>   _7;
2683_LIBCPP_FUNC_VIS extern const __ph<8>   _8;
2684_LIBCPP_FUNC_VIS extern const __ph<9>   _9;
2685_LIBCPP_FUNC_VIS extern const __ph<10> _10;
2686#else
2687/* _LIBCPP_INLINE_VAR */ constexpr __ph<1>   _1{};
2688/* _LIBCPP_INLINE_VAR */ constexpr __ph<2>   _2{};
2689/* _LIBCPP_INLINE_VAR */ constexpr __ph<3>   _3{};
2690/* _LIBCPP_INLINE_VAR */ constexpr __ph<4>   _4{};
2691/* _LIBCPP_INLINE_VAR */ constexpr __ph<5>   _5{};
2692/* _LIBCPP_INLINE_VAR */ constexpr __ph<6>   _6{};
2693/* _LIBCPP_INLINE_VAR */ constexpr __ph<7>   _7{};
2694/* _LIBCPP_INLINE_VAR */ constexpr __ph<8>   _8{};
2695/* _LIBCPP_INLINE_VAR */ constexpr __ph<9>   _9{};
2696/* _LIBCPP_INLINE_VAR */ constexpr __ph<10> _10{};
2697#endif // defined(_LIBCPP_CXX03_LANG) || defined(_LIBCPP_BUILDING_LIBRARY)
2698
2699}  // placeholders
2700
2701template<int _Np>
2702struct __is_placeholder<placeholders::__ph<_Np> >
2703    : public integral_constant<int, _Np> {};
2704
2705
2706#ifndef _LIBCPP_CXX03_LANG
2707
2708template <class _Tp, class _Uj>
2709inline _LIBCPP_INLINE_VISIBILITY
2710_Tp&
2711__mu(reference_wrapper<_Tp> __t, _Uj&)
2712{
2713    return __t.get();
2714}
2715
2716template <class _Ti, class ..._Uj, size_t ..._Indx>
2717inline _LIBCPP_INLINE_VISIBILITY
2718typename __invoke_of<_Ti&, _Uj...>::type
2719__mu_expand(_Ti& __ti, tuple<_Uj...>& __uj, __tuple_indices<_Indx...>)
2720{
2721    return __ti(_VSTD::forward<_Uj>(_VSTD::get<_Indx>(__uj))...);
2722}
2723
2724template <class _Ti, class ..._Uj>
2725inline _LIBCPP_INLINE_VISIBILITY
2726typename _EnableIf
2727<
2728    is_bind_expression<_Ti>::value,
2729    __invoke_of<_Ti&, _Uj...>
2730>::type
2731__mu(_Ti& __ti, tuple<_Uj...>& __uj)
2732{
2733    typedef typename __make_tuple_indices<sizeof...(_Uj)>::type __indices;
2734    return _VSTD::__mu_expand(__ti, __uj, __indices());
2735}
2736
2737template <bool IsPh, class _Ti, class _Uj>
2738struct __mu_return2 {};
2739
2740template <class _Ti, class _Uj>
2741struct __mu_return2<true, _Ti, _Uj>
2742{
2743    typedef typename tuple_element<is_placeholder<_Ti>::value - 1, _Uj>::type type;
2744};
2745
2746template <class _Ti, class _Uj>
2747inline _LIBCPP_INLINE_VISIBILITY
2748typename enable_if
2749<
2750    0 < is_placeholder<_Ti>::value,
2751    typename __mu_return2<0 < is_placeholder<_Ti>::value, _Ti, _Uj>::type
2752>::type
2753__mu(_Ti&, _Uj& __uj)
2754{
2755    const size_t _Indx = is_placeholder<_Ti>::value - 1;
2756    return _VSTD::forward<typename tuple_element<_Indx, _Uj>::type>(_VSTD::get<_Indx>(__uj));
2757}
2758
2759template <class _Ti, class _Uj>
2760inline _LIBCPP_INLINE_VISIBILITY
2761typename enable_if
2762<
2763    !is_bind_expression<_Ti>::value &&
2764    is_placeholder<_Ti>::value == 0 &&
2765    !__is_reference_wrapper<_Ti>::value,
2766    _Ti&
2767>::type
2768__mu(_Ti& __ti, _Uj&)
2769{
2770    return __ti;
2771}
2772
2773template <class _Ti, bool IsReferenceWrapper, bool IsBindEx, bool IsPh,
2774          class _TupleUj>
2775struct __mu_return_impl;
2776
2777template <bool _Invokable, class _Ti, class ..._Uj>
2778struct __mu_return_invokable  // false
2779{
2780    typedef __nat type;
2781};
2782
2783template <class _Ti, class ..._Uj>
2784struct __mu_return_invokable<true, _Ti, _Uj...>
2785{
2786    typedef typename __invoke_of<_Ti&, _Uj...>::type type;
2787};
2788
2789template <class _Ti, class ..._Uj>
2790struct __mu_return_impl<_Ti, false, true, false, tuple<_Uj...> >
2791    : public __mu_return_invokable<__invokable<_Ti&, _Uj...>::value, _Ti, _Uj...>
2792{
2793};
2794
2795template <class _Ti, class _TupleUj>
2796struct __mu_return_impl<_Ti, false, false, true, _TupleUj>
2797{
2798    typedef typename tuple_element<is_placeholder<_Ti>::value - 1,
2799                                   _TupleUj>::type&& type;
2800};
2801
2802template <class _Ti, class _TupleUj>
2803struct __mu_return_impl<_Ti, true, false, false, _TupleUj>
2804{
2805    typedef typename _Ti::type& type;
2806};
2807
2808template <class _Ti, class _TupleUj>
2809struct __mu_return_impl<_Ti, false, false, false, _TupleUj>
2810{
2811    typedef _Ti& type;
2812};
2813
2814template <class _Ti, class _TupleUj>
2815struct __mu_return
2816    : public __mu_return_impl<_Ti,
2817                              __is_reference_wrapper<_Ti>::value,
2818                              is_bind_expression<_Ti>::value,
2819                              0 < is_placeholder<_Ti>::value &&
2820                              is_placeholder<_Ti>::value <= tuple_size<_TupleUj>::value,
2821                              _TupleUj>
2822{
2823};
2824
2825template <class _Fp, class _BoundArgs, class _TupleUj>
2826struct __is_valid_bind_return
2827{
2828    static const bool value = false;
2829};
2830
2831template <class _Fp, class ..._BoundArgs, class _TupleUj>
2832struct __is_valid_bind_return<_Fp, tuple<_BoundArgs...>, _TupleUj>
2833{
2834    static const bool value = __invokable<_Fp,
2835                    typename __mu_return<_BoundArgs, _TupleUj>::type...>::value;
2836};
2837
2838template <class _Fp, class ..._BoundArgs, class _TupleUj>
2839struct __is_valid_bind_return<_Fp, const tuple<_BoundArgs...>, _TupleUj>
2840{
2841    static const bool value = __invokable<_Fp,
2842                    typename __mu_return<const _BoundArgs, _TupleUj>::type...>::value;
2843};
2844
2845template <class _Fp, class _BoundArgs, class _TupleUj,
2846          bool = __is_valid_bind_return<_Fp, _BoundArgs, _TupleUj>::value>
2847struct __bind_return;
2848
2849template <class _Fp, class ..._BoundArgs, class _TupleUj>
2850struct __bind_return<_Fp, tuple<_BoundArgs...>, _TupleUj, true>
2851{
2852    typedef typename __invoke_of
2853    <
2854        _Fp&,
2855        typename __mu_return
2856        <
2857            _BoundArgs,
2858            _TupleUj
2859        >::type...
2860    >::type type;
2861};
2862
2863template <class _Fp, class ..._BoundArgs, class _TupleUj>
2864struct __bind_return<_Fp, const tuple<_BoundArgs...>, _TupleUj, true>
2865{
2866    typedef typename __invoke_of
2867    <
2868        _Fp&,
2869        typename __mu_return
2870        <
2871            const _BoundArgs,
2872            _TupleUj
2873        >::type...
2874    >::type type;
2875};
2876
2877template <class _Fp, class _BoundArgs, size_t ..._Indx, class _Args>
2878inline _LIBCPP_INLINE_VISIBILITY
2879typename __bind_return<_Fp, _BoundArgs, _Args>::type
2880__apply_functor(_Fp& __f, _BoundArgs& __bound_args, __tuple_indices<_Indx...>,
2881                _Args&& __args)
2882{
2883    return _VSTD::__invoke(__f, _VSTD::__mu(_VSTD::get<_Indx>(__bound_args), __args)...);
2884}
2885
2886template<class _Fp, class ..._BoundArgs>
2887class __bind
2888    : public __weak_result_type<typename decay<_Fp>::type>
2889{
2890protected:
2891    typedef typename decay<_Fp>::type _Fd;
2892    typedef tuple<typename decay<_BoundArgs>::type...> _Td;
2893private:
2894    _Fd __f_;
2895    _Td __bound_args_;
2896
2897    typedef typename __make_tuple_indices<sizeof...(_BoundArgs)>::type __indices;
2898public:
2899    template <class _Gp, class ..._BA,
2900              class = typename enable_if
2901                               <
2902                                  is_constructible<_Fd, _Gp>::value &&
2903                                  !is_same<typename remove_reference<_Gp>::type,
2904                                           __bind>::value
2905                               >::type>
2906      _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
2907      explicit __bind(_Gp&& __f, _BA&& ...__bound_args)
2908        : __f_(_VSTD::forward<_Gp>(__f)),
2909          __bound_args_(_VSTD::forward<_BA>(__bound_args)...) {}
2910
2911    template <class ..._Args>
2912        _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
2913        typename __bind_return<_Fd, _Td, tuple<_Args&&...> >::type
2914        operator()(_Args&& ...__args)
2915        {
2916            return _VSTD::__apply_functor(__f_, __bound_args_, __indices(),
2917                                  tuple<_Args&&...>(_VSTD::forward<_Args>(__args)...));
2918        }
2919
2920    template <class ..._Args>
2921        _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
2922        typename __bind_return<const _Fd, const _Td, tuple<_Args&&...> >::type
2923        operator()(_Args&& ...__args) const
2924        {
2925            return _VSTD::__apply_functor(__f_, __bound_args_, __indices(),
2926                                   tuple<_Args&&...>(_VSTD::forward<_Args>(__args)...));
2927        }
2928};
2929
2930template<class _Fp, class ..._BoundArgs>
2931struct __is_bind_expression<__bind<_Fp, _BoundArgs...> > : public true_type {};
2932
2933template<class _Rp, class _Fp, class ..._BoundArgs>
2934class __bind_r
2935    : public __bind<_Fp, _BoundArgs...>
2936{
2937    typedef __bind<_Fp, _BoundArgs...> base;
2938    typedef typename base::_Fd _Fd;
2939    typedef typename base::_Td _Td;
2940public:
2941    typedef _Rp result_type;
2942
2943
2944    template <class _Gp, class ..._BA,
2945              class = typename enable_if
2946                               <
2947                                  is_constructible<_Fd, _Gp>::value &&
2948                                  !is_same<typename remove_reference<_Gp>::type,
2949                                           __bind_r>::value
2950                               >::type>
2951      _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
2952      explicit __bind_r(_Gp&& __f, _BA&& ...__bound_args)
2953        : base(_VSTD::forward<_Gp>(__f),
2954               _VSTD::forward<_BA>(__bound_args)...) {}
2955
2956    template <class ..._Args>
2957        _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
2958        typename enable_if
2959        <
2960            is_convertible<typename __bind_return<_Fd, _Td, tuple<_Args&&...> >::type,
2961                           result_type>::value || is_void<_Rp>::value,
2962            result_type
2963        >::type
2964        operator()(_Args&& ...__args)
2965        {
2966            typedef __invoke_void_return_wrapper<_Rp> _Invoker;
2967            return _Invoker::__call(static_cast<base&>(*this), _VSTD::forward<_Args>(__args)...);
2968        }
2969
2970    template <class ..._Args>
2971        _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
2972        typename enable_if
2973        <
2974            is_convertible<typename __bind_return<const _Fd, const _Td, tuple<_Args&&...> >::type,
2975                           result_type>::value || is_void<_Rp>::value,
2976            result_type
2977        >::type
2978        operator()(_Args&& ...__args) const
2979        {
2980            typedef __invoke_void_return_wrapper<_Rp> _Invoker;
2981            return _Invoker::__call(static_cast<base const&>(*this), _VSTD::forward<_Args>(__args)...);
2982        }
2983};
2984
2985template<class _Rp, class _Fp, class ..._BoundArgs>
2986struct __is_bind_expression<__bind_r<_Rp, _Fp, _BoundArgs...> > : public true_type {};
2987
2988template<class _Fp, class ..._BoundArgs>
2989inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
2990__bind<_Fp, _BoundArgs...>
2991bind(_Fp&& __f, _BoundArgs&&... __bound_args)
2992{
2993    typedef __bind<_Fp, _BoundArgs...> type;
2994    return type(_VSTD::forward<_Fp>(__f), _VSTD::forward<_BoundArgs>(__bound_args)...);
2995}
2996
2997template<class _Rp, class _Fp, class ..._BoundArgs>
2998inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
2999__bind_r<_Rp, _Fp, _BoundArgs...>
3000bind(_Fp&& __f, _BoundArgs&&... __bound_args)
3001{
3002    typedef __bind_r<_Rp, _Fp, _BoundArgs...> type;
3003    return type(_VSTD::forward<_Fp>(__f), _VSTD::forward<_BoundArgs>(__bound_args)...);
3004}
3005
3006#endif // _LIBCPP_CXX03_LANG
3007
3008#if _LIBCPP_STD_VER > 14
3009
3010template<class _Op, class _Tuple,
3011         class _Idxs = typename __make_tuple_indices<tuple_size<_Tuple>::value>::type>
3012struct __perfect_forward_impl;
3013
3014template<class _Op, class... _Bound, size_t... _Idxs>
3015struct __perfect_forward_impl<_Op, __tuple_types<_Bound...>, __tuple_indices<_Idxs...>>
3016{
3017    tuple<_Bound...> __bound_;
3018
3019    template<class... _Args>
3020    _LIBCPP_INLINE_VISIBILITY constexpr auto operator()(_Args&&... __args) &
3021    noexcept(noexcept(_Op::__call(_VSTD::get<_Idxs>(__bound_)..., _VSTD::forward<_Args>(__args)...)))
3022    -> decltype(      _Op::__call(_VSTD::get<_Idxs>(__bound_)..., _VSTD::forward<_Args>(__args)...))
3023    {return           _Op::__call(_VSTD::get<_Idxs>(__bound_)..., _VSTD::forward<_Args>(__args)...);}
3024
3025    template<class... _Args>
3026    _LIBCPP_INLINE_VISIBILITY constexpr auto operator()(_Args&&... __args) const&
3027    noexcept(noexcept(_Op::__call(_VSTD::get<_Idxs>(__bound_)..., _VSTD::forward<_Args>(__args)...)))
3028    -> decltype(      _Op::__call(_VSTD::get<_Idxs>(__bound_)..., _VSTD::forward<_Args>(__args)...))
3029    {return           _Op::__call(_VSTD::get<_Idxs>(__bound_)..., _VSTD::forward<_Args>(__args)...);}
3030
3031    template<class... _Args>
3032    _LIBCPP_INLINE_VISIBILITY constexpr auto operator()(_Args&&... __args) &&
3033    noexcept(noexcept(_Op::__call(_VSTD::get<_Idxs>(_VSTD::move(__bound_))...,
3034                                  _VSTD::forward<_Args>(__args)...)))
3035    -> decltype(      _Op::__call(_VSTD::get<_Idxs>(_VSTD::move(__bound_))...,
3036                                  _VSTD::forward<_Args>(__args)...))
3037    {return           _Op::__call(_VSTD::get<_Idxs>(_VSTD::move(__bound_))...,
3038                                  _VSTD::forward<_Args>(__args)...);}
3039
3040    template<class... _Args>
3041    _LIBCPP_INLINE_VISIBILITY constexpr auto operator()(_Args&&... __args) const&&
3042    noexcept(noexcept(_Op::__call(_VSTD::get<_Idxs>(_VSTD::move(__bound_))...,
3043                                  _VSTD::forward<_Args>(__args)...)))
3044    -> decltype(      _Op::__call(_VSTD::get<_Idxs>(_VSTD::move(__bound_))...,
3045                                  _VSTD::forward<_Args>(__args)...))
3046    {return           _Op::__call(_VSTD::get<_Idxs>(_VSTD::move(__bound_))...,
3047                                  _VSTD::forward<_Args>(__args)...);}
3048
3049    template<class _Fn = typename tuple_element<0, tuple<_Bound...>>::type,
3050             class = _EnableIf<is_copy_constructible_v<_Fn>>>
3051    constexpr __perfect_forward_impl(__perfect_forward_impl const& __other)
3052        : __bound_(__other.__bound_) {}
3053
3054    template<class _Fn = typename tuple_element<0, tuple<_Bound...>>::type,
3055             class = _EnableIf<is_move_constructible_v<_Fn>>>
3056    constexpr __perfect_forward_impl(__perfect_forward_impl && __other)
3057        : __bound_(_VSTD::move(__other.__bound_)) {}
3058
3059    template<class... _BoundArgs>
3060    explicit constexpr __perfect_forward_impl(_BoundArgs&&... __bound) :
3061        __bound_(_VSTD::forward<_BoundArgs>(__bound)...) { }
3062};
3063
3064template<class _Op, class... _Args>
3065using __perfect_forward =
3066    __perfect_forward_impl<_Op, __tuple_types<decay_t<_Args>...>>;
3067
3068struct __not_fn_op
3069{
3070    template<class... _Args>
3071    static _LIBCPP_CONSTEXPR_AFTER_CXX17 auto __call(_Args&&... __args)
3072    noexcept(noexcept(!_VSTD::invoke(_VSTD::forward<_Args>(__args)...)))
3073    -> decltype(      !_VSTD::invoke(_VSTD::forward<_Args>(__args)...))
3074    { return          !_VSTD::invoke(_VSTD::forward<_Args>(__args)...); }
3075};
3076
3077template<class _Fn,
3078         class = _EnableIf<is_constructible_v<decay_t<_Fn>, _Fn> &&
3079                           is_move_constructible_v<_Fn>>>
3080_LIBCPP_CONSTEXPR_AFTER_CXX17 auto not_fn(_Fn&& __f)
3081{
3082    return __perfect_forward<__not_fn_op, _Fn>(_VSTD::forward<_Fn>(__f));
3083}
3084
3085#endif // _LIBCPP_STD_VER > 14
3086
3087#if _LIBCPP_STD_VER > 17
3088
3089struct __bind_front_op
3090{
3091    template<class... _Args>
3092    constexpr static auto __call(_Args&&... __args)
3093    noexcept(noexcept(_VSTD::invoke(_VSTD::forward<_Args>(__args)...)))
3094    -> decltype(      _VSTD::invoke(_VSTD::forward<_Args>(__args)...))
3095    { return          _VSTD::invoke(_VSTD::forward<_Args>(__args)...); }
3096};
3097
3098template<class _Fn, class... _Args,
3099         class = _EnableIf<conjunction<is_constructible<decay_t<_Fn>, _Fn>,
3100                                       is_move_constructible<decay_t<_Fn>>,
3101                                       is_constructible<decay_t<_Args>, _Args>...,
3102                                       is_move_constructible<decay_t<_Args>>...
3103                                       >::value>>
3104constexpr auto bind_front(_Fn&& __f, _Args&&... __args)
3105{
3106    return __perfect_forward<__bind_front_op, _Fn, _Args...>(_VSTD::forward<_Fn>(__f),
3107                                                             _VSTD::forward<_Args>(__args)...);
3108}
3109
3110#endif // _LIBCPP_STD_VER > 17
3111
3112// struct hash<T*> in <memory>
3113
3114#if _LIBCPP_STD_VER > 14
3115
3116// default searcher
3117template<class _ForwardIterator, class _BinaryPredicate = equal_to<>>
3118class _LIBCPP_TEMPLATE_VIS default_searcher {
3119public:
3120    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
3121    default_searcher(_ForwardIterator __f, _ForwardIterator __l,
3122                       _BinaryPredicate __p = _BinaryPredicate())
3123        : __first_(__f), __last_(__l), __pred_(__p) {}
3124
3125    template <typename _ForwardIterator2>
3126    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
3127    pair<_ForwardIterator2, _ForwardIterator2>
3128    operator () (_ForwardIterator2 __f, _ForwardIterator2 __l) const
3129    {
3130        return _VSTD::__search(__f, __l, __first_, __last_, __pred_,
3131            typename iterator_traits<_ForwardIterator>::iterator_category(),
3132            typename iterator_traits<_ForwardIterator2>::iterator_category());
3133    }
3134
3135private:
3136    _ForwardIterator __first_;
3137    _ForwardIterator __last_;
3138    _BinaryPredicate __pred_;
3139    };
3140
3141#endif // _LIBCPP_STD_VER > 14
3142
3143#if _LIBCPP_STD_VER > 17
3144template <class _Tp>
3145using unwrap_reference_t = typename unwrap_reference<_Tp>::type;
3146
3147template <class _Tp>
3148using unwrap_ref_decay_t = typename unwrap_ref_decay<_Tp>::type;
3149#endif // > C++17
3150
3151#if _LIBCPP_STD_VER > 17
3152// [func.identity]
3153struct identity {
3154    template<class _Tp>
3155    _LIBCPP_NODISCARD_EXT constexpr _Tp&& operator()(_Tp&& __t) const noexcept
3156    {
3157        return _VSTD::forward<_Tp>(__t);
3158    }
3159
3160    using is_transparent = void;
3161};
3162#endif // _LIBCPP_STD_VER > 17
3163
3164#if !defined(_LIBCPP_HAS_NO_RANGES)
3165
3166namespace ranges {
3167
3168struct equal_to {
3169  template <class _Tp, class _Up>
3170  requires equality_comparable_with<_Tp, _Up>
3171  [[nodiscard]] constexpr bool operator()(_Tp &&__t, _Up &&__u) const
3172      noexcept(noexcept(bool(_VSTD::forward<_Tp>(__t) == _VSTD::forward<_Up>(__u)))) {
3173    return _VSTD::forward<_Tp>(__t) == _VSTD::forward<_Up>(__u);
3174  }
3175
3176  using is_transparent = void;
3177};
3178
3179struct not_equal_to {
3180  template <class _Tp, class _Up>
3181  requires equality_comparable_with<_Tp, _Up>
3182  [[nodiscard]] constexpr bool operator()(_Tp &&__t, _Up &&__u) const
3183      noexcept(noexcept(bool(!(_VSTD::forward<_Tp>(__t) == _VSTD::forward<_Up>(__u))))) {
3184    return !(_VSTD::forward<_Tp>(__t) == _VSTD::forward<_Up>(__u));
3185  }
3186
3187  using is_transparent = void;
3188};
3189
3190struct greater {
3191  template <class _Tp, class _Up>
3192  requires totally_ordered_with<_Tp, _Up>
3193  [[nodiscard]] constexpr bool operator()(_Tp &&__t, _Up &&__u) const
3194      noexcept(noexcept(bool(_VSTD::forward<_Up>(__u) < _VSTD::forward<_Tp>(__t)))) {
3195    return _VSTD::forward<_Up>(__u) < _VSTD::forward<_Tp>(__t);
3196  }
3197
3198  using is_transparent = void;
3199};
3200
3201struct less {
3202  template <class _Tp, class _Up>
3203  requires totally_ordered_with<_Tp, _Up>
3204  [[nodiscard]] constexpr bool operator()(_Tp &&__t, _Up &&__u) const
3205      noexcept(noexcept(bool(_VSTD::forward<_Tp>(__t) < _VSTD::forward<_Up>(__u)))) {
3206    return _VSTD::forward<_Tp>(__t) < _VSTD::forward<_Up>(__u);
3207  }
3208
3209  using is_transparent = void;
3210};
3211
3212struct greater_equal {
3213  template <class _Tp, class _Up>
3214  requires totally_ordered_with<_Tp, _Up>
3215  [[nodiscard]] constexpr bool operator()(_Tp &&__t, _Up &&__u) const
3216      noexcept(noexcept(bool(!(_VSTD::forward<_Tp>(__t) < _VSTD::forward<_Up>(__u))))) {
3217    return !(_VSTD::forward<_Tp>(__t) < _VSTD::forward<_Up>(__u));
3218  }
3219
3220  using is_transparent = void;
3221};
3222
3223struct less_equal {
3224  template <class _Tp, class _Up>
3225  requires totally_ordered_with<_Tp, _Up>
3226  [[nodiscard]] constexpr bool operator()(_Tp &&__t, _Up &&__u) const
3227      noexcept(noexcept(bool(!(_VSTD::forward<_Up>(__u) < _VSTD::forward<_Tp>(__t))))) {
3228    return !(_VSTD::forward<_Up>(__u) < _VSTD::forward<_Tp>(__t));
3229  }
3230
3231  using is_transparent = void;
3232};
3233
3234} // namespace ranges
3235
3236#endif // !defined(_LIBCPP_HAS_NO_RANGES)
3237
3238_LIBCPP_END_NAMESPACE_STD
3239
3240#endif // _LIBCPP_FUNCTIONAL
3241