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