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