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