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&);
47    reference_wrapper(T&&) = delete; // do not bind to temps
48    reference_wrapper(const reference_wrapper<T>& x);
49
50    // assignment
51    reference_wrapper& operator=(const reference_wrapper<T>& x);
52
53    // access
54    operator T& () const;
55    T& get() const;
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);
64template <class T> void ref(const T&& t) = delete;
65template <class T> reference_wrapper<T> ref(reference_wrapper<T>t);
66
67template <class T> reference_wrapper<const T> cref(const T& t);
68template <class T> void cref(const T&& t) = delete;
69template <class T> reference_wrapper<const T> cref(reference_wrapper<T> t);
70
71template <class T>
72struct plus : binary_function<T, T, T>
73{
74    T operator()(const T& x, const T& y) const;
75};
76
77template <class T>
78struct minus : binary_function<T, T, T>
79{
80    T operator()(const T& x, const T& y) const;
81};
82
83template <class T>
84struct multiplies : binary_function<T, T, T>
85{
86    T operator()(const T& x, const T& y) const;
87};
88
89template <class T>
90struct divides : binary_function<T, T, T>
91{
92    T operator()(const T& x, const T& y) const;
93};
94
95template <class T>
96struct modulus : binary_function<T, T, T>
97{
98    T operator()(const T& x, const T& y) const;
99};
100
101template <class T>
102struct negate : unary_function<T, T>
103{
104    T operator()(const T& x) const;
105};
106
107template <class T>
108struct equal_to : binary_function<T, T, bool>
109{
110    bool operator()(const T& x, const T& y) const;
111};
112
113template <class T>
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>
120struct greater : binary_function<T, T, bool>
121{
122    bool operator()(const T& x, const T& y) const;
123};
124
125template <class T>
126struct less : binary_function<T, T, bool>
127{
128    bool operator()(const T& x, const T& y) const;
129};
130
131template <class T>
132struct greater_equal : binary_function<T, T, bool>
133{
134    bool operator()(const T& x, const T& y) const;
135};
136
137template <class T>
138struct less_equal : binary_function<T, T, bool>
139{
140    bool operator()(const T& x, const T& y) const;
141};
142
143template <class T>
144struct logical_and : binary_function<T, T, bool>
145{
146    bool operator()(const T& x, const T& y) const;
147};
148
149template <class T>
150struct logical_or : binary_function<T, T, bool>
151{
152    bool operator()(const T& x, const T& y) const;
153};
154
155template <class T>
156struct logical_not : unary_function<T, bool>
157{
158    bool operator()(const T& x) const;
159};
160
161template <class Predicate>
162class unary_negate
163    : public unary_function<typename Predicate::argument_type, bool>
164{
165public:
166    explicit unary_negate(const Predicate& pred);
167    bool operator()(const typename Predicate::argument_type& x) const;
168};
169
170template <class Predicate> unary_negate<Predicate> not1(const Predicate& pred);
171
172template <class Predicate>
173class binary_negate
174    : public binary_function<typename Predicate::first_argument_type,
175                             typename Predicate::second_argument_type,
176                             bool>
177{
178public:
179    explicit binary_negate(const Predicate& pred);
180    bool operator()(const typename Predicate::first_argument_type& x,
181                    const typename Predicate::second_argument_type& y) const;
182};
183
184template <class Predicate> binary_negate<Predicate> not2(const Predicate& pred);
185
186template<class T> struct is_bind_expression;
187template<class T> struct is_placeholder;
188
189template<class Fn, class... BoundArgs>
190  unspecified bind(Fn&&, BoundArgs&&...);
191template<class R, class Fn, class... BoundArgs>
192  unspecified bind(Fn&&, BoundArgs&&...);
193
194namespace placeholders {
195  // M is the implementation-defined number of placeholders
196  extern unspecified _1;
197  extern unspecified _2;
198  .
199  .
200  .
201  extern unspecified _M;
202}
203
204template <class Operation>
205class binder1st
206    : public unary_function<typename Operation::second_argument_type,
207                            typename Operation::result_type>
208{
209protected:
210    Operation                               op;
211    typename Operation::first_argument_type value;
212public:
213    binder1st(const Operation& x, const typename Operation::first_argument_type y);
214    typename Operation::result_type operator()(      typename Operation::second_argument_type& x) const;
215    typename Operation::result_type operator()(const typename Operation::second_argument_type& x) const;
216};
217
218template <class Operation, class T>
219binder1st<Operation> bind1st(const Operation& op, const T& x);
220
221template <class Operation>
222class binder2nd
223    : public unary_function<typename Operation::first_argument_type,
224                            typename Operation::result_type>
225{
226protected:
227    Operation                                op;
228    typename Operation::second_argument_type value;
229public:
230    binder2nd(const Operation& x, const typename Operation::second_argument_type y);
231    typename Operation::result_type operator()(      typename Operation::first_argument_type& x) const;
232    typename Operation::result_type operator()(const typename Operation::first_argument_type& x) const;
233};
234
235template <class Operation, class T>
236binder2nd<Operation> bind2nd(const Operation& op, const T& x);
237
238template <class Arg, class Result>
239class pointer_to_unary_function : public unary_function<Arg, Result>
240{
241public:
242    explicit pointer_to_unary_function(Result (*f)(Arg));
243    Result operator()(Arg x) const;
244};
245
246template <class Arg, class Result>
247pointer_to_unary_function<Arg,Result> ptr_fun(Result (*f)(Arg));
248
249template <class Arg1, class Arg2, class Result>
250class pointer_to_binary_function : public binary_function<Arg1, Arg2, Result>
251{
252public:
253    explicit pointer_to_binary_function(Result (*f)(Arg1, Arg2));
254    Result operator()(Arg1 x, Arg2 y) const;
255};
256
257template <class Arg1, class Arg2, class Result>
258pointer_to_binary_function<Arg1,Arg2,Result> ptr_fun(Result (*f)(Arg1,Arg2));
259
260template<class S, class T>
261class mem_fun_t : public unary_function<T*, S>
262{
263public:
264    explicit mem_fun_t(S (T::*p)());
265    S operator()(T* p) const;
266};
267
268template<class S, class T, class A>
269class mem_fun1_t : public binary_function<T*, A, S>
270{
271public:
272    explicit mem_fun1_t(S (T::*p)(A));
273    S operator()(T* p, A x) const;
274};
275
276template<class S, class T>          mem_fun_t<S,T>    mem_fun(S (T::*f)());
277template<class S, class T, class A> mem_fun1_t<S,T,A> mem_fun(S (T::*f)(A));
278
279template<class S, class T>
280class mem_fun_ref_t : public unary_function<T, S>
281{
282public:
283    explicit mem_fun_ref_t(S (T::*p)());
284    S operator()(T& p) const;
285};
286
287template<class S, class T, class A>
288class mem_fun1_ref_t : public binary_function<T, A, S>
289{
290public:
291    explicit mem_fun1_ref_t(S (T::*p)(A));
292    S operator()(T& p, A x) const;
293};
294
295template<class S, class T>          mem_fun_ref_t<S,T>    mem_fun_ref(S (T::*f)());
296template<class S, class T, class A> mem_fun1_ref_t<S,T,A> mem_fun_ref(S (T::*f)(A));
297
298template <class S, class T>
299class const_mem_fun_t : public unary_function<const T*, S>
300{
301public:
302    explicit const_mem_fun_t(S (T::*p)() const);
303    S operator()(const T* p) const;
304};
305
306template <class S, class T, class A>
307class const_mem_fun1_t : public binary_function<const T*, A, S>
308{
309public:
310    explicit const_mem_fun1_t(S (T::*p)(A) const);
311    S operator()(const T* p, A x) const;
312};
313
314template <class S, class T>          const_mem_fun_t<S,T>    mem_fun(S (T::*f)() const);
315template <class S, class T, class A> const_mem_fun1_t<S,T,A> mem_fun(S (T::*f)(A) const);
316
317template <class S, class T>
318class const_mem_fun_ref_t : public unary_function<T, S>
319{
320public:
321    explicit const_mem_fun_ref_t(S (T::*p)() const);
322    S operator()(const T& p) const;
323};
324
325template <class S, class T, class A>
326class const_mem_fun1_ref_t : public binary_function<T, A, S>
327{
328public:
329    explicit const_mem_fun1_ref_t(S (T::*p)(A) const);
330    S operator()(const T& p, A x) const;
331};
332
333template <class S, class T>          const_mem_fun_ref_t<S,T>    mem_fun_ref(S (T::*f)() const);
334template <class S, class T, class A> const_mem_fun1_ref_t<S,T,A> mem_fun_ref(S (T::*f)(A) const);
335
336template<class R, class T> unspecified mem_fn(R T::*);
337template<class R, class T, class... Args> unspecified mem_fn(R (T::*)(Args...));
338template<class R, class T, class... Args> unspecified mem_fn(R (T::*)(Args...) const);
339template<class R, class T, class... Args> unspecified mem_fn(R (T::*)(Args...) volatile);
340template<class R, class T, class... Args> unspecified mem_fn(R (T::*)(Args...) const volatile);
341template<class R, class T, class... Args> unspecified mem_fn(R (T::*)(Args...) &);
342template<class R, class T, class... Args> unspecified mem_fn(R (T::*)(Args...) const &);
343template<class R, class T, class... Args> unspecified mem_fn(R (T::*)(Args...) volatile &);
344template<class R, class T, class... Args> unspecified mem_fn(R (T::*)(Args...) const volatile &);
345template<class R, class T, class... Args> unspecified mem_fn(R (T::*)(Args...) &&);
346template<class R, class T, class... Args> unspecified mem_fn(R (T::*)(Args...) const &&);
347template<class R, class T, class... Args> unspecified mem_fn(R (T::*)(Args...) volatile &&);
348template<class R, class T, class... Args> unspecified mem_fn(R (T::*)(Args...) const volatile &&);
349
350class bad_function_call
351    : public exception
352{
353};
354
355template<class> class function; // undefined
356
357template<class R, class... ArgTypes>
358class function<R(ArgTypes...)>
359  : public unary_function<T1, R>      // iff sizeof...(ArgTypes) == 1 and
360                                      // ArgTypes contains T1
361  : public binary_function<T1, T2, R> // iff sizeof...(ArgTypes) == 2 and
362                                      // ArgTypes contains T1 and T2
363{
364public:
365    typedef R result_type;
366
367    // construct/copy/destroy:
368    function();
369    function(nullptr_t);
370    function(const function&);
371    function(function&&);
372    template<class F>
373      function(F);
374    template<Allocator Alloc>
375      function(allocator_arg_t, const Alloc&);
376    template<Allocator Alloc>
377      function(allocator_arg_t, const Alloc&, nullptr_t);
378    template<Allocator Alloc>
379      function(allocator_arg_t, const Alloc&, const function&);
380    template<Allocator Alloc>
381      function(allocator_arg_t, const Alloc&, function&&);
382    template<class F, Allocator Alloc>
383      function(allocator_arg_t, const Alloc&, F);
384
385    function& operator=(const function&);
386    function& operator=(function&&);
387    function& operator=(nullptr_t);
388    template<class F>
389      function& operator=(F&&);
390    template<class F>
391      function& operator=(reference_wrapper<F>);
392
393    ~function();
394
395    // function modifiers:
396    void swap(function&);
397    template<class F, class Alloc>
398      void assign(F&&, const Alloc&);
399
400    // function capacity:
401    explicit operator bool() const;
402
403    // deleted overloads close possible hole in the type system
404    template<class R2, class... ArgTypes2>
405      bool operator==(const function<R2(ArgTypes2...)>&) = delete;
406    template<class R2, class... ArgTypes2>
407      bool operator!=(const function<R2(ArgTypes2...)>&) = delete;
408
409    // function invocation:
410    R operator()(ArgTypes...) const;
411
412    // function target access:
413    const std::type_info& target_type() const;
414    template <typename T>       T* target();
415    template <typename T> const T* target() const;
416};
417
418// Null pointer comparisons:
419template <class R, class ... ArgTypes>
420  bool operator==(const function<R(ArgTypes...)>&, nullptr_t);
421
422template <class R, class ... ArgTypes>
423  bool operator==(nullptr_t, const function<R(ArgTypes...)>&);
424
425template <class R, class ... ArgTypes>
426  bool operator!=(const function<R(ArgTypes...)>&, nullptr_t);
427
428template <class  R, class ... ArgTypes>
429  bool operator!=(nullptr_t, const function<R(ArgTypes...)>&);
430
431// specialized algorithms:
432template <class  R, class ... ArgTypes>
433  void swap(function<R(ArgTypes...)>&, function<R(ArgTypes...)>&);
434
435template <class T> struct hash;
436
437template <> struct hash<bool>;
438template <> struct hash<char>;
439template <> struct hash<signed char>;
440template <> struct hash<unsigned char>;
441template <> struct hash<char16_t>;
442template <> struct hash<char32_t>;
443template <> struct hash<wchar_t>;
444template <> struct hash<short>;
445template <> struct hash<unsigned short>;
446template <> struct hash<int>;
447template <> struct hash<unsigned int>;
448template <> struct hash<long>;
449template <> struct hash<long long>;
450template <> struct hash<unsigned long>;
451template <> struct hash<unsigned long long>;
452
453template <> struct hash<float>;
454template <> struct hash<double>;
455template <> struct hash<long double>;
456
457template<class T> struct hash<T*>;
458
459}  // std
460
461POLICY:  For non-variadic implementations, the number of arguments is limited
462         to 3.  It is hoped that the need for non-variadic implementations
463         will be minimal.
464
465*/
466
467#include <__config>
468#include <type_traits>
469#include <typeinfo>
470#include <exception>
471#include <memory>
472#include <tuple>
473
474#include <__functional_base>
475
476#pragma GCC system_header
477
478_LIBCPP_BEGIN_NAMESPACE_STD
479
480template <class _Tp>
481struct _LIBCPP_VISIBLE plus : binary_function<_Tp, _Tp, _Tp>
482{
483    _LIBCPP_INLINE_VISIBILITY _Tp operator()(const _Tp& __x, const _Tp& __y) const
484        {return __x + __y;}
485};
486
487template <class _Tp>
488struct _LIBCPP_VISIBLE minus : binary_function<_Tp, _Tp, _Tp>
489{
490    _LIBCPP_INLINE_VISIBILITY _Tp operator()(const _Tp& __x, const _Tp& __y) const
491        {return __x - __y;}
492};
493
494template <class _Tp>
495struct _LIBCPP_VISIBLE multiplies : binary_function<_Tp, _Tp, _Tp>
496{
497    _LIBCPP_INLINE_VISIBILITY _Tp operator()(const _Tp& __x, const _Tp& __y) const
498        {return __x * __y;}
499};
500
501template <class _Tp>
502struct _LIBCPP_VISIBLE divides : binary_function<_Tp, _Tp, _Tp>
503{
504    _LIBCPP_INLINE_VISIBILITY _Tp operator()(const _Tp& __x, const _Tp& __y) const
505        {return __x / __y;}
506};
507
508template <class _Tp>
509struct _LIBCPP_VISIBLE modulus : binary_function<_Tp, _Tp, _Tp>
510{
511    _LIBCPP_INLINE_VISIBILITY _Tp operator()(const _Tp& __x, const _Tp& __y) const
512        {return __x % __y;}
513};
514
515template <class _Tp>
516struct _LIBCPP_VISIBLE negate : unary_function<_Tp, _Tp>
517{
518    _LIBCPP_INLINE_VISIBILITY _Tp operator()(const _Tp& __x) const
519        {return -__x;}
520};
521
522template <class _Tp>
523struct _LIBCPP_VISIBLE equal_to : binary_function<_Tp, _Tp, bool>
524{
525    _LIBCPP_INLINE_VISIBILITY bool operator()(const _Tp& __x, const _Tp& __y) const
526        {return __x == __y;}
527};
528
529template <class _Tp>
530struct _LIBCPP_VISIBLE not_equal_to : binary_function<_Tp, _Tp, bool>
531{
532    _LIBCPP_INLINE_VISIBILITY bool operator()(const _Tp& __x, const _Tp& __y) const
533        {return __x != __y;}
534};
535
536template <class _Tp>
537struct _LIBCPP_VISIBLE greater : binary_function<_Tp, _Tp, bool>
538{
539    _LIBCPP_INLINE_VISIBILITY bool operator()(const _Tp& __x, const _Tp& __y) const
540        {return __x > __y;}
541};
542
543template <class _Tp>
544struct _LIBCPP_VISIBLE less : binary_function<_Tp, _Tp, bool>
545{
546    _LIBCPP_INLINE_VISIBILITY bool operator()(const _Tp& __x, const _Tp& __y) const
547        {return __x < __y;}
548};
549
550template <class _Tp>
551struct _LIBCPP_VISIBLE greater_equal : binary_function<_Tp, _Tp, bool>
552{
553    _LIBCPP_INLINE_VISIBILITY bool operator()(const _Tp& __x, const _Tp& __y) const
554        {return __x >= __y;}
555};
556
557template <class _Tp>
558struct _LIBCPP_VISIBLE less_equal : binary_function<_Tp, _Tp, bool>
559{
560    _LIBCPP_INLINE_VISIBILITY bool operator()(const _Tp& __x, const _Tp& __y) const
561        {return __x <= __y;}
562};
563
564template <class _Tp>
565struct _LIBCPP_VISIBLE logical_and : binary_function<_Tp, _Tp, bool>
566{
567    _LIBCPP_INLINE_VISIBILITY bool operator()(const _Tp& __x, const _Tp& __y) const
568        {return __x && __y;}
569};
570
571template <class _Tp>
572struct _LIBCPP_VISIBLE logical_or : binary_function<_Tp, _Tp, bool>
573{
574    _LIBCPP_INLINE_VISIBILITY bool operator()(const _Tp& __x, const _Tp& __y) const
575        {return __x || __y;}
576};
577
578template <class _Tp>
579struct _LIBCPP_VISIBLE logical_not : unary_function<_Tp, bool>
580{
581    _LIBCPP_INLINE_VISIBILITY bool operator()(const _Tp& __x) const
582        {return !__x;}
583};
584
585template <class _Tp>
586struct _LIBCPP_VISIBLE bit_and : binary_function<_Tp, _Tp, _Tp>
587{
588    _LIBCPP_INLINE_VISIBILITY _Tp operator()(const _Tp& __x, const _Tp& __y) const
589        {return __x & __y;}
590};
591
592template <class _Tp>
593struct _LIBCPP_VISIBLE bit_or : binary_function<_Tp, _Tp, _Tp>
594{
595    _LIBCPP_INLINE_VISIBILITY _Tp operator()(const _Tp& __x, const _Tp& __y) const
596        {return __x | __y;}
597};
598
599template <class _Tp>
600struct _LIBCPP_VISIBLE bit_xor : binary_function<_Tp, _Tp, _Tp>
601{
602    _LIBCPP_INLINE_VISIBILITY _Tp operator()(const _Tp& __x, const _Tp& __y) const
603        {return __x ^ __y;}
604};
605
606template <class _Predicate>
607class _LIBCPP_VISIBLE unary_negate
608    : public unary_function<typename _Predicate::argument_type, bool>
609{
610    _Predicate __pred_;
611public:
612    _LIBCPP_INLINE_VISIBILITY explicit unary_negate(const _Predicate& __pred)
613        : __pred_(__pred) {}
614    _LIBCPP_INLINE_VISIBILITY bool operator()(const typename _Predicate::argument_type& __x) const
615        {return !__pred_(__x);}
616};
617
618template <class _Predicate>
619inline _LIBCPP_INLINE_VISIBILITY
620unary_negate<_Predicate>
621not1(const _Predicate& __pred) {return unary_negate<_Predicate>(__pred);}
622
623template <class _Predicate>
624class _LIBCPP_VISIBLE binary_negate
625    : public binary_function<typename _Predicate::first_argument_type,
626                             typename _Predicate::second_argument_type,
627                             bool>
628{
629    _Predicate __pred_;
630public:
631    _LIBCPP_INLINE_VISIBILITY explicit binary_negate(const _Predicate& __pred)
632        : __pred_(__pred) {}
633    _LIBCPP_INLINE_VISIBILITY bool operator()(const typename _Predicate::first_argument_type& __x,
634                    const typename _Predicate::second_argument_type& __y) const
635        {return !__pred_(__x, __y);}
636};
637
638template <class _Predicate>
639inline _LIBCPP_INLINE_VISIBILITY
640binary_negate<_Predicate>
641not2(const _Predicate& __pred) {return binary_negate<_Predicate>(__pred);}
642
643template <class __Operation>
644class _LIBCPP_VISIBLE binder1st
645    : public unary_function<typename __Operation::second_argument_type,
646                            typename __Operation::result_type>
647{
648protected:
649    __Operation                               op;
650    typename __Operation::first_argument_type value;
651public:
652    _LIBCPP_INLINE_VISIBILITY binder1st(const __Operation& __x,
653                               const typename __Operation::first_argument_type __y)
654        : op(__x), value(__y) {}
655    _LIBCPP_INLINE_VISIBILITY typename __Operation::result_type operator()
656        (typename __Operation::second_argument_type& __x) const
657            {return op(value, __x);}
658    _LIBCPP_INLINE_VISIBILITY typename __Operation::result_type operator()
659        (const typename __Operation::second_argument_type& __x) const
660            {return op(value, __x);}
661};
662
663template <class __Operation, class _Tp>
664inline _LIBCPP_INLINE_VISIBILITY
665binder1st<__Operation>
666bind1st(const __Operation& __op, const _Tp& __x)
667    {return binder1st<__Operation>(__op, __x);}
668
669template <class __Operation>
670class _LIBCPP_VISIBLE binder2nd
671    : public unary_function<typename __Operation::first_argument_type,
672                            typename __Operation::result_type>
673{
674protected:
675    __Operation                                op;
676    typename __Operation::second_argument_type value;
677public:
678    _LIBCPP_INLINE_VISIBILITY
679    binder2nd(const __Operation& __x, const typename __Operation::second_argument_type __y)
680        : op(__x), value(__y) {}
681    _LIBCPP_INLINE_VISIBILITY typename __Operation::result_type operator()
682        (      typename __Operation::first_argument_type& __x) const
683            {return op(__x, value);}
684    _LIBCPP_INLINE_VISIBILITY typename __Operation::result_type operator()
685        (const typename __Operation::first_argument_type& __x) const
686            {return op(__x, value);}
687};
688
689template <class __Operation, class _Tp>
690inline _LIBCPP_INLINE_VISIBILITY
691binder2nd<__Operation>
692bind2nd(const __Operation& __op, const _Tp& __x)
693    {return binder2nd<__Operation>(__op, __x);}
694
695template <class _Arg, class _Result>
696class _LIBCPP_VISIBLE pointer_to_unary_function
697    : public unary_function<_Arg, _Result>
698{
699    _Result (*__f_)(_Arg);
700public:
701    _LIBCPP_INLINE_VISIBILITY explicit pointer_to_unary_function(_Result (*__f)(_Arg))
702        : __f_(__f) {}
703    _LIBCPP_INLINE_VISIBILITY _Result operator()(_Arg __x) const
704        {return __f_(__x);}
705};
706
707template <class _Arg, class _Result>
708inline _LIBCPP_INLINE_VISIBILITY
709pointer_to_unary_function<_Arg,_Result>
710ptr_fun(_Result (*__f)(_Arg))
711    {return pointer_to_unary_function<_Arg,_Result>(__f);}
712
713template <class _Arg1, class _Arg2, class _Result>
714class _LIBCPP_VISIBLE pointer_to_binary_function
715    : public binary_function<_Arg1, _Arg2, _Result>
716{
717    _Result (*__f_)(_Arg1, _Arg2);
718public:
719    _LIBCPP_INLINE_VISIBILITY explicit pointer_to_binary_function(_Result (*__f)(_Arg1, _Arg2))
720        : __f_(__f) {}
721    _LIBCPP_INLINE_VISIBILITY _Result operator()(_Arg1 __x, _Arg2 __y) const
722        {return __f_(__x, __y);}
723};
724
725template <class _Arg1, class _Arg2, class _Result>
726inline _LIBCPP_INLINE_VISIBILITY
727pointer_to_binary_function<_Arg1,_Arg2,_Result>
728ptr_fun(_Result (*__f)(_Arg1,_Arg2))
729    {return pointer_to_binary_function<_Arg1,_Arg2,_Result>(__f);}
730
731template<class _Sp, class _Tp>
732class _LIBCPP_VISIBLE mem_fun_t : public unary_function<_Tp*, _Sp>
733{
734    _Sp (_Tp::*__p_)();
735public:
736    _LIBCPP_INLINE_VISIBILITY explicit mem_fun_t(_Sp (_Tp::*__p)())
737        : __p_(__p) {}
738    _LIBCPP_INLINE_VISIBILITY _Sp operator()(_Tp* __p) const
739        {return (__p->*__p_)();}
740};
741
742template<class _Sp, class _Tp, class _Ap>
743class _LIBCPP_VISIBLE mem_fun1_t : public binary_function<_Tp*, _Ap, _Sp>
744{
745    _Sp (_Tp::*__p_)(_Ap);
746public:
747    _LIBCPP_INLINE_VISIBILITY explicit mem_fun1_t(_Sp (_Tp::*__p)(_Ap))
748        : __p_(__p) {}
749    _LIBCPP_INLINE_VISIBILITY _Sp operator()(_Tp* __p, _Ap __x) const
750        {return (__p->*__p_)(__x);}
751};
752
753template<class _Sp, class _Tp>
754inline _LIBCPP_INLINE_VISIBILITY
755mem_fun_t<_Sp,_Tp>
756mem_fun(_Sp (_Tp::*__f)())
757    {return mem_fun_t<_Sp,_Tp>(__f);}
758
759template<class _Sp, class _Tp, class _Ap>
760inline _LIBCPP_INLINE_VISIBILITY
761mem_fun1_t<_Sp,_Tp,_Ap>
762mem_fun(_Sp (_Tp::*__f)(_Ap))
763    {return mem_fun1_t<_Sp,_Tp,_Ap>(__f);}
764
765template<class _Sp, class _Tp>
766class _LIBCPP_VISIBLE mem_fun_ref_t : public unary_function<_Tp, _Sp>
767{
768    _Sp (_Tp::*__p_)();
769public:
770    _LIBCPP_INLINE_VISIBILITY explicit mem_fun_ref_t(_Sp (_Tp::*__p)())
771        : __p_(__p) {}
772    _LIBCPP_INLINE_VISIBILITY _Sp operator()(_Tp& __p) const
773        {return (__p.*__p_)();}
774};
775
776template<class _Sp, class _Tp, class _Ap>
777class _LIBCPP_VISIBLE mem_fun1_ref_t : public binary_function<_Tp, _Ap, _Sp>
778{
779    _Sp (_Tp::*__p_)(_Ap);
780public:
781    _LIBCPP_INLINE_VISIBILITY explicit mem_fun1_ref_t(_Sp (_Tp::*__p)(_Ap))
782        : __p_(__p) {}
783    _LIBCPP_INLINE_VISIBILITY _Sp operator()(_Tp& __p, _Ap __x) const
784        {return (__p.*__p_)(__x);}
785};
786
787template<class _Sp, class _Tp>
788inline _LIBCPP_INLINE_VISIBILITY
789mem_fun_ref_t<_Sp,_Tp>
790mem_fun_ref(_Sp (_Tp::*__f)())
791    {return mem_fun_ref_t<_Sp,_Tp>(__f);}
792
793template<class _Sp, class _Tp, class _Ap>
794inline _LIBCPP_INLINE_VISIBILITY
795mem_fun1_ref_t<_Sp,_Tp,_Ap>
796mem_fun_ref(_Sp (_Tp::*__f)(_Ap))
797    {return mem_fun1_ref_t<_Sp,_Tp,_Ap>(__f);}
798
799template <class _Sp, class _Tp>
800class _LIBCPP_VISIBLE const_mem_fun_t : public unary_function<const _Tp*, _Sp>
801{
802    _Sp (_Tp::*__p_)() const;
803public:
804    _LIBCPP_INLINE_VISIBILITY explicit const_mem_fun_t(_Sp (_Tp::*__p)() const)
805        : __p_(__p) {}
806    _LIBCPP_INLINE_VISIBILITY _Sp operator()(const _Tp* __p) const
807        {return (__p->*__p_)();}
808};
809
810template <class _Sp, class _Tp, class _Ap>
811class _LIBCPP_VISIBLE const_mem_fun1_t : public binary_function<const _Tp*, _Ap, _Sp>
812{
813    _Sp (_Tp::*__p_)(_Ap) const;
814public:
815    _LIBCPP_INLINE_VISIBILITY explicit const_mem_fun1_t(_Sp (_Tp::*__p)(_Ap) const)
816        : __p_(__p) {}
817    _LIBCPP_INLINE_VISIBILITY _Sp operator()(const _Tp* __p, _Ap __x) const
818        {return (__p->*__p_)(__x);}
819};
820
821template <class _Sp, class _Tp>
822inline _LIBCPP_INLINE_VISIBILITY
823const_mem_fun_t<_Sp,_Tp>
824mem_fun(_Sp (_Tp::*__f)() const)
825    {return const_mem_fun_t<_Sp,_Tp>(__f);}
826
827template <class _Sp, class _Tp, class _Ap>
828inline _LIBCPP_INLINE_VISIBILITY
829const_mem_fun1_t<_Sp,_Tp,_Ap>
830mem_fun(_Sp (_Tp::*__f)(_Ap) const)
831    {return const_mem_fun1_t<_Sp,_Tp,_Ap>(__f);}
832
833template <class _Sp, class _Tp>
834class _LIBCPP_VISIBLE const_mem_fun_ref_t : public unary_function<_Tp, _Sp>
835{
836    _Sp (_Tp::*__p_)() const;
837public:
838    _LIBCPP_INLINE_VISIBILITY explicit const_mem_fun_ref_t(_Sp (_Tp::*__p)() const)
839        : __p_(__p) {}
840    _LIBCPP_INLINE_VISIBILITY _Sp operator()(const _Tp& __p) const
841        {return (__p.*__p_)();}
842};
843
844template <class _Sp, class _Tp, class _Ap>
845class _LIBCPP_VISIBLE const_mem_fun1_ref_t
846    : public binary_function<_Tp, _Ap, _Sp>
847{
848    _Sp (_Tp::*__p_)(_Ap) const;
849public:
850    _LIBCPP_INLINE_VISIBILITY explicit const_mem_fun1_ref_t(_Sp (_Tp::*__p)(_Ap) const)
851        : __p_(__p) {}
852    _LIBCPP_INLINE_VISIBILITY _Sp operator()(const _Tp& __p, _Ap __x) const
853        {return (__p.*__p_)(__x);}
854};
855
856template <class _Sp, class _Tp>
857inline _LIBCPP_INLINE_VISIBILITY
858const_mem_fun_ref_t<_Sp,_Tp>
859mem_fun_ref(_Sp (_Tp::*__f)() const)
860    {return const_mem_fun_ref_t<_Sp,_Tp>(__f);}
861
862template <class _Sp, class _Tp, class _Ap>
863inline _LIBCPP_INLINE_VISIBILITY
864const_mem_fun1_ref_t<_Sp,_Tp,_Ap>
865mem_fun_ref(_Sp (_Tp::*__f)(_Ap) const)
866    {return const_mem_fun1_ref_t<_Sp,_Tp,_Ap>(__f);}
867
868#ifdef _LIBCPP_HAS_NO_VARIADICS
869
870#include <__functional_03>
871
872#else  // _LIBCPP_HAS_NO_VARIADICS
873
874template <class _Tp>
875class __mem_fn
876    : public __weak_result_type<_Tp>
877{
878public:
879    // types
880    typedef _Tp type;
881private:
882    type __f_;
883
884public:
885    _LIBCPP_INLINE_VISIBILITY __mem_fn(type __f) : __f_(__f) {}
886
887    // invoke
888    template <class... _ArgTypes>
889       _LIBCPP_INLINE_VISIBILITY
890       typename __invoke_return<type, _ArgTypes...>::type
891          operator() (_ArgTypes&&... __args)
892          {
893              return __invoke(__f_, _STD::forward<_ArgTypes>(__args)...);
894          }
895};
896
897template<class _R, class _T>
898inline _LIBCPP_INLINE_VISIBILITY
899__mem_fn<_R _T::*>
900mem_fn(_R _T::* __pm)
901{
902    return __mem_fn<_R _T::*>(__pm);
903}
904
905template<class _R, class _T, class ..._Args>
906inline _LIBCPP_INLINE_VISIBILITY
907__mem_fn<_R (_T::*)(_Args...)>
908mem_fn(_R (_T::* __pm)(_Args...))
909{
910    return __mem_fn<_R (_T::*)(_Args...)>(__pm);
911}
912
913template<class _R, class _T, class ..._Args>
914inline _LIBCPP_INLINE_VISIBILITY
915__mem_fn<_R (_T::*)(_Args...) const>
916mem_fn(_R (_T::* __pm)(_Args...) const)
917{
918    return __mem_fn<_R (_T::*)(_Args...) const>(__pm);
919}
920
921template<class _R, class _T, class ..._Args>
922inline _LIBCPP_INLINE_VISIBILITY
923__mem_fn<_R (_T::*)(_Args...) volatile>
924mem_fn(_R (_T::* __pm)(_Args...) volatile)
925{
926    return __mem_fn<_R (_T::*)(_Args...) volatile>(__pm);
927}
928
929template<class _R, class _T, class ..._Args>
930inline _LIBCPP_INLINE_VISIBILITY
931__mem_fn<_R (_T::*)(_Args...) const volatile>
932mem_fn(_R (_T::* __pm)(_Args...) const volatile)
933{
934    return __mem_fn<_R (_T::*)(_Args...) const volatile>(__pm);
935}
936
937// bad_function_call
938
939class _LIBCPP_EXCEPTION_ABI bad_function_call
940    : public exception
941{
942};
943
944template<class _Fp> class _LIBCPP_VISIBLE function; // undefined
945
946namespace __function
947{
948
949template<class _R, class ..._ArgTypes>
950struct __maybe_derive_from_unary_function
951{
952};
953
954template<class _R, class _A1>
955struct __maybe_derive_from_unary_function<_R(_A1)>
956    : public unary_function<_A1, _R>
957{
958};
959
960template<class _R, class ..._ArgTypes>
961struct __maybe_derive_from_binary_function
962{
963};
964
965template<class _R, class _A1, class _A2>
966struct __maybe_derive_from_binary_function<_R(_A1, _A2)>
967    : public binary_function<_A1, _A2, _R>
968{
969};
970
971template<class _Fp> class __base;
972
973template<class _R, class ..._ArgTypes>
974class __base<_R(_ArgTypes...)>
975{
976    __base(const __base&);
977    __base& operator=(const __base&);
978public:
979    _LIBCPP_INLINE_VISIBILITY __base() {}
980    _LIBCPP_INLINE_VISIBILITY virtual ~__base() {}
981    virtual __base* __clone() const = 0;
982    virtual void __clone(__base*) const = 0;
983    virtual void destroy() = 0;
984    virtual void destroy_deallocate() = 0;
985    virtual _R operator()(_ArgTypes&& ...) = 0;
986#ifndef _LIBCPP_NO_RTTI
987    virtual const void* target(const type_info&) const = 0;
988    virtual const std::type_info& target_type() const = 0;
989#endif  // _LIBCPP_NO_RTTI
990};
991
992template<class _FD, class _Alloc, class _FB> class __func;
993
994template<class _F, class _Alloc, class _R, class ..._ArgTypes>
995class __func<_F, _Alloc, _R(_ArgTypes...)>
996    : public  __base<_R(_ArgTypes...)>
997{
998    __compressed_pair<_F, _Alloc> __f_;
999public:
1000    _LIBCPP_INLINE_VISIBILITY
1001    explicit __func(_F __f) : __f_(_STD::move(__f)) {}
1002    _LIBCPP_INLINE_VISIBILITY
1003    explicit __func(_F __f, _Alloc __a) : __f_(_STD::move(__f), _STD::move(__a)) {}
1004    virtual __base<_R(_ArgTypes...)>* __clone() const;
1005    virtual void __clone(__base<_R(_ArgTypes...)>*) const;
1006    virtual void destroy();
1007    virtual void destroy_deallocate();
1008    virtual _R operator()(_ArgTypes&& ... __arg);
1009#ifndef _LIBCPP_NO_RTTI
1010    virtual const void* target(const type_info&) const;
1011    virtual const std::type_info& target_type() const;
1012#endif  // _LIBCPP_NO_RTTI
1013};
1014
1015template<class _F, class _Alloc, class _R, class ..._ArgTypes>
1016__base<_R(_ArgTypes...)>*
1017__func<_F, _Alloc, _R(_ArgTypes...)>::__clone() const
1018{
1019    typedef typename _Alloc::template rebind<__func>::other _A;
1020    _A __a(__f_.second());
1021    typedef __allocator_destructor<_A> _D;
1022    unique_ptr<__func, _D> __hold(__a.allocate(1), _D(__a, 1));
1023    ::new (__hold.get()) __func(__f_.first(), _Alloc(__a));
1024    return __hold.release();
1025}
1026
1027template<class _F, class _Alloc, class _R, class ..._ArgTypes>
1028void
1029__func<_F, _Alloc, _R(_ArgTypes...)>::__clone(__base<_R(_ArgTypes...)>* __p) const
1030{
1031    ::new (__p) __func(__f_.first(), __f_.second());
1032}
1033
1034template<class _F, class _Alloc, class _R, class ..._ArgTypes>
1035void
1036__func<_F, _Alloc, _R(_ArgTypes...)>::destroy()
1037{
1038    __f_.~__compressed_pair<_F, _Alloc>();
1039}
1040
1041template<class _F, class _Alloc, class _R, class ..._ArgTypes>
1042void
1043__func<_F, _Alloc, _R(_ArgTypes...)>::destroy_deallocate()
1044{
1045    typedef typename _Alloc::template rebind<__func>::other _A;
1046    _A __a(__f_.second());
1047    __f_.~__compressed_pair<_F, _Alloc>();
1048    __a.deallocate(this, 1);
1049}
1050
1051template<class _F, class _Alloc, class _R, class ..._ArgTypes>
1052_R
1053__func<_F, _Alloc, _R(_ArgTypes...)>::operator()(_ArgTypes&& ... __arg)
1054{
1055    return __invoke(__f_.first(), _STD::forward<_ArgTypes>(__arg)...);
1056}
1057
1058#ifndef _LIBCPP_NO_RTTI
1059
1060template<class _F, class _Alloc, class _R, class ..._ArgTypes>
1061const void*
1062__func<_F, _Alloc, _R(_ArgTypes...)>::target(const type_info& __ti) const
1063{
1064    if (__ti == typeid(_F))
1065        return &__f_.first();
1066    return (const void*)0;
1067}
1068
1069template<class _F, class _Alloc, class _R, class ..._ArgTypes>
1070const std::type_info&
1071__func<_F, _Alloc, _R(_ArgTypes...)>::target_type() const
1072{
1073    return typeid(_F);
1074}
1075
1076#endif  // _LIBCPP_NO_RTTI
1077
1078}  // __function
1079
1080template<class _R, class ..._ArgTypes>
1081class _LIBCPP_VISIBLE function<_R(_ArgTypes...)>
1082    : public __function::__maybe_derive_from_unary_function<_R(_ArgTypes...)>,
1083      public __function::__maybe_derive_from_binary_function<_R(_ArgTypes...)>
1084{
1085    typedef __function::__base<_R(_ArgTypes...)> __base;
1086    aligned_storage<3*sizeof(void*)>::type __buf_;
1087    __base* __f_;
1088
1089    template <class _F>
1090        _LIBCPP_INLINE_VISIBILITY
1091        static bool __not_null(const _F&) {return true;}
1092    template <class _R2, class ..._A>
1093        _LIBCPP_INLINE_VISIBILITY
1094        static bool __not_null(_R2 (*__p)(_A...)) {return __p;}
1095    template <class _R2, class _C, class ..._A>
1096        _LIBCPP_INLINE_VISIBILITY
1097        static bool __not_null(_R2 (_C::*__p)(_A...)) {return __p;}
1098    template <class _R2, class _C, class ..._A>
1099        _LIBCPP_INLINE_VISIBILITY
1100        static bool __not_null(_R2 (_C::*__p)(_A...) const) {return __p;}
1101    template <class _R2, class _C, class ..._A>
1102        _LIBCPP_INLINE_VISIBILITY
1103        static bool __not_null(_R2 (_C::*__p)(_A...) volatile) {return __p;}
1104    template <class _R2, class _C, class ..._A>
1105        _LIBCPP_INLINE_VISIBILITY
1106        static bool __not_null(_R2 (_C::*__p)(_A...) const volatile) {return __p;}
1107    template <class _R2, class ..._A>
1108        _LIBCPP_INLINE_VISIBILITY
1109        static bool __not_null(const function<_R(_A...)>& __p) {return __p;}
1110public:
1111    typedef _R result_type;
1112
1113    // construct/copy/destroy:
1114    _LIBCPP_INLINE_VISIBILITY
1115    function() : __f_(0) {}
1116    _LIBCPP_INLINE_VISIBILITY
1117    function(nullptr_t) : __f_(0) {}
1118    function(const function&);
1119    function(function&&);
1120    template<class _F>
1121      function(_F,
1122               typename enable_if<!is_integral<_F>::value>::type* = 0);
1123
1124    template<class _Alloc>
1125      _LIBCPP_INLINE_VISIBILITY
1126      function(allocator_arg_t, const _Alloc&) : __f_(0) {}
1127    template<class _Alloc>
1128      _LIBCPP_INLINE_VISIBILITY
1129      function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {}
1130    template<class _Alloc>
1131      function(allocator_arg_t, const _Alloc&, const function&);
1132    template<class _Alloc>
1133      function(allocator_arg_t, const _Alloc&, function&&);
1134    template<class _F, class _Alloc>
1135      function(allocator_arg_t, const _Alloc& __a, _F __f,
1136               typename enable_if<!is_integral<_F>::value>::type* = 0);
1137
1138    function& operator=(const function&);
1139    function& operator=(function&&);
1140    function& operator=(nullptr_t);
1141    template<class _F>
1142      typename enable_if
1143      <
1144        !is_integral<typename decay<_F>::type>::value,
1145        function&
1146      >::type
1147      operator=(_F&&);
1148
1149    ~function();
1150
1151    // function modifiers:
1152    void swap(function&);
1153    template<class _F, class _Alloc>
1154      _LIBCPP_INLINE_VISIBILITY
1155      void assign(_F&& __f, const _Alloc& __a)
1156        {function(allocator_arg, __a, _STD::forward<_F>(__f)).swap(*this);}
1157
1158    // function capacity:
1159    _LIBCPP_INLINE_VISIBILITY
1160    /*explicit*/ operator bool() const {return __f_;}
1161
1162    // deleted overloads close possible hole in the type system
1163    template<class _R2, class... _ArgTypes2>
1164      bool operator==(const function<_R2(_ArgTypes2...)>&) const = delete;
1165    template<class _R2, class... _ArgTypes2>
1166      bool operator!=(const function<_R2(_ArgTypes2...)>&) const = delete;
1167public:
1168    // function invocation:
1169    _R operator()(_ArgTypes...) const;
1170
1171#ifndef _LIBCPP_NO_RTTI
1172    // function target access:
1173    const std::type_info& target_type() const;
1174    template <typename _T> _T* target();
1175    template <typename _T> const _T* target() const;
1176#endif  // _LIBCPP_NO_RTTI
1177};
1178
1179template<class _R, class ..._ArgTypes>
1180function<_R(_ArgTypes...)>::function(const function& __f)
1181{
1182    if (__f.__f_ == 0)
1183        __f_ = 0;
1184    else if (__f.__f_ == (const __base*)&__f.__buf_)
1185    {
1186        __f_ = (__base*)&__buf_;
1187        __f.__f_->__clone(__f_);
1188    }
1189    else
1190        __f_ = __f.__f_->__clone();
1191}
1192
1193template<class _R, class ..._ArgTypes>
1194template <class _Alloc>
1195function<_R(_ArgTypes...)>::function(allocator_arg_t, const _Alloc&,
1196                                     const function& __f)
1197{
1198    if (__f.__f_ == 0)
1199        __f_ = 0;
1200    else if (__f.__f_ == (const __base*)&__f.__buf_)
1201    {
1202        __f_ = (__base*)&__buf_;
1203        __f.__f_->__clone(__f_);
1204    }
1205    else
1206        __f_ = __f.__f_->__clone();
1207}
1208
1209template<class _R, class ..._ArgTypes>
1210function<_R(_ArgTypes...)>::function(function&& __f)
1211{
1212    if (__f.__f_ == 0)
1213        __f_ = 0;
1214    else if (__f.__f_ == (__base*)&__f.__buf_)
1215    {
1216        __f_ = (__base*)&__buf_;
1217        __f.__f_->__clone(__f_);
1218    }
1219    else
1220    {
1221        __f_ = __f.__f_;
1222        __f.__f_ = 0;
1223    }
1224}
1225
1226template<class _R, class ..._ArgTypes>
1227template <class _Alloc>
1228function<_R(_ArgTypes...)>::function(allocator_arg_t, const _Alloc&,
1229                                     function&& __f)
1230{
1231    if (__f.__f_ == 0)
1232        __f_ = 0;
1233    else if (__f.__f_ == (__base*)&__f.__buf_)
1234    {
1235        __f_ = (__base*)&__buf_;
1236        __f.__f_->__clone(__f_);
1237    }
1238    else
1239    {
1240        __f_ = __f.__f_;
1241        __f.__f_ = 0;
1242    }
1243}
1244
1245template<class _R, class ..._ArgTypes>
1246template <class _F>
1247function<_R(_ArgTypes...)>::function(_F __f,
1248                                     typename enable_if<!is_integral<_F>::value>::type*)
1249    : __f_(0)
1250{
1251    if (__not_null(__f))
1252    {
1253        typedef __function::__func<_F, allocator<_F>, _R(_ArgTypes...)> _FF;
1254        if (sizeof(_FF) <= sizeof(__buf_))
1255        {
1256            __f_ = (__base*)&__buf_;
1257            ::new (__f_) _FF(_STD::move(__f));
1258        }
1259        else
1260        {
1261            typedef allocator<_FF> _A;
1262            _A __a;
1263            typedef __allocator_destructor<_A> _D;
1264            unique_ptr<__base, _D> __hold(__a.allocate(1), _D(__a, 1));
1265            ::new (__hold.get()) _FF(_STD::move(__f), allocator<_F>(__a));
1266            __f_ = __hold.release();
1267        }
1268    }
1269}
1270
1271template<class _R, class ..._ArgTypes>
1272template <class _F, class _Alloc>
1273function<_R(_ArgTypes...)>::function(allocator_arg_t, const _Alloc& __a0, _F __f,
1274                                     typename enable_if<!is_integral<_F>::value>::type*)
1275    : __f_(0)
1276{
1277    typedef allocator_traits<_Alloc> __alloc_traits;
1278    if (__not_null(__f))
1279    {
1280        typedef __function::__func<_F, _Alloc, _R(_ArgTypes...)> _FF;
1281        if (sizeof(_FF) <= sizeof(__buf_))
1282        {
1283            __f_ = (__base*)&__buf_;
1284            ::new (__f_) _FF(_STD::move(__f));
1285        }
1286        else
1287        {
1288            typedef typename __alloc_traits::template
1289#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1290                rebind_alloc<_FF>
1291#else
1292                rebind_alloc<_FF>::other
1293#endif
1294                                                         _A;
1295            _A __a(__a0);
1296            typedef __allocator_destructor<_A> _D;
1297            unique_ptr<__base, _D> __hold(__a.allocate(1), _D(__a, 1));
1298            ::new (__hold.get()) _FF(_STD::move(__f), _Alloc(__a));
1299            __f_ = __hold.release();
1300        }
1301    }
1302}
1303
1304template<class _R, class ..._ArgTypes>
1305function<_R(_ArgTypes...)>&
1306function<_R(_ArgTypes...)>::operator=(const function& __f)
1307{
1308    function(__f).swap(*this);
1309    return *this;
1310}
1311
1312template<class _R, class ..._ArgTypes>
1313function<_R(_ArgTypes...)>&
1314function<_R(_ArgTypes...)>::operator=(function&& __f)
1315{
1316    if (__f_ == (__base*)&__buf_)
1317        __f_->destroy();
1318    else if (__f_)
1319        __f_->destroy_deallocate();
1320    __f_ = 0;
1321    if (__f.__f_ == 0)
1322        __f_ = 0;
1323    else if (__f.__f_ == (__base*)&__f.__buf_)
1324    {
1325        __f_ = (__base*)&__buf_;
1326        __f.__f_->__clone(__f_);
1327    }
1328    else
1329    {
1330        __f_ = __f.__f_;
1331        __f.__f_ = 0;
1332    }
1333}
1334
1335template<class _R, class ..._ArgTypes>
1336function<_R(_ArgTypes...)>&
1337function<_R(_ArgTypes...)>::operator=(nullptr_t)
1338{
1339    if (__f_ == (__base*)&__buf_)
1340        __f_->destroy();
1341    else if (__f_)
1342        __f_->destroy_deallocate();
1343    __f_ = 0;
1344}
1345
1346template<class _R, class ..._ArgTypes>
1347template <class _F>
1348typename enable_if
1349<
1350    !is_integral<typename decay<_F>::type>::value,
1351    function<_R(_ArgTypes...)>&
1352>::type
1353function<_R(_ArgTypes...)>::operator=(_F&& __f)
1354{
1355    function(_STD::forward<_F>(__f)).swap(*this);
1356    return *this;
1357}
1358
1359template<class _R, class ..._ArgTypes>
1360function<_R(_ArgTypes...)>::~function()
1361{
1362    if (__f_ == (__base*)&__buf_)
1363        __f_->destroy();
1364    else if (__f_)
1365        __f_->destroy_deallocate();
1366}
1367
1368template<class _R, class ..._ArgTypes>
1369void
1370function<_R(_ArgTypes...)>::swap(function& __f)
1371{
1372    if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
1373    {
1374        typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1375        __base* __t = (__base*)&__tempbuf;
1376        __f_->__clone(__t);
1377        __f_->destroy();
1378        __f_ = 0;
1379        __f.__f_->__clone((__base*)&__buf_);
1380        __f.__f_->destroy();
1381        __f.__f_ = 0;
1382        __f_ = (__base*)&__buf_;
1383        __t->__clone((__base*)&__f.__buf_);
1384        __t->destroy();
1385        __f.__f_ = (__base*)&__f.__buf_;
1386    }
1387    else if (__f_ == (__base*)&__buf_)
1388    {
1389        __f_->__clone((__base*)&__f.__buf_);
1390        __f_->destroy();
1391        __f_ = __f.__f_;
1392        __f.__f_ = (__base*)&__f.__buf_;
1393    }
1394    else if (__f.__f_ == (__base*)&__f.__buf_)
1395    {
1396        __f.__f_->__clone((__base*)&__buf_);
1397        __f.__f_->destroy();
1398        __f.__f_ = __f_;
1399        __f_ = (__base*)&__buf_;
1400    }
1401    else
1402        _STD::swap(__f_, __f.__f_);
1403}
1404
1405template<class _R, class ..._ArgTypes>
1406_R
1407function<_R(_ArgTypes...)>::operator()(_ArgTypes... __arg) const
1408{
1409#ifndef _LIBCPP_NO_EXCEPTIONS
1410    if (__f_ == 0)
1411        throw bad_function_call();
1412#endif  // _LIBCPP_NO_EXCEPTIONS
1413    return (*__f_)(_STD::forward<_ArgTypes>(__arg)...);
1414}
1415
1416#ifndef _LIBCPP_NO_RTTI
1417
1418template<class _R, class ..._ArgTypes>
1419const std::type_info&
1420function<_R(_ArgTypes...)>::target_type() const
1421{
1422    if (__f_ == 0)
1423        return typeid(void);
1424    return __f_->target_type();
1425}
1426
1427template<class _R, class ..._ArgTypes>
1428template <typename _T>
1429_T*
1430function<_R(_ArgTypes...)>::target()
1431{
1432    if (__f_ == 0)
1433        return (_T*)0;
1434    return (_T*)__f_->target(typeid(_T));
1435}
1436
1437template<class _R, class ..._ArgTypes>
1438template <typename _T>
1439const _T*
1440function<_R(_ArgTypes...)>::target() const
1441{
1442    if (__f_ == 0)
1443        return (const _T*)0;
1444    return (const _T*)__f_->target(typeid(_T));
1445}
1446
1447#endif  // _LIBCPP_NO_RTTI
1448
1449template <class _R, class... _ArgTypes>
1450inline _LIBCPP_INLINE_VISIBILITY
1451bool
1452operator==(const function<_R(_ArgTypes...)>& __f, nullptr_t) {return !__f;}
1453
1454template <class _R, class... _ArgTypes>
1455inline _LIBCPP_INLINE_VISIBILITY
1456bool
1457operator==(nullptr_t, const function<_R(_ArgTypes...)>& __f) {return !__f;}
1458
1459template <class _R, class... _ArgTypes>
1460inline _LIBCPP_INLINE_VISIBILITY
1461bool
1462operator!=(const function<_R(_ArgTypes...)>& __f, nullptr_t) {return (bool)__f;}
1463
1464template <class _R, class... _ArgTypes>
1465inline _LIBCPP_INLINE_VISIBILITY
1466bool
1467operator!=(nullptr_t, const function<_R(_ArgTypes...)>& __f) {return (bool)__f;}
1468
1469template <class _R, class... _ArgTypes>
1470inline _LIBCPP_INLINE_VISIBILITY
1471void
1472swap(function<_R(_ArgTypes...)>& __x, function<_R(_ArgTypes...)>& __y)
1473{return __x.swap(__y);}
1474
1475template<class _Tp> struct __is_bind_expression : public false_type {};
1476template<class _Tp> struct _LIBCPP_VISIBLE is_bind_expression
1477    : public __is_bind_expression<typename remove_cv<_Tp>::type> {};
1478
1479template<class _Tp> struct __is_placeholder : public integral_constant<int, 0> {};
1480template<class _Tp> struct _LIBCPP_VISIBLE is_placeholder
1481    : public __is_placeholder<typename remove_cv<_Tp>::type> {};
1482
1483namespace placeholders
1484{
1485
1486template <int _N> struct __ph {};
1487
1488extern __ph<1>   _1;
1489extern __ph<2>   _2;
1490extern __ph<3>   _3;
1491extern __ph<4>   _4;
1492extern __ph<5>   _5;
1493extern __ph<6>   _6;
1494extern __ph<7>   _7;
1495extern __ph<8>   _8;
1496extern __ph<9>   _9;
1497extern __ph<10> _10;
1498
1499}  // placeholders
1500
1501template<int _N>
1502struct __is_placeholder<placeholders::__ph<_N> >
1503    : public integral_constant<int, _N> {};
1504
1505template <class _Tp, class _Uj>
1506inline _LIBCPP_INLINE_VISIBILITY
1507_Tp&
1508__mu(reference_wrapper<_Tp> __t, _Uj&)
1509{
1510    return __t.get();
1511}
1512
1513template <bool _IsBindExpr, class _Ti, class ..._Uj>
1514struct __mu_return1 {};
1515
1516template <class _Ti, class ..._Uj>
1517struct __mu_return1<true, _Ti, _Uj...>
1518{
1519    typedef typename result_of<_Ti(_Uj...)>::type type;
1520};
1521
1522template <class _Ti, class ..._Uj, size_t ..._Indx>
1523inline _LIBCPP_INLINE_VISIBILITY
1524typename __mu_return1<true, _Ti, _Uj...>::type
1525__mu_expand(_Ti& __ti, tuple<_Uj...>&& __uj, __tuple_indices<_Indx...>)
1526{
1527    return __ti(_STD::forward<typename tuple_element<_Indx, _Uj>::type>(get<_Indx>(__uj))...);
1528}
1529
1530template <class _Ti, class ..._Uj>
1531inline _LIBCPP_INLINE_VISIBILITY
1532typename enable_if
1533<
1534    is_bind_expression<_Ti>::value,
1535    typename __mu_return1<is_bind_expression<_Ti>::value, _Ti, _Uj...>::type
1536>::type
1537__mu(_Ti& __ti, tuple<_Uj...>& __uj)
1538{
1539    typedef typename __make_tuple_indices<sizeof...(_Uj)>::type __indices;
1540    return  __mu_expand(__ti, __uj, __indices());
1541}
1542
1543template <bool IsPh, class _Ti, class _Uj>
1544struct __mu_return2 {};
1545
1546template <class _Ti, class _Uj>
1547struct __mu_return2<true, _Ti, _Uj>
1548{
1549    typedef typename tuple_element<is_placeholder<_Ti>::value - 1, _Uj>::type type;
1550};
1551
1552template <class _Ti, class _Uj>
1553inline _LIBCPP_INLINE_VISIBILITY
1554typename enable_if
1555<
1556    0 < is_placeholder<_Ti>::value,
1557    typename __mu_return2<0 < is_placeholder<_Ti>::value, _Ti, _Uj>::type
1558>::type
1559__mu(_Ti&, _Uj& __uj)
1560{
1561    const size_t _Indx = is_placeholder<_Ti>::value - 1;
1562    return _STD::forward<typename tuple_element<_Indx, _Uj>::type>(get<_Indx>(__uj));
1563}
1564
1565template <class _Ti, class _Uj>
1566inline _LIBCPP_INLINE_VISIBILITY
1567typename enable_if
1568<
1569    !is_bind_expression<_Ti>::value &&
1570    is_placeholder<_Ti>::value == 0 &&
1571    !__is_reference_wrapper<_Ti>::value,
1572    _Ti&
1573>::type
1574__mu(_Ti& __ti, _Uj& __uj)
1575{
1576    return __ti;
1577}
1578
1579template <class _Ti, bool IsBindEx, bool IsPh, class _TupleUj>
1580struct ____mu_return;
1581
1582template <class _Ti, class ..._Uj>
1583struct ____mu_return<_Ti, true, false, tuple<_Uj...> >
1584{
1585    typedef typename result_of<_Ti(_Uj...)>::type type;
1586};
1587
1588template <class _Ti, class _TupleUj>
1589struct ____mu_return<_Ti, false, true, _TupleUj>
1590{
1591    typedef typename tuple_element<is_placeholder<_Ti>::value - 1,
1592                                   _TupleUj>::type&& type;
1593};
1594
1595template <class _Ti, class _TupleUj>
1596struct ____mu_return<_Ti, false, false, _TupleUj>
1597{
1598    typedef _Ti& type;
1599};
1600
1601template <class _Ti, class _TupleUj>
1602struct __mu_return
1603    : public ____mu_return<_Ti,
1604                           is_bind_expression<_Ti>::value,
1605                           0 < is_placeholder<_Ti>::value,
1606                           _TupleUj>
1607{
1608};
1609
1610template <class _Ti, class _TupleUj>
1611struct __mu_return<reference_wrapper<_Ti>, _TupleUj>
1612{
1613    typedef _Ti& type;
1614};
1615
1616template <class _F, class _BoundArgs, class _TupleUj>
1617struct __bind_return;
1618
1619template <class _F, class ..._BoundArgs, class _TupleUj>
1620struct __bind_return<_F, tuple<_BoundArgs...>, _TupleUj>
1621{
1622    typedef typename __invoke_return
1623    <
1624        _F&,
1625        typename __mu_return
1626        <
1627            _BoundArgs,
1628            _TupleUj
1629        >::type...
1630    >::type type;
1631};
1632
1633template <class _F, class ..._BoundArgs, class _TupleUj>
1634struct __bind_return<_F, const tuple<_BoundArgs...>, _TupleUj>
1635{
1636    typedef typename __invoke_return
1637    <
1638        _F&,
1639        typename __mu_return
1640        <
1641            const _BoundArgs,
1642            _TupleUj
1643        >::type...
1644    >::type type;
1645};
1646
1647template <class _F, class _BoundArgs, size_t ..._Indx, class _Args>
1648inline _LIBCPP_INLINE_VISIBILITY
1649typename __bind_return<_F, _BoundArgs, _Args>::type
1650__apply_functor(_F& __f, _BoundArgs& __bound_args, __tuple_indices<_Indx...>,
1651                _Args&& __args)
1652{
1653    return __invoke(__f, __mu(get<_Indx>(__bound_args), __args)...);
1654}
1655
1656template<class _F, class ..._BoundArgs>
1657class __bind
1658    : public __weak_result_type<_F>
1659{
1660    _F __f_;
1661    tuple<_BoundArgs...> __bound_args_;
1662
1663    typedef typename __make_tuple_indices<sizeof...(_BoundArgs)>::type __indices;
1664public:
1665    _LIBCPP_INLINE_VISIBILITY
1666    __bind(__bind&& __b)
1667        : __f_(_STD::move(__b.__f_)),
1668          __bound_args_(_STD::move(__b.__bound_args_)) {}
1669
1670    template <class _G, class ..._BA>
1671      _LIBCPP_INLINE_VISIBILITY
1672      explicit __bind(_G&& __f, _BA&& ...__bound_args)
1673        : __f_(_STD::forward<_G>(__f)),
1674          __bound_args_(_STD::forward<_BA>(__bound_args)...) {}
1675
1676    template <class ..._Args>
1677        _LIBCPP_INLINE_VISIBILITY
1678        typename __bind_return<_F, tuple<_BoundArgs...>, tuple<_Args&&...> >::type
1679        operator()(_Args&& ...__args)
1680        {
1681            // compiler bug workaround
1682            return __apply_functor(__f_, __bound_args_, __indices(),
1683                                  tuple<_Args&&...>(_STD::forward<_Args>(__args)...));
1684        }
1685
1686    template <class ..._Args>
1687        _LIBCPP_INLINE_VISIBILITY
1688        typename __bind_return<_F, tuple<_BoundArgs...>, tuple<_Args&&...> >::type
1689        operator()(_Args&& ...__args) const
1690        {
1691            return __apply_functor(__f_, __bound_args_, __indices(),
1692                                   tuple<_Args&&...>(_STD::forward<_Args>(__args)...));
1693        }
1694};
1695
1696template<class _F, class ..._BoundArgs>
1697struct __is_bind_expression<__bind<_F, _BoundArgs...> > : public true_type {};
1698
1699template<class _R, class _F, class ..._BoundArgs>
1700class __bind_r
1701    : public __bind<_F, _BoundArgs...>
1702{
1703    typedef __bind<_F, _BoundArgs...> base;
1704public:
1705    typedef _R result_type;
1706
1707    template <class _G, class ..._BA>
1708      _LIBCPP_INLINE_VISIBILITY
1709      explicit __bind_r(_G&& __f, _BA&& ...__bound_args)
1710        : base(_STD::forward<_G>(__f),
1711               _STD::forward<_BA>(__bound_args)...) {}
1712
1713    template <class ..._Args>
1714        _LIBCPP_INLINE_VISIBILITY
1715        result_type
1716        operator()(_Args&& ...__args)
1717        {
1718            return base::operator()(_STD::forward<_Args>(__args)...);
1719        }
1720
1721    template <class ..._Args>
1722        _LIBCPP_INLINE_VISIBILITY
1723        result_type
1724        operator()(_Args&& ...__args) const
1725        {
1726            return base::operator()(_STD::forward<_Args>(__args)...);
1727        }
1728};
1729
1730template<class _R, class _F, class ..._BoundArgs>
1731struct __is_bind_expression<__bind_r<_R, _F, _BoundArgs...> > : public true_type {};
1732
1733template<class _F, class ..._BoundArgs>
1734inline _LIBCPP_INLINE_VISIBILITY
1735__bind<typename decay<_F>::type, typename decay<_BoundArgs>::type...>
1736bind(_F&& __f, _BoundArgs&&... __bound_args)
1737{
1738    typedef __bind<typename decay<_F>::type, typename decay<_BoundArgs>::type...> type;
1739    return type(_STD::forward<_F>(__f), _STD::forward<_BoundArgs>(__bound_args)...);
1740}
1741
1742template<class _R, class _F, class ..._BoundArgs>
1743inline _LIBCPP_INLINE_VISIBILITY
1744__bind_r<_R, typename decay<_F>::type, typename decay<_BoundArgs>::type...>
1745bind(_F&& __f, _BoundArgs&&... __bound_args)
1746{
1747    typedef __bind_r<_R, typename decay<_F>::type, typename decay<_BoundArgs>::type...> type;
1748    return type(_STD::forward<_F>(__f), _STD::forward<_BoundArgs>(__bound_args)...);
1749}
1750
1751#endif  // _LIBCPP_HAS_NO_VARIADICS
1752
1753template <>
1754struct _LIBCPP_VISIBLE hash<bool>
1755    : public unary_function<bool, size_t>
1756{
1757    _LIBCPP_INLINE_VISIBILITY
1758    size_t operator()(bool __v) const {return static_cast<size_t>(__v);}
1759};
1760
1761template <>
1762struct _LIBCPP_VISIBLE hash<char>
1763    : public unary_function<char, size_t>
1764{
1765    _LIBCPP_INLINE_VISIBILITY
1766    size_t operator()(char __v) const {return static_cast<size_t>(__v);}
1767};
1768
1769template <>
1770struct _LIBCPP_VISIBLE hash<signed char>
1771    : public unary_function<signed char, size_t>
1772{
1773    _LIBCPP_INLINE_VISIBILITY
1774    size_t operator()(signed char __v) const {return static_cast<size_t>(__v);}
1775};
1776
1777template <>
1778struct _LIBCPP_VISIBLE hash<unsigned char>
1779    : public unary_function<unsigned char, size_t>
1780{
1781    _LIBCPP_INLINE_VISIBILITY
1782    size_t operator()(unsigned char __v) const {return static_cast<size_t>(__v);}
1783};
1784
1785#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
1786
1787template <>
1788struct _LIBCPP_VISIBLE hash<char16_t>
1789    : public unary_function<char16_t, size_t>
1790{
1791    _LIBCPP_INLINE_VISIBILITY
1792    size_t operator()(char16_t __v) const {return static_cast<size_t>(__v);}
1793};
1794
1795template <>
1796struct _LIBCPP_VISIBLE hash<char32_t>
1797    : public unary_function<char32_t, size_t>
1798{
1799    _LIBCPP_INLINE_VISIBILITY
1800    size_t operator()(char32_t __v) const {return static_cast<size_t>(__v);}
1801};
1802
1803#endif  // _LIBCPP_HAS_NO_UNICODE_CHARS
1804
1805template <>
1806struct _LIBCPP_VISIBLE hash<wchar_t>
1807    : public unary_function<wchar_t, size_t>
1808{
1809    _LIBCPP_INLINE_VISIBILITY
1810    size_t operator()(wchar_t __v) const {return static_cast<size_t>(__v);}
1811};
1812
1813template <>
1814struct _LIBCPP_VISIBLE hash<short>
1815    : public unary_function<short, size_t>
1816{
1817    _LIBCPP_INLINE_VISIBILITY
1818    size_t operator()(short __v) const {return static_cast<size_t>(__v);}
1819};
1820
1821template <>
1822struct _LIBCPP_VISIBLE hash<unsigned short>
1823    : public unary_function<unsigned short, size_t>
1824{
1825    _LIBCPP_INLINE_VISIBILITY
1826    size_t operator()(unsigned short __v) const {return static_cast<size_t>(__v);}
1827};
1828
1829template <>
1830struct _LIBCPP_VISIBLE hash<int>
1831    : public unary_function<int, size_t>
1832{
1833    _LIBCPP_INLINE_VISIBILITY
1834    size_t operator()(int __v) const {return static_cast<size_t>(__v);}
1835};
1836
1837template <>
1838struct _LIBCPP_VISIBLE hash<unsigned int>
1839    : public unary_function<unsigned int, size_t>
1840{
1841    _LIBCPP_INLINE_VISIBILITY
1842    size_t operator()(unsigned int __v) const {return static_cast<size_t>(__v);}
1843};
1844
1845template <>
1846struct _LIBCPP_VISIBLE hash<long>
1847    : public unary_function<long, size_t>
1848{
1849    _LIBCPP_INLINE_VISIBILITY
1850    size_t operator()(long __v) const {return static_cast<size_t>(__v);}
1851};
1852
1853template <>
1854struct _LIBCPP_VISIBLE hash<unsigned long>
1855    : public unary_function<unsigned long, size_t>
1856{
1857    _LIBCPP_INLINE_VISIBILITY
1858    size_t operator()(unsigned long __v) const {return static_cast<size_t>(__v);}
1859};
1860
1861template <>
1862struct _LIBCPP_VISIBLE hash<long long>
1863    : public unary_function<long long, size_t>
1864{
1865    _LIBCPP_INLINE_VISIBILITY
1866    size_t operator()(long long __v) const
1867    {
1868        size_t __r = 0;
1869        const size_t* const __p = reinterpret_cast<const size_t*>(&__v);
1870        for (unsigned __i = 0; __i < sizeof(argument_type)/sizeof(size_t); ++__i)
1871            __r ^= __p[__i];
1872        return __r;
1873    }
1874};
1875
1876template <>
1877struct _LIBCPP_VISIBLE hash<unsigned long long>
1878    : public unary_function<unsigned long long, size_t>
1879{
1880    _LIBCPP_INLINE_VISIBILITY
1881    size_t operator()(unsigned long long __v) const
1882    {
1883        size_t __r = 0;
1884        const size_t* const __p = reinterpret_cast<const size_t*>(&__v);
1885        for (unsigned __i = 0; __i < sizeof(argument_type)/sizeof(size_t); ++__i)
1886            __r ^= __p[__i];
1887        return __r;
1888    }
1889};
1890
1891template <>
1892struct _LIBCPP_VISIBLE hash<float>
1893    : public unary_function<float, size_t>
1894{
1895    _LIBCPP_INLINE_VISIBILITY
1896    size_t operator()(float __v) const
1897    {
1898        if (__v == 0)
1899            return 0;
1900        const size_t* const __p = reinterpret_cast<const size_t*>(&__v);
1901        return *__p;
1902    }
1903};
1904
1905template <>
1906struct _LIBCPP_VISIBLE hash<double>
1907    : public unary_function<double, size_t>
1908{
1909    _LIBCPP_INLINE_VISIBILITY
1910    size_t operator()(double __v) const
1911    {
1912        if (__v == 0)
1913            return 0;
1914        size_t __r = 0;
1915        const size_t* const __p = reinterpret_cast<const size_t*>(&__v);
1916        for (unsigned __i = 0; __i < sizeof(argument_type)/sizeof(size_t); ++__i)
1917            __r ^= __p[__i];
1918        return __r;
1919    }
1920};
1921
1922template <>
1923struct _LIBCPP_VISIBLE hash<long double>
1924    : public unary_function<long double, size_t>
1925{
1926    _LIBCPP_INLINE_VISIBILITY
1927    size_t operator()(long double __v) const
1928    {
1929        if (__v == 0)
1930            return 0;
1931        size_t __r = 0;
1932        const size_t* const __p = reinterpret_cast<const size_t*>(&__v);
1933        for (unsigned __i = 0; __i < sizeof(argument_type)/sizeof(size_t); ++__i)
1934            __r ^= __p[__i];
1935        return __r;
1936    }
1937};
1938
1939// struct hash<T*> in <memory>
1940
1941_LIBCPP_END_NAMESPACE_STD
1942
1943#endif  // _LIBCPP_FUNCTIONAL
1944