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