1 // -*- C++ -*-
2 //===----------------------------------------------------------------------===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef _LIBCPP___FUNCTIONAL_FUNCTION_H
11 #define _LIBCPP___FUNCTIONAL_FUNCTION_H
12 
13 #include <__assert>
14 #include <__config>
15 #include <__functional/binary_function.h>
16 #include <__functional/invoke.h>
17 #include <__functional/unary_function.h>
18 #include <__iterator/iterator_traits.h>
19 #include <__memory/addressof.h>
20 #include <__memory/allocator_traits.h>
21 #include <__memory/compressed_pair.h>
22 #include <__memory/shared_ptr.h>
23 #include <__utility/forward.h>
24 #include <__utility/move.h>
25 #include <exception>
26 #include <memory> // TODO: replace with <__memory/__builtin_new_allocator.h>
27 #include <type_traits>
28 
29 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
30 #  pragma GCC system_header
31 #endif
32 
33 _LIBCPP_BEGIN_NAMESPACE_STD
34 
35 // bad_function_call
36 
37 class _LIBCPP_EXCEPTION_ABI bad_function_call
38     : public exception
39 {
40 public:
41 // Note that when a key function is not used, every translation unit that uses
42 // bad_function_call will end up containing a weak definition of the vtable and
43 // typeinfo.
44 #ifdef _LIBCPP_ABI_BAD_FUNCTION_CALL_KEY_FUNCTION
45     virtual ~bad_function_call() _NOEXCEPT;
46 #else
47     virtual ~bad_function_call() _NOEXCEPT {}
48 #endif
49 
50 #ifdef _LIBCPP_ABI_BAD_FUNCTION_CALL_GOOD_WHAT_MESSAGE
51     virtual const char* what() const _NOEXCEPT;
52 #endif
53 };
54 
55 _LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
56 void __throw_bad_function_call()
57 {
58 #ifndef _LIBCPP_NO_EXCEPTIONS
59     throw bad_function_call();
60 #else
61     _VSTD::abort();
62 #endif
63 }
64 
65 #if defined(_LIBCPP_CXX03_LANG) && !defined(_LIBCPP_DISABLE_DEPRECATION_WARNINGS) && __has_attribute(deprecated)
66 #   define _LIBCPP_DEPRECATED_CXX03_FUNCTION \
67         __attribute__((deprecated("Using std::function in C++03 is not supported anymore. Please upgrade to C++11 or later, or use a different type")))
68 #else
69 #   define _LIBCPP_DEPRECATED_CXX03_FUNCTION /* nothing */
70 #endif
71 
72 template<class _Fp> class _LIBCPP_DEPRECATED_CXX03_FUNCTION _LIBCPP_TEMPLATE_VIS function; // undefined
73 
74 namespace __function
75 {
76 
77 template<class _Rp>
78 struct __maybe_derive_from_unary_function
79 {
80 };
81 
82 template<class _Rp, class _A1>
83 struct __maybe_derive_from_unary_function<_Rp(_A1)>
84     : public unary_function<_A1, _Rp>
85 {
86 };
87 
88 template<class _Rp>
89 struct __maybe_derive_from_binary_function
90 {
91 };
92 
93 template<class _Rp, class _A1, class _A2>
94 struct __maybe_derive_from_binary_function<_Rp(_A1, _A2)>
95     : public binary_function<_A1, _A2, _Rp>
96 {
97 };
98 
99 template <class _Fp>
100 _LIBCPP_INLINE_VISIBILITY
101 bool __not_null(_Fp const&) { return true; }
102 
103 template <class _Fp>
104 _LIBCPP_INLINE_VISIBILITY
105 bool __not_null(_Fp* __ptr) { return __ptr; }
106 
107 template <class _Ret, class _Class>
108 _LIBCPP_INLINE_VISIBILITY
109 bool __not_null(_Ret _Class::*__ptr) { return __ptr; }
110 
111 template <class _Fp>
112 _LIBCPP_INLINE_VISIBILITY
113 bool __not_null(function<_Fp> const& __f) { return !!__f; }
114 
115 #ifdef _LIBCPP_HAS_EXTENSION_BLOCKS
116 template <class _Rp, class ..._Args>
117 _LIBCPP_INLINE_VISIBILITY
118 bool __not_null(_Rp (^__p)(_Args...)) { return __p; }
119 #endif
120 
121 } // namespace __function
122 
123 #ifndef _LIBCPP_CXX03_LANG
124 
125 namespace __function {
126 
127 // __alloc_func holds a functor and an allocator.
128 
129 template <class _Fp, class _Ap, class _FB> class __alloc_func;
130 template <class _Fp, class _FB>
131 class __default_alloc_func;
132 
133 template <class _Fp, class _Ap, class _Rp, class... _ArgTypes>
134 class __alloc_func<_Fp, _Ap, _Rp(_ArgTypes...)>
135 {
136     __compressed_pair<_Fp, _Ap> __f_;
137 
138   public:
139     typedef _LIBCPP_NODEBUG _Fp _Target;
140     typedef _LIBCPP_NODEBUG _Ap _Alloc;
141 
142     _LIBCPP_INLINE_VISIBILITY
143     const _Target& __target() const { return __f_.first(); }
144 
145     // WIN32 APIs may define __allocator, so use __get_allocator instead.
146     _LIBCPP_INLINE_VISIBILITY
147     const _Alloc& __get_allocator() const { return __f_.second(); }
148 
149     _LIBCPP_INLINE_VISIBILITY
150     explicit __alloc_func(_Target&& __f)
151         : __f_(piecewise_construct, _VSTD::forward_as_tuple(_VSTD::move(__f)),
152                _VSTD::forward_as_tuple())
153     {
154     }
155 
156     _LIBCPP_INLINE_VISIBILITY
157     explicit __alloc_func(const _Target& __f, const _Alloc& __a)
158         : __f_(piecewise_construct, _VSTD::forward_as_tuple(__f),
159                _VSTD::forward_as_tuple(__a))
160     {
161     }
162 
163     _LIBCPP_INLINE_VISIBILITY
164     explicit __alloc_func(const _Target& __f, _Alloc&& __a)
165         : __f_(piecewise_construct, _VSTD::forward_as_tuple(__f),
166                _VSTD::forward_as_tuple(_VSTD::move(__a)))
167     {
168     }
169 
170     _LIBCPP_INLINE_VISIBILITY
171     explicit __alloc_func(_Target&& __f, _Alloc&& __a)
172         : __f_(piecewise_construct, _VSTD::forward_as_tuple(_VSTD::move(__f)),
173                _VSTD::forward_as_tuple(_VSTD::move(__a)))
174     {
175     }
176 
177     _LIBCPP_INLINE_VISIBILITY
178     _Rp operator()(_ArgTypes&&... __arg)
179     {
180         typedef __invoke_void_return_wrapper<_Rp> _Invoker;
181         return _Invoker::__call(__f_.first(),
182                                 _VSTD::forward<_ArgTypes>(__arg)...);
183     }
184 
185     _LIBCPP_INLINE_VISIBILITY
186     __alloc_func* __clone() const
187     {
188         typedef allocator_traits<_Alloc> __alloc_traits;
189         typedef
190             typename __rebind_alloc_helper<__alloc_traits, __alloc_func>::type
191                 _AA;
192         _AA __a(__f_.second());
193         typedef __allocator_destructor<_AA> _Dp;
194         unique_ptr<__alloc_func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
195         ::new ((void*)__hold.get()) __alloc_func(__f_.first(), _Alloc(__a));
196         return __hold.release();
197     }
198 
199     _LIBCPP_INLINE_VISIBILITY
200     void destroy() _NOEXCEPT { __f_.~__compressed_pair<_Target, _Alloc>(); }
201 
202     static void __destroy_and_delete(__alloc_func* __f) {
203       typedef allocator_traits<_Alloc> __alloc_traits;
204       typedef typename __rebind_alloc_helper<__alloc_traits, __alloc_func>::type
205           _FunAlloc;
206       _FunAlloc __a(__f->__get_allocator());
207       __f->destroy();
208       __a.deallocate(__f, 1);
209     }
210 };
211 
212 template <class _Fp, class _Rp, class... _ArgTypes>
213 class __default_alloc_func<_Fp, _Rp(_ArgTypes...)> {
214   _Fp __f_;
215 
216 public:
217   typedef _LIBCPP_NODEBUG _Fp _Target;
218 
219   _LIBCPP_INLINE_VISIBILITY
220   const _Target& __target() const { return __f_; }
221 
222   _LIBCPP_INLINE_VISIBILITY
223   explicit __default_alloc_func(_Target&& __f) : __f_(_VSTD::move(__f)) {}
224 
225   _LIBCPP_INLINE_VISIBILITY
226   explicit __default_alloc_func(const _Target& __f) : __f_(__f) {}
227 
228   _LIBCPP_INLINE_VISIBILITY
229   _Rp operator()(_ArgTypes&&... __arg) {
230     typedef __invoke_void_return_wrapper<_Rp> _Invoker;
231     return _Invoker::__call(__f_, _VSTD::forward<_ArgTypes>(__arg)...);
232   }
233 
234   _LIBCPP_INLINE_VISIBILITY
235   __default_alloc_func* __clone() const {
236       __builtin_new_allocator::__holder_t __hold =
237         __builtin_new_allocator::__allocate_type<__default_alloc_func>(1);
238     __default_alloc_func* __res =
239         ::new ((void*)__hold.get()) __default_alloc_func(__f_);
240     (void)__hold.release();
241     return __res;
242   }
243 
244   _LIBCPP_INLINE_VISIBILITY
245   void destroy() _NOEXCEPT { __f_.~_Target(); }
246 
247   static void __destroy_and_delete(__default_alloc_func* __f) {
248     __f->destroy();
249       __builtin_new_allocator::__deallocate_type<__default_alloc_func>(__f, 1);
250   }
251 };
252 
253 // __base provides an abstract interface for copyable functors.
254 
255 template<class _Fp> class _LIBCPP_TEMPLATE_VIS __base;
256 
257 template<class _Rp, class ..._ArgTypes>
258 class __base<_Rp(_ArgTypes...)>
259 {
260     __base(const __base&);
261     __base& operator=(const __base&);
262 public:
263     _LIBCPP_INLINE_VISIBILITY __base() {}
264     _LIBCPP_INLINE_VISIBILITY virtual ~__base() {}
265     virtual __base* __clone() const = 0;
266     virtual void __clone(__base*) const = 0;
267     virtual void destroy() _NOEXCEPT = 0;
268     virtual void destroy_deallocate() _NOEXCEPT = 0;
269     virtual _Rp operator()(_ArgTypes&& ...) = 0;
270 #ifndef _LIBCPP_NO_RTTI
271     virtual const void* target(const type_info&) const _NOEXCEPT = 0;
272     virtual const std::type_info& target_type() const _NOEXCEPT = 0;
273 #endif // _LIBCPP_NO_RTTI
274 };
275 
276 // __func implements __base for a given functor type.
277 
278 template<class _FD, class _Alloc, class _FB> class __func;
279 
280 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
281 class __func<_Fp, _Alloc, _Rp(_ArgTypes...)>
282     : public  __base<_Rp(_ArgTypes...)>
283 {
284     __alloc_func<_Fp, _Alloc, _Rp(_ArgTypes...)> __f_;
285 public:
286     _LIBCPP_INLINE_VISIBILITY
287     explicit __func(_Fp&& __f)
288         : __f_(_VSTD::move(__f)) {}
289 
290     _LIBCPP_INLINE_VISIBILITY
291     explicit __func(const _Fp& __f, const _Alloc& __a)
292         : __f_(__f, __a) {}
293 
294     _LIBCPP_INLINE_VISIBILITY
295     explicit __func(const _Fp& __f, _Alloc&& __a)
296         : __f_(__f, _VSTD::move(__a)) {}
297 
298     _LIBCPP_INLINE_VISIBILITY
299     explicit __func(_Fp&& __f, _Alloc&& __a)
300         : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
301 
302     virtual __base<_Rp(_ArgTypes...)>* __clone() const;
303     virtual void __clone(__base<_Rp(_ArgTypes...)>*) const;
304     virtual void destroy() _NOEXCEPT;
305     virtual void destroy_deallocate() _NOEXCEPT;
306     virtual _Rp operator()(_ArgTypes&&... __arg);
307 #ifndef _LIBCPP_NO_RTTI
308     virtual const void* target(const type_info&) const _NOEXCEPT;
309     virtual const std::type_info& target_type() const _NOEXCEPT;
310 #endif // _LIBCPP_NO_RTTI
311 };
312 
313 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
314 __base<_Rp(_ArgTypes...)>*
315 __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__clone() const
316 {
317     typedef allocator_traits<_Alloc> __alloc_traits;
318     typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
319     _Ap __a(__f_.__get_allocator());
320     typedef __allocator_destructor<_Ap> _Dp;
321     unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
322     ::new ((void*)__hold.get()) __func(__f_.__target(), _Alloc(__a));
323     return __hold.release();
324 }
325 
326 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
327 void
328 __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__clone(__base<_Rp(_ArgTypes...)>* __p) const
329 {
330     ::new ((void*)__p) __func(__f_.__target(), __f_.__get_allocator());
331 }
332 
333 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
334 void
335 __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy() _NOEXCEPT
336 {
337     __f_.destroy();
338 }
339 
340 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
341 void
342 __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy_deallocate() _NOEXCEPT
343 {
344     typedef allocator_traits<_Alloc> __alloc_traits;
345     typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
346     _Ap __a(__f_.__get_allocator());
347     __f_.destroy();
348     __a.deallocate(this, 1);
349 }
350 
351 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
352 _Rp
353 __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::operator()(_ArgTypes&& ... __arg)
354 {
355     return __f_(_VSTD::forward<_ArgTypes>(__arg)...);
356 }
357 
358 #ifndef _LIBCPP_NO_RTTI
359 
360 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
361 const void*
362 __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::target(const type_info& __ti) const _NOEXCEPT
363 {
364     if (__ti == typeid(_Fp))
365         return _VSTD::addressof(__f_.__target());
366     return nullptr;
367 }
368 
369 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
370 const std::type_info&
371 __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::target_type() const _NOEXCEPT
372 {
373     return typeid(_Fp);
374 }
375 
376 #endif // _LIBCPP_NO_RTTI
377 
378 // __value_func creates a value-type from a __func.
379 
380 template <class _Fp> class __value_func;
381 
382 template <class _Rp, class... _ArgTypes> class __value_func<_Rp(_ArgTypes...)>
383 {
384     typename aligned_storage<3 * sizeof(void*)>::type __buf_;
385 
386     typedef __base<_Rp(_ArgTypes...)> __func;
387     __func* __f_;
388 
389     _LIBCPP_NO_CFI static __func* __as_base(void* p)
390     {
391         return reinterpret_cast<__func*>(p);
392     }
393 
394   public:
395     _LIBCPP_INLINE_VISIBILITY
396     __value_func() _NOEXCEPT : __f_(nullptr) {}
397 
398     template <class _Fp, class _Alloc>
399     _LIBCPP_INLINE_VISIBILITY __value_func(_Fp&& __f, const _Alloc& __a)
400         : __f_(nullptr)
401     {
402         typedef allocator_traits<_Alloc> __alloc_traits;
403         typedef __function::__func<_Fp, _Alloc, _Rp(_ArgTypes...)> _Fun;
404         typedef typename __rebind_alloc_helper<__alloc_traits, _Fun>::type
405             _FunAlloc;
406 
407         if (__function::__not_null(__f))
408         {
409             _FunAlloc __af(__a);
410             if (sizeof(_Fun) <= sizeof(__buf_) &&
411                 is_nothrow_copy_constructible<_Fp>::value &&
412                 is_nothrow_copy_constructible<_FunAlloc>::value)
413             {
414                 __f_ =
415                     ::new ((void*)&__buf_) _Fun(_VSTD::move(__f), _Alloc(__af));
416             }
417             else
418             {
419                 typedef __allocator_destructor<_FunAlloc> _Dp;
420                 unique_ptr<__func, _Dp> __hold(__af.allocate(1), _Dp(__af, 1));
421                 ::new ((void*)__hold.get()) _Fun(_VSTD::move(__f), _Alloc(__a));
422                 __f_ = __hold.release();
423             }
424         }
425     }
426 
427     template <class _Fp,
428         class = typename enable_if<!is_same<typename decay<_Fp>::type, __value_func>::value>::type>
429     _LIBCPP_INLINE_VISIBILITY explicit __value_func(_Fp&& __f)
430         : __value_func(_VSTD::forward<_Fp>(__f), allocator<_Fp>()) {}
431 
432     _LIBCPP_INLINE_VISIBILITY
433     __value_func(const __value_func& __f)
434     {
435         if (__f.__f_ == nullptr)
436             __f_ = nullptr;
437         else if ((void*)__f.__f_ == &__f.__buf_)
438         {
439             __f_ = __as_base(&__buf_);
440             __f.__f_->__clone(__f_);
441         }
442         else
443             __f_ = __f.__f_->__clone();
444     }
445 
446     _LIBCPP_INLINE_VISIBILITY
447     __value_func(__value_func&& __f) _NOEXCEPT
448     {
449         if (__f.__f_ == nullptr)
450             __f_ = nullptr;
451         else if ((void*)__f.__f_ == &__f.__buf_)
452         {
453             __f_ = __as_base(&__buf_);
454             __f.__f_->__clone(__f_);
455         }
456         else
457         {
458             __f_ = __f.__f_;
459             __f.__f_ = nullptr;
460         }
461     }
462 
463     _LIBCPP_INLINE_VISIBILITY
464     ~__value_func()
465     {
466         if ((void*)__f_ == &__buf_)
467             __f_->destroy();
468         else if (__f_)
469             __f_->destroy_deallocate();
470     }
471 
472     _LIBCPP_INLINE_VISIBILITY
473     __value_func& operator=(__value_func&& __f)
474     {
475         *this = nullptr;
476         if (__f.__f_ == nullptr)
477             __f_ = nullptr;
478         else if ((void*)__f.__f_ == &__f.__buf_)
479         {
480             __f_ = __as_base(&__buf_);
481             __f.__f_->__clone(__f_);
482         }
483         else
484         {
485             __f_ = __f.__f_;
486             __f.__f_ = nullptr;
487         }
488         return *this;
489     }
490 
491     _LIBCPP_INLINE_VISIBILITY
492     __value_func& operator=(nullptr_t)
493     {
494         __func* __f = __f_;
495         __f_ = nullptr;
496         if ((void*)__f == &__buf_)
497             __f->destroy();
498         else if (__f)
499             __f->destroy_deallocate();
500         return *this;
501     }
502 
503     _LIBCPP_INLINE_VISIBILITY
504     _Rp operator()(_ArgTypes&&... __args) const
505     {
506         if (__f_ == nullptr)
507             __throw_bad_function_call();
508         return (*__f_)(_VSTD::forward<_ArgTypes>(__args)...);
509     }
510 
511     _LIBCPP_INLINE_VISIBILITY
512     void swap(__value_func& __f) _NOEXCEPT
513     {
514         if (&__f == this)
515             return;
516         if ((void*)__f_ == &__buf_ && (void*)__f.__f_ == &__f.__buf_)
517         {
518             typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
519             __func* __t = __as_base(&__tempbuf);
520             __f_->__clone(__t);
521             __f_->destroy();
522             __f_ = nullptr;
523             __f.__f_->__clone(__as_base(&__buf_));
524             __f.__f_->destroy();
525             __f.__f_ = nullptr;
526             __f_ = __as_base(&__buf_);
527             __t->__clone(__as_base(&__f.__buf_));
528             __t->destroy();
529             __f.__f_ = __as_base(&__f.__buf_);
530         }
531         else if ((void*)__f_ == &__buf_)
532         {
533             __f_->__clone(__as_base(&__f.__buf_));
534             __f_->destroy();
535             __f_ = __f.__f_;
536             __f.__f_ = __as_base(&__f.__buf_);
537         }
538         else if ((void*)__f.__f_ == &__f.__buf_)
539         {
540             __f.__f_->__clone(__as_base(&__buf_));
541             __f.__f_->destroy();
542             __f.__f_ = __f_;
543             __f_ = __as_base(&__buf_);
544         }
545         else
546             _VSTD::swap(__f_, __f.__f_);
547     }
548 
549     _LIBCPP_INLINE_VISIBILITY
550     explicit operator bool() const _NOEXCEPT { return __f_ != nullptr; }
551 
552 #ifndef _LIBCPP_NO_RTTI
553     _LIBCPP_INLINE_VISIBILITY
554     const std::type_info& target_type() const _NOEXCEPT
555     {
556         if (__f_ == nullptr)
557             return typeid(void);
558         return __f_->target_type();
559     }
560 
561     template <typename _Tp>
562     _LIBCPP_INLINE_VISIBILITY const _Tp* target() const _NOEXCEPT
563     {
564         if (__f_ == nullptr)
565             return nullptr;
566         return (const _Tp*)__f_->target(typeid(_Tp));
567     }
568 #endif // _LIBCPP_NO_RTTI
569 };
570 
571 // Storage for a functor object, to be used with __policy to manage copy and
572 // destruction.
573 union __policy_storage
574 {
575     mutable char __small[sizeof(void*) * 2];
576     void* __large;
577 };
578 
579 // True if _Fun can safely be held in __policy_storage.__small.
580 template <typename _Fun>
581 struct __use_small_storage
582     : public integral_constant<
583           bool, sizeof(_Fun) <= sizeof(__policy_storage) &&
584                     _LIBCPP_ALIGNOF(_Fun) <= _LIBCPP_ALIGNOF(__policy_storage) &&
585                     is_trivially_copy_constructible<_Fun>::value &&
586                     is_trivially_destructible<_Fun>::value> {};
587 
588 // Policy contains information about how to copy, destroy, and move the
589 // underlying functor. You can think of it as a vtable of sorts.
590 struct __policy
591 {
592     // Used to copy or destroy __large values. null for trivial objects.
593     void* (*const __clone)(const void*);
594     void (*const __destroy)(void*);
595 
596     // True if this is the null policy (no value).
597     const bool __is_null;
598 
599     // The target type. May be null if RTTI is disabled.
600     const std::type_info* const __type_info;
601 
602     // Returns a pointer to a static policy object suitable for the functor
603     // type.
604     template <typename _Fun>
605     _LIBCPP_INLINE_VISIBILITY static const __policy* __create()
606     {
607         return __choose_policy<_Fun>(__use_small_storage<_Fun>());
608     }
609 
610     _LIBCPP_INLINE_VISIBILITY
611     static const __policy* __create_empty()
612     {
613         static const _LIBCPP_CONSTEXPR __policy __policy_ = {nullptr, nullptr,
614                                                              true,
615 #ifndef _LIBCPP_NO_RTTI
616                                                              &typeid(void)
617 #else
618                                                              nullptr
619 #endif
620         };
621         return &__policy_;
622     }
623 
624   private:
625     template <typename _Fun> static void* __large_clone(const void* __s)
626     {
627         const _Fun* __f = static_cast<const _Fun*>(__s);
628         return __f->__clone();
629     }
630 
631     template <typename _Fun>
632     static void __large_destroy(void* __s) {
633       _Fun::__destroy_and_delete(static_cast<_Fun*>(__s));
634     }
635 
636     template <typename _Fun>
637     _LIBCPP_INLINE_VISIBILITY static const __policy*
638     __choose_policy(/* is_small = */ false_type) {
639       static const _LIBCPP_CONSTEXPR __policy __policy_ = {
640           &__large_clone<_Fun>, &__large_destroy<_Fun>, false,
641 #ifndef _LIBCPP_NO_RTTI
642           &typeid(typename _Fun::_Target)
643 #else
644           nullptr
645 #endif
646       };
647         return &__policy_;
648     }
649 
650     template <typename _Fun>
651     _LIBCPP_INLINE_VISIBILITY static const __policy*
652         __choose_policy(/* is_small = */ true_type)
653     {
654         static const _LIBCPP_CONSTEXPR __policy __policy_ = {
655             nullptr, nullptr, false,
656 #ifndef _LIBCPP_NO_RTTI
657             &typeid(typename _Fun::_Target)
658 #else
659             nullptr
660 #endif
661         };
662         return &__policy_;
663     }
664 };
665 
666 // Used to choose between perfect forwarding or pass-by-value. Pass-by-value is
667 // faster for types that can be passed in registers.
668 template <typename _Tp>
669 using __fast_forward =
670     typename conditional<is_scalar<_Tp>::value, _Tp, _Tp&&>::type;
671 
672 // __policy_invoker calls an instance of __alloc_func held in __policy_storage.
673 
674 template <class _Fp> struct __policy_invoker;
675 
676 template <class _Rp, class... _ArgTypes>
677 struct __policy_invoker<_Rp(_ArgTypes...)>
678 {
679     typedef _Rp (*__Call)(const __policy_storage*,
680                           __fast_forward<_ArgTypes>...);
681 
682     __Call __call_;
683 
684     // Creates an invoker that throws bad_function_call.
685     _LIBCPP_INLINE_VISIBILITY
686     __policy_invoker() : __call_(&__call_empty) {}
687 
688     // Creates an invoker that calls the given instance of __func.
689     template <typename _Fun>
690     _LIBCPP_INLINE_VISIBILITY static __policy_invoker __create()
691     {
692         return __policy_invoker(&__call_impl<_Fun>);
693     }
694 
695   private:
696     _LIBCPP_INLINE_VISIBILITY
697     explicit __policy_invoker(__Call __c) : __call_(__c) {}
698 
699     static _Rp __call_empty(const __policy_storage*,
700                             __fast_forward<_ArgTypes>...)
701     {
702         __throw_bad_function_call();
703     }
704 
705     template <typename _Fun>
706     static _Rp __call_impl(const __policy_storage* __buf,
707                            __fast_forward<_ArgTypes>... __args)
708     {
709         _Fun* __f = reinterpret_cast<_Fun*>(__use_small_storage<_Fun>::value
710                                                 ? &__buf->__small
711                                                 : __buf->__large);
712         return (*__f)(_VSTD::forward<_ArgTypes>(__args)...);
713     }
714 };
715 
716 // __policy_func uses a __policy and __policy_invoker to create a type-erased,
717 // copyable functor.
718 
719 template <class _Fp> class __policy_func;
720 
721 template <class _Rp, class... _ArgTypes> class __policy_func<_Rp(_ArgTypes...)>
722 {
723     // Inline storage for small objects.
724     __policy_storage __buf_;
725 
726     // Calls the value stored in __buf_. This could technically be part of
727     // policy, but storing it here eliminates a level of indirection inside
728     // operator().
729     typedef __function::__policy_invoker<_Rp(_ArgTypes...)> __invoker;
730     __invoker __invoker_;
731 
732     // The policy that describes how to move / copy / destroy __buf_. Never
733     // null, even if the function is empty.
734     const __policy* __policy_;
735 
736   public:
737     _LIBCPP_INLINE_VISIBILITY
738     __policy_func() : __policy_(__policy::__create_empty()) {}
739 
740     template <class _Fp, class _Alloc>
741     _LIBCPP_INLINE_VISIBILITY __policy_func(_Fp&& __f, const _Alloc& __a)
742         : __policy_(__policy::__create_empty())
743     {
744         typedef __alloc_func<_Fp, _Alloc, _Rp(_ArgTypes...)> _Fun;
745         typedef allocator_traits<_Alloc> __alloc_traits;
746         typedef typename __rebind_alloc_helper<__alloc_traits, _Fun>::type
747             _FunAlloc;
748 
749         if (__function::__not_null(__f))
750         {
751             __invoker_ = __invoker::template __create<_Fun>();
752             __policy_ = __policy::__create<_Fun>();
753 
754             _FunAlloc __af(__a);
755             if (__use_small_storage<_Fun>())
756             {
757                 ::new ((void*)&__buf_.__small)
758                     _Fun(_VSTD::move(__f), _Alloc(__af));
759             }
760             else
761             {
762                 typedef __allocator_destructor<_FunAlloc> _Dp;
763                 unique_ptr<_Fun, _Dp> __hold(__af.allocate(1), _Dp(__af, 1));
764                 ::new ((void*)__hold.get())
765                     _Fun(_VSTD::move(__f), _Alloc(__af));
766                 __buf_.__large = __hold.release();
767             }
768         }
769     }
770 
771     template <class _Fp, class = typename enable_if<!is_same<typename decay<_Fp>::type, __policy_func>::value>::type>
772     _LIBCPP_INLINE_VISIBILITY explicit __policy_func(_Fp&& __f)
773         : __policy_(__policy::__create_empty()) {
774       typedef __default_alloc_func<_Fp, _Rp(_ArgTypes...)> _Fun;
775 
776       if (__function::__not_null(__f)) {
777         __invoker_ = __invoker::template __create<_Fun>();
778         __policy_ = __policy::__create<_Fun>();
779         if (__use_small_storage<_Fun>()) {
780           ::new ((void*)&__buf_.__small) _Fun(_VSTD::move(__f));
781         } else {
782           __builtin_new_allocator::__holder_t __hold =
783               __builtin_new_allocator::__allocate_type<_Fun>(1);
784           __buf_.__large = ::new ((void*)__hold.get()) _Fun(_VSTD::move(__f));
785           (void)__hold.release();
786         }
787       }
788     }
789 
790     _LIBCPP_INLINE_VISIBILITY
791     __policy_func(const __policy_func& __f)
792         : __buf_(__f.__buf_), __invoker_(__f.__invoker_),
793           __policy_(__f.__policy_)
794     {
795         if (__policy_->__clone)
796             __buf_.__large = __policy_->__clone(__f.__buf_.__large);
797     }
798 
799     _LIBCPP_INLINE_VISIBILITY
800     __policy_func(__policy_func&& __f)
801         : __buf_(__f.__buf_), __invoker_(__f.__invoker_),
802           __policy_(__f.__policy_)
803     {
804         if (__policy_->__destroy)
805         {
806             __f.__policy_ = __policy::__create_empty();
807             __f.__invoker_ = __invoker();
808         }
809     }
810 
811     _LIBCPP_INLINE_VISIBILITY
812     ~__policy_func()
813     {
814         if (__policy_->__destroy)
815             __policy_->__destroy(__buf_.__large);
816     }
817 
818     _LIBCPP_INLINE_VISIBILITY
819     __policy_func& operator=(__policy_func&& __f)
820     {
821         *this = nullptr;
822         __buf_ = __f.__buf_;
823         __invoker_ = __f.__invoker_;
824         __policy_ = __f.__policy_;
825         __f.__policy_ = __policy::__create_empty();
826         __f.__invoker_ = __invoker();
827         return *this;
828     }
829 
830     _LIBCPP_INLINE_VISIBILITY
831     __policy_func& operator=(nullptr_t)
832     {
833         const __policy* __p = __policy_;
834         __policy_ = __policy::__create_empty();
835         __invoker_ = __invoker();
836         if (__p->__destroy)
837             __p->__destroy(__buf_.__large);
838         return *this;
839     }
840 
841     _LIBCPP_INLINE_VISIBILITY
842     _Rp operator()(_ArgTypes&&... __args) const
843     {
844         return __invoker_.__call_(_VSTD::addressof(__buf_),
845                                   _VSTD::forward<_ArgTypes>(__args)...);
846     }
847 
848     _LIBCPP_INLINE_VISIBILITY
849     void swap(__policy_func& __f)
850     {
851         _VSTD::swap(__invoker_, __f.__invoker_);
852         _VSTD::swap(__policy_, __f.__policy_);
853         _VSTD::swap(__buf_, __f.__buf_);
854     }
855 
856     _LIBCPP_INLINE_VISIBILITY
857     explicit operator bool() const _NOEXCEPT
858     {
859         return !__policy_->__is_null;
860     }
861 
862 #ifndef _LIBCPP_NO_RTTI
863     _LIBCPP_INLINE_VISIBILITY
864     const std::type_info& target_type() const _NOEXCEPT
865     {
866         return *__policy_->__type_info;
867     }
868 
869     template <typename _Tp>
870     _LIBCPP_INLINE_VISIBILITY const _Tp* target() const _NOEXCEPT
871     {
872         if (__policy_->__is_null || typeid(_Tp) != *__policy_->__type_info)
873             return nullptr;
874         if (__policy_->__clone) // Out of line storage.
875             return reinterpret_cast<const _Tp*>(__buf_.__large);
876         else
877             return reinterpret_cast<const _Tp*>(&__buf_.__small);
878     }
879 #endif // _LIBCPP_NO_RTTI
880 };
881 
882 #if defined(_LIBCPP_HAS_BLOCKS_RUNTIME) && !defined(_LIBCPP_HAS_OBJC_ARC)
883 
884 extern "C" void *_Block_copy(const void *);
885 extern "C" void _Block_release(const void *);
886 
887 template<class _Rp1, class ..._ArgTypes1, class _Alloc, class _Rp, class ..._ArgTypes>
888 class __func<_Rp1(^)(_ArgTypes1...), _Alloc, _Rp(_ArgTypes...)>
889     : public  __base<_Rp(_ArgTypes...)>
890 {
891     typedef _Rp1(^__block_type)(_ArgTypes1...);
892     __block_type __f_;
893 
894 public:
895     _LIBCPP_INLINE_VISIBILITY
896     explicit __func(__block_type const& __f)
897         : __f_(reinterpret_cast<__block_type>(__f ? _Block_copy(__f) : nullptr))
898     { }
899 
900     // [TODO] add && to save on a retain
901 
902     _LIBCPP_INLINE_VISIBILITY
903     explicit __func(__block_type __f, const _Alloc& /* unused */)
904         : __f_(reinterpret_cast<__block_type>(__f ? _Block_copy(__f) : nullptr))
905     { }
906 
907     virtual __base<_Rp(_ArgTypes...)>* __clone() const {
908         _LIBCPP_ASSERT(false,
909             "Block pointers are just pointers, so they should always fit into "
910             "std::function's small buffer optimization. This function should "
911             "never be invoked.");
912         return nullptr;
913     }
914 
915     virtual void __clone(__base<_Rp(_ArgTypes...)>* __p) const {
916         ::new ((void*)__p) __func(__f_);
917     }
918 
919     virtual void destroy() _NOEXCEPT {
920         if (__f_)
921             _Block_release(__f_);
922         __f_ = 0;
923     }
924 
925     virtual void destroy_deallocate() _NOEXCEPT {
926         _LIBCPP_ASSERT(false,
927             "Block pointers are just pointers, so they should always fit into "
928             "std::function's small buffer optimization. This function should "
929             "never be invoked.");
930     }
931 
932     virtual _Rp operator()(_ArgTypes&& ... __arg) {
933         return _VSTD::__invoke(__f_, _VSTD::forward<_ArgTypes>(__arg)...);
934     }
935 
936 #ifndef _LIBCPP_NO_RTTI
937     virtual const void* target(type_info const& __ti) const _NOEXCEPT {
938         if (__ti == typeid(__func::__block_type))
939             return &__f_;
940         return (const void*)nullptr;
941     }
942 
943     virtual const std::type_info& target_type() const _NOEXCEPT {
944         return typeid(__func::__block_type);
945     }
946 #endif // _LIBCPP_NO_RTTI
947 };
948 
949 #endif // _LIBCPP_HAS_EXTENSION_BLOCKS && !_LIBCPP_HAS_OBJC_ARC
950 
951 } // namespace __function
952 
953 template<class _Rp, class ..._ArgTypes>
954 class _LIBCPP_TEMPLATE_VIS function<_Rp(_ArgTypes...)>
955 #if _LIBCPP_STD_VER <= 17 || !defined(_LIBCPP_ABI_NO_BINDER_BASES)
956     : public __function::__maybe_derive_from_unary_function<_Rp(_ArgTypes...)>,
957       public __function::__maybe_derive_from_binary_function<_Rp(_ArgTypes...)>
958 #endif
959 {
960 #ifndef _LIBCPP_ABI_OPTIMIZED_FUNCTION
961     typedef __function::__value_func<_Rp(_ArgTypes...)> __func;
962 #else
963     typedef __function::__policy_func<_Rp(_ArgTypes...)> __func;
964 #endif
965 
966     __func __f_;
967 
968     template <class _Fp, bool = _And<
969         _IsNotSame<__uncvref_t<_Fp>, function>,
970         __invokable<_Fp, _ArgTypes...>
971     >::value>
972     struct __callable;
973     template <class _Fp>
974         struct __callable<_Fp, true>
975         {
976             static const bool value = is_void<_Rp>::value ||
977                 __is_core_convertible<typename __invoke_of<_Fp, _ArgTypes...>::type,
978                                       _Rp>::value;
979         };
980     template <class _Fp>
981         struct __callable<_Fp, false>
982         {
983             static const bool value = false;
984         };
985 
986   template <class _Fp>
987   using _EnableIfLValueCallable = typename enable_if<__callable<_Fp&>::value>::type;
988 public:
989     typedef _Rp result_type;
990 
991     // construct/copy/destroy:
992     _LIBCPP_INLINE_VISIBILITY
993     function() _NOEXCEPT { }
994     _LIBCPP_INLINE_VISIBILITY
995     function(nullptr_t) _NOEXCEPT {}
996     function(const function&);
997     function(function&&) _NOEXCEPT;
998     template<class _Fp, class = _EnableIfLValueCallable<_Fp>>
999     function(_Fp);
1000 
1001 #if _LIBCPP_STD_VER <= 14
1002     template<class _Alloc>
1003       _LIBCPP_INLINE_VISIBILITY
1004       function(allocator_arg_t, const _Alloc&) _NOEXCEPT {}
1005     template<class _Alloc>
1006       _LIBCPP_INLINE_VISIBILITY
1007       function(allocator_arg_t, const _Alloc&, nullptr_t) _NOEXCEPT {}
1008     template<class _Alloc>
1009       function(allocator_arg_t, const _Alloc&, const function&);
1010     template<class _Alloc>
1011       function(allocator_arg_t, const _Alloc&, function&&);
1012     template<class _Fp, class _Alloc, class = _EnableIfLValueCallable<_Fp>>
1013       function(allocator_arg_t, const _Alloc& __a, _Fp __f);
1014 #endif
1015 
1016     function& operator=(const function&);
1017     function& operator=(function&&) _NOEXCEPT;
1018     function& operator=(nullptr_t) _NOEXCEPT;
1019     template<class _Fp, class = _EnableIfLValueCallable<typename decay<_Fp>::type>>
1020     function& operator=(_Fp&&);
1021 
1022     ~function();
1023 
1024     // function modifiers:
1025     void swap(function&) _NOEXCEPT;
1026 
1027 #if _LIBCPP_STD_VER <= 14
1028     template<class _Fp, class _Alloc>
1029       _LIBCPP_INLINE_VISIBILITY
1030       void assign(_Fp&& __f, const _Alloc& __a)
1031         {function(allocator_arg, __a, _VSTD::forward<_Fp>(__f)).swap(*this);}
1032 #endif
1033 
1034     // function capacity:
1035     _LIBCPP_INLINE_VISIBILITY
1036     explicit operator bool() const _NOEXCEPT {
1037       return static_cast<bool>(__f_);
1038     }
1039 
1040     // deleted overloads close possible hole in the type system
1041     template<class _R2, class... _ArgTypes2>
1042       bool operator==(const function<_R2(_ArgTypes2...)>&) const = delete;
1043     template<class _R2, class... _ArgTypes2>
1044       bool operator!=(const function<_R2(_ArgTypes2...)>&) const = delete;
1045 public:
1046     // function invocation:
1047     _Rp operator()(_ArgTypes...) const;
1048 
1049 #ifndef _LIBCPP_NO_RTTI
1050     // function target access:
1051     const std::type_info& target_type() const _NOEXCEPT;
1052     template <typename _Tp> _Tp* target() _NOEXCEPT;
1053     template <typename _Tp> const _Tp* target() const _NOEXCEPT;
1054 #endif // _LIBCPP_NO_RTTI
1055 };
1056 
1057 #if _LIBCPP_STD_VER >= 17
1058 template<class _Rp, class ..._Ap>
1059 function(_Rp(*)(_Ap...)) -> function<_Rp(_Ap...)>;
1060 
1061 template<class _Fp>
1062 struct __strip_signature;
1063 
1064 template<class _Rp, class _Gp, class ..._Ap>
1065 struct __strip_signature<_Rp (_Gp::*) (_Ap...)> { using type = _Rp(_Ap...); };
1066 template<class _Rp, class _Gp, class ..._Ap>
1067 struct __strip_signature<_Rp (_Gp::*) (_Ap...) const> { using type = _Rp(_Ap...); };
1068 template<class _Rp, class _Gp, class ..._Ap>
1069 struct __strip_signature<_Rp (_Gp::*) (_Ap...) volatile> { using type = _Rp(_Ap...); };
1070 template<class _Rp, class _Gp, class ..._Ap>
1071 struct __strip_signature<_Rp (_Gp::*) (_Ap...) const volatile> { using type = _Rp(_Ap...); };
1072 
1073 template<class _Rp, class _Gp, class ..._Ap>
1074 struct __strip_signature<_Rp (_Gp::*) (_Ap...) &> { using type = _Rp(_Ap...); };
1075 template<class _Rp, class _Gp, class ..._Ap>
1076 struct __strip_signature<_Rp (_Gp::*) (_Ap...) const &> { using type = _Rp(_Ap...); };
1077 template<class _Rp, class _Gp, class ..._Ap>
1078 struct __strip_signature<_Rp (_Gp::*) (_Ap...) volatile &> { using type = _Rp(_Ap...); };
1079 template<class _Rp, class _Gp, class ..._Ap>
1080 struct __strip_signature<_Rp (_Gp::*) (_Ap...) const volatile &> { using type = _Rp(_Ap...); };
1081 
1082 template<class _Rp, class _Gp, class ..._Ap>
1083 struct __strip_signature<_Rp (_Gp::*) (_Ap...) noexcept> { using type = _Rp(_Ap...); };
1084 template<class _Rp, class _Gp, class ..._Ap>
1085 struct __strip_signature<_Rp (_Gp::*) (_Ap...) const noexcept> { using type = _Rp(_Ap...); };
1086 template<class _Rp, class _Gp, class ..._Ap>
1087 struct __strip_signature<_Rp (_Gp::*) (_Ap...) volatile noexcept> { using type = _Rp(_Ap...); };
1088 template<class _Rp, class _Gp, class ..._Ap>
1089 struct __strip_signature<_Rp (_Gp::*) (_Ap...) const volatile noexcept> { using type = _Rp(_Ap...); };
1090 
1091 template<class _Rp, class _Gp, class ..._Ap>
1092 struct __strip_signature<_Rp (_Gp::*) (_Ap...) & noexcept> { using type = _Rp(_Ap...); };
1093 template<class _Rp, class _Gp, class ..._Ap>
1094 struct __strip_signature<_Rp (_Gp::*) (_Ap...) const & noexcept> { using type = _Rp(_Ap...); };
1095 template<class _Rp, class _Gp, class ..._Ap>
1096 struct __strip_signature<_Rp (_Gp::*) (_Ap...) volatile & noexcept> { using type = _Rp(_Ap...); };
1097 template<class _Rp, class _Gp, class ..._Ap>
1098 struct __strip_signature<_Rp (_Gp::*) (_Ap...) const volatile & noexcept> { using type = _Rp(_Ap...); };
1099 
1100 template<class _Fp, class _Stripped = typename __strip_signature<decltype(&_Fp::operator())>::type>
1101 function(_Fp) -> function<_Stripped>;
1102 #endif // _LIBCPP_STD_VER >= 17
1103 
1104 template<class _Rp, class ..._ArgTypes>
1105 function<_Rp(_ArgTypes...)>::function(const function& __f) : __f_(__f.__f_) {}
1106 
1107 #if _LIBCPP_STD_VER <= 14
1108 template<class _Rp, class ..._ArgTypes>
1109 template <class _Alloc>
1110 function<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc&,
1111                                      const function& __f) : __f_(__f.__f_) {}
1112 #endif
1113 
1114 template <class _Rp, class... _ArgTypes>
1115 function<_Rp(_ArgTypes...)>::function(function&& __f) _NOEXCEPT
1116     : __f_(_VSTD::move(__f.__f_)) {}
1117 
1118 #if _LIBCPP_STD_VER <= 14
1119 template<class _Rp, class ..._ArgTypes>
1120 template <class _Alloc>
1121 function<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc&,
1122                                       function&& __f)
1123     : __f_(_VSTD::move(__f.__f_)) {}
1124 #endif
1125 
1126 template <class _Rp, class... _ArgTypes>
1127 template <class _Fp, class>
1128 function<_Rp(_ArgTypes...)>::function(_Fp __f) : __f_(_VSTD::move(__f)) {}
1129 
1130 #if _LIBCPP_STD_VER <= 14
1131 template <class _Rp, class... _ArgTypes>
1132 template <class _Fp, class _Alloc, class>
1133 function<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc& __a,
1134                                       _Fp __f)
1135     : __f_(_VSTD::move(__f), __a) {}
1136 #endif
1137 
1138 template<class _Rp, class ..._ArgTypes>
1139 function<_Rp(_ArgTypes...)>&
1140 function<_Rp(_ArgTypes...)>::operator=(const function& __f)
1141 {
1142     function(__f).swap(*this);
1143     return *this;
1144 }
1145 
1146 template<class _Rp, class ..._ArgTypes>
1147 function<_Rp(_ArgTypes...)>&
1148 function<_Rp(_ArgTypes...)>::operator=(function&& __f) _NOEXCEPT
1149 {
1150     __f_ = _VSTD::move(__f.__f_);
1151     return *this;
1152 }
1153 
1154 template<class _Rp, class ..._ArgTypes>
1155 function<_Rp(_ArgTypes...)>&
1156 function<_Rp(_ArgTypes...)>::operator=(nullptr_t) _NOEXCEPT
1157 {
1158     __f_ = nullptr;
1159     return *this;
1160 }
1161 
1162 template<class _Rp, class ..._ArgTypes>
1163 template <class _Fp, class>
1164 function<_Rp(_ArgTypes...)>&
1165 function<_Rp(_ArgTypes...)>::operator=(_Fp&& __f)
1166 {
1167     function(_VSTD::forward<_Fp>(__f)).swap(*this);
1168     return *this;
1169 }
1170 
1171 template<class _Rp, class ..._ArgTypes>
1172 function<_Rp(_ArgTypes...)>::~function() {}
1173 
1174 template<class _Rp, class ..._ArgTypes>
1175 void
1176 function<_Rp(_ArgTypes...)>::swap(function& __f) _NOEXCEPT
1177 {
1178     __f_.swap(__f.__f_);
1179 }
1180 
1181 template<class _Rp, class ..._ArgTypes>
1182 _Rp
1183 function<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __arg) const
1184 {
1185     return __f_(_VSTD::forward<_ArgTypes>(__arg)...);
1186 }
1187 
1188 #ifndef _LIBCPP_NO_RTTI
1189 
1190 template<class _Rp, class ..._ArgTypes>
1191 const std::type_info&
1192 function<_Rp(_ArgTypes...)>::target_type() const _NOEXCEPT
1193 {
1194     return __f_.target_type();
1195 }
1196 
1197 template<class _Rp, class ..._ArgTypes>
1198 template <typename _Tp>
1199 _Tp*
1200 function<_Rp(_ArgTypes...)>::target() _NOEXCEPT
1201 {
1202     return (_Tp*)(__f_.template target<_Tp>());
1203 }
1204 
1205 template<class _Rp, class ..._ArgTypes>
1206 template <typename _Tp>
1207 const _Tp*
1208 function<_Rp(_ArgTypes...)>::target() const _NOEXCEPT
1209 {
1210     return __f_.template target<_Tp>();
1211 }
1212 
1213 #endif // _LIBCPP_NO_RTTI
1214 
1215 template <class _Rp, class... _ArgTypes>
1216 inline _LIBCPP_INLINE_VISIBILITY
1217 bool
1218 operator==(const function<_Rp(_ArgTypes...)>& __f, nullptr_t) _NOEXCEPT {return !__f;}
1219 
1220 template <class _Rp, class... _ArgTypes>
1221 inline _LIBCPP_INLINE_VISIBILITY
1222 bool
1223 operator==(nullptr_t, const function<_Rp(_ArgTypes...)>& __f) _NOEXCEPT {return !__f;}
1224 
1225 template <class _Rp, class... _ArgTypes>
1226 inline _LIBCPP_INLINE_VISIBILITY
1227 bool
1228 operator!=(const function<_Rp(_ArgTypes...)>& __f, nullptr_t) _NOEXCEPT {return (bool)__f;}
1229 
1230 template <class _Rp, class... _ArgTypes>
1231 inline _LIBCPP_INLINE_VISIBILITY
1232 bool
1233 operator!=(nullptr_t, const function<_Rp(_ArgTypes...)>& __f) _NOEXCEPT {return (bool)__f;}
1234 
1235 template <class _Rp, class... _ArgTypes>
1236 inline _LIBCPP_INLINE_VISIBILITY
1237 void
1238 swap(function<_Rp(_ArgTypes...)>& __x, function<_Rp(_ArgTypes...)>& __y) _NOEXCEPT
1239 {return __x.swap(__y);}
1240 
1241 #else // _LIBCPP_CXX03_LANG
1242 
1243 namespace __function {
1244 
1245 template<class _Fp> class __base;
1246 
1247 template<class _Rp>
1248 class __base<_Rp()>
1249 {
1250     __base(const __base&);
1251     __base& operator=(const __base&);
1252 public:
1253     __base() {}
1254     virtual ~__base() {}
1255     virtual __base* __clone() const = 0;
1256     virtual void __clone(__base*) const = 0;
1257     virtual void destroy() = 0;
1258     virtual void destroy_deallocate() = 0;
1259     virtual _Rp operator()() = 0;
1260 #ifndef _LIBCPP_NO_RTTI
1261     virtual const void* target(const type_info&) const = 0;
1262     virtual const std::type_info& target_type() const = 0;
1263 #endif // _LIBCPP_NO_RTTI
1264 };
1265 
1266 template<class _Rp, class _A0>
1267 class __base<_Rp(_A0)>
1268 {
1269     __base(const __base&);
1270     __base& operator=(const __base&);
1271 public:
1272     __base() {}
1273     virtual ~__base() {}
1274     virtual __base* __clone() const = 0;
1275     virtual void __clone(__base*) const = 0;
1276     virtual void destroy() = 0;
1277     virtual void destroy_deallocate() = 0;
1278     virtual _Rp operator()(_A0) = 0;
1279 #ifndef _LIBCPP_NO_RTTI
1280     virtual const void* target(const type_info&) const = 0;
1281     virtual const std::type_info& target_type() const = 0;
1282 #endif // _LIBCPP_NO_RTTI
1283 };
1284 
1285 template<class _Rp, class _A0, class _A1>
1286 class __base<_Rp(_A0, _A1)>
1287 {
1288     __base(const __base&);
1289     __base& operator=(const __base&);
1290 public:
1291     __base() {}
1292     virtual ~__base() {}
1293     virtual __base* __clone() const = 0;
1294     virtual void __clone(__base*) const = 0;
1295     virtual void destroy() = 0;
1296     virtual void destroy_deallocate() = 0;
1297     virtual _Rp operator()(_A0, _A1) = 0;
1298 #ifndef _LIBCPP_NO_RTTI
1299     virtual const void* target(const type_info&) const = 0;
1300     virtual const std::type_info& target_type() const = 0;
1301 #endif // _LIBCPP_NO_RTTI
1302 };
1303 
1304 template<class _Rp, class _A0, class _A1, class _A2>
1305 class __base<_Rp(_A0, _A1, _A2)>
1306 {
1307     __base(const __base&);
1308     __base& operator=(const __base&);
1309 public:
1310     __base() {}
1311     virtual ~__base() {}
1312     virtual __base* __clone() const = 0;
1313     virtual void __clone(__base*) const = 0;
1314     virtual void destroy() = 0;
1315     virtual void destroy_deallocate() = 0;
1316     virtual _Rp operator()(_A0, _A1, _A2) = 0;
1317 #ifndef _LIBCPP_NO_RTTI
1318     virtual const void* target(const type_info&) const = 0;
1319     virtual const std::type_info& target_type() const = 0;
1320 #endif // _LIBCPP_NO_RTTI
1321 };
1322 
1323 template<class _FD, class _Alloc, class _FB> class __func;
1324 
1325 template<class _Fp, class _Alloc, class _Rp>
1326 class __func<_Fp, _Alloc, _Rp()>
1327     : public  __base<_Rp()>
1328 {
1329     __compressed_pair<_Fp, _Alloc> __f_;
1330 public:
1331     explicit __func(_Fp __f) : __f_(_VSTD::move(__f), __default_init_tag()) {}
1332     explicit __func(_Fp __f, _Alloc __a) : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
1333     virtual __base<_Rp()>* __clone() const;
1334     virtual void __clone(__base<_Rp()>*) const;
1335     virtual void destroy();
1336     virtual void destroy_deallocate();
1337     virtual _Rp operator()();
1338 #ifndef _LIBCPP_NO_RTTI
1339     virtual const void* target(const type_info&) const;
1340     virtual const std::type_info& target_type() const;
1341 #endif // _LIBCPP_NO_RTTI
1342 };
1343 
1344 template<class _Fp, class _Alloc, class _Rp>
1345 __base<_Rp()>*
1346 __func<_Fp, _Alloc, _Rp()>::__clone() const
1347 {
1348     typedef allocator_traits<_Alloc> __alloc_traits;
1349     typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
1350     _Ap __a(__f_.second());
1351     typedef __allocator_destructor<_Ap> _Dp;
1352     unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1353     ::new ((void*)__hold.get()) __func(__f_.first(), _Alloc(__a));
1354     return __hold.release();
1355 }
1356 
1357 template<class _Fp, class _Alloc, class _Rp>
1358 void
1359 __func<_Fp, _Alloc, _Rp()>::__clone(__base<_Rp()>* __p) const
1360 {
1361     ::new ((void*)__p) __func(__f_.first(), __f_.second());
1362 }
1363 
1364 template<class _Fp, class _Alloc, class _Rp>
1365 void
1366 __func<_Fp, _Alloc, _Rp()>::destroy()
1367 {
1368     __f_.~__compressed_pair<_Fp, _Alloc>();
1369 }
1370 
1371 template<class _Fp, class _Alloc, class _Rp>
1372 void
1373 __func<_Fp, _Alloc, _Rp()>::destroy_deallocate()
1374 {
1375     typedef allocator_traits<_Alloc> __alloc_traits;
1376     typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
1377     _Ap __a(__f_.second());
1378     __f_.~__compressed_pair<_Fp, _Alloc>();
1379     __a.deallocate(this, 1);
1380 }
1381 
1382 template<class _Fp, class _Alloc, class _Rp>
1383 _Rp
1384 __func<_Fp, _Alloc, _Rp()>::operator()()
1385 {
1386     typedef __invoke_void_return_wrapper<_Rp> _Invoker;
1387     return _Invoker::__call(__f_.first());
1388 }
1389 
1390 #ifndef _LIBCPP_NO_RTTI
1391 
1392 template<class _Fp, class _Alloc, class _Rp>
1393 const void*
1394 __func<_Fp, _Alloc, _Rp()>::target(const type_info& __ti) const
1395 {
1396     if (__ti == typeid(_Fp))
1397         return _VSTD::addressof(__f_.first());
1398     return (const void*)0;
1399 }
1400 
1401 template<class _Fp, class _Alloc, class _Rp>
1402 const std::type_info&
1403 __func<_Fp, _Alloc, _Rp()>::target_type() const
1404 {
1405     return typeid(_Fp);
1406 }
1407 
1408 #endif // _LIBCPP_NO_RTTI
1409 
1410 template<class _Fp, class _Alloc, class _Rp, class _A0>
1411 class __func<_Fp, _Alloc, _Rp(_A0)>
1412     : public  __base<_Rp(_A0)>
1413 {
1414     __compressed_pair<_Fp, _Alloc> __f_;
1415 public:
1416     _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f) : __f_(_VSTD::move(__f), __default_init_tag()) {}
1417     _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f, _Alloc __a)
1418         : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
1419     virtual __base<_Rp(_A0)>* __clone() const;
1420     virtual void __clone(__base<_Rp(_A0)>*) const;
1421     virtual void destroy();
1422     virtual void destroy_deallocate();
1423     virtual _Rp operator()(_A0);
1424 #ifndef _LIBCPP_NO_RTTI
1425     virtual const void* target(const type_info&) const;
1426     virtual const std::type_info& target_type() const;
1427 #endif // _LIBCPP_NO_RTTI
1428 };
1429 
1430 template<class _Fp, class _Alloc, class _Rp, class _A0>
1431 __base<_Rp(_A0)>*
1432 __func<_Fp, _Alloc, _Rp(_A0)>::__clone() const
1433 {
1434     typedef allocator_traits<_Alloc> __alloc_traits;
1435     typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
1436     _Ap __a(__f_.second());
1437     typedef __allocator_destructor<_Ap> _Dp;
1438     unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1439     ::new ((void*)__hold.get()) __func(__f_.first(), _Alloc(__a));
1440     return __hold.release();
1441 }
1442 
1443 template<class _Fp, class _Alloc, class _Rp, class _A0>
1444 void
1445 __func<_Fp, _Alloc, _Rp(_A0)>::__clone(__base<_Rp(_A0)>* __p) const
1446 {
1447     ::new ((void*)__p) __func(__f_.first(), __f_.second());
1448 }
1449 
1450 template<class _Fp, class _Alloc, class _Rp, class _A0>
1451 void
1452 __func<_Fp, _Alloc, _Rp(_A0)>::destroy()
1453 {
1454     __f_.~__compressed_pair<_Fp, _Alloc>();
1455 }
1456 
1457 template<class _Fp, class _Alloc, class _Rp, class _A0>
1458 void
1459 __func<_Fp, _Alloc, _Rp(_A0)>::destroy_deallocate()
1460 {
1461     typedef allocator_traits<_Alloc> __alloc_traits;
1462     typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
1463     _Ap __a(__f_.second());
1464     __f_.~__compressed_pair<_Fp, _Alloc>();
1465     __a.deallocate(this, 1);
1466 }
1467 
1468 template<class _Fp, class _Alloc, class _Rp, class _A0>
1469 _Rp
1470 __func<_Fp, _Alloc, _Rp(_A0)>::operator()(_A0 __a0)
1471 {
1472     typedef __invoke_void_return_wrapper<_Rp> _Invoker;
1473     return _Invoker::__call(__f_.first(), __a0);
1474 }
1475 
1476 #ifndef _LIBCPP_NO_RTTI
1477 
1478 template<class _Fp, class _Alloc, class _Rp, class _A0>
1479 const void*
1480 __func<_Fp, _Alloc, _Rp(_A0)>::target(const type_info& __ti) const
1481 {
1482     if (__ti == typeid(_Fp))
1483         return &__f_.first();
1484     return (const void*)0;
1485 }
1486 
1487 template<class _Fp, class _Alloc, class _Rp, class _A0>
1488 const std::type_info&
1489 __func<_Fp, _Alloc, _Rp(_A0)>::target_type() const
1490 {
1491     return typeid(_Fp);
1492 }
1493 
1494 #endif // _LIBCPP_NO_RTTI
1495 
1496 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
1497 class __func<_Fp, _Alloc, _Rp(_A0, _A1)>
1498     : public  __base<_Rp(_A0, _A1)>
1499 {
1500     __compressed_pair<_Fp, _Alloc> __f_;
1501 public:
1502     _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f) : __f_(_VSTD::move(__f), __default_init_tag()) {}
1503     _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f, _Alloc __a)
1504         : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
1505     virtual __base<_Rp(_A0, _A1)>* __clone() const;
1506     virtual void __clone(__base<_Rp(_A0, _A1)>*) const;
1507     virtual void destroy();
1508     virtual void destroy_deallocate();
1509     virtual _Rp operator()(_A0, _A1);
1510 #ifndef _LIBCPP_NO_RTTI
1511     virtual const void* target(const type_info&) const;
1512     virtual const std::type_info& target_type() const;
1513 #endif // _LIBCPP_NO_RTTI
1514 };
1515 
1516 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
1517 __base<_Rp(_A0, _A1)>*
1518 __func<_Fp, _Alloc, _Rp(_A0, _A1)>::__clone() const
1519 {
1520     typedef allocator_traits<_Alloc> __alloc_traits;
1521     typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
1522     _Ap __a(__f_.second());
1523     typedef __allocator_destructor<_Ap> _Dp;
1524     unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1525     ::new ((void*)__hold.get()) __func(__f_.first(), _Alloc(__a));
1526     return __hold.release();
1527 }
1528 
1529 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
1530 void
1531 __func<_Fp, _Alloc, _Rp(_A0, _A1)>::__clone(__base<_Rp(_A0, _A1)>* __p) const
1532 {
1533     ::new ((void*)__p) __func(__f_.first(), __f_.second());
1534 }
1535 
1536 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
1537 void
1538 __func<_Fp, _Alloc, _Rp(_A0, _A1)>::destroy()
1539 {
1540     __f_.~__compressed_pair<_Fp, _Alloc>();
1541 }
1542 
1543 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
1544 void
1545 __func<_Fp, _Alloc, _Rp(_A0, _A1)>::destroy_deallocate()
1546 {
1547     typedef allocator_traits<_Alloc> __alloc_traits;
1548     typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
1549     _Ap __a(__f_.second());
1550     __f_.~__compressed_pair<_Fp, _Alloc>();
1551     __a.deallocate(this, 1);
1552 }
1553 
1554 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
1555 _Rp
1556 __func<_Fp, _Alloc, _Rp(_A0, _A1)>::operator()(_A0 __a0, _A1 __a1)
1557 {
1558     typedef __invoke_void_return_wrapper<_Rp> _Invoker;
1559     return _Invoker::__call(__f_.first(), __a0, __a1);
1560 }
1561 
1562 #ifndef _LIBCPP_NO_RTTI
1563 
1564 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
1565 const void*
1566 __func<_Fp, _Alloc, _Rp(_A0, _A1)>::target(const type_info& __ti) const
1567 {
1568     if (__ti == typeid(_Fp))
1569         return &__f_.first();
1570     return (const void*)0;
1571 }
1572 
1573 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
1574 const std::type_info&
1575 __func<_Fp, _Alloc, _Rp(_A0, _A1)>::target_type() const
1576 {
1577     return typeid(_Fp);
1578 }
1579 
1580 #endif // _LIBCPP_NO_RTTI
1581 
1582 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
1583 class __func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>
1584     : public  __base<_Rp(_A0, _A1, _A2)>
1585 {
1586     __compressed_pair<_Fp, _Alloc> __f_;
1587 public:
1588     _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f) : __f_(_VSTD::move(__f), __default_init_tag()) {}
1589     _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f, _Alloc __a)
1590         : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
1591     virtual __base<_Rp(_A0, _A1, _A2)>* __clone() const;
1592     virtual void __clone(__base<_Rp(_A0, _A1, _A2)>*) const;
1593     virtual void destroy();
1594     virtual void destroy_deallocate();
1595     virtual _Rp operator()(_A0, _A1, _A2);
1596 #ifndef _LIBCPP_NO_RTTI
1597     virtual const void* target(const type_info&) const;
1598     virtual const std::type_info& target_type() const;
1599 #endif // _LIBCPP_NO_RTTI
1600 };
1601 
1602 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
1603 __base<_Rp(_A0, _A1, _A2)>*
1604 __func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::__clone() const
1605 {
1606     typedef allocator_traits<_Alloc> __alloc_traits;
1607     typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
1608     _Ap __a(__f_.second());
1609     typedef __allocator_destructor<_Ap> _Dp;
1610     unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1611     ::new ((void*)__hold.get()) __func(__f_.first(), _Alloc(__a));
1612     return __hold.release();
1613 }
1614 
1615 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
1616 void
1617 __func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::__clone(__base<_Rp(_A0, _A1, _A2)>* __p) const
1618 {
1619     ::new ((void*)__p) __func(__f_.first(), __f_.second());
1620 }
1621 
1622 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
1623 void
1624 __func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::destroy()
1625 {
1626     __f_.~__compressed_pair<_Fp, _Alloc>();
1627 }
1628 
1629 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
1630 void
1631 __func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::destroy_deallocate()
1632 {
1633     typedef allocator_traits<_Alloc> __alloc_traits;
1634     typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
1635     _Ap __a(__f_.second());
1636     __f_.~__compressed_pair<_Fp, _Alloc>();
1637     __a.deallocate(this, 1);
1638 }
1639 
1640 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
1641 _Rp
1642 __func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::operator()(_A0 __a0, _A1 __a1, _A2 __a2)
1643 {
1644     typedef __invoke_void_return_wrapper<_Rp> _Invoker;
1645     return _Invoker::__call(__f_.first(), __a0, __a1, __a2);
1646 }
1647 
1648 #ifndef _LIBCPP_NO_RTTI
1649 
1650 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
1651 const void*
1652 __func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::target(const type_info& __ti) const
1653 {
1654     if (__ti == typeid(_Fp))
1655         return &__f_.first();
1656     return (const void*)0;
1657 }
1658 
1659 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
1660 const std::type_info&
1661 __func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::target_type() const
1662 {
1663     return typeid(_Fp);
1664 }
1665 
1666 #endif // _LIBCPP_NO_RTTI
1667 
1668 } // namespace __function
1669 
1670 template<class _Rp>
1671 class _LIBCPP_TEMPLATE_VIS function<_Rp()>
1672 {
1673     typedef __function::__base<_Rp()> __base;
1674     aligned_storage<3*sizeof(void*)>::type __buf_;
1675     __base* __f_;
1676 
1677 public:
1678     typedef _Rp result_type;
1679 
1680     // 20.7.16.2.1, construct/copy/destroy:
1681     _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {}
1682     _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {}
1683     function(const function&);
1684     template<class _Fp>
1685       function(_Fp,
1686                typename enable_if<!is_integral<_Fp>::value>::type* = 0);
1687 
1688     template<class _Alloc>
1689       _LIBCPP_INLINE_VISIBILITY
1690       function(allocator_arg_t, const _Alloc&) : __f_(0) {}
1691     template<class _Alloc>
1692       _LIBCPP_INLINE_VISIBILITY
1693       function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {}
1694     template<class _Alloc>
1695       function(allocator_arg_t, const _Alloc&, const function&);
1696     template<class _Fp, class _Alloc>
1697       function(allocator_arg_t, const _Alloc& __a, _Fp __f,
1698                typename enable_if<!is_integral<_Fp>::value>::type* = 0);
1699 
1700     function& operator=(const function&);
1701     function& operator=(nullptr_t);
1702     template<class _Fp>
1703       typename enable_if
1704       <
1705         !is_integral<_Fp>::value,
1706         function&
1707       >::type
1708       operator=(_Fp);
1709 
1710     ~function();
1711 
1712     // 20.7.16.2.2, function modifiers:
1713     void swap(function&);
1714     template<class _Fp, class _Alloc>
1715       _LIBCPP_INLINE_VISIBILITY
1716       void assign(_Fp __f, const _Alloc& __a)
1717         {function(allocator_arg, __a, __f).swap(*this);}
1718 
1719     // 20.7.16.2.3, function capacity:
1720     _LIBCPP_INLINE_VISIBILITY explicit operator bool() const {return __f_;}
1721 
1722     template<class _R2>
1723       bool operator==(const function<_R2()>&) const = delete;
1724     template<class _R2>
1725       bool operator!=(const function<_R2()>&) const = delete;
1726 
1727     // 20.7.16.2.4, function invocation:
1728     _Rp operator()() const;
1729 
1730 #ifndef _LIBCPP_NO_RTTI
1731     // 20.7.16.2.5, function target access:
1732     const std::type_info& target_type() const;
1733     template <typename _Tp> _Tp* target();
1734     template <typename _Tp> const _Tp* target() const;
1735 #endif // _LIBCPP_NO_RTTI
1736 };
1737 
1738 template<class _Rp>
1739 function<_Rp()>::function(const function& __f)
1740 {
1741     if (__f.__f_ == 0)
1742         __f_ = 0;
1743     else if (__f.__f_ == (const __base*)&__f.__buf_)
1744     {
1745         __f_ = (__base*)&__buf_;
1746         __f.__f_->__clone(__f_);
1747     }
1748     else
1749         __f_ = __f.__f_->__clone();
1750 }
1751 
1752 template<class _Rp>
1753 template<class _Alloc>
1754 function<_Rp()>::function(allocator_arg_t, const _Alloc&, const function& __f)
1755 {
1756     if (__f.__f_ == 0)
1757         __f_ = 0;
1758     else if (__f.__f_ == (const __base*)&__f.__buf_)
1759     {
1760         __f_ = (__base*)&__buf_;
1761         __f.__f_->__clone(__f_);
1762     }
1763     else
1764         __f_ = __f.__f_->__clone();
1765 }
1766 
1767 template<class _Rp>
1768 template <class _Fp>
1769 function<_Rp()>::function(_Fp __f,
1770                                      typename enable_if<!is_integral<_Fp>::value>::type*)
1771     : __f_(0)
1772 {
1773     if (__function::__not_null(__f))
1774     {
1775         typedef __function::__func<_Fp, allocator<_Fp>, _Rp()> _FF;
1776         if (sizeof(_FF) <= sizeof(__buf_))
1777         {
1778             __f_ = (__base*)&__buf_;
1779             ::new ((void*)__f_) _FF(__f);
1780         }
1781         else
1782         {
1783             typedef allocator<_FF> _Ap;
1784             _Ap __a;
1785             typedef __allocator_destructor<_Ap> _Dp;
1786             unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1787             ::new ((void*)__hold.get()) _FF(__f, allocator<_Fp>(__a));
1788             __f_ = __hold.release();
1789         }
1790     }
1791 }
1792 
1793 template<class _Rp>
1794 template <class _Fp, class _Alloc>
1795 function<_Rp()>::function(allocator_arg_t, const _Alloc& __a0, _Fp __f,
1796                                      typename enable_if<!is_integral<_Fp>::value>::type*)
1797     : __f_(0)
1798 {
1799     typedef allocator_traits<_Alloc> __alloc_traits;
1800     if (__function::__not_null(__f))
1801     {
1802         typedef __function::__func<_Fp, _Alloc, _Rp()> _FF;
1803         if (sizeof(_FF) <= sizeof(__buf_))
1804         {
1805             __f_ = (__base*)&__buf_;
1806             ::new ((void*)__f_) _FF(__f, __a0);
1807         }
1808         else
1809         {
1810             typedef typename __rebind_alloc_helper<__alloc_traits, _FF>::type _Ap;
1811             _Ap __a(__a0);
1812             typedef __allocator_destructor<_Ap> _Dp;
1813             unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1814             ::new ((void*)__hold.get()) _FF(__f, _Alloc(__a));
1815             __f_ = __hold.release();
1816         }
1817     }
1818 }
1819 
1820 template<class _Rp>
1821 function<_Rp()>&
1822 function<_Rp()>::operator=(const function& __f)
1823 {
1824     if (__f)
1825         function(__f).swap(*this);
1826     else
1827         *this = nullptr;
1828     return *this;
1829 }
1830 
1831 template<class _Rp>
1832 function<_Rp()>&
1833 function<_Rp()>::operator=(nullptr_t)
1834 {
1835     __base* __t = __f_;
1836     __f_ = 0;
1837     if (__t == (__base*)&__buf_)
1838         __t->destroy();
1839     else if (__t)
1840         __t->destroy_deallocate();
1841     return *this;
1842 }
1843 
1844 template<class _Rp>
1845 template <class _Fp>
1846 typename enable_if
1847 <
1848     !is_integral<_Fp>::value,
1849     function<_Rp()>&
1850 >::type
1851 function<_Rp()>::operator=(_Fp __f)
1852 {
1853     function(_VSTD::move(__f)).swap(*this);
1854     return *this;
1855 }
1856 
1857 template<class _Rp>
1858 function<_Rp()>::~function()
1859 {
1860     if (__f_ == (__base*)&__buf_)
1861         __f_->destroy();
1862     else if (__f_)
1863         __f_->destroy_deallocate();
1864 }
1865 
1866 template<class _Rp>
1867 void
1868 function<_Rp()>::swap(function& __f)
1869 {
1870     if (_VSTD::addressof(__f) == this)
1871       return;
1872     if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
1873     {
1874         typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1875         __base* __t = (__base*)&__tempbuf;
1876         __f_->__clone(__t);
1877         __f_->destroy();
1878         __f_ = 0;
1879         __f.__f_->__clone((__base*)&__buf_);
1880         __f.__f_->destroy();
1881         __f.__f_ = 0;
1882         __f_ = (__base*)&__buf_;
1883         __t->__clone((__base*)&__f.__buf_);
1884         __t->destroy();
1885         __f.__f_ = (__base*)&__f.__buf_;
1886     }
1887     else if (__f_ == (__base*)&__buf_)
1888     {
1889         __f_->__clone((__base*)&__f.__buf_);
1890         __f_->destroy();
1891         __f_ = __f.__f_;
1892         __f.__f_ = (__base*)&__f.__buf_;
1893     }
1894     else if (__f.__f_ == (__base*)&__f.__buf_)
1895     {
1896         __f.__f_->__clone((__base*)&__buf_);
1897         __f.__f_->destroy();
1898         __f.__f_ = __f_;
1899         __f_ = (__base*)&__buf_;
1900     }
1901     else
1902         _VSTD::swap(__f_, __f.__f_);
1903 }
1904 
1905 template<class _Rp>
1906 _Rp
1907 function<_Rp()>::operator()() const
1908 {
1909     if (__f_ == 0)
1910         __throw_bad_function_call();
1911     return (*__f_)();
1912 }
1913 
1914 #ifndef _LIBCPP_NO_RTTI
1915 
1916 template<class _Rp>
1917 const std::type_info&
1918 function<_Rp()>::target_type() const
1919 {
1920     if (__f_ == 0)
1921         return typeid(void);
1922     return __f_->target_type();
1923 }
1924 
1925 template<class _Rp>
1926 template <typename _Tp>
1927 _Tp*
1928 function<_Rp()>::target()
1929 {
1930     if (__f_ == 0)
1931         return (_Tp*)0;
1932     return (_Tp*) const_cast<void *>(__f_->target(typeid(_Tp)));
1933 }
1934 
1935 template<class _Rp>
1936 template <typename _Tp>
1937 const _Tp*
1938 function<_Rp()>::target() const
1939 {
1940     if (__f_ == 0)
1941         return (const _Tp*)0;
1942     return (const _Tp*)__f_->target(typeid(_Tp));
1943 }
1944 
1945 #endif // _LIBCPP_NO_RTTI
1946 
1947 template<class _Rp, class _A0>
1948 class _LIBCPP_TEMPLATE_VIS function<_Rp(_A0)>
1949     : public unary_function<_A0, _Rp>
1950 {
1951     typedef __function::__base<_Rp(_A0)> __base;
1952     aligned_storage<3*sizeof(void*)>::type __buf_;
1953     __base* __f_;
1954 
1955 public:
1956     typedef _Rp result_type;
1957 
1958     // 20.7.16.2.1, construct/copy/destroy:
1959     _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {}
1960     _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {}
1961     function(const function&);
1962     template<class _Fp>
1963       function(_Fp,
1964                typename enable_if<!is_integral<_Fp>::value>::type* = 0);
1965 
1966     template<class _Alloc>
1967       _LIBCPP_INLINE_VISIBILITY
1968       function(allocator_arg_t, const _Alloc&) : __f_(0) {}
1969     template<class _Alloc>
1970       _LIBCPP_INLINE_VISIBILITY
1971       function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {}
1972     template<class _Alloc>
1973       function(allocator_arg_t, const _Alloc&, const function&);
1974     template<class _Fp, class _Alloc>
1975       function(allocator_arg_t, const _Alloc& __a, _Fp __f,
1976                typename enable_if<!is_integral<_Fp>::value>::type* = 0);
1977 
1978     function& operator=(const function&);
1979     function& operator=(nullptr_t);
1980     template<class _Fp>
1981       typename enable_if
1982       <
1983         !is_integral<_Fp>::value,
1984         function&
1985       >::type
1986       operator=(_Fp);
1987 
1988     ~function();
1989 
1990     // 20.7.16.2.2, function modifiers:
1991     void swap(function&);
1992     template<class _Fp, class _Alloc>
1993       _LIBCPP_INLINE_VISIBILITY
1994       void assign(_Fp __f, const _Alloc& __a)
1995         {function(allocator_arg, __a, __f).swap(*this);}
1996 
1997     // 20.7.16.2.3, function capacity:
1998     _LIBCPP_INLINE_VISIBILITY explicit operator bool() const {return __f_;}
1999 
2000     template<class _R2, class _B0>
2001       bool operator==(const function<_R2(_B0)>&) const = delete;
2002     template<class _R2, class _B0>
2003       bool operator!=(const function<_R2(_B0)>&) const = delete;
2004 
2005     // 20.7.16.2.4, function invocation:
2006     _Rp operator()(_A0) const;
2007 
2008 #ifndef _LIBCPP_NO_RTTI
2009     // 20.7.16.2.5, function target access:
2010     const std::type_info& target_type() const;
2011     template <typename _Tp> _Tp* target();
2012     template <typename _Tp> const _Tp* target() const;
2013 #endif // _LIBCPP_NO_RTTI
2014 };
2015 
2016 template<class _Rp, class _A0>
2017 function<_Rp(_A0)>::function(const function& __f)
2018 {
2019     if (__f.__f_ == 0)
2020         __f_ = 0;
2021     else if (__f.__f_ == (const __base*)&__f.__buf_)
2022     {
2023         __f_ = (__base*)&__buf_;
2024         __f.__f_->__clone(__f_);
2025     }
2026     else
2027         __f_ = __f.__f_->__clone();
2028 }
2029 
2030 template<class _Rp, class _A0>
2031 template<class _Alloc>
2032 function<_Rp(_A0)>::function(allocator_arg_t, const _Alloc&, const function& __f)
2033 {
2034     if (__f.__f_ == 0)
2035         __f_ = 0;
2036     else if (__f.__f_ == (const __base*)&__f.__buf_)
2037     {
2038         __f_ = (__base*)&__buf_;
2039         __f.__f_->__clone(__f_);
2040     }
2041     else
2042         __f_ = __f.__f_->__clone();
2043 }
2044 
2045 template<class _Rp, class _A0>
2046 template <class _Fp>
2047 function<_Rp(_A0)>::function(_Fp __f,
2048                                      typename enable_if<!is_integral<_Fp>::value>::type*)
2049     : __f_(0)
2050 {
2051     if (__function::__not_null(__f))
2052     {
2053         typedef __function::__func<_Fp, allocator<_Fp>, _Rp(_A0)> _FF;
2054         if (sizeof(_FF) <= sizeof(__buf_))
2055         {
2056             __f_ = (__base*)&__buf_;
2057             ::new ((void*)__f_) _FF(__f);
2058         }
2059         else
2060         {
2061             typedef allocator<_FF> _Ap;
2062             _Ap __a;
2063             typedef __allocator_destructor<_Ap> _Dp;
2064             unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
2065             ::new ((void*)__hold.get()) _FF(__f, allocator<_Fp>(__a));
2066             __f_ = __hold.release();
2067         }
2068     }
2069 }
2070 
2071 template<class _Rp, class _A0>
2072 template <class _Fp, class _Alloc>
2073 function<_Rp(_A0)>::function(allocator_arg_t, const _Alloc& __a0, _Fp __f,
2074                                      typename enable_if<!is_integral<_Fp>::value>::type*)
2075     : __f_(0)
2076 {
2077     typedef allocator_traits<_Alloc> __alloc_traits;
2078     if (__function::__not_null(__f))
2079     {
2080         typedef __function::__func<_Fp, _Alloc, _Rp(_A0)> _FF;
2081         if (sizeof(_FF) <= sizeof(__buf_))
2082         {
2083             __f_ = (__base*)&__buf_;
2084             ::new ((void*)__f_) _FF(__f, __a0);
2085         }
2086         else
2087         {
2088             typedef typename __rebind_alloc_helper<__alloc_traits, _FF>::type _Ap;
2089             _Ap __a(__a0);
2090             typedef __allocator_destructor<_Ap> _Dp;
2091             unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
2092             ::new ((void*)__hold.get()) _FF(__f, _Alloc(__a));
2093             __f_ = __hold.release();
2094         }
2095     }
2096 }
2097 
2098 template<class _Rp, class _A0>
2099 function<_Rp(_A0)>&
2100 function<_Rp(_A0)>::operator=(const function& __f)
2101 {
2102     if (__f)
2103         function(__f).swap(*this);
2104     else
2105         *this = nullptr;
2106     return *this;
2107 }
2108 
2109 template<class _Rp, class _A0>
2110 function<_Rp(_A0)>&
2111 function<_Rp(_A0)>::operator=(nullptr_t)
2112 {
2113     __base* __t = __f_;
2114     __f_ = 0;
2115     if (__t == (__base*)&__buf_)
2116         __t->destroy();
2117     else if (__t)
2118         __t->destroy_deallocate();
2119     return *this;
2120 }
2121 
2122 template<class _Rp, class _A0>
2123 template <class _Fp>
2124 typename enable_if
2125 <
2126     !is_integral<_Fp>::value,
2127     function<_Rp(_A0)>&
2128 >::type
2129 function<_Rp(_A0)>::operator=(_Fp __f)
2130 {
2131     function(_VSTD::move(__f)).swap(*this);
2132     return *this;
2133 }
2134 
2135 template<class _Rp, class _A0>
2136 function<_Rp(_A0)>::~function()
2137 {
2138     if (__f_ == (__base*)&__buf_)
2139         __f_->destroy();
2140     else if (__f_)
2141         __f_->destroy_deallocate();
2142 }
2143 
2144 template<class _Rp, class _A0>
2145 void
2146 function<_Rp(_A0)>::swap(function& __f)
2147 {
2148     if (_VSTD::addressof(__f) == this)
2149       return;
2150     if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
2151     {
2152         typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
2153         __base* __t = (__base*)&__tempbuf;
2154         __f_->__clone(__t);
2155         __f_->destroy();
2156         __f_ = 0;
2157         __f.__f_->__clone((__base*)&__buf_);
2158         __f.__f_->destroy();
2159         __f.__f_ = 0;
2160         __f_ = (__base*)&__buf_;
2161         __t->__clone((__base*)&__f.__buf_);
2162         __t->destroy();
2163         __f.__f_ = (__base*)&__f.__buf_;
2164     }
2165     else if (__f_ == (__base*)&__buf_)
2166     {
2167         __f_->__clone((__base*)&__f.__buf_);
2168         __f_->destroy();
2169         __f_ = __f.__f_;
2170         __f.__f_ = (__base*)&__f.__buf_;
2171     }
2172     else if (__f.__f_ == (__base*)&__f.__buf_)
2173     {
2174         __f.__f_->__clone((__base*)&__buf_);
2175         __f.__f_->destroy();
2176         __f.__f_ = __f_;
2177         __f_ = (__base*)&__buf_;
2178     }
2179     else
2180         _VSTD::swap(__f_, __f.__f_);
2181 }
2182 
2183 template<class _Rp, class _A0>
2184 _Rp
2185 function<_Rp(_A0)>::operator()(_A0 __a0) const
2186 {
2187     if (__f_ == 0)
2188         __throw_bad_function_call();
2189     return (*__f_)(__a0);
2190 }
2191 
2192 #ifndef _LIBCPP_NO_RTTI
2193 
2194 template<class _Rp, class _A0>
2195 const std::type_info&
2196 function<_Rp(_A0)>::target_type() const
2197 {
2198     if (__f_ == 0)
2199         return typeid(void);
2200     return __f_->target_type();
2201 }
2202 
2203 template<class _Rp, class _A0>
2204 template <typename _Tp>
2205 _Tp*
2206 function<_Rp(_A0)>::target()
2207 {
2208     if (__f_ == 0)
2209         return (_Tp*)0;
2210     return (_Tp*) const_cast<void *>(__f_->target(typeid(_Tp)));
2211 }
2212 
2213 template<class _Rp, class _A0>
2214 template <typename _Tp>
2215 const _Tp*
2216 function<_Rp(_A0)>::target() const
2217 {
2218     if (__f_ == 0)
2219         return (const _Tp*)0;
2220     return (const _Tp*)__f_->target(typeid(_Tp));
2221 }
2222 
2223 #endif // _LIBCPP_NO_RTTI
2224 
2225 template<class _Rp, class _A0, class _A1>
2226 class _LIBCPP_TEMPLATE_VIS function<_Rp(_A0, _A1)>
2227     : public binary_function<_A0, _A1, _Rp>
2228 {
2229     typedef __function::__base<_Rp(_A0, _A1)> __base;
2230     aligned_storage<3*sizeof(void*)>::type __buf_;
2231     __base* __f_;
2232 
2233 public:
2234     typedef _Rp result_type;
2235 
2236     // 20.7.16.2.1, construct/copy/destroy:
2237     _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {}
2238     _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {}
2239     function(const function&);
2240     template<class _Fp>
2241       function(_Fp,
2242                typename enable_if<!is_integral<_Fp>::value>::type* = 0);
2243 
2244     template<class _Alloc>
2245       _LIBCPP_INLINE_VISIBILITY
2246       function(allocator_arg_t, const _Alloc&) : __f_(0) {}
2247     template<class _Alloc>
2248       _LIBCPP_INLINE_VISIBILITY
2249       function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {}
2250     template<class _Alloc>
2251       function(allocator_arg_t, const _Alloc&, const function&);
2252     template<class _Fp, class _Alloc>
2253       function(allocator_arg_t, const _Alloc& __a, _Fp __f,
2254                typename enable_if<!is_integral<_Fp>::value>::type* = 0);
2255 
2256     function& operator=(const function&);
2257     function& operator=(nullptr_t);
2258     template<class _Fp>
2259       typename enable_if
2260       <
2261         !is_integral<_Fp>::value,
2262         function&
2263       >::type
2264       operator=(_Fp);
2265 
2266     ~function();
2267 
2268     // 20.7.16.2.2, function modifiers:
2269     void swap(function&);
2270     template<class _Fp, class _Alloc>
2271       _LIBCPP_INLINE_VISIBILITY
2272       void assign(_Fp __f, const _Alloc& __a)
2273         {function(allocator_arg, __a, __f).swap(*this);}
2274 
2275     // 20.7.16.2.3, function capacity:
2276     _LIBCPP_INLINE_VISIBILITY explicit operator bool() const {return __f_;}
2277 
2278     template<class _R2, class _B0, class _B1>
2279       bool operator==(const function<_R2(_B0, _B1)>&) const = delete;
2280     template<class _R2, class _B0, class _B1>
2281       bool operator!=(const function<_R2(_B0, _B1)>&) const = delete;
2282 
2283     // 20.7.16.2.4, function invocation:
2284     _Rp operator()(_A0, _A1) const;
2285 
2286 #ifndef _LIBCPP_NO_RTTI
2287     // 20.7.16.2.5, function target access:
2288     const std::type_info& target_type() const;
2289     template <typename _Tp> _Tp* target();
2290     template <typename _Tp> const _Tp* target() const;
2291 #endif // _LIBCPP_NO_RTTI
2292 };
2293 
2294 template<class _Rp, class _A0, class _A1>
2295 function<_Rp(_A0, _A1)>::function(const function& __f)
2296 {
2297     if (__f.__f_ == 0)
2298         __f_ = 0;
2299     else if (__f.__f_ == (const __base*)&__f.__buf_)
2300     {
2301         __f_ = (__base*)&__buf_;
2302         __f.__f_->__clone(__f_);
2303     }
2304     else
2305         __f_ = __f.__f_->__clone();
2306 }
2307 
2308 template<class _Rp, class _A0, class _A1>
2309 template<class _Alloc>
2310 function<_Rp(_A0, _A1)>::function(allocator_arg_t, const _Alloc&, const function& __f)
2311 {
2312     if (__f.__f_ == 0)
2313         __f_ = 0;
2314     else if (__f.__f_ == (const __base*)&__f.__buf_)
2315     {
2316         __f_ = (__base*)&__buf_;
2317         __f.__f_->__clone(__f_);
2318     }
2319     else
2320         __f_ = __f.__f_->__clone();
2321 }
2322 
2323 template<class _Rp, class _A0, class _A1>
2324 template <class _Fp>
2325 function<_Rp(_A0, _A1)>::function(_Fp __f,
2326                                  typename enable_if<!is_integral<_Fp>::value>::type*)
2327     : __f_(0)
2328 {
2329     if (__function::__not_null(__f))
2330     {
2331         typedef __function::__func<_Fp, allocator<_Fp>, _Rp(_A0, _A1)> _FF;
2332         if (sizeof(_FF) <= sizeof(__buf_))
2333         {
2334             __f_ = (__base*)&__buf_;
2335             ::new ((void*)__f_) _FF(__f);
2336         }
2337         else
2338         {
2339             typedef allocator<_FF> _Ap;
2340             _Ap __a;
2341             typedef __allocator_destructor<_Ap> _Dp;
2342             unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
2343             ::new ((void*)__hold.get()) _FF(__f, allocator<_Fp>(__a));
2344             __f_ = __hold.release();
2345         }
2346     }
2347 }
2348 
2349 template<class _Rp, class _A0, class _A1>
2350 template <class _Fp, class _Alloc>
2351 function<_Rp(_A0, _A1)>::function(allocator_arg_t, const _Alloc& __a0, _Fp __f,
2352                                  typename enable_if<!is_integral<_Fp>::value>::type*)
2353     : __f_(0)
2354 {
2355     typedef allocator_traits<_Alloc> __alloc_traits;
2356     if (__function::__not_null(__f))
2357     {
2358         typedef __function::__func<_Fp, _Alloc, _Rp(_A0, _A1)> _FF;
2359         if (sizeof(_FF) <= sizeof(__buf_))
2360         {
2361             __f_ = (__base*)&__buf_;
2362             ::new ((void*)__f_) _FF(__f, __a0);
2363         }
2364         else
2365         {
2366             typedef typename __rebind_alloc_helper<__alloc_traits, _FF>::type _Ap;
2367             _Ap __a(__a0);
2368             typedef __allocator_destructor<_Ap> _Dp;
2369             unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
2370             ::new ((void*)__hold.get()) _FF(__f, _Alloc(__a));
2371             __f_ = __hold.release();
2372         }
2373     }
2374 }
2375 
2376 template<class _Rp, class _A0, class _A1>
2377 function<_Rp(_A0, _A1)>&
2378 function<_Rp(_A0, _A1)>::operator=(const function& __f)
2379 {
2380     if (__f)
2381         function(__f).swap(*this);
2382     else
2383         *this = nullptr;
2384     return *this;
2385 }
2386 
2387 template<class _Rp, class _A0, class _A1>
2388 function<_Rp(_A0, _A1)>&
2389 function<_Rp(_A0, _A1)>::operator=(nullptr_t)
2390 {
2391     __base* __t = __f_;
2392     __f_ = 0;
2393     if (__t == (__base*)&__buf_)
2394         __t->destroy();
2395     else if (__t)
2396         __t->destroy_deallocate();
2397     return *this;
2398 }
2399 
2400 template<class _Rp, class _A0, class _A1>
2401 template <class _Fp>
2402 typename enable_if
2403 <
2404     !is_integral<_Fp>::value,
2405     function<_Rp(_A0, _A1)>&
2406 >::type
2407 function<_Rp(_A0, _A1)>::operator=(_Fp __f)
2408 {
2409     function(_VSTD::move(__f)).swap(*this);
2410     return *this;
2411 }
2412 
2413 template<class _Rp, class _A0, class _A1>
2414 function<_Rp(_A0, _A1)>::~function()
2415 {
2416     if (__f_ == (__base*)&__buf_)
2417         __f_->destroy();
2418     else if (__f_)
2419         __f_->destroy_deallocate();
2420 }
2421 
2422 template<class _Rp, class _A0, class _A1>
2423 void
2424 function<_Rp(_A0, _A1)>::swap(function& __f)
2425 {
2426     if (_VSTD::addressof(__f) == this)
2427       return;
2428     if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
2429     {
2430         typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
2431         __base* __t = (__base*)&__tempbuf;
2432         __f_->__clone(__t);
2433         __f_->destroy();
2434         __f_ = 0;
2435         __f.__f_->__clone((__base*)&__buf_);
2436         __f.__f_->destroy();
2437         __f.__f_ = 0;
2438         __f_ = (__base*)&__buf_;
2439         __t->__clone((__base*)&__f.__buf_);
2440         __t->destroy();
2441         __f.__f_ = (__base*)&__f.__buf_;
2442     }
2443     else if (__f_ == (__base*)&__buf_)
2444     {
2445         __f_->__clone((__base*)&__f.__buf_);
2446         __f_->destroy();
2447         __f_ = __f.__f_;
2448         __f.__f_ = (__base*)&__f.__buf_;
2449     }
2450     else if (__f.__f_ == (__base*)&__f.__buf_)
2451     {
2452         __f.__f_->__clone((__base*)&__buf_);
2453         __f.__f_->destroy();
2454         __f.__f_ = __f_;
2455         __f_ = (__base*)&__buf_;
2456     }
2457     else
2458         _VSTD::swap(__f_, __f.__f_);
2459 }
2460 
2461 template<class _Rp, class _A0, class _A1>
2462 _Rp
2463 function<_Rp(_A0, _A1)>::operator()(_A0 __a0, _A1 __a1) const
2464 {
2465     if (__f_ == 0)
2466         __throw_bad_function_call();
2467     return (*__f_)(__a0, __a1);
2468 }
2469 
2470 #ifndef _LIBCPP_NO_RTTI
2471 
2472 template<class _Rp, class _A0, class _A1>
2473 const std::type_info&
2474 function<_Rp(_A0, _A1)>::target_type() const
2475 {
2476     if (__f_ == 0)
2477         return typeid(void);
2478     return __f_->target_type();
2479 }
2480 
2481 template<class _Rp, class _A0, class _A1>
2482 template <typename _Tp>
2483 _Tp*
2484 function<_Rp(_A0, _A1)>::target()
2485 {
2486     if (__f_ == 0)
2487         return (_Tp*)0;
2488     return (_Tp*) const_cast<void *>(__f_->target(typeid(_Tp)));
2489 }
2490 
2491 template<class _Rp, class _A0, class _A1>
2492 template <typename _Tp>
2493 const _Tp*
2494 function<_Rp(_A0, _A1)>::target() const
2495 {
2496     if (__f_ == 0)
2497         return (const _Tp*)0;
2498     return (const _Tp*)__f_->target(typeid(_Tp));
2499 }
2500 
2501 #endif // _LIBCPP_NO_RTTI
2502 
2503 template<class _Rp, class _A0, class _A1, class _A2>
2504 class _LIBCPP_TEMPLATE_VIS function<_Rp(_A0, _A1, _A2)>
2505 {
2506     typedef __function::__base<_Rp(_A0, _A1, _A2)> __base;
2507     aligned_storage<3*sizeof(void*)>::type __buf_;
2508     __base* __f_;
2509 
2510 public:
2511     typedef _Rp result_type;
2512 
2513     // 20.7.16.2.1, construct/copy/destroy:
2514     _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {}
2515     _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {}
2516     function(const function&);
2517     template<class _Fp>
2518       function(_Fp,
2519                typename enable_if<!is_integral<_Fp>::value>::type* = 0);
2520 
2521     template<class _Alloc>
2522       _LIBCPP_INLINE_VISIBILITY
2523       function(allocator_arg_t, const _Alloc&) : __f_(0) {}
2524     template<class _Alloc>
2525       _LIBCPP_INLINE_VISIBILITY
2526       function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {}
2527     template<class _Alloc>
2528       function(allocator_arg_t, const _Alloc&, const function&);
2529     template<class _Fp, class _Alloc>
2530       function(allocator_arg_t, const _Alloc& __a, _Fp __f,
2531                typename enable_if<!is_integral<_Fp>::value>::type* = 0);
2532 
2533     function& operator=(const function&);
2534     function& operator=(nullptr_t);
2535     template<class _Fp>
2536       typename enable_if
2537       <
2538         !is_integral<_Fp>::value,
2539         function&
2540       >::type
2541       operator=(_Fp);
2542 
2543     ~function();
2544 
2545     // 20.7.16.2.2, function modifiers:
2546     void swap(function&);
2547     template<class _Fp, class _Alloc>
2548       _LIBCPP_INLINE_VISIBILITY
2549       void assign(_Fp __f, const _Alloc& __a)
2550         {function(allocator_arg, __a, __f).swap(*this);}
2551 
2552     // 20.7.16.2.3, function capacity:
2553     _LIBCPP_INLINE_VISIBILITY explicit operator bool() const {return __f_;}
2554 
2555     template<class _R2, class _B0, class _B1, class _B2>
2556       bool operator==(const function<_R2(_B0, _B1, _B2)>&) const = delete;
2557     template<class _R2, class _B0, class _B1, class _B2>
2558       bool operator!=(const function<_R2(_B0, _B1, _B2)>&) const = delete;
2559 
2560     // 20.7.16.2.4, function invocation:
2561     _Rp operator()(_A0, _A1, _A2) const;
2562 
2563 #ifndef _LIBCPP_NO_RTTI
2564     // 20.7.16.2.5, function target access:
2565     const std::type_info& target_type() const;
2566     template <typename _Tp> _Tp* target();
2567     template <typename _Tp> const _Tp* target() const;
2568 #endif // _LIBCPP_NO_RTTI
2569 };
2570 
2571 template<class _Rp, class _A0, class _A1, class _A2>
2572 function<_Rp(_A0, _A1, _A2)>::function(const function& __f)
2573 {
2574     if (__f.__f_ == 0)
2575         __f_ = 0;
2576     else if (__f.__f_ == (const __base*)&__f.__buf_)
2577     {
2578         __f_ = (__base*)&__buf_;
2579         __f.__f_->__clone(__f_);
2580     }
2581     else
2582         __f_ = __f.__f_->__clone();
2583 }
2584 
2585 template<class _Rp, class _A0, class _A1, class _A2>
2586 template<class _Alloc>
2587 function<_Rp(_A0, _A1, _A2)>::function(allocator_arg_t, const _Alloc&,
2588                                       const function& __f)
2589 {
2590     if (__f.__f_ == 0)
2591         __f_ = 0;
2592     else if (__f.__f_ == (const __base*)&__f.__buf_)
2593     {
2594         __f_ = (__base*)&__buf_;
2595         __f.__f_->__clone(__f_);
2596     }
2597     else
2598         __f_ = __f.__f_->__clone();
2599 }
2600 
2601 template<class _Rp, class _A0, class _A1, class _A2>
2602 template <class _Fp>
2603 function<_Rp(_A0, _A1, _A2)>::function(_Fp __f,
2604                                      typename enable_if<!is_integral<_Fp>::value>::type*)
2605     : __f_(0)
2606 {
2607     if (__function::__not_null(__f))
2608     {
2609         typedef __function::__func<_Fp, allocator<_Fp>, _Rp(_A0, _A1, _A2)> _FF;
2610         if (sizeof(_FF) <= sizeof(__buf_))
2611         {
2612             __f_ = (__base*)&__buf_;
2613             ::new ((void*)__f_) _FF(__f);
2614         }
2615         else
2616         {
2617             typedef allocator<_FF> _Ap;
2618             _Ap __a;
2619             typedef __allocator_destructor<_Ap> _Dp;
2620             unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
2621             ::new ((void*)__hold.get()) _FF(__f, allocator<_Fp>(__a));
2622             __f_ = __hold.release();
2623         }
2624     }
2625 }
2626 
2627 template<class _Rp, class _A0, class _A1, class _A2>
2628 template <class _Fp, class _Alloc>
2629 function<_Rp(_A0, _A1, _A2)>::function(allocator_arg_t, const _Alloc& __a0, _Fp __f,
2630                                      typename enable_if<!is_integral<_Fp>::value>::type*)
2631     : __f_(0)
2632 {
2633     typedef allocator_traits<_Alloc> __alloc_traits;
2634     if (__function::__not_null(__f))
2635     {
2636         typedef __function::__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)> _FF;
2637         if (sizeof(_FF) <= sizeof(__buf_))
2638         {
2639             __f_ = (__base*)&__buf_;
2640             ::new ((void*)__f_) _FF(__f, __a0);
2641         }
2642         else
2643         {
2644             typedef typename __rebind_alloc_helper<__alloc_traits, _FF>::type _Ap;
2645             _Ap __a(__a0);
2646             typedef __allocator_destructor<_Ap> _Dp;
2647             unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
2648             ::new ((void*)__hold.get()) _FF(__f, _Alloc(__a));
2649             __f_ = __hold.release();
2650         }
2651     }
2652 }
2653 
2654 template<class _Rp, class _A0, class _A1, class _A2>
2655 function<_Rp(_A0, _A1, _A2)>&
2656 function<_Rp(_A0, _A1, _A2)>::operator=(const function& __f)
2657 {
2658     if (__f)
2659         function(__f).swap(*this);
2660     else
2661         *this = nullptr;
2662     return *this;
2663 }
2664 
2665 template<class _Rp, class _A0, class _A1, class _A2>
2666 function<_Rp(_A0, _A1, _A2)>&
2667 function<_Rp(_A0, _A1, _A2)>::operator=(nullptr_t)
2668 {
2669     __base* __t = __f_;
2670     __f_ = 0;
2671     if (__t == (__base*)&__buf_)
2672         __t->destroy();
2673     else if (__t)
2674         __t->destroy_deallocate();
2675     return *this;
2676 }
2677 
2678 template<class _Rp, class _A0, class _A1, class _A2>
2679 template <class _Fp>
2680 typename enable_if
2681 <
2682     !is_integral<_Fp>::value,
2683     function<_Rp(_A0, _A1, _A2)>&
2684 >::type
2685 function<_Rp(_A0, _A1, _A2)>::operator=(_Fp __f)
2686 {
2687     function(_VSTD::move(__f)).swap(*this);
2688     return *this;
2689 }
2690 
2691 template<class _Rp, class _A0, class _A1, class _A2>
2692 function<_Rp(_A0, _A1, _A2)>::~function()
2693 {
2694     if (__f_ == (__base*)&__buf_)
2695         __f_->destroy();
2696     else if (__f_)
2697         __f_->destroy_deallocate();
2698 }
2699 
2700 template<class _Rp, class _A0, class _A1, class _A2>
2701 void
2702 function<_Rp(_A0, _A1, _A2)>::swap(function& __f)
2703 {
2704     if (_VSTD::addressof(__f) == this)
2705       return;
2706     if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
2707     {
2708         typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
2709         __base* __t = (__base*)&__tempbuf;
2710         __f_->__clone(__t);
2711         __f_->destroy();
2712         __f_ = 0;
2713         __f.__f_->__clone((__base*)&__buf_);
2714         __f.__f_->destroy();
2715         __f.__f_ = 0;
2716         __f_ = (__base*)&__buf_;
2717         __t->__clone((__base*)&__f.__buf_);
2718         __t->destroy();
2719         __f.__f_ = (__base*)&__f.__buf_;
2720     }
2721     else if (__f_ == (__base*)&__buf_)
2722     {
2723         __f_->__clone((__base*)&__f.__buf_);
2724         __f_->destroy();
2725         __f_ = __f.__f_;
2726         __f.__f_ = (__base*)&__f.__buf_;
2727     }
2728     else if (__f.__f_ == (__base*)&__f.__buf_)
2729     {
2730         __f.__f_->__clone((__base*)&__buf_);
2731         __f.__f_->destroy();
2732         __f.__f_ = __f_;
2733         __f_ = (__base*)&__buf_;
2734     }
2735     else
2736         _VSTD::swap(__f_, __f.__f_);
2737 }
2738 
2739 template<class _Rp, class _A0, class _A1, class _A2>
2740 _Rp
2741 function<_Rp(_A0, _A1, _A2)>::operator()(_A0 __a0, _A1 __a1, _A2 __a2) const
2742 {
2743     if (__f_ == 0)
2744         __throw_bad_function_call();
2745     return (*__f_)(__a0, __a1, __a2);
2746 }
2747 
2748 #ifndef _LIBCPP_NO_RTTI
2749 
2750 template<class _Rp, class _A0, class _A1, class _A2>
2751 const std::type_info&
2752 function<_Rp(_A0, _A1, _A2)>::target_type() const
2753 {
2754     if (__f_ == 0)
2755         return typeid(void);
2756     return __f_->target_type();
2757 }
2758 
2759 template<class _Rp, class _A0, class _A1, class _A2>
2760 template <typename _Tp>
2761 _Tp*
2762 function<_Rp(_A0, _A1, _A2)>::target()
2763 {
2764     if (__f_ == 0)
2765         return (_Tp*)0;
2766     return (_Tp*) const_cast<void *>(__f_->target(typeid(_Tp)));
2767 }
2768 
2769 template<class _Rp, class _A0, class _A1, class _A2>
2770 template <typename _Tp>
2771 const _Tp*
2772 function<_Rp(_A0, _A1, _A2)>::target() const
2773 {
2774     if (__f_ == 0)
2775         return (const _Tp*)0;
2776     return (const _Tp*)__f_->target(typeid(_Tp));
2777 }
2778 
2779 #endif // _LIBCPP_NO_RTTI
2780 
2781 template <class _Fp>
2782 inline _LIBCPP_INLINE_VISIBILITY
2783 bool
2784 operator==(const function<_Fp>& __f, nullptr_t) {return !__f;}
2785 
2786 template <class _Fp>
2787 inline _LIBCPP_INLINE_VISIBILITY
2788 bool
2789 operator==(nullptr_t, const function<_Fp>& __f) {return !__f;}
2790 
2791 template <class _Fp>
2792 inline _LIBCPP_INLINE_VISIBILITY
2793 bool
2794 operator!=(const function<_Fp>& __f, nullptr_t) {return (bool)__f;}
2795 
2796 template <class _Fp>
2797 inline _LIBCPP_INLINE_VISIBILITY
2798 bool
2799 operator!=(nullptr_t, const function<_Fp>& __f) {return (bool)__f;}
2800 
2801 template <class _Fp>
2802 inline _LIBCPP_INLINE_VISIBILITY
2803 void
2804 swap(function<_Fp>& __x, function<_Fp>& __y)
2805 {return __x.swap(__y);}
2806 
2807 #endif
2808 
2809 _LIBCPP_END_NAMESPACE_STD
2810 
2811 #endif // _LIBCPP___FUNCTIONAL_FUNCTION_H
2812