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