1// -*- C++ -*-
2//===------------------------ functional ----------------------------------===//
3//
4//                     The LLVM Compiler Infrastructure
5//
6// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
8//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_FUNCTIONAL
12#define _LIBCPP_FUNCTIONAL
13
14/*
15    functional synopsis
16
17namespace std
18{
19
20template <class Arg, class Result>
21struct unary_function
22{
23    typedef Arg    argument_type;
24    typedef Result result_type;
25};
26
27template <class Arg1, class Arg2, class Result>
28struct binary_function
29{
30    typedef Arg1   first_argument_type;
31    typedef Arg2   second_argument_type;
32    typedef Result result_type;
33};
34
35template <class T>
36class reference_wrapper
37    : public unary_function<T1, R> // if wrapping a unary functor
38    : public binary_function<T1, T2, R> // if wraping a binary functor
39{
40public:
41    // types
42    typedef T type;
43    typedef see below result_type; // Not always defined
44
45    // construct/copy/destroy
46    reference_wrapper(T&) noexcept;
47    reference_wrapper(T&&) = delete; // do not bind to temps
48    reference_wrapper(const reference_wrapper<T>& x) noexcept;
49
50    // assignment
51    reference_wrapper& operator=(const reference_wrapper<T>& x) noexcept;
52
53    // access
54    operator T& () const noexcept;
55    T& get() const noexcept;
56
57    // invoke
58    template <class... ArgTypes>
59      typename result_of<T&(ArgTypes&&...)>::type
60          operator() (ArgTypes&&...) const;
61};
62
63template <class T> reference_wrapper<T> ref(T& t) noexcept;
64template <class T> void ref(const T&& t) = delete;
65template <class T> reference_wrapper<T> ref(reference_wrapper<T>t) noexcept;
66
67template <class T> reference_wrapper<const T> cref(const T& t) noexcept;
68template <class T> void cref(const T&& t) = delete;
69template <class T> reference_wrapper<const T> cref(reference_wrapper<T> t) noexcept;
70
71template <class T> struct unwrap_reference;                                       // since C++20
72template <class T> struct unwrap_ref_decay : unwrap_reference<decay_t<T>> { };    // since C++20
73template <class T> using unwrap_reference_t = typename unwrap_reference<T>::type; // since C++20
74template <class T> using unwrap_ref_decay_t = typename unwrap_ref_decay<T>::type; // since C++20
75
76template <class T> // <class T=void> in C++14
77struct plus : binary_function<T, T, T>
78{
79    T operator()(const T& x, const T& y) const;
80};
81
82template <class T> // <class T=void> in C++14
83struct minus : binary_function<T, T, T>
84{
85    T operator()(const T& x, const T& y) const;
86};
87
88template <class T> // <class T=void> in C++14
89struct multiplies : binary_function<T, T, T>
90{
91    T operator()(const T& x, const T& y) const;
92};
93
94template <class T> // <class T=void> in C++14
95struct divides : binary_function<T, T, T>
96{
97    T operator()(const T& x, const T& y) const;
98};
99
100template <class T> // <class T=void> in C++14
101struct modulus : binary_function<T, T, T>
102{
103    T operator()(const T& x, const T& y) const;
104};
105
106template <class T> // <class T=void> in C++14
107struct negate : unary_function<T, T>
108{
109    T operator()(const T& x) const;
110};
111
112template <class T> // <class T=void> in C++14
113struct equal_to : binary_function<T, T, bool>
114{
115    bool operator()(const T& x, const T& y) const;
116};
117
118template <class T> // <class T=void> in C++14
119struct not_equal_to : binary_function<T, T, bool>
120{
121    bool operator()(const T& x, const T& y) const;
122};
123
124template <class T> // <class T=void> in C++14
125struct greater : binary_function<T, T, bool>
126{
127    bool operator()(const T& x, const T& y) const;
128};
129
130template <class T> // <class T=void> in C++14
131struct less : binary_function<T, T, bool>
132{
133    bool operator()(const T& x, const T& y) const;
134};
135
136template <class T> // <class T=void> in C++14
137struct greater_equal : binary_function<T, T, bool>
138{
139    bool operator()(const T& x, const T& y) const;
140};
141
142template <class T> // <class T=void> in C++14
143struct less_equal : binary_function<T, T, bool>
144{
145    bool operator()(const T& x, const T& y) const;
146};
147
148template <class T> // <class T=void> in C++14
149struct logical_and : binary_function<T, T, bool>
150{
151    bool operator()(const T& x, const T& y) const;
152};
153
154template <class T> // <class T=void> in C++14
155struct logical_or : binary_function<T, T, bool>
156{
157    bool operator()(const T& x, const T& y) const;
158};
159
160template <class T> // <class T=void> in C++14
161struct logical_not : unary_function<T, bool>
162{
163    bool operator()(const T& x) const;
164};
165
166template <class T> // <class T=void> in C++14
167struct bit_and : unary_function<T, bool>
168{
169    bool operator()(const T& x, const T& y) const;
170};
171
172template <class T> // <class T=void> in C++14
173struct bit_or : unary_function<T, bool>
174{
175    bool operator()(const T& x, const T& y) const;
176};
177
178template <class T> // <class T=void> in C++14
179struct bit_xor : unary_function<T, bool>
180{
181    bool operator()(const T& x, const T& y) const;
182};
183
184template <class T=void> // C++14
185struct bit_xor : unary_function<T, bool>
186{
187    bool operator()(const T& x) const;
188};
189
190template <class Predicate>
191class unary_negate // deprecated in C++17
192    : public unary_function<typename Predicate::argument_type, bool>
193{
194public:
195    explicit unary_negate(const Predicate& pred);
196    bool operator()(const typename Predicate::argument_type& x) const;
197};
198
199template <class Predicate> // deprecated in C++17
200unary_negate<Predicate> not1(const Predicate& pred);
201
202template <class Predicate>
203class binary_negate // deprecated in C++17
204    : public binary_function<typename Predicate::first_argument_type,
205                             typename Predicate::second_argument_type,
206                             bool>
207{
208public:
209    explicit binary_negate(const Predicate& pred);
210    bool operator()(const typename Predicate::first_argument_type& x,
211                    const typename Predicate::second_argument_type& y) const;
212};
213
214template <class Predicate> // deprecated in C++17
215binary_negate<Predicate> not2(const Predicate& pred);
216
217template <class F> unspecified not_fn(F&& f); // C++17
218
219template<class T> struct is_bind_expression;
220template<class T> struct is_placeholder;
221
222    // See C++14 20.9.9, Function object binders
223template <class T> inline constexpr bool is_bind_expression_v
224  = is_bind_expression<T>::value; // C++17
225template <class T> inline constexpr int is_placeholder_v
226  = is_placeholder<T>::value; // C++17
227
228
229template<class Fn, class... BoundArgs>
230  unspecified bind(Fn&&, BoundArgs&&...);
231template<class R, class Fn, class... BoundArgs>
232  unspecified bind(Fn&&, BoundArgs&&...);
233
234namespace placeholders {
235  // M is the implementation-defined number of placeholders
236  extern unspecified _1;
237  extern unspecified _2;
238  .
239  .
240  .
241  extern unspecified _Mp;
242}
243
244template <class Operation>
245class binder1st     // deprecated in C++11, removed in C++17
246    : public unary_function<typename Operation::second_argument_type,
247                            typename Operation::result_type>
248{
249protected:
250    Operation                               op;
251    typename Operation::first_argument_type value;
252public:
253    binder1st(const Operation& x, const typename Operation::first_argument_type y);
254    typename Operation::result_type operator()(      typename Operation::second_argument_type& x) const;
255    typename Operation::result_type operator()(const typename Operation::second_argument_type& x) const;
256};
257
258template <class Operation, class T>
259binder1st<Operation> bind1st(const Operation& op, const T& x);  // deprecated in C++11, removed in C++17
260
261template <class Operation>
262class binder2nd     // deprecated in C++11, removed in C++17
263    : public unary_function<typename Operation::first_argument_type,
264                            typename Operation::result_type>
265{
266protected:
267    Operation                                op;
268    typename Operation::second_argument_type value;
269public:
270    binder2nd(const Operation& x, const typename Operation::second_argument_type y);
271    typename Operation::result_type operator()(      typename Operation::first_argument_type& x) const;
272    typename Operation::result_type operator()(const typename Operation::first_argument_type& x) const;
273};
274
275template <class Operation, class T>
276binder2nd<Operation> bind2nd(const Operation& op, const T& x);  // deprecated in C++11, removed in C++17
277
278template <class Arg, class Result>      // deprecated in C++11, removed in C++17
279class pointer_to_unary_function : public unary_function<Arg, Result>
280{
281public:
282    explicit pointer_to_unary_function(Result (*f)(Arg));
283    Result operator()(Arg x) const;
284};
285
286template <class Arg, class Result>
287pointer_to_unary_function<Arg,Result> ptr_fun(Result (*f)(Arg));      // deprecated in C++11, removed in C++17
288
289template <class Arg1, class Arg2, class Result>      // deprecated in C++11, removed in C++17
290class pointer_to_binary_function : public binary_function<Arg1, Arg2, Result>
291{
292public:
293    explicit pointer_to_binary_function(Result (*f)(Arg1, Arg2));
294    Result operator()(Arg1 x, Arg2 y) const;
295};
296
297template <class Arg1, class Arg2, class Result>
298pointer_to_binary_function<Arg1,Arg2,Result> ptr_fun(Result (*f)(Arg1,Arg2));      // deprecated in C++11, removed in C++17
299
300template<class S, class T>      // deprecated in C++11, removed in C++17
301class mem_fun_t : public unary_function<T*, S>
302{
303public:
304    explicit mem_fun_t(S (T::*p)());
305    S operator()(T* p) const;
306};
307
308template<class S, class T, class A>
309class mem_fun1_t : public binary_function<T*, A, S>      // deprecated in C++11, removed in C++17
310{
311public:
312    explicit mem_fun1_t(S (T::*p)(A));
313    S operator()(T* p, A x) const;
314};
315
316template<class S, class T>          mem_fun_t<S,T>    mem_fun(S (T::*f)());      // deprecated in C++11, removed in C++17
317template<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
318
319template<class S, class T>
320class mem_fun_ref_t : public unary_function<T, S>      // deprecated in C++11, removed in C++17
321{
322public:
323    explicit mem_fun_ref_t(S (T::*p)());
324    S operator()(T& p) const;
325};
326
327template<class S, class T, class A>
328class mem_fun1_ref_t : public binary_function<T, A, S>      // deprecated in C++11, removed in C++17
329{
330public:
331    explicit mem_fun1_ref_t(S (T::*p)(A));
332    S operator()(T& p, A x) const;
333};
334
335template<class S, class T>          mem_fun_ref_t<S,T>    mem_fun_ref(S (T::*f)());      // deprecated in C++11, removed in C++17
336template<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
337
338template <class S, class T>
339class const_mem_fun_t : public unary_function<const T*, S>      // deprecated in C++11, removed in C++17
340{
341public:
342    explicit const_mem_fun_t(S (T::*p)() const);
343    S operator()(const T* p) const;
344};
345
346template <class S, class T, class A>
347class const_mem_fun1_t : public binary_function<const T*, A, S>      // deprecated in C++11, removed in C++17
348{
349public:
350    explicit const_mem_fun1_t(S (T::*p)(A) const);
351    S operator()(const T* p, A x) const;
352};
353
354template <class S, class T>          const_mem_fun_t<S,T>    mem_fun(S (T::*f)() const);      // deprecated in C++11, removed in C++17
355template <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
356
357template <class S, class T>
358class const_mem_fun_ref_t : public unary_function<T, S>      // deprecated in C++11, removed in C++17
359{
360public:
361    explicit const_mem_fun_ref_t(S (T::*p)() const);
362    S operator()(const T& p) const;
363};
364
365template <class S, class T, class A>
366class const_mem_fun1_ref_t : public binary_function<T, A, S>      // deprecated in C++11, removed in C++17
367{
368public:
369    explicit const_mem_fun1_ref_t(S (T::*p)(A) const);
370    S operator()(const T& p, A x) const;
371};
372
373template <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
374template <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
375
376template<class R, class T> unspecified mem_fn(R T::*);
377
378class bad_function_call
379    : public exception
380{
381};
382
383template<class> class function; // undefined
384
385template<class R, class... ArgTypes>
386class function<R(ArgTypes...)>
387  : public unary_function<T1, R>      // iff sizeof...(ArgTypes) == 1 and
388                                      // ArgTypes contains T1
389  : public binary_function<T1, T2, R> // iff sizeof...(ArgTypes) == 2 and
390                                      // ArgTypes contains T1 and T2
391{
392public:
393    typedef R result_type;
394
395    // construct/copy/destroy:
396    function() noexcept;
397    function(nullptr_t) noexcept;
398    function(const function&);
399    function(function&&) noexcept;
400    template<class F>
401      function(F);
402    template<Allocator Alloc>
403      function(allocator_arg_t, const Alloc&) noexcept;            // removed in C++17
404    template<Allocator Alloc>
405      function(allocator_arg_t, const Alloc&, nullptr_t) noexcept; // removed in C++17
406    template<Allocator Alloc>
407      function(allocator_arg_t, const Alloc&, const function&);    // removed in C++17
408    template<Allocator Alloc>
409      function(allocator_arg_t, const Alloc&, function&&);         // removed in C++17
410    template<class F, Allocator Alloc>
411      function(allocator_arg_t, const Alloc&, F);                  // removed in C++17
412
413    function& operator=(const function&);
414    function& operator=(function&&) noexcept;
415    function& operator=(nullptr_t) noexcept;
416    template<class F>
417      function& operator=(F&&);
418    template<class F>
419      function& operator=(reference_wrapper<F>) noexcept;
420
421    ~function();
422
423    // function modifiers:
424    void swap(function&) noexcept;
425    template<class F, class Alloc>
426      void assign(F&&, const Alloc&);                 // Removed in C++17
427
428    // function capacity:
429    explicit operator bool() const noexcept;
430
431    // function invocation:
432    R operator()(ArgTypes...) const;
433
434    // function target access:
435    const std::type_info& target_type() const noexcept;
436    template <typename T>       T* target() noexcept;
437    template <typename T> const T* target() const noexcept;
438};
439
440// Null pointer comparisons:
441template <class R, class ... ArgTypes>
442  bool operator==(const function<R(ArgTypes...)>&, nullptr_t) noexcept;
443
444template <class R, class ... ArgTypes>
445  bool operator==(nullptr_t, const function<R(ArgTypes...)>&) noexcept;
446
447template <class R, class ... ArgTypes>
448  bool operator!=(const function<R(ArgTypes...)>&, nullptr_t) noexcept;
449
450template <class  R, class ... ArgTypes>
451  bool operator!=(nullptr_t, const function<R(ArgTypes...)>&) noexcept;
452
453// specialized algorithms:
454template <class  R, class ... ArgTypes>
455  void swap(function<R(ArgTypes...)>&, function<R(ArgTypes...)>&) noexcept;
456
457template <class T> struct hash;
458
459template <> struct hash<bool>;
460template <> struct hash<char>;
461template <> struct hash<signed char>;
462template <> struct hash<unsigned char>;
463template <> struct hash<char16_t>;
464template <> struct hash<char32_t>;
465template <> struct hash<wchar_t>;
466template <> struct hash<short>;
467template <> struct hash<unsigned short>;
468template <> struct hash<int>;
469template <> struct hash<unsigned int>;
470template <> struct hash<long>;
471template <> struct hash<long long>;
472template <> struct hash<unsigned long>;
473template <> struct hash<unsigned long long>;
474
475template <> struct hash<float>;
476template <> struct hash<double>;
477template <> struct hash<long double>;
478
479template<class T> struct hash<T*>;
480template <> struct hash<nullptr_t>;  // C++17
481
482}  // std
483
484POLICY:  For non-variadic implementations, the number of arguments is limited
485         to 3.  It is hoped that the need for non-variadic implementations
486         will be minimal.
487
488*/
489
490#include <__config>
491#include <type_traits>
492#include <typeinfo>
493#include <exception>
494#include <memory>
495#include <tuple>
496#include <utility>
497#include <version>
498
499#include <__functional_base>
500
501#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
502#pragma GCC system_header
503#endif
504
505_LIBCPP_BEGIN_NAMESPACE_STD
506
507#if _LIBCPP_STD_VER > 11
508template <class _Tp = void>
509#else
510template <class _Tp>
511#endif
512struct _LIBCPP_TEMPLATE_VIS plus : binary_function<_Tp, _Tp, _Tp>
513{
514    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
515    _Tp operator()(const _Tp& __x, const _Tp& __y) const
516        {return __x + __y;}
517};
518
519#if _LIBCPP_STD_VER > 11
520template <>
521struct _LIBCPP_TEMPLATE_VIS plus<void>
522{
523    template <class _T1, class _T2>
524    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
525    auto operator()(_T1&& __t, _T2&& __u) const
526    _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u)))
527    -> decltype        (_VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u))
528        { return        _VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u); }
529    typedef void is_transparent;
530};
531#endif
532
533
534#if _LIBCPP_STD_VER > 11
535template <class _Tp = void>
536#else
537template <class _Tp>
538#endif
539struct _LIBCPP_TEMPLATE_VIS minus : binary_function<_Tp, _Tp, _Tp>
540{
541    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
542    _Tp operator()(const _Tp& __x, const _Tp& __y) const
543        {return __x - __y;}
544};
545
546#if _LIBCPP_STD_VER > 11
547template <>
548struct _LIBCPP_TEMPLATE_VIS minus<void>
549{
550    template <class _T1, class _T2>
551    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
552    auto operator()(_T1&& __t, _T2&& __u) const
553    _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) - _VSTD::forward<_T2>(__u)))
554    -> decltype        (_VSTD::forward<_T1>(__t) - _VSTD::forward<_T2>(__u))
555        { return        _VSTD::forward<_T1>(__t) - _VSTD::forward<_T2>(__u); }
556    typedef void is_transparent;
557};
558#endif
559
560
561#if _LIBCPP_STD_VER > 11
562template <class _Tp = void>
563#else
564template <class _Tp>
565#endif
566struct _LIBCPP_TEMPLATE_VIS multiplies : binary_function<_Tp, _Tp, _Tp>
567{
568    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
569    _Tp operator()(const _Tp& __x, const _Tp& __y) const
570        {return __x * __y;}
571};
572
573#if _LIBCPP_STD_VER > 11
574template <>
575struct _LIBCPP_TEMPLATE_VIS multiplies<void>
576{
577    template <class _T1, class _T2>
578    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
579    auto operator()(_T1&& __t, _T2&& __u) const
580    _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) * _VSTD::forward<_T2>(__u)))
581    -> decltype        (_VSTD::forward<_T1>(__t) * _VSTD::forward<_T2>(__u))
582        { return        _VSTD::forward<_T1>(__t) * _VSTD::forward<_T2>(__u); }
583    typedef void is_transparent;
584};
585#endif
586
587
588#if _LIBCPP_STD_VER > 11
589template <class _Tp = void>
590#else
591template <class _Tp>
592#endif
593struct _LIBCPP_TEMPLATE_VIS divides : binary_function<_Tp, _Tp, _Tp>
594{
595    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
596    _Tp operator()(const _Tp& __x, const _Tp& __y) const
597        {return __x / __y;}
598};
599
600#if _LIBCPP_STD_VER > 11
601template <>
602struct _LIBCPP_TEMPLATE_VIS divides<void>
603{
604    template <class _T1, class _T2>
605    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
606    auto operator()(_T1&& __t, _T2&& __u) const
607    _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) / _VSTD::forward<_T2>(__u)))
608    -> decltype        (_VSTD::forward<_T1>(__t) / _VSTD::forward<_T2>(__u))
609        { return        _VSTD::forward<_T1>(__t) / _VSTD::forward<_T2>(__u); }
610    typedef void is_transparent;
611};
612#endif
613
614
615#if _LIBCPP_STD_VER > 11
616template <class _Tp = void>
617#else
618template <class _Tp>
619#endif
620struct _LIBCPP_TEMPLATE_VIS modulus : binary_function<_Tp, _Tp, _Tp>
621{
622    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
623    _Tp operator()(const _Tp& __x, const _Tp& __y) const
624        {return __x % __y;}
625};
626
627#if _LIBCPP_STD_VER > 11
628template <>
629struct _LIBCPP_TEMPLATE_VIS modulus<void>
630{
631    template <class _T1, class _T2>
632    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
633    auto operator()(_T1&& __t, _T2&& __u) const
634    _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) % _VSTD::forward<_T2>(__u)))
635    -> decltype        (_VSTD::forward<_T1>(__t) % _VSTD::forward<_T2>(__u))
636        { return        _VSTD::forward<_T1>(__t) % _VSTD::forward<_T2>(__u); }
637    typedef void is_transparent;
638};
639#endif
640
641
642#if _LIBCPP_STD_VER > 11
643template <class _Tp = void>
644#else
645template <class _Tp>
646#endif
647struct _LIBCPP_TEMPLATE_VIS negate : unary_function<_Tp, _Tp>
648{
649    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
650    _Tp operator()(const _Tp& __x) const
651        {return -__x;}
652};
653
654#if _LIBCPP_STD_VER > 11
655template <>
656struct _LIBCPP_TEMPLATE_VIS negate<void>
657{
658    template <class _Tp>
659    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
660    auto operator()(_Tp&& __x) const
661    _NOEXCEPT_(noexcept(- _VSTD::forward<_Tp>(__x)))
662    -> decltype        (- _VSTD::forward<_Tp>(__x))
663        { return        - _VSTD::forward<_Tp>(__x); }
664    typedef void is_transparent;
665};
666#endif
667
668
669#if _LIBCPP_STD_VER > 11
670template <class _Tp = void>
671#else
672template <class _Tp>
673#endif
674struct _LIBCPP_TEMPLATE_VIS equal_to : binary_function<_Tp, _Tp, bool>
675{
676    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
677    bool operator()(const _Tp& __x, const _Tp& __y) const
678        {return __x == __y;}
679};
680
681#if _LIBCPP_STD_VER > 11
682template <>
683struct _LIBCPP_TEMPLATE_VIS equal_to<void>
684{
685    template <class _T1, class _T2>
686    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
687    auto operator()(_T1&& __t, _T2&& __u) const
688    _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) == _VSTD::forward<_T2>(__u)))
689    -> decltype        (_VSTD::forward<_T1>(__t) == _VSTD::forward<_T2>(__u))
690        { return        _VSTD::forward<_T1>(__t) == _VSTD::forward<_T2>(__u); }
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 not_equal_to : binary_function<_Tp, _Tp, bool>
702{
703    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
704    bool operator()(const _Tp& __x, const _Tp& __y) const
705        {return __x != __y;}
706};
707
708#if _LIBCPP_STD_VER > 11
709template <>
710struct _LIBCPP_TEMPLATE_VIS not_equal_to<void>
711{
712    template <class _T1, class _T2>
713    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
714    auto operator()(_T1&& __t, _T2&& __u) const
715    _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) != _VSTD::forward<_T2>(__u)))
716    -> decltype        (_VSTD::forward<_T1>(__t) != _VSTD::forward<_T2>(__u))
717        { return        _VSTD::forward<_T1>(__t) != _VSTD::forward<_T2>(__u); }
718    typedef void is_transparent;
719};
720#endif
721
722
723#if _LIBCPP_STD_VER > 11
724template <class _Tp = void>
725#else
726template <class _Tp>
727#endif
728struct _LIBCPP_TEMPLATE_VIS greater : binary_function<_Tp, _Tp, bool>
729{
730    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
731    bool operator()(const _Tp& __x, const _Tp& __y) const
732        {return __x > __y;}
733};
734
735#if _LIBCPP_STD_VER > 11
736template <>
737struct _LIBCPP_TEMPLATE_VIS greater<void>
738{
739    template <class _T1, class _T2>
740    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
741    auto operator()(_T1&& __t, _T2&& __u) const
742    _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) > _VSTD::forward<_T2>(__u)))
743    -> decltype        (_VSTD::forward<_T1>(__t) > _VSTD::forward<_T2>(__u))
744        { return        _VSTD::forward<_T1>(__t) > _VSTD::forward<_T2>(__u); }
745    typedef void is_transparent;
746};
747#endif
748
749
750// less in <__functional_base>
751
752#if _LIBCPP_STD_VER > 11
753template <class _Tp = void>
754#else
755template <class _Tp>
756#endif
757struct _LIBCPP_TEMPLATE_VIS greater_equal : binary_function<_Tp, _Tp, bool>
758{
759    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
760    bool operator()(const _Tp& __x, const _Tp& __y) const
761        {return __x >= __y;}
762};
763
764#if _LIBCPP_STD_VER > 11
765template <>
766struct _LIBCPP_TEMPLATE_VIS greater_equal<void>
767{
768    template <class _T1, class _T2>
769    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
770    auto operator()(_T1&& __t, _T2&& __u) const
771    _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) >= _VSTD::forward<_T2>(__u)))
772    -> decltype        (_VSTD::forward<_T1>(__t) >= _VSTD::forward<_T2>(__u))
773        { return        _VSTD::forward<_T1>(__t) >= _VSTD::forward<_T2>(__u); }
774    typedef void is_transparent;
775};
776#endif
777
778
779#if _LIBCPP_STD_VER > 11
780template <class _Tp = void>
781#else
782template <class _Tp>
783#endif
784struct _LIBCPP_TEMPLATE_VIS less_equal : binary_function<_Tp, _Tp, bool>
785{
786    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
787    bool operator()(const _Tp& __x, const _Tp& __y) const
788        {return __x <= __y;}
789};
790
791#if _LIBCPP_STD_VER > 11
792template <>
793struct _LIBCPP_TEMPLATE_VIS less_equal<void>
794{
795    template <class _T1, class _T2>
796    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
797    auto operator()(_T1&& __t, _T2&& __u) const
798    _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) <= _VSTD::forward<_T2>(__u)))
799    -> decltype        (_VSTD::forward<_T1>(__t) <= _VSTD::forward<_T2>(__u))
800        { return        _VSTD::forward<_T1>(__t) <= _VSTD::forward<_T2>(__u); }
801    typedef void is_transparent;
802};
803#endif
804
805
806#if _LIBCPP_STD_VER > 11
807template <class _Tp = void>
808#else
809template <class _Tp>
810#endif
811struct _LIBCPP_TEMPLATE_VIS logical_and : binary_function<_Tp, _Tp, bool>
812{
813    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
814    bool operator()(const _Tp& __x, const _Tp& __y) const
815        {return __x && __y;}
816};
817
818#if _LIBCPP_STD_VER > 11
819template <>
820struct _LIBCPP_TEMPLATE_VIS logical_and<void>
821{
822    template <class _T1, class _T2>
823    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
824    auto operator()(_T1&& __t, _T2&& __u) const
825    _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) && _VSTD::forward<_T2>(__u)))
826    -> decltype        (_VSTD::forward<_T1>(__t) && _VSTD::forward<_T2>(__u))
827        { return        _VSTD::forward<_T1>(__t) && _VSTD::forward<_T2>(__u); }
828    typedef void is_transparent;
829};
830#endif
831
832
833#if _LIBCPP_STD_VER > 11
834template <class _Tp = void>
835#else
836template <class _Tp>
837#endif
838struct _LIBCPP_TEMPLATE_VIS logical_or : binary_function<_Tp, _Tp, bool>
839{
840    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
841    bool operator()(const _Tp& __x, const _Tp& __y) const
842        {return __x || __y;}
843};
844
845#if _LIBCPP_STD_VER > 11
846template <>
847struct _LIBCPP_TEMPLATE_VIS logical_or<void>
848{
849    template <class _T1, class _T2>
850    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
851    auto operator()(_T1&& __t, _T2&& __u) const
852    _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) || _VSTD::forward<_T2>(__u)))
853    -> decltype        (_VSTD::forward<_T1>(__t) || _VSTD::forward<_T2>(__u))
854        { return        _VSTD::forward<_T1>(__t) || _VSTD::forward<_T2>(__u); }
855    typedef void is_transparent;
856};
857#endif
858
859
860#if _LIBCPP_STD_VER > 11
861template <class _Tp = void>
862#else
863template <class _Tp>
864#endif
865struct _LIBCPP_TEMPLATE_VIS logical_not : unary_function<_Tp, bool>
866{
867    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
868    bool operator()(const _Tp& __x) const
869        {return !__x;}
870};
871
872#if _LIBCPP_STD_VER > 11
873template <>
874struct _LIBCPP_TEMPLATE_VIS logical_not<void>
875{
876    template <class _Tp>
877    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
878    auto operator()(_Tp&& __x) const
879    _NOEXCEPT_(noexcept(!_VSTD::forward<_Tp>(__x)))
880    -> decltype        (!_VSTD::forward<_Tp>(__x))
881        { return        !_VSTD::forward<_Tp>(__x); }
882    typedef void is_transparent;
883};
884#endif
885
886
887#if _LIBCPP_STD_VER > 11
888template <class _Tp = void>
889#else
890template <class _Tp>
891#endif
892struct _LIBCPP_TEMPLATE_VIS bit_and : binary_function<_Tp, _Tp, _Tp>
893{
894    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
895    _Tp operator()(const _Tp& __x, const _Tp& __y) const
896        {return __x & __y;}
897};
898
899#if _LIBCPP_STD_VER > 11
900template <>
901struct _LIBCPP_TEMPLATE_VIS bit_and<void>
902{
903    template <class _T1, class _T2>
904    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
905    auto operator()(_T1&& __t, _T2&& __u) const
906    _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) & _VSTD::forward<_T2>(__u)))
907    -> decltype        (_VSTD::forward<_T1>(__t) & _VSTD::forward<_T2>(__u))
908        { return        _VSTD::forward<_T1>(__t) & _VSTD::forward<_T2>(__u); }
909    typedef void is_transparent;
910};
911#endif
912
913
914#if _LIBCPP_STD_VER > 11
915template <class _Tp = void>
916#else
917template <class _Tp>
918#endif
919struct _LIBCPP_TEMPLATE_VIS bit_or : binary_function<_Tp, _Tp, _Tp>
920{
921    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
922    _Tp operator()(const _Tp& __x, const _Tp& __y) const
923        {return __x | __y;}
924};
925
926#if _LIBCPP_STD_VER > 11
927template <>
928struct _LIBCPP_TEMPLATE_VIS bit_or<void>
929{
930    template <class _T1, class _T2>
931    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
932    auto operator()(_T1&& __t, _T2&& __u) const
933    _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) | _VSTD::forward<_T2>(__u)))
934    -> decltype        (_VSTD::forward<_T1>(__t) | _VSTD::forward<_T2>(__u))
935        { return        _VSTD::forward<_T1>(__t) | _VSTD::forward<_T2>(__u); }
936    typedef void is_transparent;
937};
938#endif
939
940
941#if _LIBCPP_STD_VER > 11
942template <class _Tp = void>
943#else
944template <class _Tp>
945#endif
946struct _LIBCPP_TEMPLATE_VIS bit_xor : binary_function<_Tp, _Tp, _Tp>
947{
948    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
949    _Tp operator()(const _Tp& __x, const _Tp& __y) const
950        {return __x ^ __y;}
951};
952
953#if _LIBCPP_STD_VER > 11
954template <>
955struct _LIBCPP_TEMPLATE_VIS bit_xor<void>
956{
957    template <class _T1, class _T2>
958    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
959    auto operator()(_T1&& __t, _T2&& __u) const
960    _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) ^ _VSTD::forward<_T2>(__u)))
961    -> decltype        (_VSTD::forward<_T1>(__t) ^ _VSTD::forward<_T2>(__u))
962        { return        _VSTD::forward<_T1>(__t) ^ _VSTD::forward<_T2>(__u); }
963    typedef void is_transparent;
964};
965#endif
966
967
968#if _LIBCPP_STD_VER > 11
969template <class _Tp = void>
970struct _LIBCPP_TEMPLATE_VIS bit_not : unary_function<_Tp, _Tp>
971{
972    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
973    _Tp operator()(const _Tp& __x) const
974        {return ~__x;}
975};
976
977template <>
978struct _LIBCPP_TEMPLATE_VIS bit_not<void>
979{
980    template <class _Tp>
981    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
982    auto operator()(_Tp&& __x) const
983    _NOEXCEPT_(noexcept(~_VSTD::forward<_Tp>(__x)))
984    -> decltype        (~_VSTD::forward<_Tp>(__x))
985        { return        ~_VSTD::forward<_Tp>(__x); }
986    typedef void is_transparent;
987};
988#endif
989
990template <class _Predicate>
991class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX17 unary_negate
992    : public unary_function<typename _Predicate::argument_type, bool>
993{
994    _Predicate __pred_;
995public:
996    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
997    explicit unary_negate(const _Predicate& __pred)
998        : __pred_(__pred) {}
999    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1000    bool operator()(const typename _Predicate::argument_type& __x) const
1001        {return !__pred_(__x);}
1002};
1003
1004template <class _Predicate>
1005_LIBCPP_DEPRECATED_IN_CXX17 inline _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1006unary_negate<_Predicate>
1007not1(const _Predicate& __pred) {return unary_negate<_Predicate>(__pred);}
1008
1009template <class _Predicate>
1010class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX17 binary_negate
1011    : public binary_function<typename _Predicate::first_argument_type,
1012                             typename _Predicate::second_argument_type,
1013                             bool>
1014{
1015    _Predicate __pred_;
1016public:
1017    _LIBCPP_INLINE_VISIBILITY explicit _LIBCPP_CONSTEXPR_AFTER_CXX11
1018    binary_negate(const _Predicate& __pred) : __pred_(__pred) {}
1019
1020    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1021    bool operator()(const typename _Predicate::first_argument_type& __x,
1022                    const typename _Predicate::second_argument_type& __y) const
1023        {return !__pred_(__x, __y);}
1024};
1025
1026template <class _Predicate>
1027_LIBCPP_DEPRECATED_IN_CXX17 inline _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1028binary_negate<_Predicate>
1029not2(const _Predicate& __pred) {return binary_negate<_Predicate>(__pred);}
1030
1031#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_BINDERS)
1032template <class __Operation>
1033class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 binder1st
1034    : public unary_function<typename __Operation::second_argument_type,
1035                            typename __Operation::result_type>
1036{
1037protected:
1038    __Operation                               op;
1039    typename __Operation::first_argument_type value;
1040public:
1041    _LIBCPP_INLINE_VISIBILITY binder1st(const __Operation& __x,
1042                               const typename __Operation::first_argument_type __y)
1043        : op(__x), value(__y) {}
1044    _LIBCPP_INLINE_VISIBILITY typename __Operation::result_type operator()
1045        (typename __Operation::second_argument_type& __x) const
1046            {return op(value, __x);}
1047    _LIBCPP_INLINE_VISIBILITY typename __Operation::result_type operator()
1048        (const typename __Operation::second_argument_type& __x) const
1049            {return op(value, __x);}
1050};
1051
1052template <class __Operation, class _Tp>
1053_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
1054binder1st<__Operation>
1055bind1st(const __Operation& __op, const _Tp& __x)
1056    {return binder1st<__Operation>(__op, __x);}
1057
1058template <class __Operation>
1059class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 binder2nd
1060    : public unary_function<typename __Operation::first_argument_type,
1061                            typename __Operation::result_type>
1062{
1063protected:
1064    __Operation                                op;
1065    typename __Operation::second_argument_type value;
1066public:
1067    _LIBCPP_INLINE_VISIBILITY
1068    binder2nd(const __Operation& __x, const typename __Operation::second_argument_type __y)
1069        : op(__x), value(__y) {}
1070    _LIBCPP_INLINE_VISIBILITY typename __Operation::result_type operator()
1071        (      typename __Operation::first_argument_type& __x) const
1072            {return op(__x, value);}
1073    _LIBCPP_INLINE_VISIBILITY typename __Operation::result_type operator()
1074        (const typename __Operation::first_argument_type& __x) const
1075            {return op(__x, value);}
1076};
1077
1078template <class __Operation, class _Tp>
1079_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
1080binder2nd<__Operation>
1081bind2nd(const __Operation& __op, const _Tp& __x)
1082    {return binder2nd<__Operation>(__op, __x);}
1083
1084template <class _Arg, class _Result>
1085class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 pointer_to_unary_function
1086    : public unary_function<_Arg, _Result>
1087{
1088    _Result (*__f_)(_Arg);
1089public:
1090    _LIBCPP_INLINE_VISIBILITY explicit pointer_to_unary_function(_Result (*__f)(_Arg))
1091        : __f_(__f) {}
1092    _LIBCPP_INLINE_VISIBILITY _Result operator()(_Arg __x) const
1093        {return __f_(__x);}
1094};
1095
1096template <class _Arg, class _Result>
1097_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
1098pointer_to_unary_function<_Arg,_Result>
1099ptr_fun(_Result (*__f)(_Arg))
1100    {return pointer_to_unary_function<_Arg,_Result>(__f);}
1101
1102template <class _Arg1, class _Arg2, class _Result>
1103class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 pointer_to_binary_function
1104    : public binary_function<_Arg1, _Arg2, _Result>
1105{
1106    _Result (*__f_)(_Arg1, _Arg2);
1107public:
1108    _LIBCPP_INLINE_VISIBILITY explicit pointer_to_binary_function(_Result (*__f)(_Arg1, _Arg2))
1109        : __f_(__f) {}
1110    _LIBCPP_INLINE_VISIBILITY _Result operator()(_Arg1 __x, _Arg2 __y) const
1111        {return __f_(__x, __y);}
1112};
1113
1114template <class _Arg1, class _Arg2, class _Result>
1115_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
1116pointer_to_binary_function<_Arg1,_Arg2,_Result>
1117ptr_fun(_Result (*__f)(_Arg1,_Arg2))
1118    {return pointer_to_binary_function<_Arg1,_Arg2,_Result>(__f);}
1119
1120template<class _Sp, class _Tp>
1121class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 mem_fun_t
1122    : public unary_function<_Tp*, _Sp>
1123{
1124    _Sp (_Tp::*__p_)();
1125public:
1126    _LIBCPP_INLINE_VISIBILITY explicit mem_fun_t(_Sp (_Tp::*__p)())
1127        : __p_(__p) {}
1128    _LIBCPP_INLINE_VISIBILITY _Sp operator()(_Tp* __p) const
1129        {return (__p->*__p_)();}
1130};
1131
1132template<class _Sp, class _Tp, class _Ap>
1133class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 mem_fun1_t
1134    : public binary_function<_Tp*, _Ap, _Sp>
1135{
1136    _Sp (_Tp::*__p_)(_Ap);
1137public:
1138    _LIBCPP_INLINE_VISIBILITY explicit mem_fun1_t(_Sp (_Tp::*__p)(_Ap))
1139        : __p_(__p) {}
1140    _LIBCPP_INLINE_VISIBILITY _Sp operator()(_Tp* __p, _Ap __x) const
1141        {return (__p->*__p_)(__x);}
1142};
1143
1144template<class _Sp, class _Tp>
1145_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
1146mem_fun_t<_Sp,_Tp>
1147mem_fun(_Sp (_Tp::*__f)())
1148    {return mem_fun_t<_Sp,_Tp>(__f);}
1149
1150template<class _Sp, class _Tp, class _Ap>
1151_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
1152mem_fun1_t<_Sp,_Tp,_Ap>
1153mem_fun(_Sp (_Tp::*__f)(_Ap))
1154    {return mem_fun1_t<_Sp,_Tp,_Ap>(__f);}
1155
1156template<class _Sp, class _Tp>
1157class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 mem_fun_ref_t
1158    : public unary_function<_Tp, _Sp>
1159{
1160    _Sp (_Tp::*__p_)();
1161public:
1162    _LIBCPP_INLINE_VISIBILITY explicit mem_fun_ref_t(_Sp (_Tp::*__p)())
1163        : __p_(__p) {}
1164    _LIBCPP_INLINE_VISIBILITY _Sp operator()(_Tp& __p) const
1165        {return (__p.*__p_)();}
1166};
1167
1168template<class _Sp, class _Tp, class _Ap>
1169class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 mem_fun1_ref_t
1170    : public binary_function<_Tp, _Ap, _Sp>
1171{
1172    _Sp (_Tp::*__p_)(_Ap);
1173public:
1174    _LIBCPP_INLINE_VISIBILITY explicit mem_fun1_ref_t(_Sp (_Tp::*__p)(_Ap))
1175        : __p_(__p) {}
1176    _LIBCPP_INLINE_VISIBILITY _Sp operator()(_Tp& __p, _Ap __x) const
1177        {return (__p.*__p_)(__x);}
1178};
1179
1180template<class _Sp, class _Tp>
1181_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
1182mem_fun_ref_t<_Sp,_Tp>
1183mem_fun_ref(_Sp (_Tp::*__f)())
1184    {return mem_fun_ref_t<_Sp,_Tp>(__f);}
1185
1186template<class _Sp, class _Tp, class _Ap>
1187_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
1188mem_fun1_ref_t<_Sp,_Tp,_Ap>
1189mem_fun_ref(_Sp (_Tp::*__f)(_Ap))
1190    {return mem_fun1_ref_t<_Sp,_Tp,_Ap>(__f);}
1191
1192template <class _Sp, class _Tp>
1193class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 const_mem_fun_t
1194    : public unary_function<const _Tp*, _Sp>
1195{
1196    _Sp (_Tp::*__p_)() const;
1197public:
1198    _LIBCPP_INLINE_VISIBILITY explicit const_mem_fun_t(_Sp (_Tp::*__p)() const)
1199        : __p_(__p) {}
1200    _LIBCPP_INLINE_VISIBILITY _Sp operator()(const _Tp* __p) const
1201        {return (__p->*__p_)();}
1202};
1203
1204template <class _Sp, class _Tp, class _Ap>
1205class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 const_mem_fun1_t
1206    : public binary_function<const _Tp*, _Ap, _Sp>
1207{
1208    _Sp (_Tp::*__p_)(_Ap) const;
1209public:
1210    _LIBCPP_INLINE_VISIBILITY explicit const_mem_fun1_t(_Sp (_Tp::*__p)(_Ap) const)
1211        : __p_(__p) {}
1212    _LIBCPP_INLINE_VISIBILITY _Sp operator()(const _Tp* __p, _Ap __x) const
1213        {return (__p->*__p_)(__x);}
1214};
1215
1216template <class _Sp, class _Tp>
1217_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
1218const_mem_fun_t<_Sp,_Tp>
1219mem_fun(_Sp (_Tp::*__f)() const)
1220    {return const_mem_fun_t<_Sp,_Tp>(__f);}
1221
1222template <class _Sp, class _Tp, class _Ap>
1223_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
1224const_mem_fun1_t<_Sp,_Tp,_Ap>
1225mem_fun(_Sp (_Tp::*__f)(_Ap) const)
1226    {return const_mem_fun1_t<_Sp,_Tp,_Ap>(__f);}
1227
1228template <class _Sp, class _Tp>
1229class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 const_mem_fun_ref_t
1230    : public unary_function<_Tp, _Sp>
1231{
1232    _Sp (_Tp::*__p_)() const;
1233public:
1234    _LIBCPP_INLINE_VISIBILITY explicit const_mem_fun_ref_t(_Sp (_Tp::*__p)() const)
1235        : __p_(__p) {}
1236    _LIBCPP_INLINE_VISIBILITY _Sp operator()(const _Tp& __p) const
1237        {return (__p.*__p_)();}
1238};
1239
1240template <class _Sp, class _Tp, class _Ap>
1241class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 const_mem_fun1_ref_t
1242    : public binary_function<_Tp, _Ap, _Sp>
1243{
1244    _Sp (_Tp::*__p_)(_Ap) const;
1245public:
1246    _LIBCPP_INLINE_VISIBILITY explicit const_mem_fun1_ref_t(_Sp (_Tp::*__p)(_Ap) const)
1247        : __p_(__p) {}
1248    _LIBCPP_INLINE_VISIBILITY _Sp operator()(const _Tp& __p, _Ap __x) const
1249        {return (__p.*__p_)(__x);}
1250};
1251
1252template <class _Sp, class _Tp>
1253_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
1254const_mem_fun_ref_t<_Sp,_Tp>
1255mem_fun_ref(_Sp (_Tp::*__f)() const)
1256    {return const_mem_fun_ref_t<_Sp,_Tp>(__f);}
1257
1258template <class _Sp, class _Tp, class _Ap>
1259_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
1260const_mem_fun1_ref_t<_Sp,_Tp,_Ap>
1261mem_fun_ref(_Sp (_Tp::*__f)(_Ap) const)
1262    {return const_mem_fun1_ref_t<_Sp,_Tp,_Ap>(__f);}
1263#endif
1264
1265////////////////////////////////////////////////////////////////////////////////
1266//                                MEMFUN
1267//==============================================================================
1268
1269template <class _Tp>
1270class __mem_fn
1271    : public __weak_result_type<_Tp>
1272{
1273public:
1274    // types
1275    typedef _Tp type;
1276private:
1277    type __f_;
1278
1279public:
1280    _LIBCPP_INLINE_VISIBILITY __mem_fn(type __f) _NOEXCEPT : __f_(__f) {}
1281
1282#ifndef _LIBCPP_CXX03_LANG
1283    // invoke
1284    template <class... _ArgTypes>
1285    _LIBCPP_INLINE_VISIBILITY
1286    typename __invoke_return<type, _ArgTypes...>::type
1287    operator() (_ArgTypes&&... __args) const {
1288        return __invoke(__f_, _VSTD::forward<_ArgTypes>(__args)...);
1289    }
1290#else
1291
1292    template <class _A0>
1293    _LIBCPP_INLINE_VISIBILITY
1294    typename __invoke_return0<type, _A0>::type
1295    operator() (_A0& __a0) const {
1296        return __invoke(__f_, __a0);
1297    }
1298
1299    template <class _A0>
1300    _LIBCPP_INLINE_VISIBILITY
1301    typename __invoke_return0<type, _A0 const>::type
1302    operator() (_A0 const& __a0) const {
1303        return __invoke(__f_, __a0);
1304    }
1305
1306    template <class _A0, class _A1>
1307    _LIBCPP_INLINE_VISIBILITY
1308    typename __invoke_return1<type, _A0, _A1>::type
1309    operator() (_A0& __a0, _A1& __a1) const {
1310        return __invoke(__f_, __a0, __a1);
1311    }
1312
1313    template <class _A0, class _A1>
1314    _LIBCPP_INLINE_VISIBILITY
1315    typename __invoke_return1<type, _A0 const, _A1>::type
1316    operator() (_A0 const& __a0, _A1& __a1) const {
1317        return __invoke(__f_, __a0, __a1);
1318    }
1319
1320    template <class _A0, class _A1>
1321    _LIBCPP_INLINE_VISIBILITY
1322    typename __invoke_return1<type, _A0, _A1 const>::type
1323    operator() (_A0& __a0, _A1 const& __a1) const {
1324        return __invoke(__f_, __a0, __a1);
1325    }
1326
1327    template <class _A0, class _A1>
1328    _LIBCPP_INLINE_VISIBILITY
1329    typename __invoke_return1<type, _A0 const, _A1 const>::type
1330    operator() (_A0 const& __a0, _A1 const& __a1) const {
1331        return __invoke(__f_, __a0, __a1);
1332    }
1333
1334    template <class _A0, class _A1, class _A2>
1335    _LIBCPP_INLINE_VISIBILITY
1336    typename __invoke_return2<type, _A0, _A1, _A2>::type
1337    operator() (_A0& __a0, _A1& __a1, _A2& __a2) const {
1338        return __invoke(__f_, __a0, __a1, __a2);
1339    }
1340
1341    template <class _A0, class _A1, class _A2>
1342    _LIBCPP_INLINE_VISIBILITY
1343    typename __invoke_return2<type, _A0 const, _A1, _A2>::type
1344    operator() (_A0 const& __a0, _A1& __a1, _A2& __a2) const {
1345        return __invoke(__f_, __a0, __a1, __a2);
1346    }
1347
1348    template <class _A0, class _A1, class _A2>
1349    _LIBCPP_INLINE_VISIBILITY
1350    typename __invoke_return2<type, _A0, _A1 const, _A2>::type
1351    operator() (_A0& __a0, _A1 const& __a1, _A2& __a2) const {
1352        return __invoke(__f_, __a0, __a1, __a2);
1353    }
1354
1355    template <class _A0, class _A1, class _A2>
1356    _LIBCPP_INLINE_VISIBILITY
1357    typename __invoke_return2<type, _A0, _A1, _A2 const>::type
1358    operator() (_A0& __a0, _A1& __a1, _A2 const& __a2) const {
1359        return __invoke(__f_, __a0, __a1, __a2);
1360    }
1361
1362    template <class _A0, class _A1, class _A2>
1363    _LIBCPP_INLINE_VISIBILITY
1364    typename __invoke_return2<type, _A0 const, _A1 const, _A2>::type
1365    operator() (_A0 const& __a0, _A1 const& __a1, _A2& __a2) const {
1366        return __invoke(__f_, __a0, __a1, __a2);
1367    }
1368
1369    template <class _A0, class _A1, class _A2>
1370    _LIBCPP_INLINE_VISIBILITY
1371    typename __invoke_return2<type, _A0 const, _A1, _A2 const>::type
1372    operator() (_A0 const& __a0, _A1& __a1, _A2 const& __a2) const {
1373        return __invoke(__f_, __a0, __a1, __a2);
1374    }
1375
1376    template <class _A0, class _A1, class _A2>
1377    _LIBCPP_INLINE_VISIBILITY
1378    typename __invoke_return2<type, _A0, _A1 const, _A2 const>::type
1379    operator() (_A0& __a0, _A1 const& __a1, _A2 const& __a2) const {
1380        return __invoke(__f_, __a0, __a1, __a2);
1381    }
1382
1383    template <class _A0, class _A1, class _A2>
1384    _LIBCPP_INLINE_VISIBILITY
1385    typename __invoke_return2<type, _A0 const, _A1 const, _A2 const>::type
1386    operator() (_A0 const& __a0, _A1 const& __a1, _A2 const& __a2) const {
1387        return __invoke(__f_, __a0, __a1, __a2);
1388    }
1389#endif
1390};
1391
1392template<class _Rp, class _Tp>
1393inline _LIBCPP_INLINE_VISIBILITY
1394__mem_fn<_Rp _Tp::*>
1395mem_fn(_Rp _Tp::* __pm) _NOEXCEPT
1396{
1397    return __mem_fn<_Rp _Tp::*>(__pm);
1398}
1399
1400////////////////////////////////////////////////////////////////////////////////
1401//                                FUNCTION
1402//==============================================================================
1403
1404// bad_function_call
1405
1406class _LIBCPP_EXCEPTION_ABI bad_function_call
1407    : public exception
1408{
1409#ifdef _LIBCPP_ABI_BAD_FUNCTION_CALL_KEY_FUNCTION
1410public:
1411    virtual ~bad_function_call() _NOEXCEPT;
1412
1413    virtual const char* what() const _NOEXCEPT;
1414#endif
1415};
1416
1417_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
1418void __throw_bad_function_call()
1419{
1420#ifndef _LIBCPP_NO_EXCEPTIONS
1421    throw bad_function_call();
1422#else
1423    _VSTD::abort();
1424#endif
1425}
1426
1427template<class _Fp> class _LIBCPP_TEMPLATE_VIS function; // undefined
1428
1429namespace __function
1430{
1431
1432template<class _Rp>
1433struct __maybe_derive_from_unary_function
1434{
1435};
1436
1437template<class _Rp, class _A1>
1438struct __maybe_derive_from_unary_function<_Rp(_A1)>
1439    : public unary_function<_A1, _Rp>
1440{
1441};
1442
1443template<class _Rp>
1444struct __maybe_derive_from_binary_function
1445{
1446};
1447
1448template<class _Rp, class _A1, class _A2>
1449struct __maybe_derive_from_binary_function<_Rp(_A1, _A2)>
1450    : public binary_function<_A1, _A2, _Rp>
1451{
1452};
1453
1454template <class _Fp>
1455_LIBCPP_INLINE_VISIBILITY
1456bool __not_null(_Fp const&) { return true; }
1457
1458template <class _Fp>
1459_LIBCPP_INLINE_VISIBILITY
1460bool __not_null(_Fp* __ptr) { return __ptr; }
1461
1462template <class _Ret, class _Class>
1463_LIBCPP_INLINE_VISIBILITY
1464bool __not_null(_Ret _Class::*__ptr) { return __ptr; }
1465
1466template <class _Fp>
1467_LIBCPP_INLINE_VISIBILITY
1468bool __not_null(function<_Fp> const& __f) { return !!__f; }
1469
1470} // namespace __function
1471
1472#ifndef _LIBCPP_CXX03_LANG
1473
1474namespace __function {
1475
1476template<class _Fp> class __base;
1477
1478template<class _Rp, class ..._ArgTypes>
1479class __base<_Rp(_ArgTypes...)>
1480{
1481    __base(const __base&);
1482    __base& operator=(const __base&);
1483public:
1484    _LIBCPP_INLINE_VISIBILITY __base() {}
1485    _LIBCPP_INLINE_VISIBILITY virtual ~__base() {}
1486    virtual __base* __clone() const = 0;
1487    virtual void __clone(__base*) const = 0;
1488    virtual void destroy() _NOEXCEPT = 0;
1489    virtual void destroy_deallocate() _NOEXCEPT = 0;
1490    virtual _Rp operator()(_ArgTypes&& ...) = 0;
1491#ifndef _LIBCPP_NO_RTTI
1492    virtual const void* target(const type_info&) const _NOEXCEPT = 0;
1493    virtual const std::type_info& target_type() const _NOEXCEPT = 0;
1494#endif  // _LIBCPP_NO_RTTI
1495};
1496
1497template<class _FD, class _Alloc, class _FB> class __func;
1498
1499template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1500class __func<_Fp, _Alloc, _Rp(_ArgTypes...)>
1501    : public  __base<_Rp(_ArgTypes...)>
1502{
1503    __compressed_pair<_Fp, _Alloc> __f_;
1504public:
1505    _LIBCPP_INLINE_VISIBILITY
1506    explicit __func(_Fp&& __f)
1507        : __f_(piecewise_construct, _VSTD::forward_as_tuple(_VSTD::move(__f)),
1508                                    _VSTD::forward_as_tuple()) {}
1509    _LIBCPP_INLINE_VISIBILITY
1510    explicit __func(const _Fp& __f, const _Alloc& __a)
1511        : __f_(piecewise_construct, _VSTD::forward_as_tuple(__f),
1512                                    _VSTD::forward_as_tuple(__a)) {}
1513
1514    _LIBCPP_INLINE_VISIBILITY
1515    explicit __func(const _Fp& __f, _Alloc&& __a)
1516        : __f_(piecewise_construct, _VSTD::forward_as_tuple(__f),
1517                                    _VSTD::forward_as_tuple(_VSTD::move(__a))) {}
1518
1519    _LIBCPP_INLINE_VISIBILITY
1520    explicit __func(_Fp&& __f, _Alloc&& __a)
1521        : __f_(piecewise_construct, _VSTD::forward_as_tuple(_VSTD::move(__f)),
1522                                    _VSTD::forward_as_tuple(_VSTD::move(__a))) {}
1523    virtual __base<_Rp(_ArgTypes...)>* __clone() const;
1524    virtual void __clone(__base<_Rp(_ArgTypes...)>*) const;
1525    virtual void destroy() _NOEXCEPT;
1526    virtual void destroy_deallocate() _NOEXCEPT;
1527    virtual _Rp operator()(_ArgTypes&& ... __arg);
1528#ifndef _LIBCPP_NO_RTTI
1529    virtual const void* target(const type_info&) const _NOEXCEPT;
1530    virtual const std::type_info& target_type() const _NOEXCEPT;
1531#endif  // _LIBCPP_NO_RTTI
1532};
1533
1534template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1535__base<_Rp(_ArgTypes...)>*
1536__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__clone() const
1537{
1538    typedef allocator_traits<_Alloc> __alloc_traits;
1539    typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
1540    _Ap __a(__f_.second());
1541    typedef __allocator_destructor<_Ap> _Dp;
1542    unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1543    ::new (__hold.get()) __func(__f_.first(), _Alloc(__a));
1544    return __hold.release();
1545}
1546
1547template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1548void
1549__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__clone(__base<_Rp(_ArgTypes...)>* __p) const
1550{
1551    ::new (__p) __func(__f_.first(), __f_.second());
1552}
1553
1554template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1555void
1556__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy() _NOEXCEPT
1557{
1558    __f_.~__compressed_pair<_Fp, _Alloc>();
1559}
1560
1561template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1562void
1563__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy_deallocate() _NOEXCEPT
1564{
1565    typedef allocator_traits<_Alloc> __alloc_traits;
1566    typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
1567    _Ap __a(__f_.second());
1568    __f_.~__compressed_pair<_Fp, _Alloc>();
1569    __a.deallocate(this, 1);
1570}
1571
1572template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1573_Rp
1574__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::operator()(_ArgTypes&& ... __arg)
1575{
1576    typedef __invoke_void_return_wrapper<_Rp> _Invoker;
1577    return _Invoker::__call(__f_.first(), _VSTD::forward<_ArgTypes>(__arg)...);
1578}
1579
1580#ifndef _LIBCPP_NO_RTTI
1581
1582template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1583const void*
1584__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::target(const type_info& __ti) const _NOEXCEPT
1585{
1586    if (__ti == typeid(_Fp))
1587        return &__f_.first();
1588    return (const void*)0;
1589}
1590
1591template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1592const std::type_info&
1593__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::target_type() const _NOEXCEPT
1594{
1595    return typeid(_Fp);
1596}
1597
1598#endif  // _LIBCPP_NO_RTTI
1599
1600}  // __function
1601
1602template<class _Rp, class ..._ArgTypes>
1603class _LIBCPP_TEMPLATE_VIS function<_Rp(_ArgTypes...)>
1604    : public __function::__maybe_derive_from_unary_function<_Rp(_ArgTypes...)>,
1605      public __function::__maybe_derive_from_binary_function<_Rp(_ArgTypes...)>
1606{
1607    typedef __function::__base<_Rp(_ArgTypes...)> __base;
1608    typename aligned_storage<3*sizeof(void*)>::type __buf_;
1609    __base* __f_;
1610
1611    _LIBCPP_NO_CFI static __base *__as_base(void *p) {
1612      return reinterpret_cast<__base*>(p);
1613    }
1614
1615    template <class _Fp, bool = __lazy_and<
1616        integral_constant<bool, !is_same<__uncvref_t<_Fp>, function>::value>,
1617        __invokable<_Fp&, _ArgTypes...>
1618    >::value>
1619    struct __callable;
1620    template <class _Fp>
1621        struct __callable<_Fp, true>
1622        {
1623            static const bool value = is_same<void, _Rp>::value ||
1624                is_convertible<typename __invoke_of<_Fp&, _ArgTypes...>::type,
1625                               _Rp>::value;
1626        };
1627    template <class _Fp>
1628        struct __callable<_Fp, false>
1629        {
1630            static const bool value = false;
1631        };
1632
1633  template <class _Fp>
1634  using _EnableIfCallable = typename enable_if<__callable<_Fp>::value>::type;
1635public:
1636    typedef _Rp result_type;
1637
1638    // construct/copy/destroy:
1639    _LIBCPP_INLINE_VISIBILITY
1640    function() _NOEXCEPT : __f_(0) {}
1641    _LIBCPP_INLINE_VISIBILITY
1642    function(nullptr_t) _NOEXCEPT : __f_(0) {}
1643    function(const function&);
1644    function(function&&) _NOEXCEPT;
1645    template<class _Fp, class = _EnableIfCallable<_Fp>>
1646    function(_Fp);
1647
1648#if _LIBCPP_STD_VER <= 14
1649    template<class _Alloc>
1650      _LIBCPP_INLINE_VISIBILITY
1651      function(allocator_arg_t, const _Alloc&) _NOEXCEPT : __f_(0) {}
1652    template<class _Alloc>
1653      _LIBCPP_INLINE_VISIBILITY
1654      function(allocator_arg_t, const _Alloc&, nullptr_t) _NOEXCEPT : __f_(0) {}
1655    template<class _Alloc>
1656      function(allocator_arg_t, const _Alloc&, const function&);
1657    template<class _Alloc>
1658      function(allocator_arg_t, const _Alloc&, function&&);
1659    template<class _Fp, class _Alloc, class = _EnableIfCallable<_Fp>>
1660      function(allocator_arg_t, const _Alloc& __a, _Fp __f);
1661#endif
1662
1663    function& operator=(const function&);
1664    function& operator=(function&&) _NOEXCEPT;
1665    function& operator=(nullptr_t) _NOEXCEPT;
1666    template<class _Fp, class = _EnableIfCallable<_Fp>>
1667    function& operator=(_Fp&&);
1668
1669    ~function();
1670
1671    // function modifiers:
1672    void swap(function&) _NOEXCEPT;
1673
1674#if _LIBCPP_STD_VER <= 14
1675    template<class _Fp, class _Alloc>
1676      _LIBCPP_INLINE_VISIBILITY
1677      void assign(_Fp&& __f, const _Alloc& __a)
1678        {function(allocator_arg, __a, _VSTD::forward<_Fp>(__f)).swap(*this);}
1679#endif
1680
1681    // function capacity:
1682    _LIBCPP_INLINE_VISIBILITY
1683        _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {return __f_;}
1684
1685    // deleted overloads close possible hole in the type system
1686    template<class _R2, class... _ArgTypes2>
1687      bool operator==(const function<_R2(_ArgTypes2...)>&) const = delete;
1688    template<class _R2, class... _ArgTypes2>
1689      bool operator!=(const function<_R2(_ArgTypes2...)>&) const = delete;
1690public:
1691    // function invocation:
1692    _Rp operator()(_ArgTypes...) const;
1693
1694#ifndef _LIBCPP_NO_RTTI
1695    // function target access:
1696    const std::type_info& target_type() const _NOEXCEPT;
1697    template <typename _Tp> _Tp* target() _NOEXCEPT;
1698    template <typename _Tp> const _Tp* target() const _NOEXCEPT;
1699#endif  // _LIBCPP_NO_RTTI
1700};
1701
1702template<class _Rp, class ..._ArgTypes>
1703function<_Rp(_ArgTypes...)>::function(const function& __f)
1704{
1705    if (__f.__f_ == 0)
1706        __f_ = 0;
1707    else if ((void *)__f.__f_ == &__f.__buf_)
1708    {
1709        __f_ = __as_base(&__buf_);
1710        __f.__f_->__clone(__f_);
1711    }
1712    else
1713        __f_ = __f.__f_->__clone();
1714}
1715
1716#if _LIBCPP_STD_VER <= 14
1717template<class _Rp, class ..._ArgTypes>
1718template <class _Alloc>
1719function<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc&,
1720                                     const function& __f)
1721{
1722    if (__f.__f_ == 0)
1723        __f_ = 0;
1724    else if ((void *)__f.__f_ == &__f.__buf_)
1725    {
1726        __f_ = __as_base(&__buf_);
1727        __f.__f_->__clone(__f_);
1728    }
1729    else
1730        __f_ = __f.__f_->__clone();
1731}
1732#endif
1733
1734template<class _Rp, class ..._ArgTypes>
1735function<_Rp(_ArgTypes...)>::function(function&& __f) _NOEXCEPT
1736{
1737    if (__f.__f_ == 0)
1738        __f_ = 0;
1739    else if ((void *)__f.__f_ == &__f.__buf_)
1740    {
1741        __f_ = __as_base(&__buf_);
1742        __f.__f_->__clone(__f_);
1743    }
1744    else
1745    {
1746        __f_ = __f.__f_;
1747        __f.__f_ = 0;
1748    }
1749}
1750
1751#if _LIBCPP_STD_VER <= 14
1752template<class _Rp, class ..._ArgTypes>
1753template <class _Alloc>
1754function<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc&,
1755                                     function&& __f)
1756{
1757    if (__f.__f_ == 0)
1758        __f_ = 0;
1759    else if ((void *)__f.__f_ == &__f.__buf_)
1760    {
1761        __f_ = __as_base(&__buf_);
1762        __f.__f_->__clone(__f_);
1763    }
1764    else
1765    {
1766        __f_ = __f.__f_;
1767        __f.__f_ = 0;
1768    }
1769}
1770#endif
1771
1772template<class _Rp, class ..._ArgTypes>
1773template <class _Fp, class>
1774function<_Rp(_ArgTypes...)>::function(_Fp __f)
1775    : __f_(0)
1776{
1777    if (__function::__not_null(__f))
1778    {
1779        typedef __function::__func<_Fp, allocator<_Fp>, _Rp(_ArgTypes...)> _FF;
1780        if (sizeof(_FF) <= sizeof(__buf_) && is_nothrow_copy_constructible<_Fp>::value)
1781        {
1782            __f_ = ::new((void*)&__buf_) _FF(_VSTD::move(__f));
1783        }
1784        else
1785        {
1786            typedef allocator<_FF> _Ap;
1787            _Ap __a;
1788            typedef __allocator_destructor<_Ap> _Dp;
1789            unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1790            ::new (__hold.get()) _FF(_VSTD::move(__f), allocator<_Fp>(__a));
1791            __f_ = __hold.release();
1792        }
1793    }
1794}
1795
1796#if _LIBCPP_STD_VER <= 14
1797template<class _Rp, class ..._ArgTypes>
1798template <class _Fp, class _Alloc, class>
1799function<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc& __a0, _Fp __f)
1800    : __f_(0)
1801{
1802    typedef allocator_traits<_Alloc> __alloc_traits;
1803    if (__function::__not_null(__f))
1804    {
1805        typedef __function::__func<_Fp, _Alloc, _Rp(_ArgTypes...)> _FF;
1806        typedef typename __rebind_alloc_helper<__alloc_traits, _FF>::type _Ap;
1807        _Ap __a(__a0);
1808        if (sizeof(_FF) <= sizeof(__buf_) &&
1809            is_nothrow_copy_constructible<_Fp>::value && is_nothrow_copy_constructible<_Ap>::value)
1810        {
1811            __f_ = ::new((void*)&__buf_) _FF(_VSTD::move(__f), _Alloc(__a));
1812        }
1813        else
1814        {
1815            typedef __allocator_destructor<_Ap> _Dp;
1816            unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1817            ::new (__hold.get()) _FF(_VSTD::move(__f), _Alloc(__a));
1818            __f_ = __hold.release();
1819        }
1820    }
1821}
1822#endif
1823
1824template<class _Rp, class ..._ArgTypes>
1825function<_Rp(_ArgTypes...)>&
1826function<_Rp(_ArgTypes...)>::operator=(const function& __f)
1827{
1828    function(__f).swap(*this);
1829    return *this;
1830}
1831
1832template<class _Rp, class ..._ArgTypes>
1833function<_Rp(_ArgTypes...)>&
1834function<_Rp(_ArgTypes...)>::operator=(function&& __f) _NOEXCEPT
1835{
1836    *this = nullptr;
1837    if (__f.__f_ == 0)
1838        __f_ = 0;
1839    else if ((void *)__f.__f_ == &__f.__buf_)
1840    {
1841        __f_ = __as_base(&__buf_);
1842        __f.__f_->__clone(__f_);
1843    }
1844    else
1845    {
1846        __f_ = __f.__f_;
1847        __f.__f_ = 0;
1848    }
1849    return *this;
1850}
1851
1852template<class _Rp, class ..._ArgTypes>
1853function<_Rp(_ArgTypes...)>&
1854function<_Rp(_ArgTypes...)>::operator=(nullptr_t) _NOEXCEPT
1855{
1856    __base* __t = __f_;
1857    __f_ = 0;
1858    if ((void *)__t == &__buf_)
1859        __t->destroy();
1860    else if (__t)
1861        __t->destroy_deallocate();
1862    return *this;
1863}
1864
1865template<class _Rp, class ..._ArgTypes>
1866template <class _Fp, class>
1867function<_Rp(_ArgTypes...)>&
1868function<_Rp(_ArgTypes...)>::operator=(_Fp&& __f)
1869{
1870    function(_VSTD::forward<_Fp>(__f)).swap(*this);
1871    return *this;
1872}
1873
1874template<class _Rp, class ..._ArgTypes>
1875function<_Rp(_ArgTypes...)>::~function()
1876{
1877    if ((void *)__f_ == &__buf_)
1878        __f_->destroy();
1879    else if (__f_)
1880        __f_->destroy_deallocate();
1881}
1882
1883template<class _Rp, class ..._ArgTypes>
1884void
1885function<_Rp(_ArgTypes...)>::swap(function& __f) _NOEXCEPT
1886{
1887    if (_VSTD::addressof(__f) == this)
1888      return;
1889    if ((void *)__f_ == &__buf_ && (void *)__f.__f_ == &__f.__buf_)
1890    {
1891        typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1892        __base* __t = __as_base(&__tempbuf);
1893        __f_->__clone(__t);
1894        __f_->destroy();
1895        __f_ = 0;
1896        __f.__f_->__clone(__as_base(&__buf_));
1897        __f.__f_->destroy();
1898        __f.__f_ = 0;
1899        __f_ = __as_base(&__buf_);
1900        __t->__clone(__as_base(&__f.__buf_));
1901        __t->destroy();
1902        __f.__f_ = __as_base(&__f.__buf_);
1903    }
1904    else if ((void *)__f_ == &__buf_)
1905    {
1906        __f_->__clone(__as_base(&__f.__buf_));
1907        __f_->destroy();
1908        __f_ = __f.__f_;
1909        __f.__f_ = __as_base(&__f.__buf_);
1910    }
1911    else if ((void *)__f.__f_ == &__f.__buf_)
1912    {
1913        __f.__f_->__clone(__as_base(&__buf_));
1914        __f.__f_->destroy();
1915        __f.__f_ = __f_;
1916        __f_ = __as_base(&__buf_);
1917    }
1918    else
1919        _VSTD::swap(__f_, __f.__f_);
1920}
1921
1922template<class _Rp, class ..._ArgTypes>
1923_Rp
1924function<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __arg) const
1925{
1926    if (__f_ == 0)
1927        __throw_bad_function_call();
1928    return (*__f_)(_VSTD::forward<_ArgTypes>(__arg)...);
1929}
1930
1931#ifndef _LIBCPP_NO_RTTI
1932
1933template<class _Rp, class ..._ArgTypes>
1934const std::type_info&
1935function<_Rp(_ArgTypes...)>::target_type() const _NOEXCEPT
1936{
1937    if (__f_ == 0)
1938        return typeid(void);
1939    return __f_->target_type();
1940}
1941
1942template<class _Rp, class ..._ArgTypes>
1943template <typename _Tp>
1944_Tp*
1945function<_Rp(_ArgTypes...)>::target() _NOEXCEPT
1946{
1947    if (__f_ == 0)
1948        return nullptr;
1949    return (_Tp*) const_cast<void *>(__f_->target(typeid(_Tp)));
1950}
1951
1952template<class _Rp, class ..._ArgTypes>
1953template <typename _Tp>
1954const _Tp*
1955function<_Rp(_ArgTypes...)>::target() const _NOEXCEPT
1956{
1957    if (__f_ == 0)
1958        return nullptr;
1959    return (const _Tp*)__f_->target(typeid(_Tp));
1960}
1961
1962#endif  // _LIBCPP_NO_RTTI
1963
1964template <class _Rp, class... _ArgTypes>
1965inline _LIBCPP_INLINE_VISIBILITY
1966bool
1967operator==(const function<_Rp(_ArgTypes...)>& __f, nullptr_t) _NOEXCEPT {return !__f;}
1968
1969template <class _Rp, class... _ArgTypes>
1970inline _LIBCPP_INLINE_VISIBILITY
1971bool
1972operator==(nullptr_t, const function<_Rp(_ArgTypes...)>& __f) _NOEXCEPT {return !__f;}
1973
1974template <class _Rp, class... _ArgTypes>
1975inline _LIBCPP_INLINE_VISIBILITY
1976bool
1977operator!=(const function<_Rp(_ArgTypes...)>& __f, nullptr_t) _NOEXCEPT {return (bool)__f;}
1978
1979template <class _Rp, class... _ArgTypes>
1980inline _LIBCPP_INLINE_VISIBILITY
1981bool
1982operator!=(nullptr_t, const function<_Rp(_ArgTypes...)>& __f) _NOEXCEPT {return (bool)__f;}
1983
1984template <class _Rp, class... _ArgTypes>
1985inline _LIBCPP_INLINE_VISIBILITY
1986void
1987swap(function<_Rp(_ArgTypes...)>& __x, function<_Rp(_ArgTypes...)>& __y) _NOEXCEPT
1988{return __x.swap(__y);}
1989
1990#else // _LIBCPP_CXX03_LANG
1991
1992#include <__functional_03>
1993
1994#endif
1995
1996////////////////////////////////////////////////////////////////////////////////
1997//                                  BIND
1998//==============================================================================
1999
2000template<class _Tp> struct __is_bind_expression : public false_type {};
2001template<class _Tp> struct _LIBCPP_TEMPLATE_VIS is_bind_expression
2002    : public __is_bind_expression<typename remove_cv<_Tp>::type> {};
2003
2004#if _LIBCPP_STD_VER > 14
2005template <class _Tp>
2006_LIBCPP_INLINE_VAR constexpr size_t is_bind_expression_v = is_bind_expression<_Tp>::value;
2007#endif
2008
2009template<class _Tp> struct __is_placeholder : public integral_constant<int, 0> {};
2010template<class _Tp> struct _LIBCPP_TEMPLATE_VIS is_placeholder
2011    : public __is_placeholder<typename remove_cv<_Tp>::type> {};
2012
2013#if _LIBCPP_STD_VER > 14
2014template <class _Tp>
2015_LIBCPP_INLINE_VAR constexpr size_t is_placeholder_v = is_placeholder<_Tp>::value;
2016#endif
2017
2018namespace placeholders
2019{
2020
2021template <int _Np> struct __ph {};
2022
2023#if defined(_LIBCPP_CXX03_LANG) || defined(_LIBCPP_BUILDING_LIBRARY)
2024_LIBCPP_FUNC_VIS extern const __ph<1>   _1;
2025_LIBCPP_FUNC_VIS extern const __ph<2>   _2;
2026_LIBCPP_FUNC_VIS extern const __ph<3>   _3;
2027_LIBCPP_FUNC_VIS extern const __ph<4>   _4;
2028_LIBCPP_FUNC_VIS extern const __ph<5>   _5;
2029_LIBCPP_FUNC_VIS extern const __ph<6>   _6;
2030_LIBCPP_FUNC_VIS extern const __ph<7>   _7;
2031_LIBCPP_FUNC_VIS extern const __ph<8>   _8;
2032_LIBCPP_FUNC_VIS extern const __ph<9>   _9;
2033_LIBCPP_FUNC_VIS extern const __ph<10> _10;
2034#else
2035/* _LIBCPP_INLINE_VAR */ constexpr __ph<1>   _1{};
2036/* _LIBCPP_INLINE_VAR */ constexpr __ph<2>   _2{};
2037/* _LIBCPP_INLINE_VAR */ constexpr __ph<3>   _3{};
2038/* _LIBCPP_INLINE_VAR */ constexpr __ph<4>   _4{};
2039/* _LIBCPP_INLINE_VAR */ constexpr __ph<5>   _5{};
2040/* _LIBCPP_INLINE_VAR */ constexpr __ph<6>   _6{};
2041/* _LIBCPP_INLINE_VAR */ constexpr __ph<7>   _7{};
2042/* _LIBCPP_INLINE_VAR */ constexpr __ph<8>   _8{};
2043/* _LIBCPP_INLINE_VAR */ constexpr __ph<9>   _9{};
2044/* _LIBCPP_INLINE_VAR */ constexpr __ph<10> _10{};
2045#endif // defined(_LIBCPP_CXX03_LANG) || defined(_LIBCPP_BUILDING_LIBRARY)
2046
2047}  // placeholders
2048
2049template<int _Np>
2050struct __is_placeholder<placeholders::__ph<_Np> >
2051    : public integral_constant<int, _Np> {};
2052
2053
2054#ifndef _LIBCPP_CXX03_LANG
2055
2056template <class _Tp, class _Uj>
2057inline _LIBCPP_INLINE_VISIBILITY
2058_Tp&
2059__mu(reference_wrapper<_Tp> __t, _Uj&)
2060{
2061    return __t.get();
2062}
2063
2064template <class _Ti, class ..._Uj, size_t ..._Indx>
2065inline _LIBCPP_INLINE_VISIBILITY
2066typename __invoke_of<_Ti&, _Uj...>::type
2067__mu_expand(_Ti& __ti, tuple<_Uj...>& __uj, __tuple_indices<_Indx...>)
2068{
2069    return __ti(_VSTD::forward<_Uj>(_VSTD::get<_Indx>(__uj))...);
2070}
2071
2072template <class _Ti, class ..._Uj>
2073inline _LIBCPP_INLINE_VISIBILITY
2074typename __lazy_enable_if
2075<
2076    is_bind_expression<_Ti>::value,
2077    __invoke_of<_Ti&, _Uj...>
2078>::type
2079__mu(_Ti& __ti, tuple<_Uj...>& __uj)
2080{
2081    typedef typename __make_tuple_indices<sizeof...(_Uj)>::type __indices;
2082    return  __mu_expand(__ti, __uj, __indices());
2083}
2084
2085template <bool IsPh, class _Ti, class _Uj>
2086struct __mu_return2 {};
2087
2088template <class _Ti, class _Uj>
2089struct __mu_return2<true, _Ti, _Uj>
2090{
2091    typedef typename tuple_element<is_placeholder<_Ti>::value - 1, _Uj>::type type;
2092};
2093
2094template <class _Ti, class _Uj>
2095inline _LIBCPP_INLINE_VISIBILITY
2096typename enable_if
2097<
2098    0 < is_placeholder<_Ti>::value,
2099    typename __mu_return2<0 < is_placeholder<_Ti>::value, _Ti, _Uj>::type
2100>::type
2101__mu(_Ti&, _Uj& __uj)
2102{
2103    const size_t _Indx = is_placeholder<_Ti>::value - 1;
2104    return _VSTD::forward<typename tuple_element<_Indx, _Uj>::type>(_VSTD::get<_Indx>(__uj));
2105}
2106
2107template <class _Ti, class _Uj>
2108inline _LIBCPP_INLINE_VISIBILITY
2109typename enable_if
2110<
2111    !is_bind_expression<_Ti>::value &&
2112    is_placeholder<_Ti>::value == 0 &&
2113    !__is_reference_wrapper<_Ti>::value,
2114    _Ti&
2115>::type
2116__mu(_Ti& __ti, _Uj&)
2117{
2118    return __ti;
2119}
2120
2121template <class _Ti, bool IsReferenceWrapper, bool IsBindEx, bool IsPh,
2122          class _TupleUj>
2123struct __mu_return_impl;
2124
2125template <bool _Invokable, class _Ti, class ..._Uj>
2126struct __mu_return_invokable  // false
2127{
2128    typedef __nat type;
2129};
2130
2131template <class _Ti, class ..._Uj>
2132struct __mu_return_invokable<true, _Ti, _Uj...>
2133{
2134    typedef typename __invoke_of<_Ti&, _Uj...>::type type;
2135};
2136
2137template <class _Ti, class ..._Uj>
2138struct __mu_return_impl<_Ti, false, true, false, tuple<_Uj...> >
2139    : public __mu_return_invokable<__invokable<_Ti&, _Uj...>::value, _Ti, _Uj...>
2140{
2141};
2142
2143template <class _Ti, class _TupleUj>
2144struct __mu_return_impl<_Ti, false, false, true, _TupleUj>
2145{
2146    typedef typename tuple_element<is_placeholder<_Ti>::value - 1,
2147                                   _TupleUj>::type&& type;
2148};
2149
2150template <class _Ti, class _TupleUj>
2151struct __mu_return_impl<_Ti, true, false, false, _TupleUj>
2152{
2153    typedef typename _Ti::type& type;
2154};
2155
2156template <class _Ti, class _TupleUj>
2157struct __mu_return_impl<_Ti, false, false, false, _TupleUj>
2158{
2159    typedef _Ti& type;
2160};
2161
2162template <class _Ti, class _TupleUj>
2163struct __mu_return
2164    : public __mu_return_impl<_Ti,
2165                              __is_reference_wrapper<_Ti>::value,
2166                              is_bind_expression<_Ti>::value,
2167                              0 < is_placeholder<_Ti>::value &&
2168                              is_placeholder<_Ti>::value <= tuple_size<_TupleUj>::value,
2169                              _TupleUj>
2170{
2171};
2172
2173template <class _Fp, class _BoundArgs, class _TupleUj>
2174struct __is_valid_bind_return
2175{
2176    static const bool value = false;
2177};
2178
2179template <class _Fp, class ..._BoundArgs, class _TupleUj>
2180struct __is_valid_bind_return<_Fp, tuple<_BoundArgs...>, _TupleUj>
2181{
2182    static const bool value = __invokable<_Fp,
2183                    typename __mu_return<_BoundArgs, _TupleUj>::type...>::value;
2184};
2185
2186template <class _Fp, class ..._BoundArgs, class _TupleUj>
2187struct __is_valid_bind_return<_Fp, const tuple<_BoundArgs...>, _TupleUj>
2188{
2189    static const bool value = __invokable<_Fp,
2190                    typename __mu_return<const _BoundArgs, _TupleUj>::type...>::value;
2191};
2192
2193template <class _Fp, class _BoundArgs, class _TupleUj,
2194          bool = __is_valid_bind_return<_Fp, _BoundArgs, _TupleUj>::value>
2195struct __bind_return;
2196
2197template <class _Fp, class ..._BoundArgs, class _TupleUj>
2198struct __bind_return<_Fp, tuple<_BoundArgs...>, _TupleUj, true>
2199{
2200    typedef typename __invoke_of
2201    <
2202        _Fp&,
2203        typename __mu_return
2204        <
2205            _BoundArgs,
2206            _TupleUj
2207        >::type...
2208    >::type type;
2209};
2210
2211template <class _Fp, class ..._BoundArgs, class _TupleUj>
2212struct __bind_return<_Fp, const tuple<_BoundArgs...>, _TupleUj, true>
2213{
2214    typedef typename __invoke_of
2215    <
2216        _Fp&,
2217        typename __mu_return
2218        <
2219            const _BoundArgs,
2220            _TupleUj
2221        >::type...
2222    >::type type;
2223};
2224
2225template <class _Fp, class _BoundArgs, size_t ..._Indx, class _Args>
2226inline _LIBCPP_INLINE_VISIBILITY
2227typename __bind_return<_Fp, _BoundArgs, _Args>::type
2228__apply_functor(_Fp& __f, _BoundArgs& __bound_args, __tuple_indices<_Indx...>,
2229                _Args&& __args)
2230{
2231    return _VSTD::__invoke(__f, _VSTD::__mu(_VSTD::get<_Indx>(__bound_args), __args)...);
2232}
2233
2234template<class _Fp, class ..._BoundArgs>
2235class __bind
2236    : public __weak_result_type<typename decay<_Fp>::type>
2237{
2238protected:
2239    typedef typename decay<_Fp>::type _Fd;
2240    typedef tuple<typename decay<_BoundArgs>::type...> _Td;
2241private:
2242    _Fd __f_;
2243    _Td __bound_args_;
2244
2245    typedef typename __make_tuple_indices<sizeof...(_BoundArgs)>::type __indices;
2246public:
2247    template <class _Gp, class ..._BA,
2248              class = typename enable_if
2249                               <
2250                                  is_constructible<_Fd, _Gp>::value &&
2251                                  !is_same<typename remove_reference<_Gp>::type,
2252                                           __bind>::value
2253                               >::type>
2254      _LIBCPP_INLINE_VISIBILITY
2255      explicit __bind(_Gp&& __f, _BA&& ...__bound_args)
2256        : __f_(_VSTD::forward<_Gp>(__f)),
2257          __bound_args_(_VSTD::forward<_BA>(__bound_args)...) {}
2258
2259    template <class ..._Args>
2260        _LIBCPP_INLINE_VISIBILITY
2261        typename __bind_return<_Fd, _Td, tuple<_Args&&...> >::type
2262        operator()(_Args&& ...__args)
2263        {
2264            return _VSTD::__apply_functor(__f_, __bound_args_, __indices(),
2265                                  tuple<_Args&&...>(_VSTD::forward<_Args>(__args)...));
2266        }
2267
2268    template <class ..._Args>
2269        _LIBCPP_INLINE_VISIBILITY
2270        typename __bind_return<const _Fd, const _Td, tuple<_Args&&...> >::type
2271        operator()(_Args&& ...__args) const
2272        {
2273            return _VSTD::__apply_functor(__f_, __bound_args_, __indices(),
2274                                   tuple<_Args&&...>(_VSTD::forward<_Args>(__args)...));
2275        }
2276};
2277
2278template<class _Fp, class ..._BoundArgs>
2279struct __is_bind_expression<__bind<_Fp, _BoundArgs...> > : public true_type {};
2280
2281template<class _Rp, class _Fp, class ..._BoundArgs>
2282class __bind_r
2283    : public __bind<_Fp, _BoundArgs...>
2284{
2285    typedef __bind<_Fp, _BoundArgs...> base;
2286    typedef typename base::_Fd _Fd;
2287    typedef typename base::_Td _Td;
2288public:
2289    typedef _Rp result_type;
2290
2291
2292    template <class _Gp, class ..._BA,
2293              class = typename enable_if
2294                               <
2295                                  is_constructible<_Fd, _Gp>::value &&
2296                                  !is_same<typename remove_reference<_Gp>::type,
2297                                           __bind_r>::value
2298                               >::type>
2299      _LIBCPP_INLINE_VISIBILITY
2300      explicit __bind_r(_Gp&& __f, _BA&& ...__bound_args)
2301        : base(_VSTD::forward<_Gp>(__f),
2302               _VSTD::forward<_BA>(__bound_args)...) {}
2303
2304    template <class ..._Args>
2305        _LIBCPP_INLINE_VISIBILITY
2306        typename enable_if
2307        <
2308            is_convertible<typename __bind_return<_Fd, _Td, tuple<_Args&&...> >::type,
2309                           result_type>::value || is_void<_Rp>::value,
2310            result_type
2311        >::type
2312        operator()(_Args&& ...__args)
2313        {
2314            typedef __invoke_void_return_wrapper<_Rp> _Invoker;
2315            return _Invoker::__call(static_cast<base&>(*this), _VSTD::forward<_Args>(__args)...);
2316        }
2317
2318    template <class ..._Args>
2319        _LIBCPP_INLINE_VISIBILITY
2320        typename enable_if
2321        <
2322            is_convertible<typename __bind_return<const _Fd, const _Td, tuple<_Args&&...> >::type,
2323                           result_type>::value || is_void<_Rp>::value,
2324            result_type
2325        >::type
2326        operator()(_Args&& ...__args) const
2327        {
2328            typedef __invoke_void_return_wrapper<_Rp> _Invoker;
2329            return _Invoker::__call(static_cast<base const&>(*this), _VSTD::forward<_Args>(__args)...);
2330        }
2331};
2332
2333template<class _Rp, class _Fp, class ..._BoundArgs>
2334struct __is_bind_expression<__bind_r<_Rp, _Fp, _BoundArgs...> > : public true_type {};
2335
2336template<class _Fp, class ..._BoundArgs>
2337inline _LIBCPP_INLINE_VISIBILITY
2338__bind<_Fp, _BoundArgs...>
2339bind(_Fp&& __f, _BoundArgs&&... __bound_args)
2340{
2341    typedef __bind<_Fp, _BoundArgs...> type;
2342    return type(_VSTD::forward<_Fp>(__f), _VSTD::forward<_BoundArgs>(__bound_args)...);
2343}
2344
2345template<class _Rp, class _Fp, class ..._BoundArgs>
2346inline _LIBCPP_INLINE_VISIBILITY
2347__bind_r<_Rp, _Fp, _BoundArgs...>
2348bind(_Fp&& __f, _BoundArgs&&... __bound_args)
2349{
2350    typedef __bind_r<_Rp, _Fp, _BoundArgs...> type;
2351    return type(_VSTD::forward<_Fp>(__f), _VSTD::forward<_BoundArgs>(__bound_args)...);
2352}
2353
2354#endif  // _LIBCPP_CXX03_LANG
2355
2356#if _LIBCPP_STD_VER > 14
2357
2358template <class _Fn, class ..._Args>
2359result_of_t<_Fn&&(_Args&&...)>
2360invoke(_Fn&& __f, _Args&&... __args)
2361    noexcept(noexcept(_VSTD::__invoke(_VSTD::forward<_Fn>(__f), _VSTD::forward<_Args>(__args)...)))
2362{
2363    return _VSTD::__invoke(_VSTD::forward<_Fn>(__f), _VSTD::forward<_Args>(__args)...);
2364}
2365
2366template <class _DecayFunc>
2367class _LIBCPP_TEMPLATE_VIS __not_fn_imp {
2368  _DecayFunc __fd;
2369
2370public:
2371    __not_fn_imp() = delete;
2372
2373    template <class ..._Args>
2374    _LIBCPP_INLINE_VISIBILITY
2375    auto operator()(_Args&& ...__args) &
2376            noexcept(noexcept(!_VSTD::invoke(__fd, _VSTD::forward<_Args>(__args)...)))
2377        -> decltype(          !_VSTD::invoke(__fd, _VSTD::forward<_Args>(__args)...))
2378        { return              !_VSTD::invoke(__fd, _VSTD::forward<_Args>(__args)...); }
2379
2380    template <class ..._Args>
2381    _LIBCPP_INLINE_VISIBILITY
2382    auto operator()(_Args&& ...__args) &&
2383            noexcept(noexcept(!_VSTD::invoke(_VSTD::move(__fd), _VSTD::forward<_Args>(__args)...)))
2384        -> decltype(          !_VSTD::invoke(_VSTD::move(__fd), _VSTD::forward<_Args>(__args)...))
2385        { return              !_VSTD::invoke(_VSTD::move(__fd), _VSTD::forward<_Args>(__args)...); }
2386
2387    template <class ..._Args>
2388    _LIBCPP_INLINE_VISIBILITY
2389    auto operator()(_Args&& ...__args) const&
2390            noexcept(noexcept(!_VSTD::invoke(__fd, _VSTD::forward<_Args>(__args)...)))
2391        -> decltype(          !_VSTD::invoke(__fd, _VSTD::forward<_Args>(__args)...))
2392        { return              !_VSTD::invoke(__fd, _VSTD::forward<_Args>(__args)...); }
2393
2394
2395    template <class ..._Args>
2396    _LIBCPP_INLINE_VISIBILITY
2397    auto operator()(_Args&& ...__args) const&&
2398            noexcept(noexcept(!_VSTD::invoke(_VSTD::move(__fd), _VSTD::forward<_Args>(__args)...)))
2399        -> decltype(          !_VSTD::invoke(_VSTD::move(__fd), _VSTD::forward<_Args>(__args)...))
2400        { return              !_VSTD::invoke(_VSTD::move(__fd), _VSTD::forward<_Args>(__args)...); }
2401
2402private:
2403    template <class _RawFunc,
2404              class = enable_if_t<!is_same<decay_t<_RawFunc>, __not_fn_imp>::value>>
2405    _LIBCPP_INLINE_VISIBILITY
2406    explicit __not_fn_imp(_RawFunc&& __rf)
2407        : __fd(_VSTD::forward<_RawFunc>(__rf)) {}
2408
2409    template <class _RawFunc>
2410    friend inline _LIBCPP_INLINE_VISIBILITY
2411    __not_fn_imp<decay_t<_RawFunc>> not_fn(_RawFunc&&);
2412};
2413
2414template <class _RawFunc>
2415inline _LIBCPP_INLINE_VISIBILITY
2416__not_fn_imp<decay_t<_RawFunc>> not_fn(_RawFunc&& __fn) {
2417    return __not_fn_imp<decay_t<_RawFunc>>(_VSTD::forward<_RawFunc>(__fn));
2418}
2419
2420#endif
2421
2422// struct hash<T*> in <memory>
2423
2424template <class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
2425pair<_ForwardIterator1, _ForwardIterator1> _LIBCPP_CONSTEXPR_AFTER_CXX11
2426__search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
2427         _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred,
2428         forward_iterator_tag, forward_iterator_tag)
2429{
2430    if (__first2 == __last2)
2431        return make_pair(__first1, __first1);  // Everything matches an empty sequence
2432    while (true)
2433    {
2434        // Find first element in sequence 1 that matchs *__first2, with a mininum of loop checks
2435        while (true)
2436        {
2437            if (__first1 == __last1)  // return __last1 if no element matches *__first2
2438                return make_pair(__last1, __last1);
2439            if (__pred(*__first1, *__first2))
2440                break;
2441            ++__first1;
2442        }
2443        // *__first1 matches *__first2, now match elements after here
2444        _ForwardIterator1 __m1 = __first1;
2445        _ForwardIterator2 __m2 = __first2;
2446        while (true)
2447        {
2448            if (++__m2 == __last2)  // If pattern exhausted, __first1 is the answer (works for 1 element pattern)
2449                return make_pair(__first1, __m1);
2450            if (++__m1 == __last1)  // Otherwise if source exhaused, pattern not found
2451                return make_pair(__last1, __last1);
2452            if (!__pred(*__m1, *__m2))  // if there is a mismatch, restart with a new __first1
2453            {
2454                ++__first1;
2455                break;
2456            }  // else there is a match, check next elements
2457        }
2458    }
2459}
2460
2461template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
2462_LIBCPP_CONSTEXPR_AFTER_CXX11
2463pair<_RandomAccessIterator1, _RandomAccessIterator1>
2464__search(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
2465         _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred,
2466           random_access_iterator_tag, random_access_iterator_tag)
2467{
2468    typedef typename iterator_traits<_RandomAccessIterator1>::difference_type _D1;
2469    typedef typename iterator_traits<_RandomAccessIterator2>::difference_type _D2;
2470    // Take advantage of knowing source and pattern lengths.  Stop short when source is smaller than pattern
2471    const _D2 __len2 = __last2 - __first2;
2472    if (__len2 == 0)
2473        return make_pair(__first1, __first1);
2474    const _D1 __len1 = __last1 - __first1;
2475    if (__len1 < __len2)
2476        return make_pair(__last1, __last1);
2477    const _RandomAccessIterator1 __s = __last1 - (__len2 - 1);  // Start of pattern match can't go beyond here
2478
2479    while (true)
2480    {
2481        while (true)
2482        {
2483            if (__first1 == __s)
2484                return make_pair(__last1, __last1);
2485            if (__pred(*__first1, *__first2))
2486                break;
2487            ++__first1;
2488        }
2489
2490        _RandomAccessIterator1 __m1 = __first1;
2491        _RandomAccessIterator2 __m2 = __first2;
2492         while (true)
2493         {
2494             if (++__m2 == __last2)
2495                 return make_pair(__first1, __first1 + __len2);
2496             ++__m1;          // no need to check range on __m1 because __s guarantees we have enough source
2497             if (!__pred(*__m1, *__m2))
2498             {
2499                 ++__first1;
2500                 break;
2501             }
2502         }
2503    }
2504}
2505
2506#if _LIBCPP_STD_VER > 14
2507
2508// default searcher
2509template<class _ForwardIterator, class _BinaryPredicate = equal_to<>>
2510class _LIBCPP_TYPE_VIS default_searcher {
2511public:
2512    _LIBCPP_INLINE_VISIBILITY
2513    default_searcher(_ForwardIterator __f, _ForwardIterator __l,
2514                       _BinaryPredicate __p = _BinaryPredicate())
2515        : __first_(__f), __last_(__l), __pred_(__p) {}
2516
2517    template <typename _ForwardIterator2>
2518    _LIBCPP_INLINE_VISIBILITY
2519    pair<_ForwardIterator2, _ForwardIterator2>
2520    operator () (_ForwardIterator2 __f, _ForwardIterator2 __l) const
2521    {
2522        return _VSTD::__search(__f, __l, __first_, __last_, __pred_,
2523            typename _VSTD::iterator_traits<_ForwardIterator>::iterator_category(),
2524            typename _VSTD::iterator_traits<_ForwardIterator2>::iterator_category());
2525    }
2526
2527private:
2528    _ForwardIterator __first_;
2529    _ForwardIterator __last_;
2530    _BinaryPredicate __pred_;
2531    };
2532
2533#endif // _LIBCPP_STD_VER > 14
2534
2535#if _LIBCPP_STD_VER > 17
2536template <class _Tp>
2537using unwrap_reference_t = typename unwrap_reference<_Tp>::type;
2538
2539template <class _Tp>
2540using unwrap_ref_decay_t = typename unwrap_ref_decay<_Tp>::type;
2541#endif // > C++17
2542
2543_LIBCPP_END_NAMESPACE_STD
2544
2545#endif  // _LIBCPP_FUNCTIONAL
2546