xref: /freebsd/contrib/llvm-project/libcxx/include/functional (revision a96ef4501919d7ac08e94e98dc34b0bdd744802b)
1// -*- C++ -*-
2//===------------------------ functional ----------------------------------===//
3//
4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_FUNCTIONAL
11#define _LIBCPP_FUNCTIONAL
12
13/*
14    functional synopsis
15
16namespace std
17{
18
19template <class Arg, class Result>
20struct unary_function
21{
22    typedef Arg    argument_type;
23    typedef Result result_type;
24};
25
26template <class Arg1, class Arg2, class Result>
27struct binary_function
28{
29    typedef Arg1   first_argument_type;
30    typedef Arg2   second_argument_type;
31    typedef Result result_type;
32};
33
34template <class T>
35class reference_wrapper
36    : public unary_function<T1, R> // if wrapping a unary functor
37    : public binary_function<T1, T2, R> // if wraping a binary functor
38{
39public:
40    // types
41    typedef T type;
42    typedef see below result_type; // Not always defined
43
44    // construct/copy/destroy
45    reference_wrapper(T&) noexcept;
46    reference_wrapper(T&&) = delete; // do not bind to temps
47    reference_wrapper(const reference_wrapper<T>& x) noexcept;
48
49    // assignment
50    reference_wrapper& operator=(const reference_wrapper<T>& x) noexcept;
51
52    // access
53    operator T& () const noexcept;
54    T& get() const noexcept;
55
56    // invoke
57    template <class... ArgTypes>
58      typename result_of<T&(ArgTypes&&...)>::type
59          operator() (ArgTypes&&...) const;
60};
61
62template <class T> reference_wrapper<T> ref(T& t) noexcept;
63template <class T> void ref(const T&& t) = delete;
64template <class T> reference_wrapper<T> ref(reference_wrapper<T>t) noexcept;
65
66template <class T> reference_wrapper<const T> cref(const T& t) noexcept;
67template <class T> void cref(const T&& t) = delete;
68template <class T> reference_wrapper<const T> cref(reference_wrapper<T> t) noexcept;
69
70template <class T> struct unwrap_reference;                                       // since C++20
71template <class T> struct unwrap_ref_decay : unwrap_reference<decay_t<T>> { };    // since C++20
72template <class T> using unwrap_reference_t = typename unwrap_reference<T>::type; // since C++20
73template <class T> using unwrap_ref_decay_t = typename unwrap_ref_decay<T>::type; // since C++20
74
75template <class T> // <class T=void> in C++14
76struct plus : binary_function<T, T, T>
77{
78    T operator()(const T& x, const T& y) const;
79};
80
81template <class T> // <class T=void> in C++14
82struct minus : binary_function<T, T, T>
83{
84    T operator()(const T& x, const T& y) const;
85};
86
87template <class T> // <class T=void> in C++14
88struct multiplies : binary_function<T, T, T>
89{
90    T operator()(const T& x, const T& y) const;
91};
92
93template <class T> // <class T=void> in C++14
94struct divides : binary_function<T, T, T>
95{
96    T operator()(const T& x, const T& y) const;
97};
98
99template <class T> // <class T=void> in C++14
100struct modulus : binary_function<T, T, T>
101{
102    T operator()(const T& x, const T& y) const;
103};
104
105template <class T> // <class T=void> in C++14
106struct negate : unary_function<T, T>
107{
108    T operator()(const T& x) const;
109};
110
111template <class T> // <class T=void> in C++14
112struct equal_to : binary_function<T, T, bool>
113{
114    bool operator()(const T& x, const T& y) const;
115};
116
117template <class T> // <class T=void> in C++14
118struct not_equal_to : binary_function<T, T, bool>
119{
120    bool operator()(const T& x, const T& y) const;
121};
122
123template <class T> // <class T=void> in C++14
124struct greater : binary_function<T, T, bool>
125{
126    bool operator()(const T& x, const T& y) const;
127};
128
129template <class T> // <class T=void> in C++14
130struct less : binary_function<T, T, bool>
131{
132    bool operator()(const T& x, const T& y) const;
133};
134
135template <class T> // <class T=void> in C++14
136struct greater_equal : binary_function<T, T, bool>
137{
138    bool operator()(const T& x, const T& y) const;
139};
140
141template <class T> // <class T=void> in C++14
142struct less_equal : binary_function<T, T, bool>
143{
144    bool operator()(const T& x, const T& y) const;
145};
146
147template <class T> // <class T=void> in C++14
148struct logical_and : binary_function<T, T, bool>
149{
150    bool operator()(const T& x, const T& y) const;
151};
152
153template <class T> // <class T=void> in C++14
154struct logical_or : binary_function<T, T, bool>
155{
156    bool operator()(const T& x, const T& y) const;
157};
158
159template <class T> // <class T=void> in C++14
160struct logical_not : unary_function<T, bool>
161{
162    bool operator()(const T& x) const;
163};
164
165template <class T> // <class T=void> in C++14
166struct bit_and : unary_function<T, bool>
167{
168    bool operator()(const T& x, const T& y) const;
169};
170
171template <class T> // <class T=void> in C++14
172struct bit_or : unary_function<T, bool>
173{
174    bool operator()(const T& x, const T& y) const;
175};
176
177template <class T> // <class T=void> in C++14
178struct bit_xor : unary_function<T, bool>
179{
180    bool operator()(const T& x, const T& y) const;
181};
182
183template <class T=void> // C++14
184struct bit_xor : unary_function<T, bool>
185{
186    bool operator()(const T& x) const;
187};
188
189template <class Predicate>
190class unary_negate // deprecated in C++17
191    : public unary_function<typename Predicate::argument_type, bool>
192{
193public:
194    explicit unary_negate(const Predicate& pred);
195    bool operator()(const typename Predicate::argument_type& x) const;
196};
197
198template <class Predicate> // deprecated in C++17
199unary_negate<Predicate> not1(const Predicate& pred);
200
201template <class Predicate>
202class binary_negate // deprecated in C++17
203    : public binary_function<typename Predicate::first_argument_type,
204                             typename Predicate::second_argument_type,
205                             bool>
206{
207public:
208    explicit binary_negate(const Predicate& pred);
209    bool operator()(const typename Predicate::first_argument_type& x,
210                    const typename Predicate::second_argument_type& y) const;
211};
212
213template <class Predicate> // deprecated in C++17
214binary_negate<Predicate> not2(const Predicate& pred);
215
216template <class F> unspecified not_fn(F&& f); // C++17
217
218template<class T> struct is_bind_expression;
219template<class T> struct is_placeholder;
220
221    // See C++14 20.9.9, Function object binders
222template <class T> inline constexpr bool is_bind_expression_v
223  = is_bind_expression<T>::value; // C++17
224template <class T> inline constexpr int is_placeholder_v
225  = is_placeholder<T>::value; // C++17
226
227
228template<class Fn, class... BoundArgs>
229  unspecified bind(Fn&&, BoundArgs&&...);
230template<class R, class Fn, class... BoundArgs>
231  unspecified bind(Fn&&, BoundArgs&&...);
232
233template<class F, class... Args>
234 invoke_result_t<F, Args...> invoke(F&& f, Args&&... args) // C++17
235    noexcept(is_nothrow_invocable_v<F, Args...>);
236
237namespace placeholders {
238  // M is the implementation-defined number of placeholders
239  extern unspecified _1;
240  extern unspecified _2;
241  .
242  .
243  .
244  extern unspecified _Mp;
245}
246
247template <class Operation>
248class binder1st     // deprecated in C++11, removed in C++17
249    : public unary_function<typename Operation::second_argument_type,
250                            typename Operation::result_type>
251{
252protected:
253    Operation                               op;
254    typename Operation::first_argument_type value;
255public:
256    binder1st(const Operation& x, const typename Operation::first_argument_type y);
257    typename Operation::result_type operator()(      typename Operation::second_argument_type& x) const;
258    typename Operation::result_type operator()(const typename Operation::second_argument_type& x) const;
259};
260
261template <class Operation, class T>
262binder1st<Operation> bind1st(const Operation& op, const T& x);  // deprecated in C++11, removed in C++17
263
264template <class Operation>
265class binder2nd     // deprecated in C++11, removed in C++17
266    : public unary_function<typename Operation::first_argument_type,
267                            typename Operation::result_type>
268{
269protected:
270    Operation                                op;
271    typename Operation::second_argument_type value;
272public:
273    binder2nd(const Operation& x, const typename Operation::second_argument_type y);
274    typename Operation::result_type operator()(      typename Operation::first_argument_type& x) const;
275    typename Operation::result_type operator()(const typename Operation::first_argument_type& x) const;
276};
277
278template <class Operation, class T>
279binder2nd<Operation> bind2nd(const Operation& op, const T& x);  // deprecated in C++11, removed in C++17
280
281template <class Arg, class Result>      // deprecated in C++11, removed in C++17
282class pointer_to_unary_function : public unary_function<Arg, Result>
283{
284public:
285    explicit pointer_to_unary_function(Result (*f)(Arg));
286    Result operator()(Arg x) const;
287};
288
289template <class Arg, class Result>
290pointer_to_unary_function<Arg,Result> ptr_fun(Result (*f)(Arg));      // deprecated in C++11, removed in C++17
291
292template <class Arg1, class Arg2, class Result>      // deprecated in C++11, removed in C++17
293class pointer_to_binary_function : public binary_function<Arg1, Arg2, Result>
294{
295public:
296    explicit pointer_to_binary_function(Result (*f)(Arg1, Arg2));
297    Result operator()(Arg1 x, Arg2 y) const;
298};
299
300template <class Arg1, class Arg2, class Result>
301pointer_to_binary_function<Arg1,Arg2,Result> ptr_fun(Result (*f)(Arg1,Arg2));      // deprecated in C++11, removed in C++17
302
303template<class S, class T>      // deprecated in C++11, removed in C++17
304class mem_fun_t : public unary_function<T*, S>
305{
306public:
307    explicit mem_fun_t(S (T::*p)());
308    S operator()(T* p) const;
309};
310
311template<class S, class T, class A>
312class mem_fun1_t : public binary_function<T*, A, S>      // deprecated in C++11, removed in C++17
313{
314public:
315    explicit mem_fun1_t(S (T::*p)(A));
316    S operator()(T* p, A x) const;
317};
318
319template<class S, class T>          mem_fun_t<S,T>    mem_fun(S (T::*f)());      // deprecated in C++11, removed in C++17
320template<class S, class T, class A> mem_fun1_t<S,T,A> mem_fun(S (T::*f)(A));     // deprecated in C++11, removed in C++17
321
322template<class S, class T>
323class mem_fun_ref_t : public unary_function<T, S>      // deprecated in C++11, removed in C++17
324{
325public:
326    explicit mem_fun_ref_t(S (T::*p)());
327    S operator()(T& p) const;
328};
329
330template<class S, class T, class A>
331class mem_fun1_ref_t : public binary_function<T, A, S>      // deprecated in C++11, removed in C++17
332{
333public:
334    explicit mem_fun1_ref_t(S (T::*p)(A));
335    S operator()(T& p, A x) const;
336};
337
338template<class S, class T>          mem_fun_ref_t<S,T>    mem_fun_ref(S (T::*f)());      // deprecated in C++11, removed in C++17
339template<class S, class T, class A> mem_fun1_ref_t<S,T,A> mem_fun_ref(S (T::*f)(A));     // deprecated in C++11, removed in C++17
340
341template <class S, class T>
342class const_mem_fun_t : public unary_function<const T*, S>      // deprecated in C++11, removed in C++17
343{
344public:
345    explicit const_mem_fun_t(S (T::*p)() const);
346    S operator()(const T* p) const;
347};
348
349template <class S, class T, class A>
350class const_mem_fun1_t : public binary_function<const T*, A, S>      // deprecated in C++11, removed in C++17
351{
352public:
353    explicit const_mem_fun1_t(S (T::*p)(A) const);
354    S operator()(const T* p, A x) const;
355};
356
357template <class S, class T>          const_mem_fun_t<S,T>    mem_fun(S (T::*f)() const);      // deprecated in C++11, removed in C++17
358template <class S, class T, class A> const_mem_fun1_t<S,T,A> mem_fun(S (T::*f)(A) const);     // deprecated in C++11, removed in C++17
359
360template <class S, class T>
361class const_mem_fun_ref_t : public unary_function<T, S>      // deprecated in C++11, removed in C++17
362{
363public:
364    explicit const_mem_fun_ref_t(S (T::*p)() const);
365    S operator()(const T& p) const;
366};
367
368template <class S, class T, class A>
369class const_mem_fun1_ref_t : public binary_function<T, A, S>      // deprecated in C++11, removed in C++17
370{
371public:
372    explicit const_mem_fun1_ref_t(S (T::*p)(A) const);
373    S operator()(const T& p, A x) const;
374};
375
376template <class S, class T>          const_mem_fun_ref_t<S,T>    mem_fun_ref(S (T::*f)() const);   // deprecated in C++11, removed in C++17
377template <class S, class T, class A> const_mem_fun1_ref_t<S,T,A> mem_fun_ref(S (T::*f)(A) const);  // deprecated in C++11, removed in C++17
378
379template<class R, class T> unspecified mem_fn(R T::*);
380
381class bad_function_call
382    : public exception
383{
384};
385
386template<class> class function; // undefined
387
388template<class R, class... ArgTypes>
389class function<R(ArgTypes...)>
390  : public unary_function<T1, R>      // iff sizeof...(ArgTypes) == 1 and
391                                      // ArgTypes contains T1
392  : public binary_function<T1, T2, R> // iff sizeof...(ArgTypes) == 2 and
393                                      // ArgTypes contains T1 and T2
394{
395public:
396    typedef R result_type;
397
398    // construct/copy/destroy:
399    function() noexcept;
400    function(nullptr_t) noexcept;
401    function(const function&);
402    function(function&&) noexcept;
403    template<class F>
404      function(F);
405    template<Allocator Alloc>
406      function(allocator_arg_t, const Alloc&) noexcept;            // removed in C++17
407    template<Allocator Alloc>
408      function(allocator_arg_t, const Alloc&, nullptr_t) noexcept; // removed in C++17
409    template<Allocator Alloc>
410      function(allocator_arg_t, const Alloc&, const function&);    // removed in C++17
411    template<Allocator Alloc>
412      function(allocator_arg_t, const Alloc&, function&&);         // removed in C++17
413    template<class F, Allocator Alloc>
414      function(allocator_arg_t, const Alloc&, F);                  // removed in C++17
415
416    function& operator=(const function&);
417    function& operator=(function&&) noexcept;
418    function& operator=(nullptr_t) noexcept;
419    template<class F>
420      function& operator=(F&&);
421    template<class F>
422      function& operator=(reference_wrapper<F>) noexcept;
423
424    ~function();
425
426    // function modifiers:
427    void swap(function&) noexcept;
428    template<class F, class Alloc>
429      void assign(F&&, const Alloc&);                 // Removed in C++17
430
431    // function capacity:
432    explicit operator bool() const noexcept;
433
434    // function invocation:
435    R operator()(ArgTypes...) const;
436
437    // function target access:
438    const std::type_info& target_type() const noexcept;
439    template <typename T>       T* target() noexcept;
440    template <typename T> const T* target() const noexcept;
441};
442
443// Deduction guides
444template<class R, class ...Args>
445function(R(*)(Args...)) -> function<R(Args...)>; // since C++17
446
447template<class F>
448function(F) -> function<see-below>; // since C++17
449
450// Null pointer comparisons:
451template <class R, class ... ArgTypes>
452  bool operator==(const function<R(ArgTypes...)>&, nullptr_t) noexcept;
453
454template <class R, class ... ArgTypes>
455  bool operator==(nullptr_t, const function<R(ArgTypes...)>&) noexcept;
456
457template <class R, class ... ArgTypes>
458  bool operator!=(const function<R(ArgTypes...)>&, nullptr_t) noexcept;
459
460template <class  R, class ... ArgTypes>
461  bool operator!=(nullptr_t, const function<R(ArgTypes...)>&) noexcept;
462
463// specialized algorithms:
464template <class  R, class ... ArgTypes>
465  void swap(function<R(ArgTypes...)>&, function<R(ArgTypes...)>&) noexcept;
466
467template <class T> struct hash;
468
469template <> struct hash<bool>;
470template <> struct hash<char>;
471template <> struct hash<signed char>;
472template <> struct hash<unsigned char>;
473template <> struct hash<char16_t>;
474template <> struct hash<char32_t>;
475template <> struct hash<wchar_t>;
476template <> struct hash<short>;
477template <> struct hash<unsigned short>;
478template <> struct hash<int>;
479template <> struct hash<unsigned int>;
480template <> struct hash<long>;
481template <> struct hash<long long>;
482template <> struct hash<unsigned long>;
483template <> struct hash<unsigned long long>;
484
485template <> struct hash<float>;
486template <> struct hash<double>;
487template <> struct hash<long double>;
488
489template<class T> struct hash<T*>;
490template <> struct hash<nullptr_t>;  // C++17
491
492}  // std
493
494POLICY:  For non-variadic implementations, the number of arguments is limited
495         to 3.  It is hoped that the need for non-variadic implementations
496         will be minimal.
497
498*/
499
500#include <__config>
501#include <type_traits>
502#include <typeinfo>
503#include <exception>
504#include <memory>
505#include <tuple>
506#include <utility>
507#include <version>
508
509#include <__functional_base>
510
511#if defined(_LIBCPP_HAS_BLOCKS_RUNTIME) && !defined(_LIBCPP_HAS_OBJC_ARC)
512#include <Block.h>
513#endif
514
515#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
516#pragma GCC system_header
517#endif
518
519_LIBCPP_BEGIN_NAMESPACE_STD
520
521#if _LIBCPP_STD_VER > 11
522template <class _Tp = void>
523#else
524template <class _Tp>
525#endif
526struct _LIBCPP_TEMPLATE_VIS plus : binary_function<_Tp, _Tp, _Tp>
527{
528    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
529    _Tp operator()(const _Tp& __x, const _Tp& __y) const
530        {return __x + __y;}
531};
532
533#if _LIBCPP_STD_VER > 11
534template <>
535struct _LIBCPP_TEMPLATE_VIS plus<void>
536{
537    template <class _T1, class _T2>
538    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
539    auto operator()(_T1&& __t, _T2&& __u) const
540    _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u)))
541    -> decltype        (_VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u))
542        { return        _VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u); }
543    typedef void is_transparent;
544};
545#endif
546
547
548#if _LIBCPP_STD_VER > 11
549template <class _Tp = void>
550#else
551template <class _Tp>
552#endif
553struct _LIBCPP_TEMPLATE_VIS minus : binary_function<_Tp, _Tp, _Tp>
554{
555    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
556    _Tp operator()(const _Tp& __x, const _Tp& __y) const
557        {return __x - __y;}
558};
559
560#if _LIBCPP_STD_VER > 11
561template <>
562struct _LIBCPP_TEMPLATE_VIS minus<void>
563{
564    template <class _T1, class _T2>
565    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
566    auto operator()(_T1&& __t, _T2&& __u) const
567    _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) - _VSTD::forward<_T2>(__u)))
568    -> decltype        (_VSTD::forward<_T1>(__t) - _VSTD::forward<_T2>(__u))
569        { return        _VSTD::forward<_T1>(__t) - _VSTD::forward<_T2>(__u); }
570    typedef void is_transparent;
571};
572#endif
573
574
575#if _LIBCPP_STD_VER > 11
576template <class _Tp = void>
577#else
578template <class _Tp>
579#endif
580struct _LIBCPP_TEMPLATE_VIS multiplies : binary_function<_Tp, _Tp, _Tp>
581{
582    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
583    _Tp operator()(const _Tp& __x, const _Tp& __y) const
584        {return __x * __y;}
585};
586
587#if _LIBCPP_STD_VER > 11
588template <>
589struct _LIBCPP_TEMPLATE_VIS multiplies<void>
590{
591    template <class _T1, class _T2>
592    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
593    auto operator()(_T1&& __t, _T2&& __u) const
594    _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) * _VSTD::forward<_T2>(__u)))
595    -> decltype        (_VSTD::forward<_T1>(__t) * _VSTD::forward<_T2>(__u))
596        { return        _VSTD::forward<_T1>(__t) * _VSTD::forward<_T2>(__u); }
597    typedef void is_transparent;
598};
599#endif
600
601
602#if _LIBCPP_STD_VER > 11
603template <class _Tp = void>
604#else
605template <class _Tp>
606#endif
607struct _LIBCPP_TEMPLATE_VIS divides : binary_function<_Tp, _Tp, _Tp>
608{
609    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
610    _Tp operator()(const _Tp& __x, const _Tp& __y) const
611        {return __x / __y;}
612};
613
614#if _LIBCPP_STD_VER > 11
615template <>
616struct _LIBCPP_TEMPLATE_VIS divides<void>
617{
618    template <class _T1, class _T2>
619    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
620    auto operator()(_T1&& __t, _T2&& __u) const
621    _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) / _VSTD::forward<_T2>(__u)))
622    -> decltype        (_VSTD::forward<_T1>(__t) / _VSTD::forward<_T2>(__u))
623        { return        _VSTD::forward<_T1>(__t) / _VSTD::forward<_T2>(__u); }
624    typedef void is_transparent;
625};
626#endif
627
628
629#if _LIBCPP_STD_VER > 11
630template <class _Tp = void>
631#else
632template <class _Tp>
633#endif
634struct _LIBCPP_TEMPLATE_VIS modulus : binary_function<_Tp, _Tp, _Tp>
635{
636    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
637    _Tp operator()(const _Tp& __x, const _Tp& __y) const
638        {return __x % __y;}
639};
640
641#if _LIBCPP_STD_VER > 11
642template <>
643struct _LIBCPP_TEMPLATE_VIS modulus<void>
644{
645    template <class _T1, class _T2>
646    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
647    auto operator()(_T1&& __t, _T2&& __u) const
648    _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) % _VSTD::forward<_T2>(__u)))
649    -> decltype        (_VSTD::forward<_T1>(__t) % _VSTD::forward<_T2>(__u))
650        { return        _VSTD::forward<_T1>(__t) % _VSTD::forward<_T2>(__u); }
651    typedef void is_transparent;
652};
653#endif
654
655
656#if _LIBCPP_STD_VER > 11
657template <class _Tp = void>
658#else
659template <class _Tp>
660#endif
661struct _LIBCPP_TEMPLATE_VIS negate : unary_function<_Tp, _Tp>
662{
663    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
664    _Tp operator()(const _Tp& __x) const
665        {return -__x;}
666};
667
668#if _LIBCPP_STD_VER > 11
669template <>
670struct _LIBCPP_TEMPLATE_VIS negate<void>
671{
672    template <class _Tp>
673    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
674    auto operator()(_Tp&& __x) const
675    _NOEXCEPT_(noexcept(- _VSTD::forward<_Tp>(__x)))
676    -> decltype        (- _VSTD::forward<_Tp>(__x))
677        { return        - _VSTD::forward<_Tp>(__x); }
678    typedef void is_transparent;
679};
680#endif
681
682
683#if _LIBCPP_STD_VER > 11
684template <class _Tp = void>
685#else
686template <class _Tp>
687#endif
688struct _LIBCPP_TEMPLATE_VIS equal_to : binary_function<_Tp, _Tp, bool>
689{
690    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
691    bool operator()(const _Tp& __x, const _Tp& __y) const
692        {return __x == __y;}
693};
694
695#if _LIBCPP_STD_VER > 11
696template <>
697struct _LIBCPP_TEMPLATE_VIS equal_to<void>
698{
699    template <class _T1, class _T2>
700    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
701    auto operator()(_T1&& __t, _T2&& __u) const
702    _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) == _VSTD::forward<_T2>(__u)))
703    -> decltype        (_VSTD::forward<_T1>(__t) == _VSTD::forward<_T2>(__u))
704        { return        _VSTD::forward<_T1>(__t) == _VSTD::forward<_T2>(__u); }
705    typedef void is_transparent;
706};
707#endif
708
709
710#if _LIBCPP_STD_VER > 11
711template <class _Tp = void>
712#else
713template <class _Tp>
714#endif
715struct _LIBCPP_TEMPLATE_VIS not_equal_to : binary_function<_Tp, _Tp, bool>
716{
717    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
718    bool operator()(const _Tp& __x, const _Tp& __y) const
719        {return __x != __y;}
720};
721
722#if _LIBCPP_STD_VER > 11
723template <>
724struct _LIBCPP_TEMPLATE_VIS not_equal_to<void>
725{
726    template <class _T1, class _T2>
727    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
728    auto operator()(_T1&& __t, _T2&& __u) const
729    _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) != _VSTD::forward<_T2>(__u)))
730    -> decltype        (_VSTD::forward<_T1>(__t) != _VSTD::forward<_T2>(__u))
731        { return        _VSTD::forward<_T1>(__t) != _VSTD::forward<_T2>(__u); }
732    typedef void is_transparent;
733};
734#endif
735
736
737#if _LIBCPP_STD_VER > 11
738template <class _Tp = void>
739#else
740template <class _Tp>
741#endif
742struct _LIBCPP_TEMPLATE_VIS greater : binary_function<_Tp, _Tp, bool>
743{
744    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
745    bool operator()(const _Tp& __x, const _Tp& __y) const
746        {return __x > __y;}
747};
748
749#if _LIBCPP_STD_VER > 11
750template <>
751struct _LIBCPP_TEMPLATE_VIS greater<void>
752{
753    template <class _T1, class _T2>
754    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
755    auto operator()(_T1&& __t, _T2&& __u) const
756    _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) > _VSTD::forward<_T2>(__u)))
757    -> decltype        (_VSTD::forward<_T1>(__t) > _VSTD::forward<_T2>(__u))
758        { return        _VSTD::forward<_T1>(__t) > _VSTD::forward<_T2>(__u); }
759    typedef void is_transparent;
760};
761#endif
762
763
764// less in <__functional_base>
765
766#if _LIBCPP_STD_VER > 11
767template <class _Tp = void>
768#else
769template <class _Tp>
770#endif
771struct _LIBCPP_TEMPLATE_VIS greater_equal : binary_function<_Tp, _Tp, bool>
772{
773    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
774    bool operator()(const _Tp& __x, const _Tp& __y) const
775        {return __x >= __y;}
776};
777
778#if _LIBCPP_STD_VER > 11
779template <>
780struct _LIBCPP_TEMPLATE_VIS greater_equal<void>
781{
782    template <class _T1, class _T2>
783    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
784    auto operator()(_T1&& __t, _T2&& __u) const
785    _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) >= _VSTD::forward<_T2>(__u)))
786    -> decltype        (_VSTD::forward<_T1>(__t) >= _VSTD::forward<_T2>(__u))
787        { return        _VSTD::forward<_T1>(__t) >= _VSTD::forward<_T2>(__u); }
788    typedef void is_transparent;
789};
790#endif
791
792
793#if _LIBCPP_STD_VER > 11
794template <class _Tp = void>
795#else
796template <class _Tp>
797#endif
798struct _LIBCPP_TEMPLATE_VIS less_equal : binary_function<_Tp, _Tp, bool>
799{
800    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
801    bool operator()(const _Tp& __x, const _Tp& __y) const
802        {return __x <= __y;}
803};
804
805#if _LIBCPP_STD_VER > 11
806template <>
807struct _LIBCPP_TEMPLATE_VIS less_equal<void>
808{
809    template <class _T1, class _T2>
810    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
811    auto operator()(_T1&& __t, _T2&& __u) const
812    _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) <= _VSTD::forward<_T2>(__u)))
813    -> decltype        (_VSTD::forward<_T1>(__t) <= _VSTD::forward<_T2>(__u))
814        { return        _VSTD::forward<_T1>(__t) <= _VSTD::forward<_T2>(__u); }
815    typedef void is_transparent;
816};
817#endif
818
819
820#if _LIBCPP_STD_VER > 11
821template <class _Tp = void>
822#else
823template <class _Tp>
824#endif
825struct _LIBCPP_TEMPLATE_VIS logical_and : binary_function<_Tp, _Tp, bool>
826{
827    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
828    bool operator()(const _Tp& __x, const _Tp& __y) const
829        {return __x && __y;}
830};
831
832#if _LIBCPP_STD_VER > 11
833template <>
834struct _LIBCPP_TEMPLATE_VIS logical_and<void>
835{
836    template <class _T1, class _T2>
837    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
838    auto operator()(_T1&& __t, _T2&& __u) const
839    _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) && _VSTD::forward<_T2>(__u)))
840    -> decltype        (_VSTD::forward<_T1>(__t) && _VSTD::forward<_T2>(__u))
841        { return        _VSTD::forward<_T1>(__t) && _VSTD::forward<_T2>(__u); }
842    typedef void is_transparent;
843};
844#endif
845
846
847#if _LIBCPP_STD_VER > 11
848template <class _Tp = void>
849#else
850template <class _Tp>
851#endif
852struct _LIBCPP_TEMPLATE_VIS logical_or : binary_function<_Tp, _Tp, bool>
853{
854    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
855    bool operator()(const _Tp& __x, const _Tp& __y) const
856        {return __x || __y;}
857};
858
859#if _LIBCPP_STD_VER > 11
860template <>
861struct _LIBCPP_TEMPLATE_VIS logical_or<void>
862{
863    template <class _T1, class _T2>
864    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
865    auto operator()(_T1&& __t, _T2&& __u) const
866    _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) || _VSTD::forward<_T2>(__u)))
867    -> decltype        (_VSTD::forward<_T1>(__t) || _VSTD::forward<_T2>(__u))
868        { return        _VSTD::forward<_T1>(__t) || _VSTD::forward<_T2>(__u); }
869    typedef void is_transparent;
870};
871#endif
872
873
874#if _LIBCPP_STD_VER > 11
875template <class _Tp = void>
876#else
877template <class _Tp>
878#endif
879struct _LIBCPP_TEMPLATE_VIS logical_not : unary_function<_Tp, bool>
880{
881    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
882    bool operator()(const _Tp& __x) const
883        {return !__x;}
884};
885
886#if _LIBCPP_STD_VER > 11
887template <>
888struct _LIBCPP_TEMPLATE_VIS logical_not<void>
889{
890    template <class _Tp>
891    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
892    auto operator()(_Tp&& __x) const
893    _NOEXCEPT_(noexcept(!_VSTD::forward<_Tp>(__x)))
894    -> decltype        (!_VSTD::forward<_Tp>(__x))
895        { return        !_VSTD::forward<_Tp>(__x); }
896    typedef void is_transparent;
897};
898#endif
899
900
901#if _LIBCPP_STD_VER > 11
902template <class _Tp = void>
903#else
904template <class _Tp>
905#endif
906struct _LIBCPP_TEMPLATE_VIS bit_and : binary_function<_Tp, _Tp, _Tp>
907{
908    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
909    _Tp operator()(const _Tp& __x, const _Tp& __y) const
910        {return __x & __y;}
911};
912
913#if _LIBCPP_STD_VER > 11
914template <>
915struct _LIBCPP_TEMPLATE_VIS bit_and<void>
916{
917    template <class _T1, class _T2>
918    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
919    auto operator()(_T1&& __t, _T2&& __u) const
920    _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) & _VSTD::forward<_T2>(__u)))
921    -> decltype        (_VSTD::forward<_T1>(__t) & _VSTD::forward<_T2>(__u))
922        { return        _VSTD::forward<_T1>(__t) & _VSTD::forward<_T2>(__u); }
923    typedef void is_transparent;
924};
925#endif
926
927
928#if _LIBCPP_STD_VER > 11
929template <class _Tp = void>
930#else
931template <class _Tp>
932#endif
933struct _LIBCPP_TEMPLATE_VIS bit_or : binary_function<_Tp, _Tp, _Tp>
934{
935    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
936    _Tp operator()(const _Tp& __x, const _Tp& __y) const
937        {return __x | __y;}
938};
939
940#if _LIBCPP_STD_VER > 11
941template <>
942struct _LIBCPP_TEMPLATE_VIS bit_or<void>
943{
944    template <class _T1, class _T2>
945    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
946    auto operator()(_T1&& __t, _T2&& __u) const
947    _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) | _VSTD::forward<_T2>(__u)))
948    -> decltype        (_VSTD::forward<_T1>(__t) | _VSTD::forward<_T2>(__u))
949        { return        _VSTD::forward<_T1>(__t) | _VSTD::forward<_T2>(__u); }
950    typedef void is_transparent;
951};
952#endif
953
954
955#if _LIBCPP_STD_VER > 11
956template <class _Tp = void>
957#else
958template <class _Tp>
959#endif
960struct _LIBCPP_TEMPLATE_VIS bit_xor : binary_function<_Tp, _Tp, _Tp>
961{
962    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
963    _Tp operator()(const _Tp& __x, const _Tp& __y) const
964        {return __x ^ __y;}
965};
966
967#if _LIBCPP_STD_VER > 11
968template <>
969struct _LIBCPP_TEMPLATE_VIS bit_xor<void>
970{
971    template <class _T1, class _T2>
972    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
973    auto operator()(_T1&& __t, _T2&& __u) const
974    _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) ^ _VSTD::forward<_T2>(__u)))
975    -> decltype        (_VSTD::forward<_T1>(__t) ^ _VSTD::forward<_T2>(__u))
976        { return        _VSTD::forward<_T1>(__t) ^ _VSTD::forward<_T2>(__u); }
977    typedef void is_transparent;
978};
979#endif
980
981
982#if _LIBCPP_STD_VER > 11
983template <class _Tp = void>
984struct _LIBCPP_TEMPLATE_VIS bit_not : unary_function<_Tp, _Tp>
985{
986    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
987    _Tp operator()(const _Tp& __x) const
988        {return ~__x;}
989};
990
991template <>
992struct _LIBCPP_TEMPLATE_VIS bit_not<void>
993{
994    template <class _Tp>
995    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
996    auto operator()(_Tp&& __x) const
997    _NOEXCEPT_(noexcept(~_VSTD::forward<_Tp>(__x)))
998    -> decltype        (~_VSTD::forward<_Tp>(__x))
999        { return        ~_VSTD::forward<_Tp>(__x); }
1000    typedef void is_transparent;
1001};
1002#endif
1003
1004template <class _Predicate>
1005class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX17 unary_negate
1006    : public unary_function<typename _Predicate::argument_type, bool>
1007{
1008    _Predicate __pred_;
1009public:
1010    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1011    explicit unary_negate(const _Predicate& __pred)
1012        : __pred_(__pred) {}
1013    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1014    bool operator()(const typename _Predicate::argument_type& __x) const
1015        {return !__pred_(__x);}
1016};
1017
1018template <class _Predicate>
1019_LIBCPP_DEPRECATED_IN_CXX17 inline _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1020unary_negate<_Predicate>
1021not1(const _Predicate& __pred) {return unary_negate<_Predicate>(__pred);}
1022
1023template <class _Predicate>
1024class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX17 binary_negate
1025    : public binary_function<typename _Predicate::first_argument_type,
1026                             typename _Predicate::second_argument_type,
1027                             bool>
1028{
1029    _Predicate __pred_;
1030public:
1031    _LIBCPP_INLINE_VISIBILITY explicit _LIBCPP_CONSTEXPR_AFTER_CXX11
1032    binary_negate(const _Predicate& __pred) : __pred_(__pred) {}
1033
1034    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1035    bool operator()(const typename _Predicate::first_argument_type& __x,
1036                    const typename _Predicate::second_argument_type& __y) const
1037        {return !__pred_(__x, __y);}
1038};
1039
1040template <class _Predicate>
1041_LIBCPP_DEPRECATED_IN_CXX17 inline _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1042binary_negate<_Predicate>
1043not2(const _Predicate& __pred) {return binary_negate<_Predicate>(__pred);}
1044
1045#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_BINDERS)
1046template <class __Operation>
1047class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 binder1st
1048    : public unary_function<typename __Operation::second_argument_type,
1049                            typename __Operation::result_type>
1050{
1051protected:
1052    __Operation                               op;
1053    typename __Operation::first_argument_type value;
1054public:
1055    _LIBCPP_INLINE_VISIBILITY binder1st(const __Operation& __x,
1056                               const typename __Operation::first_argument_type __y)
1057        : op(__x), value(__y) {}
1058    _LIBCPP_INLINE_VISIBILITY typename __Operation::result_type operator()
1059        (typename __Operation::second_argument_type& __x) const
1060            {return op(value, __x);}
1061    _LIBCPP_INLINE_VISIBILITY typename __Operation::result_type operator()
1062        (const typename __Operation::second_argument_type& __x) const
1063            {return op(value, __x);}
1064};
1065
1066template <class __Operation, class _Tp>
1067_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
1068binder1st<__Operation>
1069bind1st(const __Operation& __op, const _Tp& __x)
1070    {return binder1st<__Operation>(__op, __x);}
1071
1072template <class __Operation>
1073class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 binder2nd
1074    : public unary_function<typename __Operation::first_argument_type,
1075                            typename __Operation::result_type>
1076{
1077protected:
1078    __Operation                                op;
1079    typename __Operation::second_argument_type value;
1080public:
1081    _LIBCPP_INLINE_VISIBILITY
1082    binder2nd(const __Operation& __x, const typename __Operation::second_argument_type __y)
1083        : op(__x), value(__y) {}
1084    _LIBCPP_INLINE_VISIBILITY typename __Operation::result_type operator()
1085        (      typename __Operation::first_argument_type& __x) const
1086            {return op(__x, value);}
1087    _LIBCPP_INLINE_VISIBILITY typename __Operation::result_type operator()
1088        (const typename __Operation::first_argument_type& __x) const
1089            {return op(__x, value);}
1090};
1091
1092template <class __Operation, class _Tp>
1093_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
1094binder2nd<__Operation>
1095bind2nd(const __Operation& __op, const _Tp& __x)
1096    {return binder2nd<__Operation>(__op, __x);}
1097
1098template <class _Arg, class _Result>
1099class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 pointer_to_unary_function
1100    : public unary_function<_Arg, _Result>
1101{
1102    _Result (*__f_)(_Arg);
1103public:
1104    _LIBCPP_INLINE_VISIBILITY explicit pointer_to_unary_function(_Result (*__f)(_Arg))
1105        : __f_(__f) {}
1106    _LIBCPP_INLINE_VISIBILITY _Result operator()(_Arg __x) const
1107        {return __f_(__x);}
1108};
1109
1110template <class _Arg, class _Result>
1111_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
1112pointer_to_unary_function<_Arg,_Result>
1113ptr_fun(_Result (*__f)(_Arg))
1114    {return pointer_to_unary_function<_Arg,_Result>(__f);}
1115
1116template <class _Arg1, class _Arg2, class _Result>
1117class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 pointer_to_binary_function
1118    : public binary_function<_Arg1, _Arg2, _Result>
1119{
1120    _Result (*__f_)(_Arg1, _Arg2);
1121public:
1122    _LIBCPP_INLINE_VISIBILITY explicit pointer_to_binary_function(_Result (*__f)(_Arg1, _Arg2))
1123        : __f_(__f) {}
1124    _LIBCPP_INLINE_VISIBILITY _Result operator()(_Arg1 __x, _Arg2 __y) const
1125        {return __f_(__x, __y);}
1126};
1127
1128template <class _Arg1, class _Arg2, class _Result>
1129_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
1130pointer_to_binary_function<_Arg1,_Arg2,_Result>
1131ptr_fun(_Result (*__f)(_Arg1,_Arg2))
1132    {return pointer_to_binary_function<_Arg1,_Arg2,_Result>(__f);}
1133
1134template<class _Sp, class _Tp>
1135class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 mem_fun_t
1136    : public unary_function<_Tp*, _Sp>
1137{
1138    _Sp (_Tp::*__p_)();
1139public:
1140    _LIBCPP_INLINE_VISIBILITY explicit mem_fun_t(_Sp (_Tp::*__p)())
1141        : __p_(__p) {}
1142    _LIBCPP_INLINE_VISIBILITY _Sp operator()(_Tp* __p) const
1143        {return (__p->*__p_)();}
1144};
1145
1146template<class _Sp, class _Tp, class _Ap>
1147class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 mem_fun1_t
1148    : public binary_function<_Tp*, _Ap, _Sp>
1149{
1150    _Sp (_Tp::*__p_)(_Ap);
1151public:
1152    _LIBCPP_INLINE_VISIBILITY explicit mem_fun1_t(_Sp (_Tp::*__p)(_Ap))
1153        : __p_(__p) {}
1154    _LIBCPP_INLINE_VISIBILITY _Sp operator()(_Tp* __p, _Ap __x) const
1155        {return (__p->*__p_)(__x);}
1156};
1157
1158template<class _Sp, class _Tp>
1159_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
1160mem_fun_t<_Sp,_Tp>
1161mem_fun(_Sp (_Tp::*__f)())
1162    {return mem_fun_t<_Sp,_Tp>(__f);}
1163
1164template<class _Sp, class _Tp, class _Ap>
1165_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
1166mem_fun1_t<_Sp,_Tp,_Ap>
1167mem_fun(_Sp (_Tp::*__f)(_Ap))
1168    {return mem_fun1_t<_Sp,_Tp,_Ap>(__f);}
1169
1170template<class _Sp, class _Tp>
1171class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 mem_fun_ref_t
1172    : public unary_function<_Tp, _Sp>
1173{
1174    _Sp (_Tp::*__p_)();
1175public:
1176    _LIBCPP_INLINE_VISIBILITY explicit mem_fun_ref_t(_Sp (_Tp::*__p)())
1177        : __p_(__p) {}
1178    _LIBCPP_INLINE_VISIBILITY _Sp operator()(_Tp& __p) const
1179        {return (__p.*__p_)();}
1180};
1181
1182template<class _Sp, class _Tp, class _Ap>
1183class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 mem_fun1_ref_t
1184    : public binary_function<_Tp, _Ap, _Sp>
1185{
1186    _Sp (_Tp::*__p_)(_Ap);
1187public:
1188    _LIBCPP_INLINE_VISIBILITY explicit mem_fun1_ref_t(_Sp (_Tp::*__p)(_Ap))
1189        : __p_(__p) {}
1190    _LIBCPP_INLINE_VISIBILITY _Sp operator()(_Tp& __p, _Ap __x) const
1191        {return (__p.*__p_)(__x);}
1192};
1193
1194template<class _Sp, class _Tp>
1195_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
1196mem_fun_ref_t<_Sp,_Tp>
1197mem_fun_ref(_Sp (_Tp::*__f)())
1198    {return mem_fun_ref_t<_Sp,_Tp>(__f);}
1199
1200template<class _Sp, class _Tp, class _Ap>
1201_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
1202mem_fun1_ref_t<_Sp,_Tp,_Ap>
1203mem_fun_ref(_Sp (_Tp::*__f)(_Ap))
1204    {return mem_fun1_ref_t<_Sp,_Tp,_Ap>(__f);}
1205
1206template <class _Sp, class _Tp>
1207class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 const_mem_fun_t
1208    : public unary_function<const _Tp*, _Sp>
1209{
1210    _Sp (_Tp::*__p_)() const;
1211public:
1212    _LIBCPP_INLINE_VISIBILITY explicit const_mem_fun_t(_Sp (_Tp::*__p)() const)
1213        : __p_(__p) {}
1214    _LIBCPP_INLINE_VISIBILITY _Sp operator()(const _Tp* __p) const
1215        {return (__p->*__p_)();}
1216};
1217
1218template <class _Sp, class _Tp, class _Ap>
1219class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 const_mem_fun1_t
1220    : public binary_function<const _Tp*, _Ap, _Sp>
1221{
1222    _Sp (_Tp::*__p_)(_Ap) const;
1223public:
1224    _LIBCPP_INLINE_VISIBILITY explicit const_mem_fun1_t(_Sp (_Tp::*__p)(_Ap) const)
1225        : __p_(__p) {}
1226    _LIBCPP_INLINE_VISIBILITY _Sp operator()(const _Tp* __p, _Ap __x) const
1227        {return (__p->*__p_)(__x);}
1228};
1229
1230template <class _Sp, class _Tp>
1231_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
1232const_mem_fun_t<_Sp,_Tp>
1233mem_fun(_Sp (_Tp::*__f)() const)
1234    {return const_mem_fun_t<_Sp,_Tp>(__f);}
1235
1236template <class _Sp, class _Tp, class _Ap>
1237_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
1238const_mem_fun1_t<_Sp,_Tp,_Ap>
1239mem_fun(_Sp (_Tp::*__f)(_Ap) const)
1240    {return const_mem_fun1_t<_Sp,_Tp,_Ap>(__f);}
1241
1242template <class _Sp, class _Tp>
1243class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 const_mem_fun_ref_t
1244    : public unary_function<_Tp, _Sp>
1245{
1246    _Sp (_Tp::*__p_)() const;
1247public:
1248    _LIBCPP_INLINE_VISIBILITY explicit const_mem_fun_ref_t(_Sp (_Tp::*__p)() const)
1249        : __p_(__p) {}
1250    _LIBCPP_INLINE_VISIBILITY _Sp operator()(const _Tp& __p) const
1251        {return (__p.*__p_)();}
1252};
1253
1254template <class _Sp, class _Tp, class _Ap>
1255class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 const_mem_fun1_ref_t
1256    : public binary_function<_Tp, _Ap, _Sp>
1257{
1258    _Sp (_Tp::*__p_)(_Ap) const;
1259public:
1260    _LIBCPP_INLINE_VISIBILITY explicit const_mem_fun1_ref_t(_Sp (_Tp::*__p)(_Ap) const)
1261        : __p_(__p) {}
1262    _LIBCPP_INLINE_VISIBILITY _Sp operator()(const _Tp& __p, _Ap __x) const
1263        {return (__p.*__p_)(__x);}
1264};
1265
1266template <class _Sp, class _Tp>
1267_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
1268const_mem_fun_ref_t<_Sp,_Tp>
1269mem_fun_ref(_Sp (_Tp::*__f)() const)
1270    {return const_mem_fun_ref_t<_Sp,_Tp>(__f);}
1271
1272template <class _Sp, class _Tp, class _Ap>
1273_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
1274const_mem_fun1_ref_t<_Sp,_Tp,_Ap>
1275mem_fun_ref(_Sp (_Tp::*__f)(_Ap) const)
1276    {return const_mem_fun1_ref_t<_Sp,_Tp,_Ap>(__f);}
1277#endif
1278
1279////////////////////////////////////////////////////////////////////////////////
1280//                                MEMFUN
1281//==============================================================================
1282
1283template <class _Tp>
1284class __mem_fn
1285    : public __weak_result_type<_Tp>
1286{
1287public:
1288    // types
1289    typedef _Tp type;
1290private:
1291    type __f_;
1292
1293public:
1294    _LIBCPP_INLINE_VISIBILITY __mem_fn(type __f) _NOEXCEPT : __f_(__f) {}
1295
1296#ifndef _LIBCPP_CXX03_LANG
1297    // invoke
1298    template <class... _ArgTypes>
1299    _LIBCPP_INLINE_VISIBILITY
1300    typename __invoke_return<type, _ArgTypes...>::type
1301    operator() (_ArgTypes&&... __args) const {
1302        return __invoke(__f_, _VSTD::forward<_ArgTypes>(__args)...);
1303    }
1304#else
1305
1306    template <class _A0>
1307    _LIBCPP_INLINE_VISIBILITY
1308    typename __invoke_return0<type, _A0>::type
1309    operator() (_A0& __a0) const {
1310        return __invoke(__f_, __a0);
1311    }
1312
1313    template <class _A0>
1314    _LIBCPP_INLINE_VISIBILITY
1315    typename __invoke_return0<type, _A0 const>::type
1316    operator() (_A0 const& __a0) const {
1317        return __invoke(__f_, __a0);
1318    }
1319
1320    template <class _A0, class _A1>
1321    _LIBCPP_INLINE_VISIBILITY
1322    typename __invoke_return1<type, _A0, _A1>::type
1323    operator() (_A0& __a0, _A1& __a1) const {
1324        return __invoke(__f_, __a0, __a1);
1325    }
1326
1327    template <class _A0, class _A1>
1328    _LIBCPP_INLINE_VISIBILITY
1329    typename __invoke_return1<type, _A0 const, _A1>::type
1330    operator() (_A0 const& __a0, _A1& __a1) const {
1331        return __invoke(__f_, __a0, __a1);
1332    }
1333
1334    template <class _A0, class _A1>
1335    _LIBCPP_INLINE_VISIBILITY
1336    typename __invoke_return1<type, _A0, _A1 const>::type
1337    operator() (_A0& __a0, _A1 const& __a1) const {
1338        return __invoke(__f_, __a0, __a1);
1339    }
1340
1341    template <class _A0, class _A1>
1342    _LIBCPP_INLINE_VISIBILITY
1343    typename __invoke_return1<type, _A0 const, _A1 const>::type
1344    operator() (_A0 const& __a0, _A1 const& __a1) const {
1345        return __invoke(__f_, __a0, __a1);
1346    }
1347
1348    template <class _A0, class _A1, class _A2>
1349    _LIBCPP_INLINE_VISIBILITY
1350    typename __invoke_return2<type, _A0, _A1, _A2>::type
1351    operator() (_A0& __a0, _A1& __a1, _A2& __a2) const {
1352        return __invoke(__f_, __a0, __a1, __a2);
1353    }
1354
1355    template <class _A0, class _A1, class _A2>
1356    _LIBCPP_INLINE_VISIBILITY
1357    typename __invoke_return2<type, _A0 const, _A1, _A2>::type
1358    operator() (_A0 const& __a0, _A1& __a1, _A2& __a2) const {
1359        return __invoke(__f_, __a0, __a1, __a2);
1360    }
1361
1362    template <class _A0, class _A1, class _A2>
1363    _LIBCPP_INLINE_VISIBILITY
1364    typename __invoke_return2<type, _A0, _A1 const, _A2>::type
1365    operator() (_A0& __a0, _A1 const& __a1, _A2& __a2) const {
1366        return __invoke(__f_, __a0, __a1, __a2);
1367    }
1368
1369    template <class _A0, class _A1, class _A2>
1370    _LIBCPP_INLINE_VISIBILITY
1371    typename __invoke_return2<type, _A0, _A1, _A2 const>::type
1372    operator() (_A0& __a0, _A1& __a1, _A2 const& __a2) const {
1373        return __invoke(__f_, __a0, __a1, __a2);
1374    }
1375
1376    template <class _A0, class _A1, class _A2>
1377    _LIBCPP_INLINE_VISIBILITY
1378    typename __invoke_return2<type, _A0 const, _A1 const, _A2>::type
1379    operator() (_A0 const& __a0, _A1 const& __a1, _A2& __a2) const {
1380        return __invoke(__f_, __a0, __a1, __a2);
1381    }
1382
1383    template <class _A0, class _A1, class _A2>
1384    _LIBCPP_INLINE_VISIBILITY
1385    typename __invoke_return2<type, _A0 const, _A1, _A2 const>::type
1386    operator() (_A0 const& __a0, _A1& __a1, _A2 const& __a2) const {
1387        return __invoke(__f_, __a0, __a1, __a2);
1388    }
1389
1390    template <class _A0, class _A1, class _A2>
1391    _LIBCPP_INLINE_VISIBILITY
1392    typename __invoke_return2<type, _A0, _A1 const, _A2 const>::type
1393    operator() (_A0& __a0, _A1 const& __a1, _A2 const& __a2) const {
1394        return __invoke(__f_, __a0, __a1, __a2);
1395    }
1396
1397    template <class _A0, class _A1, class _A2>
1398    _LIBCPP_INLINE_VISIBILITY
1399    typename __invoke_return2<type, _A0 const, _A1 const, _A2 const>::type
1400    operator() (_A0 const& __a0, _A1 const& __a1, _A2 const& __a2) const {
1401        return __invoke(__f_, __a0, __a1, __a2);
1402    }
1403#endif
1404};
1405
1406template<class _Rp, class _Tp>
1407inline _LIBCPP_INLINE_VISIBILITY
1408__mem_fn<_Rp _Tp::*>
1409mem_fn(_Rp _Tp::* __pm) _NOEXCEPT
1410{
1411    return __mem_fn<_Rp _Tp::*>(__pm);
1412}
1413
1414////////////////////////////////////////////////////////////////////////////////
1415//                                FUNCTION
1416//==============================================================================
1417
1418// bad_function_call
1419
1420class _LIBCPP_EXCEPTION_ABI bad_function_call
1421    : public exception
1422{
1423#ifdef _LIBCPP_ABI_BAD_FUNCTION_CALL_KEY_FUNCTION
1424public:
1425    virtual ~bad_function_call() _NOEXCEPT;
1426
1427    virtual const char* what() const _NOEXCEPT;
1428#endif
1429};
1430
1431_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
1432void __throw_bad_function_call()
1433{
1434#ifndef _LIBCPP_NO_EXCEPTIONS
1435    throw bad_function_call();
1436#else
1437    _VSTD::abort();
1438#endif
1439}
1440
1441#if defined(_LIBCPP_CXX03_LANG) && !defined(_LIBCPP_DISABLE_DEPRECATION_WARNINGS) && __has_attribute(deprecated)
1442#   define _LIBCPP_DEPRECATED_CXX03_FUNCTION \
1443        __attribute__((deprecated("Using std::function in C++03 is not supported anymore. Please upgrade to C++11 or later, or use a different type")))
1444#else
1445#   define _LIBCPP_DEPRECATED_CXX03_FUNCTION /* nothing */
1446#endif
1447
1448template<class _Fp> class _LIBCPP_DEPRECATED_CXX03_FUNCTION _LIBCPP_TEMPLATE_VIS function; // undefined
1449
1450namespace __function
1451{
1452
1453template<class _Rp>
1454struct __maybe_derive_from_unary_function
1455{
1456};
1457
1458template<class _Rp, class _A1>
1459struct __maybe_derive_from_unary_function<_Rp(_A1)>
1460    : public unary_function<_A1, _Rp>
1461{
1462};
1463
1464template<class _Rp>
1465struct __maybe_derive_from_binary_function
1466{
1467};
1468
1469template<class _Rp, class _A1, class _A2>
1470struct __maybe_derive_from_binary_function<_Rp(_A1, _A2)>
1471    : public binary_function<_A1, _A2, _Rp>
1472{
1473};
1474
1475template <class _Fp>
1476_LIBCPP_INLINE_VISIBILITY
1477bool __not_null(_Fp const&) { return true; }
1478
1479template <class _Fp>
1480_LIBCPP_INLINE_VISIBILITY
1481bool __not_null(_Fp* __ptr) { return __ptr; }
1482
1483template <class _Ret, class _Class>
1484_LIBCPP_INLINE_VISIBILITY
1485bool __not_null(_Ret _Class::*__ptr) { return __ptr; }
1486
1487template <class _Fp>
1488_LIBCPP_INLINE_VISIBILITY
1489bool __not_null(function<_Fp> const& __f) { return !!__f; }
1490
1491#ifdef _LIBCPP_HAS_EXTENSION_BLOCKS
1492template <class _Rp, class ..._Args>
1493_LIBCPP_INLINE_VISIBILITY
1494bool __not_null(_Rp (^__p)(_Args...)) { return __p; }
1495#endif
1496
1497} // namespace __function
1498
1499#ifndef _LIBCPP_CXX03_LANG
1500
1501namespace __function {
1502
1503// __alloc_func holds a functor and an allocator.
1504
1505template <class _Fp, class _Ap, class _FB> class __alloc_func;
1506template <class _Fp, class _FB>
1507class __default_alloc_func;
1508
1509template <class _Fp, class _Ap, class _Rp, class... _ArgTypes>
1510class __alloc_func<_Fp, _Ap, _Rp(_ArgTypes...)>
1511{
1512    __compressed_pair<_Fp, _Ap> __f_;
1513
1514  public:
1515    typedef _LIBCPP_NODEBUG_TYPE _Fp _Target;
1516    typedef _LIBCPP_NODEBUG_TYPE _Ap _Alloc;
1517
1518    _LIBCPP_INLINE_VISIBILITY
1519    const _Target& __target() const { return __f_.first(); }
1520
1521    // WIN32 APIs may define __allocator, so use __get_allocator instead.
1522    _LIBCPP_INLINE_VISIBILITY
1523    const _Alloc& __get_allocator() const { return __f_.second(); }
1524
1525    _LIBCPP_INLINE_VISIBILITY
1526    explicit __alloc_func(_Target&& __f)
1527        : __f_(piecewise_construct, _VSTD::forward_as_tuple(_VSTD::move(__f)),
1528               _VSTD::forward_as_tuple())
1529    {
1530    }
1531
1532    _LIBCPP_INLINE_VISIBILITY
1533    explicit __alloc_func(const _Target& __f, const _Alloc& __a)
1534        : __f_(piecewise_construct, _VSTD::forward_as_tuple(__f),
1535               _VSTD::forward_as_tuple(__a))
1536    {
1537    }
1538
1539    _LIBCPP_INLINE_VISIBILITY
1540    explicit __alloc_func(const _Target& __f, _Alloc&& __a)
1541        : __f_(piecewise_construct, _VSTD::forward_as_tuple(__f),
1542               _VSTD::forward_as_tuple(_VSTD::move(__a)))
1543    {
1544    }
1545
1546    _LIBCPP_INLINE_VISIBILITY
1547    explicit __alloc_func(_Target&& __f, _Alloc&& __a)
1548        : __f_(piecewise_construct, _VSTD::forward_as_tuple(_VSTD::move(__f)),
1549               _VSTD::forward_as_tuple(_VSTD::move(__a)))
1550    {
1551    }
1552
1553    _LIBCPP_INLINE_VISIBILITY
1554    _Rp operator()(_ArgTypes&&... __arg)
1555    {
1556        typedef __invoke_void_return_wrapper<_Rp> _Invoker;
1557        return _Invoker::__call(__f_.first(),
1558                                _VSTD::forward<_ArgTypes>(__arg)...);
1559    }
1560
1561    _LIBCPP_INLINE_VISIBILITY
1562    __alloc_func* __clone() const
1563    {
1564        typedef allocator_traits<_Alloc> __alloc_traits;
1565        typedef
1566            typename __rebind_alloc_helper<__alloc_traits, __alloc_func>::type
1567                _AA;
1568        _AA __a(__f_.second());
1569        typedef __allocator_destructor<_AA> _Dp;
1570        unique_ptr<__alloc_func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1571        ::new ((void*)__hold.get()) __alloc_func(__f_.first(), _Alloc(__a));
1572        return __hold.release();
1573    }
1574
1575    _LIBCPP_INLINE_VISIBILITY
1576    void destroy() _NOEXCEPT { __f_.~__compressed_pair<_Target, _Alloc>(); }
1577
1578    static void __destroy_and_delete(__alloc_func* __f) {
1579      typedef allocator_traits<_Alloc> __alloc_traits;
1580      typedef typename __rebind_alloc_helper<__alloc_traits, __alloc_func>::type
1581          _FunAlloc;
1582      _FunAlloc __a(__f->__get_allocator());
1583      __f->destroy();
1584      __a.deallocate(__f, 1);
1585    }
1586};
1587
1588template <class _Fp, class _Rp, class... _ArgTypes>
1589class __default_alloc_func<_Fp, _Rp(_ArgTypes...)> {
1590  _Fp __f_;
1591
1592public:
1593  typedef _LIBCPP_NODEBUG_TYPE _Fp _Target;
1594
1595  _LIBCPP_INLINE_VISIBILITY
1596  const _Target& __target() const { return __f_; }
1597
1598  _LIBCPP_INLINE_VISIBILITY
1599  explicit __default_alloc_func(_Target&& __f) : __f_(std::move(__f)) {}
1600
1601  _LIBCPP_INLINE_VISIBILITY
1602  explicit __default_alloc_func(const _Target& __f) : __f_(__f) {}
1603
1604  _LIBCPP_INLINE_VISIBILITY
1605  _Rp operator()(_ArgTypes&&... __arg) {
1606    typedef __invoke_void_return_wrapper<_Rp> _Invoker;
1607    return _Invoker::__call(__f_, _VSTD::forward<_ArgTypes>(__arg)...);
1608  }
1609
1610  _LIBCPP_INLINE_VISIBILITY
1611  __default_alloc_func* __clone() const {
1612      __builtin_new_allocator::__holder_t __hold =
1613        __builtin_new_allocator::__allocate_type<__default_alloc_func>(1);
1614    __default_alloc_func* __res =
1615        ::new (__hold.get()) __default_alloc_func(__f_);
1616    (void)__hold.release();
1617    return __res;
1618  }
1619
1620  _LIBCPP_INLINE_VISIBILITY
1621  void destroy() _NOEXCEPT { __f_.~_Target(); }
1622
1623  static void __destroy_and_delete(__default_alloc_func* __f) {
1624    __f->destroy();
1625      __builtin_new_allocator::__deallocate_type<__default_alloc_func>(__f, 1);
1626  }
1627};
1628
1629// __base provides an abstract interface for copyable functors.
1630
1631template<class _Fp> class _LIBCPP_TEMPLATE_VIS __base;
1632
1633template<class _Rp, class ..._ArgTypes>
1634class __base<_Rp(_ArgTypes...)>
1635{
1636    __base(const __base&);
1637    __base& operator=(const __base&);
1638public:
1639    _LIBCPP_INLINE_VISIBILITY __base() {}
1640    _LIBCPP_INLINE_VISIBILITY virtual ~__base() {}
1641    virtual __base* __clone() const = 0;
1642    virtual void __clone(__base*) const = 0;
1643    virtual void destroy() _NOEXCEPT = 0;
1644    virtual void destroy_deallocate() _NOEXCEPT = 0;
1645    virtual _Rp operator()(_ArgTypes&& ...) = 0;
1646#ifndef _LIBCPP_NO_RTTI
1647    virtual const void* target(const type_info&) const _NOEXCEPT = 0;
1648    virtual const std::type_info& target_type() const _NOEXCEPT = 0;
1649#endif  // _LIBCPP_NO_RTTI
1650};
1651
1652// __func implements __base for a given functor type.
1653
1654template<class _FD, class _Alloc, class _FB> class __func;
1655
1656template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1657class __func<_Fp, _Alloc, _Rp(_ArgTypes...)>
1658    : public  __base<_Rp(_ArgTypes...)>
1659{
1660    __alloc_func<_Fp, _Alloc, _Rp(_ArgTypes...)> __f_;
1661public:
1662    _LIBCPP_INLINE_VISIBILITY
1663    explicit __func(_Fp&& __f)
1664        : __f_(_VSTD::move(__f)) {}
1665
1666    _LIBCPP_INLINE_VISIBILITY
1667    explicit __func(const _Fp& __f, const _Alloc& __a)
1668        : __f_(__f, __a) {}
1669
1670    _LIBCPP_INLINE_VISIBILITY
1671    explicit __func(const _Fp& __f, _Alloc&& __a)
1672        : __f_(__f, _VSTD::move(__a)) {}
1673
1674    _LIBCPP_INLINE_VISIBILITY
1675    explicit __func(_Fp&& __f, _Alloc&& __a)
1676        : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
1677
1678    virtual __base<_Rp(_ArgTypes...)>* __clone() const;
1679    virtual void __clone(__base<_Rp(_ArgTypes...)>*) const;
1680    virtual void destroy() _NOEXCEPT;
1681    virtual void destroy_deallocate() _NOEXCEPT;
1682    virtual _Rp operator()(_ArgTypes&&... __arg);
1683#ifndef _LIBCPP_NO_RTTI
1684    virtual const void* target(const type_info&) const _NOEXCEPT;
1685    virtual const std::type_info& target_type() const _NOEXCEPT;
1686#endif  // _LIBCPP_NO_RTTI
1687};
1688
1689template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1690__base<_Rp(_ArgTypes...)>*
1691__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__clone() const
1692{
1693    typedef allocator_traits<_Alloc> __alloc_traits;
1694    typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
1695    _Ap __a(__f_.__get_allocator());
1696    typedef __allocator_destructor<_Ap> _Dp;
1697    unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1698    ::new ((void*)__hold.get()) __func(__f_.__target(), _Alloc(__a));
1699    return __hold.release();
1700}
1701
1702template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1703void
1704__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__clone(__base<_Rp(_ArgTypes...)>* __p) const
1705{
1706    ::new (__p) __func(__f_.__target(), __f_.__get_allocator());
1707}
1708
1709template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1710void
1711__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy() _NOEXCEPT
1712{
1713    __f_.destroy();
1714}
1715
1716template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1717void
1718__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy_deallocate() _NOEXCEPT
1719{
1720    typedef allocator_traits<_Alloc> __alloc_traits;
1721    typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
1722    _Ap __a(__f_.__get_allocator());
1723    __f_.destroy();
1724    __a.deallocate(this, 1);
1725}
1726
1727template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1728_Rp
1729__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::operator()(_ArgTypes&& ... __arg)
1730{
1731    return __f_(_VSTD::forward<_ArgTypes>(__arg)...);
1732}
1733
1734#ifndef _LIBCPP_NO_RTTI
1735
1736template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1737const void*
1738__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::target(const type_info& __ti) const _NOEXCEPT
1739{
1740    if (__ti == typeid(_Fp))
1741        return &__f_.__target();
1742    return (const void*)0;
1743}
1744
1745template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1746const std::type_info&
1747__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::target_type() const _NOEXCEPT
1748{
1749    return typeid(_Fp);
1750}
1751
1752#endif  // _LIBCPP_NO_RTTI
1753
1754// __value_func creates a value-type from a __func.
1755
1756template <class _Fp> class __value_func;
1757
1758template <class _Rp, class... _ArgTypes> class __value_func<_Rp(_ArgTypes...)>
1759{
1760    typename aligned_storage<3 * sizeof(void*)>::type __buf_;
1761
1762    typedef __base<_Rp(_ArgTypes...)> __func;
1763    __func* __f_;
1764
1765    _LIBCPP_NO_CFI static __func* __as_base(void* p)
1766    {
1767        return reinterpret_cast<__func*>(p);
1768    }
1769
1770  public:
1771    _LIBCPP_INLINE_VISIBILITY
1772    __value_func() _NOEXCEPT : __f_(0) {}
1773
1774    template <class _Fp, class _Alloc>
1775    _LIBCPP_INLINE_VISIBILITY __value_func(_Fp&& __f, const _Alloc& __a)
1776        : __f_(0)
1777    {
1778        typedef allocator_traits<_Alloc> __alloc_traits;
1779        typedef __function::__func<_Fp, _Alloc, _Rp(_ArgTypes...)> _Fun;
1780        typedef typename __rebind_alloc_helper<__alloc_traits, _Fun>::type
1781            _FunAlloc;
1782
1783        if (__function::__not_null(__f))
1784        {
1785            _FunAlloc __af(__a);
1786            if (sizeof(_Fun) <= sizeof(__buf_) &&
1787                is_nothrow_copy_constructible<_Fp>::value &&
1788                is_nothrow_copy_constructible<_FunAlloc>::value)
1789            {
1790                __f_ =
1791                    ::new ((void*)&__buf_) _Fun(_VSTD::move(__f), _Alloc(__af));
1792            }
1793            else
1794            {
1795                typedef __allocator_destructor<_FunAlloc> _Dp;
1796                unique_ptr<__func, _Dp> __hold(__af.allocate(1), _Dp(__af, 1));
1797                ::new ((void*)__hold.get()) _Fun(_VSTD::move(__f), _Alloc(__a));
1798                __f_ = __hold.release();
1799            }
1800        }
1801    }
1802
1803    template <class _Fp,
1804        class = typename enable_if<!is_same<typename decay<_Fp>::type, __value_func>::value>::type>
1805    _LIBCPP_INLINE_VISIBILITY explicit __value_func(_Fp&& __f)
1806        : __value_func(std::forward<_Fp>(__f), allocator<_Fp>()) {}
1807
1808    _LIBCPP_INLINE_VISIBILITY
1809    __value_func(const __value_func& __f)
1810    {
1811        if (__f.__f_ == 0)
1812            __f_ = 0;
1813        else if ((void*)__f.__f_ == &__f.__buf_)
1814        {
1815            __f_ = __as_base(&__buf_);
1816            __f.__f_->__clone(__f_);
1817        }
1818        else
1819            __f_ = __f.__f_->__clone();
1820    }
1821
1822    _LIBCPP_INLINE_VISIBILITY
1823    __value_func(__value_func&& __f) _NOEXCEPT
1824    {
1825        if (__f.__f_ == 0)
1826            __f_ = 0;
1827        else if ((void*)__f.__f_ == &__f.__buf_)
1828        {
1829            __f_ = __as_base(&__buf_);
1830            __f.__f_->__clone(__f_);
1831        }
1832        else
1833        {
1834            __f_ = __f.__f_;
1835            __f.__f_ = 0;
1836        }
1837    }
1838
1839    _LIBCPP_INLINE_VISIBILITY
1840    ~__value_func()
1841    {
1842        if ((void*)__f_ == &__buf_)
1843            __f_->destroy();
1844        else if (__f_)
1845            __f_->destroy_deallocate();
1846    }
1847
1848    _LIBCPP_INLINE_VISIBILITY
1849    __value_func& operator=(__value_func&& __f)
1850    {
1851        *this = nullptr;
1852        if (__f.__f_ == 0)
1853            __f_ = 0;
1854        else if ((void*)__f.__f_ == &__f.__buf_)
1855        {
1856            __f_ = __as_base(&__buf_);
1857            __f.__f_->__clone(__f_);
1858        }
1859        else
1860        {
1861            __f_ = __f.__f_;
1862            __f.__f_ = 0;
1863        }
1864        return *this;
1865    }
1866
1867    _LIBCPP_INLINE_VISIBILITY
1868    __value_func& operator=(nullptr_t)
1869    {
1870        __func* __f = __f_;
1871        __f_ = 0;
1872        if ((void*)__f == &__buf_)
1873            __f->destroy();
1874        else if (__f)
1875            __f->destroy_deallocate();
1876        return *this;
1877    }
1878
1879    _LIBCPP_INLINE_VISIBILITY
1880    _Rp operator()(_ArgTypes&&... __args) const
1881    {
1882        if (__f_ == 0)
1883            __throw_bad_function_call();
1884        return (*__f_)(_VSTD::forward<_ArgTypes>(__args)...);
1885    }
1886
1887    _LIBCPP_INLINE_VISIBILITY
1888    void swap(__value_func& __f) _NOEXCEPT
1889    {
1890        if (&__f == this)
1891            return;
1892        if ((void*)__f_ == &__buf_ && (void*)__f.__f_ == &__f.__buf_)
1893        {
1894            typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1895            __func* __t = __as_base(&__tempbuf);
1896            __f_->__clone(__t);
1897            __f_->destroy();
1898            __f_ = 0;
1899            __f.__f_->__clone(__as_base(&__buf_));
1900            __f.__f_->destroy();
1901            __f.__f_ = 0;
1902            __f_ = __as_base(&__buf_);
1903            __t->__clone(__as_base(&__f.__buf_));
1904            __t->destroy();
1905            __f.__f_ = __as_base(&__f.__buf_);
1906        }
1907        else if ((void*)__f_ == &__buf_)
1908        {
1909            __f_->__clone(__as_base(&__f.__buf_));
1910            __f_->destroy();
1911            __f_ = __f.__f_;
1912            __f.__f_ = __as_base(&__f.__buf_);
1913        }
1914        else if ((void*)__f.__f_ == &__f.__buf_)
1915        {
1916            __f.__f_->__clone(__as_base(&__buf_));
1917            __f.__f_->destroy();
1918            __f.__f_ = __f_;
1919            __f_ = __as_base(&__buf_);
1920        }
1921        else
1922            _VSTD::swap(__f_, __f.__f_);
1923    }
1924
1925    _LIBCPP_INLINE_VISIBILITY
1926    _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT { return __f_ != 0; }
1927
1928#ifndef _LIBCPP_NO_RTTI
1929    _LIBCPP_INLINE_VISIBILITY
1930    const std::type_info& target_type() const _NOEXCEPT
1931    {
1932        if (__f_ == 0)
1933            return typeid(void);
1934        return __f_->target_type();
1935    }
1936
1937    template <typename _Tp>
1938    _LIBCPP_INLINE_VISIBILITY const _Tp* target() const _NOEXCEPT
1939    {
1940        if (__f_ == 0)
1941            return 0;
1942        return (const _Tp*)__f_->target(typeid(_Tp));
1943    }
1944#endif // _LIBCPP_NO_RTTI
1945};
1946
1947// Storage for a functor object, to be used with __policy to manage copy and
1948// destruction.
1949union __policy_storage
1950{
1951    mutable char __small[sizeof(void*) * 2];
1952    void* __large;
1953};
1954
1955// True if _Fun can safely be held in __policy_storage.__small.
1956template <typename _Fun>
1957struct __use_small_storage
1958    : public _VSTD::integral_constant<
1959          bool, sizeof(_Fun) <= sizeof(__policy_storage) &&
1960                    _LIBCPP_ALIGNOF(_Fun) <= _LIBCPP_ALIGNOF(__policy_storage) &&
1961                    _VSTD::is_trivially_copy_constructible<_Fun>::value &&
1962                    _VSTD::is_trivially_destructible<_Fun>::value> {};
1963
1964// Policy contains information about how to copy, destroy, and move the
1965// underlying functor. You can think of it as a vtable of sorts.
1966struct __policy
1967{
1968    // Used to copy or destroy __large values. null for trivial objects.
1969    void* (*const __clone)(const void*);
1970    void (*const __destroy)(void*);
1971
1972    // True if this is the null policy (no value).
1973    const bool __is_null;
1974
1975    // The target type. May be null if RTTI is disabled.
1976    const std::type_info* const __type_info;
1977
1978    // Returns a pointer to a static policy object suitable for the functor
1979    // type.
1980    template <typename _Fun>
1981    _LIBCPP_INLINE_VISIBILITY static const __policy* __create()
1982    {
1983        return __choose_policy<_Fun>(__use_small_storage<_Fun>());
1984    }
1985
1986    _LIBCPP_INLINE_VISIBILITY
1987    static const __policy* __create_empty()
1988    {
1989        static const _LIBCPP_CONSTEXPR __policy __policy_ = {nullptr, nullptr,
1990                                                             true,
1991#ifndef _LIBCPP_NO_RTTI
1992                                                             &typeid(void)
1993#else
1994                                                             nullptr
1995#endif
1996        };
1997        return &__policy_;
1998    }
1999
2000  private:
2001    template <typename _Fun> static void* __large_clone(const void* __s)
2002    {
2003        const _Fun* __f = static_cast<const _Fun*>(__s);
2004        return __f->__clone();
2005    }
2006
2007    template <typename _Fun>
2008    static void __large_destroy(void* __s) {
2009      _Fun::__destroy_and_delete(static_cast<_Fun*>(__s));
2010    }
2011
2012    template <typename _Fun>
2013    _LIBCPP_INLINE_VISIBILITY static const __policy*
2014    __choose_policy(/* is_small = */ false_type) {
2015      static const _LIBCPP_CONSTEXPR __policy __policy_ = {
2016          &__large_clone<_Fun>, &__large_destroy<_Fun>, false,
2017#ifndef _LIBCPP_NO_RTTI
2018          &typeid(typename _Fun::_Target)
2019#else
2020          nullptr
2021#endif
2022      };
2023        return &__policy_;
2024    }
2025
2026    template <typename _Fun>
2027    _LIBCPP_INLINE_VISIBILITY static const __policy*
2028        __choose_policy(/* is_small = */ true_type)
2029    {
2030        static const _LIBCPP_CONSTEXPR __policy __policy_ = {
2031            nullptr, nullptr, false,
2032#ifndef _LIBCPP_NO_RTTI
2033            &typeid(typename _Fun::_Target)
2034#else
2035            nullptr
2036#endif
2037        };
2038        return &__policy_;
2039    }
2040};
2041
2042// Used to choose between perfect forwarding or pass-by-value. Pass-by-value is
2043// faster for types that can be passed in registers.
2044template <typename _Tp>
2045using __fast_forward =
2046    typename _VSTD::conditional<_VSTD::is_scalar<_Tp>::value, _Tp, _Tp&&>::type;
2047
2048// __policy_invoker calls an instance of __alloc_func held in __policy_storage.
2049
2050template <class _Fp> struct __policy_invoker;
2051
2052template <class _Rp, class... _ArgTypes>
2053struct __policy_invoker<_Rp(_ArgTypes...)>
2054{
2055    typedef _Rp (*__Call)(const __policy_storage*,
2056                          __fast_forward<_ArgTypes>...);
2057
2058    __Call __call_;
2059
2060    // Creates an invoker that throws bad_function_call.
2061    _LIBCPP_INLINE_VISIBILITY
2062    __policy_invoker() : __call_(&__call_empty) {}
2063
2064    // Creates an invoker that calls the given instance of __func.
2065    template <typename _Fun>
2066    _LIBCPP_INLINE_VISIBILITY static __policy_invoker __create()
2067    {
2068        return __policy_invoker(&__call_impl<_Fun>);
2069    }
2070
2071  private:
2072    _LIBCPP_INLINE_VISIBILITY
2073    explicit __policy_invoker(__Call __c) : __call_(__c) {}
2074
2075    static _Rp __call_empty(const __policy_storage*,
2076                            __fast_forward<_ArgTypes>...)
2077    {
2078        __throw_bad_function_call();
2079    }
2080
2081    template <typename _Fun>
2082    static _Rp __call_impl(const __policy_storage* __buf,
2083                           __fast_forward<_ArgTypes>... __args)
2084    {
2085        _Fun* __f = reinterpret_cast<_Fun*>(__use_small_storage<_Fun>::value
2086                                                ? &__buf->__small
2087                                                : __buf->__large);
2088        return (*__f)(_VSTD::forward<_ArgTypes>(__args)...);
2089    }
2090};
2091
2092// __policy_func uses a __policy and __policy_invoker to create a type-erased,
2093// copyable functor.
2094
2095template <class _Fp> class __policy_func;
2096
2097template <class _Rp, class... _ArgTypes> class __policy_func<_Rp(_ArgTypes...)>
2098{
2099    // Inline storage for small objects.
2100    __policy_storage __buf_;
2101
2102    // Calls the value stored in __buf_. This could technically be part of
2103    // policy, but storing it here eliminates a level of indirection inside
2104    // operator().
2105    typedef __function::__policy_invoker<_Rp(_ArgTypes...)> __invoker;
2106    __invoker __invoker_;
2107
2108    // The policy that describes how to move / copy / destroy __buf_. Never
2109    // null, even if the function is empty.
2110    const __policy* __policy_;
2111
2112  public:
2113    _LIBCPP_INLINE_VISIBILITY
2114    __policy_func() : __policy_(__policy::__create_empty()) {}
2115
2116    template <class _Fp, class _Alloc>
2117    _LIBCPP_INLINE_VISIBILITY __policy_func(_Fp&& __f, const _Alloc& __a)
2118        : __policy_(__policy::__create_empty())
2119    {
2120        typedef __alloc_func<_Fp, _Alloc, _Rp(_ArgTypes...)> _Fun;
2121        typedef allocator_traits<_Alloc> __alloc_traits;
2122        typedef typename __rebind_alloc_helper<__alloc_traits, _Fun>::type
2123            _FunAlloc;
2124
2125        if (__function::__not_null(__f))
2126        {
2127            __invoker_ = __invoker::template __create<_Fun>();
2128            __policy_ = __policy::__create<_Fun>();
2129
2130            _FunAlloc __af(__a);
2131            if (__use_small_storage<_Fun>())
2132            {
2133                ::new ((void*)&__buf_.__small)
2134                    _Fun(_VSTD::move(__f), _Alloc(__af));
2135            }
2136            else
2137            {
2138                typedef __allocator_destructor<_FunAlloc> _Dp;
2139                unique_ptr<_Fun, _Dp> __hold(__af.allocate(1), _Dp(__af, 1));
2140                ::new ((void*)__hold.get())
2141                    _Fun(_VSTD::move(__f), _Alloc(__af));
2142                __buf_.__large = __hold.release();
2143            }
2144        }
2145    }
2146
2147    template <class _Fp, class = typename enable_if<!is_same<typename decay<_Fp>::type, __policy_func>::value>::type>
2148    _LIBCPP_INLINE_VISIBILITY explicit __policy_func(_Fp&& __f)
2149        : __policy_(__policy::__create_empty()) {
2150      typedef __default_alloc_func<_Fp, _Rp(_ArgTypes...)> _Fun;
2151
2152      if (__function::__not_null(__f)) {
2153        __invoker_ = __invoker::template __create<_Fun>();
2154        __policy_ = __policy::__create<_Fun>();
2155        if (__use_small_storage<_Fun>()) {
2156          ::new ((void*)&__buf_.__small) _Fun(_VSTD::move(__f));
2157        } else {
2158          __builtin_new_allocator::__holder_t __hold =
2159              __builtin_new_allocator::__allocate_type<_Fun>(1);
2160          __buf_.__large = ::new (__hold.get()) _Fun(_VSTD::move(__f));
2161          (void)__hold.release();
2162        }
2163      }
2164    }
2165
2166    _LIBCPP_INLINE_VISIBILITY
2167    __policy_func(const __policy_func& __f)
2168        : __buf_(__f.__buf_), __invoker_(__f.__invoker_),
2169          __policy_(__f.__policy_)
2170    {
2171        if (__policy_->__clone)
2172            __buf_.__large = __policy_->__clone(__f.__buf_.__large);
2173    }
2174
2175    _LIBCPP_INLINE_VISIBILITY
2176    __policy_func(__policy_func&& __f)
2177        : __buf_(__f.__buf_), __invoker_(__f.__invoker_),
2178          __policy_(__f.__policy_)
2179    {
2180        if (__policy_->__destroy)
2181        {
2182            __f.__policy_ = __policy::__create_empty();
2183            __f.__invoker_ = __invoker();
2184        }
2185    }
2186
2187    _LIBCPP_INLINE_VISIBILITY
2188    ~__policy_func()
2189    {
2190        if (__policy_->__destroy)
2191            __policy_->__destroy(__buf_.__large);
2192    }
2193
2194    _LIBCPP_INLINE_VISIBILITY
2195    __policy_func& operator=(__policy_func&& __f)
2196    {
2197        *this = nullptr;
2198        __buf_ = __f.__buf_;
2199        __invoker_ = __f.__invoker_;
2200        __policy_ = __f.__policy_;
2201        __f.__policy_ = __policy::__create_empty();
2202        __f.__invoker_ = __invoker();
2203        return *this;
2204    }
2205
2206    _LIBCPP_INLINE_VISIBILITY
2207    __policy_func& operator=(nullptr_t)
2208    {
2209        const __policy* __p = __policy_;
2210        __policy_ = __policy::__create_empty();
2211        __invoker_ = __invoker();
2212        if (__p->__destroy)
2213            __p->__destroy(__buf_.__large);
2214        return *this;
2215    }
2216
2217    _LIBCPP_INLINE_VISIBILITY
2218    _Rp operator()(_ArgTypes&&... __args) const
2219    {
2220        return __invoker_.__call_(_VSTD::addressof(__buf_),
2221                                  _VSTD::forward<_ArgTypes>(__args)...);
2222    }
2223
2224    _LIBCPP_INLINE_VISIBILITY
2225    void swap(__policy_func& __f)
2226    {
2227        _VSTD::swap(__invoker_, __f.__invoker_);
2228        _VSTD::swap(__policy_, __f.__policy_);
2229        _VSTD::swap(__buf_, __f.__buf_);
2230    }
2231
2232    _LIBCPP_INLINE_VISIBILITY
2233    explicit operator bool() const _NOEXCEPT
2234    {
2235        return !__policy_->__is_null;
2236    }
2237
2238#ifndef _LIBCPP_NO_RTTI
2239    _LIBCPP_INLINE_VISIBILITY
2240    const std::type_info& target_type() const _NOEXCEPT
2241    {
2242        return *__policy_->__type_info;
2243    }
2244
2245    template <typename _Tp>
2246    _LIBCPP_INLINE_VISIBILITY const _Tp* target() const _NOEXCEPT
2247    {
2248        if (__policy_->__is_null || typeid(_Tp) != *__policy_->__type_info)
2249            return nullptr;
2250        if (__policy_->__clone) // Out of line storage.
2251            return reinterpret_cast<const _Tp*>(__buf_.__large);
2252        else
2253            return reinterpret_cast<const _Tp*>(&__buf_.__small);
2254    }
2255#endif // _LIBCPP_NO_RTTI
2256};
2257
2258#if defined(_LIBCPP_HAS_BLOCKS_RUNTIME) && !defined(_LIBCPP_HAS_OBJC_ARC)
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_(__f ? Block_copy(__f) : (__block_type)0)
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_(__f ? Block_copy(__f) : (__block_type)0)
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_ = std::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