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