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