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