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