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