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