]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/libc++/include/__functional_03
Upgrade to OpenSSH 6.9p1.
[FreeBSD/FreeBSD.git] / contrib / libc++ / include / __functional_03
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
20 template <class _Tp>
21 class __mem_fn
22     : public __weak_result_type<_Tp>
23 {
24 public:
25     // types
26     typedef _Tp type;
27 private:
28     type __f_;
29
30 public:
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
63 template<class _Rp, class _Tp>
64 inline _LIBCPP_INLINE_VISIBILITY
65 __mem_fn<_Rp _Tp::*>
66 mem_fn(_Rp _Tp::* __pm)
67 {
68     return __mem_fn<_Rp _Tp::*>(__pm);
69 }
70
71 template<class _Rp, class _Tp>
72 inline _LIBCPP_INLINE_VISIBILITY
73 __mem_fn<_Rp (_Tp::*)()>
74 mem_fn(_Rp (_Tp::* __pm)())
75 {
76     return __mem_fn<_Rp (_Tp::*)()>(__pm);
77 }
78
79 template<class _Rp, class _Tp, class _A0>
80 inline _LIBCPP_INLINE_VISIBILITY
81 __mem_fn<_Rp (_Tp::*)(_A0)>
82 mem_fn(_Rp (_Tp::* __pm)(_A0))
83 {
84     return __mem_fn<_Rp (_Tp::*)(_A0)>(__pm);
85 }
86
87 template<class _Rp, class _Tp, class _A0, class _A1>
88 inline _LIBCPP_INLINE_VISIBILITY
89 __mem_fn<_Rp (_Tp::*)(_A0, _A1)>
90 mem_fn(_Rp (_Tp::* __pm)(_A0, _A1))
91 {
92     return __mem_fn<_Rp (_Tp::*)(_A0, _A1)>(__pm);
93 }
94
95 template<class _Rp, class _Tp, class _A0, class _A1, class _A2>
96 inline _LIBCPP_INLINE_VISIBILITY
97 __mem_fn<_Rp (_Tp::*)(_A0, _A1, _A2)>
98 mem_fn(_Rp (_Tp::* __pm)(_A0, _A1, _A2))
99 {
100     return __mem_fn<_Rp (_Tp::*)(_A0, _A1, _A2)>(__pm);
101 }
102
103 template<class _Rp, class _Tp>
104 inline _LIBCPP_INLINE_VISIBILITY
105 __mem_fn<_Rp (_Tp::*)() const>
106 mem_fn(_Rp (_Tp::* __pm)() const)
107 {
108     return __mem_fn<_Rp (_Tp::*)() const>(__pm);
109 }
110
111 template<class _Rp, class _Tp, class _A0>
112 inline _LIBCPP_INLINE_VISIBILITY
113 __mem_fn<_Rp (_Tp::*)(_A0) const>
114 mem_fn(_Rp (_Tp::* __pm)(_A0) const)
115 {
116     return __mem_fn<_Rp (_Tp::*)(_A0) const>(__pm);
117 }
118
119 template<class _Rp, class _Tp, class _A0, class _A1>
120 inline _LIBCPP_INLINE_VISIBILITY
121 __mem_fn<_Rp (_Tp::*)(_A0, _A1) const>
122 mem_fn(_Rp (_Tp::* __pm)(_A0, _A1) const)
123 {
124     return __mem_fn<_Rp (_Tp::*)(_A0, _A1) const>(__pm);
125 }
126
127 template<class _Rp, class _Tp, class _A0, class _A1, class _A2>
128 inline _LIBCPP_INLINE_VISIBILITY
129 __mem_fn<_Rp (_Tp::*)(_A0, _A1, _A2) const>
130 mem_fn(_Rp (_Tp::* __pm)(_A0, _A1, _A2) const)
131 {
132     return __mem_fn<_Rp (_Tp::*)(_A0, _A1, _A2) const>(__pm);
133 }
134
135 template<class _Rp, class _Tp>
136 inline _LIBCPP_INLINE_VISIBILITY
137 __mem_fn<_Rp (_Tp::*)() volatile>
138 mem_fn(_Rp (_Tp::* __pm)() volatile)
139 {
140     return __mem_fn<_Rp (_Tp::*)() volatile>(__pm);
141 }
142
143 template<class _Rp, class _Tp, class _A0>
144 inline _LIBCPP_INLINE_VISIBILITY
145 __mem_fn<_Rp (_Tp::*)(_A0) volatile>
146 mem_fn(_Rp (_Tp::* __pm)(_A0) volatile)
147 {
148     return __mem_fn<_Rp (_Tp::*)(_A0) volatile>(__pm);
149 }
150
151 template<class _Rp, class _Tp, class _A0, class _A1>
152 inline _LIBCPP_INLINE_VISIBILITY
153 __mem_fn<_Rp (_Tp::*)(_A0, _A1) volatile>
154 mem_fn(_Rp (_Tp::* __pm)(_A0, _A1) volatile)
155 {
156     return __mem_fn<_Rp (_Tp::*)(_A0, _A1) volatile>(__pm);
157 }
158
159 template<class _Rp, class _Tp, class _A0, class _A1, class _A2>
160 inline _LIBCPP_INLINE_VISIBILITY
161 __mem_fn<_Rp (_Tp::*)(_A0, _A1, _A2) volatile>
162 mem_fn(_Rp (_Tp::* __pm)(_A0, _A1, _A2) volatile)
163 {
164     return __mem_fn<_Rp (_Tp::*)(_A0, _A1, _A2) volatile>(__pm);
165 }
166
167 template<class _Rp, class _Tp>
168 inline _LIBCPP_INLINE_VISIBILITY
169 __mem_fn<_Rp (_Tp::*)() const volatile>
170 mem_fn(_Rp (_Tp::* __pm)() const volatile)
171 {
172     return __mem_fn<_Rp (_Tp::*)() const volatile>(__pm);
173 }
174
175 template<class _Rp, class _Tp, class _A0>
176 inline _LIBCPP_INLINE_VISIBILITY
177 __mem_fn<_Rp (_Tp::*)(_A0) const volatile>
178 mem_fn(_Rp (_Tp::* __pm)(_A0) const volatile)
179 {
180     return __mem_fn<_Rp (_Tp::*)(_A0) const volatile>(__pm);
181 }
182
183 template<class _Rp, class _Tp, class _A0, class _A1>
184 inline _LIBCPP_INLINE_VISIBILITY
185 __mem_fn<_Rp (_Tp::*)(_A0, _A1) const volatile>
186 mem_fn(_Rp (_Tp::* __pm)(_A0, _A1) const volatile)
187 {
188     return __mem_fn<_Rp (_Tp::*)(_A0, _A1) const volatile>(__pm);
189 }
190
191 template<class _Rp, class _Tp, class _A0, class _A1, class _A2>
192 inline _LIBCPP_INLINE_VISIBILITY
193 __mem_fn<_Rp (_Tp::*)(_A0, _A1, _A2) const volatile>
194 mem_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
201 class _LIBCPP_EXCEPTION_ABI bad_function_call
202     : public exception
203 {
204 };
205
206 template<class _Fp> class _LIBCPP_TYPE_VIS_ONLY function; // undefined
207
208 namespace __function
209 {
210
211 template<class _Fp>
212 struct __maybe_derive_from_unary_function
213 {
214 };
215
216 template<class _Rp, class _A1>
217 struct __maybe_derive_from_unary_function<_Rp(_A1)>
218     : public unary_function<_A1, _Rp>
219 {
220 };
221
222 template<class _Fp>
223 struct __maybe_derive_from_binary_function
224 {
225 };
226
227 template<class _Rp, class _A1, class _A2>
228 struct __maybe_derive_from_binary_function<_Rp(_A1, _A2)>
229     : public binary_function<_A1, _A2, _Rp>
230 {
231 };
232
233 template<class _Fp> class __base;
234
235 template<class _Rp>
236 class __base<_Rp()>
237 {
238     __base(const __base&);
239     __base& operator=(const __base&);
240 public:
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
254 template<class _Rp, class _A0>
255 class __base<_Rp(_A0)>
256 {
257     __base(const __base&);
258     __base& operator=(const __base&);
259 public:
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
273 template<class _Rp, class _A0, class _A1>
274 class __base<_Rp(_A0, _A1)>
275 {
276     __base(const __base&);
277     __base& operator=(const __base&);
278 public:
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
292 template<class _Rp, class _A0, class _A1, class _A2>
293 class __base<_Rp(_A0, _A1, _A2)>
294 {
295     __base(const __base&);
296     __base& operator=(const __base&);
297 public:
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
311 template<class _FD, class _Alloc, class _FB> class __func;
312
313 template<class _Fp, class _Alloc, class _Rp>
314 class __func<_Fp, _Alloc, _Rp()>
315     : public  __base<_Rp()>
316 {
317     __compressed_pair<_Fp, _Alloc> __f_;
318 public:
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
332 template<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
345 template<class _Fp, class _Alloc, class _Rp>
346 void
347 __func<_Fp, _Alloc, _Rp()>::__clone(__base<_Rp()>* __p) const
348 {
349     ::new (__p) __func(__f_.first(), __f_.second());
350 }
351
352 template<class _Fp, class _Alloc, class _Rp>
353 void
354 __func<_Fp, _Alloc, _Rp()>::destroy()
355 {
356     __f_.~__compressed_pair<_Fp, _Alloc>();
357 }
358
359 template<class _Fp, class _Alloc, class _Rp>
360 void
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
370 template<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
380 template<class _Fp, class _Alloc, class _Rp>
381 const 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
389 template<class _Fp, class _Alloc, class _Rp>
390 const std::type_info&
391 __func<_Fp, _Alloc, _Rp()>::target_type() const
392 {
393     return typeid(_Fp);
394 }
395
396 #endif  // _LIBCPP_NO_RTTI
397
398 template<class _Fp, class _Alloc, class _Rp, class _A0>
399 class __func<_Fp, _Alloc, _Rp(_A0)>
400     : public  __base<_Rp(_A0)>
401 {
402     __compressed_pair<_Fp, _Alloc> __f_;
403 public:
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
418 template<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
431 template<class _Fp, class _Alloc, class _Rp, class _A0>
432 void
433 __func<_Fp, _Alloc, _Rp(_A0)>::__clone(__base<_Rp(_A0)>* __p) const
434 {
435     ::new (__p) __func(__f_.first(), __f_.second());
436 }
437
438 template<class _Fp, class _Alloc, class _Rp, class _A0>
439 void
440 __func<_Fp, _Alloc, _Rp(_A0)>::destroy()
441 {
442     __f_.~__compressed_pair<_Fp, _Alloc>();
443 }
444
445 template<class _Fp, class _Alloc, class _Rp, class _A0>
446 void
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
456 template<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
466 template<class _Fp, class _Alloc, class _Rp, class _A0>
467 const 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
475 template<class _Fp, class _Alloc, class _Rp, class _A0>
476 const 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
484 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
485 class __func<_Fp, _Alloc, _Rp(_A0, _A1)>
486     : public  __base<_Rp(_A0, _A1)>
487 {
488     __compressed_pair<_Fp, _Alloc> __f_;
489 public:
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
504 template<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
517 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
518 void
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
524 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
525 void
526 __func<_Fp, _Alloc, _Rp(_A0, _A1)>::destroy()
527 {
528     __f_.~__compressed_pair<_Fp, _Alloc>();
529 }
530
531 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
532 void
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
542 template<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
552 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
553 const 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
561 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
562 const 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
570 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
571 class __func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>
572     : public  __base<_Rp(_A0, _A1, _A2)>
573 {
574     __compressed_pair<_Fp, _Alloc> __f_;
575 public:
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
590 template<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
603 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
604 void
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
610 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
611 void
612 __func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::destroy()
613 {
614     __f_.~__compressed_pair<_Fp, _Alloc>();
615 }
616
617 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
618 void
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
628 template<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
638 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
639 const 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
647 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
648 const 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
658 template<class _Rp>
659 class _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;}
674 public:
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
719 private:
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;
725 public:
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
737 template<class _Rp>
738 function<_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
751 template<class _Rp>
752 template<class _Alloc>
753 function<_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
766 template<class _Rp>
767 template <class _Fp>
768 function<_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
792 template<class _Rp>
793 template <class _Fp, class _Alloc>
794 function<_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
819 template<class _Rp>
820 function<_Rp()>&
821 function<_Rp()>::operator=(const function& __f)
822 {
823     function(__f).swap(*this);
824     return *this;
825 }
826
827 template<class _Rp>
828 function<_Rp()>&
829 function<_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
839 template<class _Rp>
840 template <class _Fp>
841 typename enable_if
842 <
843     !is_integral<_Fp>::value,
844     function<_Rp()>&
845 >::type
846 function<_Rp()>::operator=(_Fp __f)
847 {
848     function(_VSTD::move(__f)).swap(*this);
849     return *this;
850 }
851
852 template<class _Rp>
853 function<_Rp()>::~function()
854 {
855     if (__f_ == (__base*)&__buf_)
856         __f_->destroy();
857     else if (__f_)
858         __f_->destroy_deallocate();
859 }
860
861 template<class _Rp>
862 void
863 function<_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
898 template<class _Rp>
899 _Rp
900 function<_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
911 template<class _Rp>
912 const std::type_info&
913 function<_Rp()>::target_type() const
914 {
915     if (__f_ == 0)
916         return typeid(void);
917     return __f_->target_type();
918 }
919
920 template<class _Rp>
921 template <typename _Tp>
922 _Tp*
923 function<_Rp()>::target()
924 {
925     if (__f_ == 0)
926         return (_Tp*)0;
927     return (_Tp*)__f_->target(typeid(_Tp));
928 }
929
930 template<class _Rp>
931 template <typename _Tp>
932 const _Tp*
933 function<_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
942 template<class _Rp, class _A0>
943 class _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;}
971 public:
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
1016 private:
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;
1022 public:
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
1034 template<class _Rp, class _A0>
1035 function<_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
1048 template<class _Rp, class _A0>
1049 template<class _Alloc>
1050 function<_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
1063 template<class _Rp, class _A0>
1064 template <class _Fp>
1065 function<_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
1089 template<class _Rp, class _A0>
1090 template <class _Fp, class _Alloc>
1091 function<_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
1116 template<class _Rp, class _A0>
1117 function<_Rp(_A0)>&
1118 function<_Rp(_A0)>::operator=(const function& __f)
1119 {
1120     function(__f).swap(*this);
1121     return *this;
1122 }
1123
1124 template<class _Rp, class _A0>
1125 function<_Rp(_A0)>&
1126 function<_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
1136 template<class _Rp, class _A0>
1137 template <class _Fp>
1138 typename enable_if
1139 <
1140     !is_integral<_Fp>::value,
1141     function<_Rp(_A0)>&
1142 >::type
1143 function<_Rp(_A0)>::operator=(_Fp __f)
1144 {
1145     function(_VSTD::move(__f)).swap(*this);
1146     return *this;
1147 }
1148
1149 template<class _Rp, class _A0>
1150 function<_Rp(_A0)>::~function()
1151 {
1152     if (__f_ == (__base*)&__buf_)
1153         __f_->destroy();
1154     else if (__f_)
1155         __f_->destroy_deallocate();
1156 }
1157
1158 template<class _Rp, class _A0>
1159 void
1160 function<_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
1195 template<class _Rp, class _A0>
1196 _Rp
1197 function<_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
1208 template<class _Rp, class _A0>
1209 const std::type_info&
1210 function<_Rp(_A0)>::target_type() const
1211 {
1212     if (__f_ == 0)
1213         return typeid(void);
1214     return __f_->target_type();
1215 }
1216
1217 template<class _Rp, class _A0>
1218 template <typename _Tp>
1219 _Tp*
1220 function<_Rp(_A0)>::target()
1221 {
1222     if (__f_ == 0)
1223         return (_Tp*)0;
1224     return (_Tp*)__f_->target(typeid(_Tp));
1225 }
1226
1227 template<class _Rp, class _A0>
1228 template <typename _Tp>
1229 const _Tp*
1230 function<_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
1239 template<class _Rp, class _A0, class _A1>
1240 class _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;}
1268 public:
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
1313 private:
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;
1319 public:
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
1331 template<class _Rp, class _A0, class _A1>
1332 function<_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
1345 template<class _Rp, class _A0, class _A1>
1346 template<class _Alloc>
1347 function<_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
1360 template<class _Rp, class _A0, class _A1>
1361 template <class _Fp>
1362 function<_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
1386 template<class _Rp, class _A0, class _A1>
1387 template <class _Fp, class _Alloc>
1388 function<_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
1413 template<class _Rp, class _A0, class _A1>
1414 function<_Rp(_A0, _A1)>&
1415 function<_Rp(_A0, _A1)>::operator=(const function& __f)
1416 {
1417     function(__f).swap(*this);
1418     return *this;
1419 }
1420
1421 template<class _Rp, class _A0, class _A1>
1422 function<_Rp(_A0, _A1)>&
1423 function<_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
1433 template<class _Rp, class _A0, class _A1>
1434 template <class _Fp>
1435 typename enable_if
1436 <
1437     !is_integral<_Fp>::value,
1438     function<_Rp(_A0, _A1)>&
1439 >::type
1440 function<_Rp(_A0, _A1)>::operator=(_Fp __f)
1441 {
1442     function(_VSTD::move(__f)).swap(*this);
1443     return *this;
1444 }
1445
1446 template<class _Rp, class _A0, class _A1>
1447 function<_Rp(_A0, _A1)>::~function()
1448 {
1449     if (__f_ == (__base*)&__buf_)
1450         __f_->destroy();
1451     else if (__f_)
1452         __f_->destroy_deallocate();
1453 }
1454
1455 template<class _Rp, class _A0, class _A1>
1456 void
1457 function<_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
1492 template<class _Rp, class _A0, class _A1>
1493 _Rp
1494 function<_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
1505 template<class _Rp, class _A0, class _A1>
1506 const std::type_info&
1507 function<_Rp(_A0, _A1)>::target_type() const
1508 {
1509     if (__f_ == 0)
1510         return typeid(void);
1511     return __f_->target_type();
1512 }
1513
1514 template<class _Rp, class _A0, class _A1>
1515 template <typename _Tp>
1516 _Tp*
1517 function<_Rp(_A0, _A1)>::target()
1518 {
1519     if (__f_ == 0)
1520         return (_Tp*)0;
1521     return (_Tp*)__f_->target(typeid(_Tp));
1522 }
1523
1524 template<class _Rp, class _A0, class _A1>
1525 template <typename _Tp>
1526 const _Tp*
1527 function<_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
1536 template<class _Rp, class _A0, class _A1, class _A2>
1537 class _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;}
1564 public:
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
1609 private:
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;
1615 public:
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
1627 template<class _Rp, class _A0, class _A1, class _A2>
1628 function<_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
1641 template<class _Rp, class _A0, class _A1, class _A2>
1642 template<class _Alloc>
1643 function<_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
1657 template<class _Rp, class _A0, class _A1, class _A2>
1658 template <class _Fp>
1659 function<_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
1683 template<class _Rp, class _A0, class _A1, class _A2>
1684 template <class _Fp, class _Alloc>
1685 function<_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
1710 template<class _Rp, class _A0, class _A1, class _A2>
1711 function<_Rp(_A0, _A1, _A2)>&
1712 function<_Rp(_A0, _A1, _A2)>::operator=(const function& __f)
1713 {
1714     function(__f).swap(*this);
1715     return *this;
1716 }
1717
1718 template<class _Rp, class _A0, class _A1, class _A2>
1719 function<_Rp(_A0, _A1, _A2)>&
1720 function<_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
1730 template<class _Rp, class _A0, class _A1, class _A2>
1731 template <class _Fp>
1732 typename enable_if
1733 <
1734     !is_integral<_Fp>::value,
1735     function<_Rp(_A0, _A1, _A2)>&
1736 >::type
1737 function<_Rp(_A0, _A1, _A2)>::operator=(_Fp __f)
1738 {
1739     function(_VSTD::move(__f)).swap(*this);
1740     return *this;
1741 }
1742
1743 template<class _Rp, class _A0, class _A1, class _A2>
1744 function<_Rp(_A0, _A1, _A2)>::~function()
1745 {
1746     if (__f_ == (__base*)&__buf_)
1747         __f_->destroy();
1748     else if (__f_)
1749         __f_->destroy_deallocate();
1750 }
1751
1752 template<class _Rp, class _A0, class _A1, class _A2>
1753 void
1754 function<_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
1789 template<class _Rp, class _A0, class _A1, class _A2>
1790 _Rp
1791 function<_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
1802 template<class _Rp, class _A0, class _A1, class _A2>
1803 const std::type_info&
1804 function<_Rp(_A0, _A1, _A2)>::target_type() const
1805 {
1806     if (__f_ == 0)
1807         return typeid(void);
1808     return __f_->target_type();
1809 }
1810
1811 template<class _Rp, class _A0, class _A1, class _A2>
1812 template <typename _Tp>
1813 _Tp*
1814 function<_Rp(_A0, _A1, _A2)>::target()
1815 {
1816     if (__f_ == 0)
1817         return (_Tp*)0;
1818     return (_Tp*)__f_->target(typeid(_Tp));
1819 }
1820
1821 template<class _Rp, class _A0, class _A1, class _A2>
1822 template <typename _Tp>
1823 const _Tp*
1824 function<_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
1833 template <class _Fp>
1834 inline _LIBCPP_INLINE_VISIBILITY
1835 bool
1836 operator==(const function<_Fp>& __f, nullptr_t) {return !__f;}
1837
1838 template <class _Fp>
1839 inline _LIBCPP_INLINE_VISIBILITY
1840 bool
1841 operator==(nullptr_t, const function<_Fp>& __f) {return !__f;}
1842
1843 template <class _Fp>
1844 inline _LIBCPP_INLINE_VISIBILITY
1845 bool
1846 operator!=(const function<_Fp>& __f, nullptr_t) {return (bool)__f;}
1847
1848 template <class _Fp>
1849 inline _LIBCPP_INLINE_VISIBILITY
1850 bool
1851 operator!=(nullptr_t, const function<_Fp>& __f) {return (bool)__f;}
1852
1853 template <class _Fp>
1854 inline _LIBCPP_INLINE_VISIBILITY
1855 void
1856 swap(function<_Fp>& __x, function<_Fp>& __y)
1857 {return __x.swap(__y);}
1858
1859 template<class _Tp> struct __is_bind_expression : public false_type {};
1860 template<class _Tp> struct _LIBCPP_TYPE_VIS_ONLY is_bind_expression
1861     : public __is_bind_expression<typename remove_cv<_Tp>::type> {};
1862
1863 template<class _Tp> struct __is_placeholder : public integral_constant<int, 0> {};
1864 template<class _Tp> struct _LIBCPP_TYPE_VIS_ONLY is_placeholder
1865     : public __is_placeholder<typename remove_cv<_Tp>::type> {};
1866
1867 namespace placeholders
1868 {
1869
1870 template <int _Np> struct __ph {};
1871
1872 extern __ph<1>   _1;
1873 extern __ph<2>   _2;
1874 extern __ph<3>   _3;
1875 extern __ph<4>   _4;
1876 extern __ph<5>   _5;
1877 extern __ph<6>   _6;
1878 extern __ph<7>   _7;
1879 extern __ph<8>   _8;
1880 extern __ph<9>   _9;
1881 extern __ph<10> _10;
1882
1883 }  // placeholders
1884
1885 template<int _Np>
1886 struct __is_placeholder<placeholders::__ph<_Np> >
1887     : public integral_constant<int, _Np> {};
1888
1889 template <class _Tp, class _Uj>
1890 inline _LIBCPP_INLINE_VISIBILITY
1891 _Tp&
1892 __mu(reference_wrapper<_Tp> __t, _Uj&)
1893 {
1894     return __t.get();
1895 }
1896 /*
1897 template <bool _IsBindExpr, class _Ti, class ..._Uj>
1898 struct __mu_return1 {};
1899
1900 template <class _Ti, class ..._Uj>
1901 struct __mu_return1<true, _Ti, _Uj...>
1902 {
1903     typedef typename result_of<_Ti(_Uj...)>::type type;
1904 };
1905
1906 template <class _Ti, class ..._Uj, size_t ..._Indx>
1907 inline _LIBCPP_INLINE_VISIBILITY
1908 typename __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
1914 template <class _Ti, class ..._Uj>
1915 inline _LIBCPP_INLINE_VISIBILITY
1916 typename 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
1927 template <bool IsPh, class _Ti, class _Uj>
1928 struct __mu_return2 {};
1929
1930 template <class _Ti, class _Uj>
1931 struct __mu_return2<true, _Ti, _Uj>
1932 {
1933     typedef typename tuple_element<is_placeholder<_Ti>::value - 1, _Uj>::type type;
1934 };
1935
1936 template <class _Ti, class _Uj>
1937 inline _LIBCPP_INLINE_VISIBILITY
1938 typename 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
1952 template <class _Ti, class _Uj>
1953 inline _LIBCPP_INLINE_VISIBILITY
1954 typename 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
1966 template <class _Ti, bool IsBindEx, bool IsPh, class _TupleUj>
1967 struct ____mu_return;
1968
1969 template <class _Ti, class ..._Uj>
1970 struct ____mu_return<_Ti, true, false, tuple<_Uj...> >
1971 {
1972     typedef typename result_of<_Ti(_Uj...)>::type type;
1973 };
1974
1975 template <class _Ti, class _TupleUj>
1976 struct ____mu_return<_Ti, false, true, _TupleUj>
1977 {
1978     typedef typename tuple_element<is_placeholder<_Ti>::value - 1,
1979                                    _TupleUj>::type&& type;
1980 };
1981
1982 template <class _Ti, class _TupleUj>
1983 struct ____mu_return<_Ti, false, false, _TupleUj>
1984 {
1985     typedef _Ti& type;
1986 };
1987
1988 template <class _Ti, class _TupleUj>
1989 struct __mu_return
1990     : public ____mu_return<_Ti,
1991                            is_bind_expression<_Ti>::value,
1992                            0 < is_placeholder<_Ti>::value,
1993                            _TupleUj>
1994 {
1995 };
1996
1997 template <class _Ti, class _TupleUj>
1998 struct __mu_return<reference_wrapper<_Ti>, _TupleUj>
1999 {
2000     typedef _Ti& type;
2001 };
2002
2003 template <class _Fp, class _BoundArgs, class _TupleUj>
2004 struct __bind_return;
2005
2006 template <class _Fp, class ..._BoundArgs, class _TupleUj>
2007 struct __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
2020 template <class _Fp, class ..._BoundArgs, class _TupleUj>
2021 struct __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
2034 template <class _Fp, class _BoundArgs, size_t ..._Indx, class _Args>
2035 inline _LIBCPP_INLINE_VISIBILITY
2036 typename __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
2043 template<class _Fp, class ..._BoundArgs>
2044 class __bind
2045 {
2046     _Fp __f_;
2047     tuple<_BoundArgs...> __bound_args_;
2048
2049     typedef typename __make_tuple_indices<sizeof...(_BoundArgs)>::type __indices;
2050 public:
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
2074 template<class _Fp, class ..._BoundArgs>
2075 struct __is_bind_expression<__bind<_Fp, _BoundArgs...> > : public true_type {};
2076
2077 template<class _Rp, class _Fp, class ..._BoundArgs>
2078 class __bind_r
2079     : public __bind<_Fp, _BoundArgs...>
2080 {
2081     typedef __bind<_Fp, _BoundArgs...> base;
2082 public:
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
2107 template<class _Rp, class _Fp, class ..._BoundArgs>
2108 struct __is_bind_expression<__bind_r<_Rp, _Fp, _BoundArgs...> > : public true_type {};
2109
2110 template<class _Fp, class ..._BoundArgs>
2111 inline _LIBCPP_INLINE_VISIBILITY
2112 __bind<typename decay<_Fp>::type, typename decay<_BoundArgs>::type...>
2113 bind(_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
2119 template<class _Rp, class _Fp, class ..._BoundArgs>
2120 inline _LIBCPP_INLINE_VISIBILITY
2121 __bind_r<_Rp, typename decay<_Fp>::type, typename decay<_BoundArgs>::type...>
2122 bind(_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