Home | History | Annotate | Download | only in include
      1 // -*- C++ -*-
      2 //===----------------------------------------------------------------------===//
      3 //
      4 //                     The LLVM Compiler Infrastructure
      5 //
      6 // This file is dual licensed under the MIT and the University of Illinois Open
      7 // Source Licenses. See LICENSE.TXT for details.
      8 //
      9 //===----------------------------------------------------------------------===//
     10 
     11 #ifndef _LIBCPP_FUNCTIONAL_03
     12 #define _LIBCPP_FUNCTIONAL_03
     13 
     14 // manual variadic expansion for <functional>
     15 
     16 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
     17 #pragma GCC system_header
     18 #endif
     19 
     20 namespace __function {
     21 
     22 template<class _Fp> class __base;
     23 
     24 template<class _Rp>
     25 class __base<_Rp()>
     26 {
     27     __base(const __base&);
     28     __base& operator=(const __base&);
     29 public:
     30     __base() {}
     31     virtual ~__base() {}
     32     virtual __base* __clone() const = 0;
     33     virtual void __clone(__base*) const = 0;
     34     virtual void destroy() = 0;
     35     virtual void destroy_deallocate() = 0;
     36     virtual _Rp operator()() = 0;
     37 #ifndef _LIBCPP_NO_RTTI
     38     virtual const void* target(const type_info&) const = 0;
     39     virtual const std::type_info& target_type() const = 0;
     40 #endif  // _LIBCPP_NO_RTTI
     41 };
     42 
     43 template<class _Rp, class _A0>
     44 class __base<_Rp(_A0)>
     45 {
     46     __base(const __base&);
     47     __base& operator=(const __base&);
     48 public:
     49     __base() {}
     50     virtual ~__base() {}
     51     virtual __base* __clone() const = 0;
     52     virtual void __clone(__base*) const = 0;
     53     virtual void destroy() = 0;
     54     virtual void destroy_deallocate() = 0;
     55     virtual _Rp operator()(_A0) = 0;
     56 #ifndef _LIBCPP_NO_RTTI
     57     virtual const void* target(const type_info&) const = 0;
     58     virtual const std::type_info& target_type() const = 0;
     59 #endif  // _LIBCPP_NO_RTTI
     60 };
     61 
     62 template<class _Rp, class _A0, class _A1>
     63 class __base<_Rp(_A0, _A1)>
     64 {
     65     __base(const __base&);
     66     __base& operator=(const __base&);
     67 public:
     68     __base() {}
     69     virtual ~__base() {}
     70     virtual __base* __clone() const = 0;
     71     virtual void __clone(__base*) const = 0;
     72     virtual void destroy() = 0;
     73     virtual void destroy_deallocate() = 0;
     74     virtual _Rp operator()(_A0, _A1) = 0;
     75 #ifndef _LIBCPP_NO_RTTI
     76     virtual const void* target(const type_info&) const = 0;
     77     virtual const std::type_info& target_type() const = 0;
     78 #endif  // _LIBCPP_NO_RTTI
     79 };
     80 
     81 template<class _Rp, class _A0, class _A1, class _A2>
     82 class __base<_Rp(_A0, _A1, _A2)>
     83 {
     84     __base(const __base&);
     85     __base& operator=(const __base&);
     86 public:
     87     __base() {}
     88     virtual ~__base() {}
     89     virtual __base* __clone() const = 0;
     90     virtual void __clone(__base*) const = 0;
     91     virtual void destroy() = 0;
     92     virtual void destroy_deallocate() = 0;
     93     virtual _Rp operator()(_A0, _A1, _A2) = 0;
     94 #ifndef _LIBCPP_NO_RTTI
     95     virtual const void* target(const type_info&) const = 0;
     96     virtual const std::type_info& target_type() const = 0;
     97 #endif  // _LIBCPP_NO_RTTI
     98 };
     99 
    100 template<class _FD, class _Alloc, class _FB> class __func;
    101 
    102 template<class _Fp, class _Alloc, class _Rp>
    103 class __func<_Fp, _Alloc, _Rp()>
    104     : public  __base<_Rp()>
    105 {
    106     __compressed_pair<_Fp, _Alloc> __f_;
    107 public:
    108     explicit __func(_Fp __f) : __f_(_VSTD::move(__f)) {}
    109     explicit __func(_Fp __f, _Alloc __a) : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
    110     virtual __base<_Rp()>* __clone() const;
    111     virtual void __clone(__base<_Rp()>*) const;
    112     virtual void destroy();
    113     virtual void destroy_deallocate();
    114     virtual _Rp operator()();
    115 #ifndef _LIBCPP_NO_RTTI
    116     virtual const void* target(const type_info&) const;
    117     virtual const std::type_info& target_type() const;
    118 #endif  // _LIBCPP_NO_RTTI
    119 };
    120 
    121 template<class _Fp, class _Alloc, class _Rp>
    122 __base<_Rp()>*
    123 __func<_Fp, _Alloc, _Rp()>::__clone() const
    124 {
    125     typedef allocator_traits<_Alloc> __alloc_traits;
    126     typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
    127     _Ap __a(__f_.second());
    128     typedef __allocator_destructor<_Ap> _Dp;
    129     unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
    130     ::new (__hold.get()) __func(__f_.first(), _Alloc(__a));
    131     return __hold.release();
    132 }
    133 
    134 template<class _Fp, class _Alloc, class _Rp>
    135 void
    136 __func<_Fp, _Alloc, _Rp()>::__clone(__base<_Rp()>* __p) const
    137 {
    138     ::new (__p) __func(__f_.first(), __f_.second());
    139 }
    140 
    141 template<class _Fp, class _Alloc, class _Rp>
    142 void
    143 __func<_Fp, _Alloc, _Rp()>::destroy()
    144 {
    145     __f_.~__compressed_pair<_Fp, _Alloc>();
    146 }
    147 
    148 template<class _Fp, class _Alloc, class _Rp>
    149 void
    150 __func<_Fp, _Alloc, _Rp()>::destroy_deallocate()
    151 {
    152     typedef allocator_traits<_Alloc> __alloc_traits;
    153     typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
    154     _Ap __a(__f_.second());
    155     __f_.~__compressed_pair<_Fp, _Alloc>();
    156     __a.deallocate(this, 1);
    157 }
    158 
    159 template<class _Fp, class _Alloc, class _Rp>
    160 _Rp
    161 __func<_Fp, _Alloc, _Rp()>::operator()()
    162 {
    163     typedef __invoke_void_return_wrapper<_Rp> _Invoker;
    164     return _Invoker::__call(__f_.first());
    165 }
    166 
    167 #ifndef _LIBCPP_NO_RTTI
    168 
    169 template<class _Fp, class _Alloc, class _Rp>
    170 const void*
    171 __func<_Fp, _Alloc, _Rp()>::target(const type_info& __ti) const
    172 {
    173     if (__ti == typeid(_Fp))
    174         return &__f_.first();
    175     return (const void*)0;
    176 }
    177 
    178 template<class _Fp, class _Alloc, class _Rp>
    179 const std::type_info&
    180 __func<_Fp, _Alloc, _Rp()>::target_type() const
    181 {
    182     return typeid(_Fp);
    183 }
    184 
    185 #endif  // _LIBCPP_NO_RTTI
    186 
    187 template<class _Fp, class _Alloc, class _Rp, class _A0>
    188 class __func<_Fp, _Alloc, _Rp(_A0)>
    189     : public  __base<_Rp(_A0)>
    190 {
    191     __compressed_pair<_Fp, _Alloc> __f_;
    192 public:
    193     _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f) : __f_(_VSTD::move(__f)) {}
    194     _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f, _Alloc __a)
    195         : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
    196     virtual __base<_Rp(_A0)>* __clone() const;
    197     virtual void __clone(__base<_Rp(_A0)>*) const;
    198     virtual void destroy();
    199     virtual void destroy_deallocate();
    200     virtual _Rp operator()(_A0);
    201 #ifndef _LIBCPP_NO_RTTI
    202     virtual const void* target(const type_info&) const;
    203     virtual const std::type_info& target_type() const;
    204 #endif  // _LIBCPP_NO_RTTI
    205 };
    206 
    207 template<class _Fp, class _Alloc, class _Rp, class _A0>
    208 __base<_Rp(_A0)>*
    209 __func<_Fp, _Alloc, _Rp(_A0)>::__clone() const
    210 {
    211     typedef allocator_traits<_Alloc> __alloc_traits;
    212     typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
    213     _Ap __a(__f_.second());
    214     typedef __allocator_destructor<_Ap> _Dp;
    215     unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
    216     ::new (__hold.get()) __func(__f_.first(), _Alloc(__a));
    217     return __hold.release();
    218 }
    219 
    220 template<class _Fp, class _Alloc, class _Rp, class _A0>
    221 void
    222 __func<_Fp, _Alloc, _Rp(_A0)>::__clone(__base<_Rp(_A0)>* __p) const
    223 {
    224     ::new (__p) __func(__f_.first(), __f_.second());
    225 }
    226 
    227 template<class _Fp, class _Alloc, class _Rp, class _A0>
    228 void
    229 __func<_Fp, _Alloc, _Rp(_A0)>::destroy()
    230 {
    231     __f_.~__compressed_pair<_Fp, _Alloc>();
    232 }
    233 
    234 template<class _Fp, class _Alloc, class _Rp, class _A0>
    235 void
    236 __func<_Fp, _Alloc, _Rp(_A0)>::destroy_deallocate()
    237 {
    238     typedef allocator_traits<_Alloc> __alloc_traits;
    239     typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
    240     _Ap __a(__f_.second());
    241     __f_.~__compressed_pair<_Fp, _Alloc>();
    242     __a.deallocate(this, 1);
    243 }
    244 
    245 template<class _Fp, class _Alloc, class _Rp, class _A0>
    246 _Rp
    247 __func<_Fp, _Alloc, _Rp(_A0)>::operator()(_A0 __a0)
    248 {
    249     typedef __invoke_void_return_wrapper<_Rp> _Invoker;
    250     return _Invoker::__call(__f_.first(), __a0);
    251 }
    252 
    253 #ifndef _LIBCPP_NO_RTTI
    254 
    255 template<class _Fp, class _Alloc, class _Rp, class _A0>
    256 const void*
    257 __func<_Fp, _Alloc, _Rp(_A0)>::target(const type_info& __ti) const
    258 {
    259     if (__ti == typeid(_Fp))
    260         return &__f_.first();
    261     return (const void*)0;
    262 }
    263 
    264 template<class _Fp, class _Alloc, class _Rp, class _A0>
    265 const std::type_info&
    266 __func<_Fp, _Alloc, _Rp(_A0)>::target_type() const
    267 {
    268     return typeid(_Fp);
    269 }
    270 
    271 #endif  // _LIBCPP_NO_RTTI
    272 
    273 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
    274 class __func<_Fp, _Alloc, _Rp(_A0, _A1)>
    275     : public  __base<_Rp(_A0, _A1)>
    276 {
    277     __compressed_pair<_Fp, _Alloc> __f_;
    278 public:
    279     _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f) : __f_(_VSTD::move(__f)) {}
    280     _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f, _Alloc __a)
    281         : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
    282     virtual __base<_Rp(_A0, _A1)>* __clone() const;
    283     virtual void __clone(__base<_Rp(_A0, _A1)>*) const;
    284     virtual void destroy();
    285     virtual void destroy_deallocate();
    286     virtual _Rp operator()(_A0, _A1);
    287 #ifndef _LIBCPP_NO_RTTI
    288     virtual const void* target(const type_info&) const;
    289     virtual const std::type_info& target_type() const;
    290 #endif  // _LIBCPP_NO_RTTI
    291 };
    292 
    293 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
    294 __base<_Rp(_A0, _A1)>*
    295 __func<_Fp, _Alloc, _Rp(_A0, _A1)>::__clone() const
    296 {
    297     typedef allocator_traits<_Alloc> __alloc_traits;
    298     typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
    299     _Ap __a(__f_.second());
    300     typedef __allocator_destructor<_Ap> _Dp;
    301     unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
    302     ::new (__hold.get()) __func(__f_.first(), _Alloc(__a));
    303     return __hold.release();
    304 }
    305 
    306 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
    307 void
    308 __func<_Fp, _Alloc, _Rp(_A0, _A1)>::__clone(__base<_Rp(_A0, _A1)>* __p) const
    309 {
    310     ::new (__p) __func(__f_.first(), __f_.second());
    311 }
    312 
    313 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
    314 void
    315 __func<_Fp, _Alloc, _Rp(_A0, _A1)>::destroy()
    316 {
    317     __f_.~__compressed_pair<_Fp, _Alloc>();
    318 }
    319 
    320 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
    321 void
    322 __func<_Fp, _Alloc, _Rp(_A0, _A1)>::destroy_deallocate()
    323 {
    324     typedef allocator_traits<_Alloc> __alloc_traits;
    325     typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
    326     _Ap __a(__f_.second());
    327     __f_.~__compressed_pair<_Fp, _Alloc>();
    328     __a.deallocate(this, 1);
    329 }
    330 
    331 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
    332 _Rp
    333 __func<_Fp, _Alloc, _Rp(_A0, _A1)>::operator()(_A0 __a0, _A1 __a1)
    334 {
    335     typedef __invoke_void_return_wrapper<_Rp> _Invoker;
    336     return _Invoker::__call(__f_.first(), __a0, __a1);
    337 }
    338 
    339 #ifndef _LIBCPP_NO_RTTI
    340 
    341 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
    342 const void*
    343 __func<_Fp, _Alloc, _Rp(_A0, _A1)>::target(const type_info& __ti) const
    344 {
    345     if (__ti == typeid(_Fp))
    346         return &__f_.first();
    347     return (const void*)0;
    348 }
    349 
    350 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
    351 const std::type_info&
    352 __func<_Fp, _Alloc, _Rp(_A0, _A1)>::target_type() const
    353 {
    354     return typeid(_Fp);
    355 }
    356 
    357 #endif  // _LIBCPP_NO_RTTI
    358 
    359 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
    360 class __func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>
    361     : public  __base<_Rp(_A0, _A1, _A2)>
    362 {
    363     __compressed_pair<_Fp, _Alloc> __f_;
    364 public:
    365     _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f) : __f_(_VSTD::move(__f)) {}
    366     _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f, _Alloc __a)
    367         : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
    368     virtual __base<_Rp(_A0, _A1, _A2)>* __clone() const;
    369     virtual void __clone(__base<_Rp(_A0, _A1, _A2)>*) const;
    370     virtual void destroy();
    371     virtual void destroy_deallocate();
    372     virtual _Rp operator()(_A0, _A1, _A2);
    373 #ifndef _LIBCPP_NO_RTTI
    374     virtual const void* target(const type_info&) const;
    375     virtual const std::type_info& target_type() const;
    376 #endif  // _LIBCPP_NO_RTTI
    377 };
    378 
    379 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
    380 __base<_Rp(_A0, _A1, _A2)>*
    381 __func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::__clone() const
    382 {
    383     typedef allocator_traits<_Alloc> __alloc_traits;
    384     typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
    385     _Ap __a(__f_.second());
    386     typedef __allocator_destructor<_Ap> _Dp;
    387     unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
    388     ::new (__hold.get()) __func(__f_.first(), _Alloc(__a));
    389     return __hold.release();
    390 }
    391 
    392 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
    393 void
    394 __func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::__clone(__base<_Rp(_A0, _A1, _A2)>* __p) const
    395 {
    396     ::new (__p) __func(__f_.first(), __f_.second());
    397 }
    398 
    399 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
    400 void
    401 __func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::destroy()
    402 {
    403     __f_.~__compressed_pair<_Fp, _Alloc>();
    404 }
    405 
    406 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
    407 void
    408 __func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::destroy_deallocate()
    409 {
    410     typedef allocator_traits<_Alloc> __alloc_traits;
    411     typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
    412     _Ap __a(__f_.second());
    413     __f_.~__compressed_pair<_Fp, _Alloc>();
    414     __a.deallocate(this, 1);
    415 }
    416 
    417 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
    418 _Rp
    419 __func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::operator()(_A0 __a0, _A1 __a1, _A2 __a2)
    420 {
    421     typedef __invoke_void_return_wrapper<_Rp> _Invoker;
    422     return _Invoker::__call(__f_.first(), __a0, __a1, __a2);
    423 }
    424 
    425 #ifndef _LIBCPP_NO_RTTI
    426 
    427 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
    428 const void*
    429 __func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::target(const type_info& __ti) const
    430 {
    431     if (__ti == typeid(_Fp))
    432         return &__f_.first();
    433     return (const void*)0;
    434 }
    435 
    436 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
    437 const std::type_info&
    438 __func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::target_type() const
    439 {
    440     return typeid(_Fp);
    441 }
    442 
    443 #endif  // _LIBCPP_NO_RTTI
    444 
    445 }  // __function
    446 
    447 template<class _Rp>
    448 class _LIBCPP_TYPE_VIS_ONLY function<_Rp()>
    449 {
    450     typedef __function::__base<_Rp()> __base;
    451     aligned_storage<3*sizeof(void*)>::type __buf_;
    452     __base* __f_;
    453 
    454     template <class _Fp>
    455         _LIBCPP_INLINE_VISIBILITY
    456         static bool __not_null(const _Fp&) {return true;}
    457     template <class _R2>
    458         _LIBCPP_INLINE_VISIBILITY
    459         static bool __not_null(_R2 (*__p)()) {return __p;}
    460     template <class _R2>
    461         _LIBCPP_INLINE_VISIBILITY
    462         static bool __not_null(const function<_R2()>& __p) {return __p;}
    463 public:
    464     typedef _Rp result_type;
    465 
    466     // 20.7.16.2.1, construct/copy/destroy:
    467     _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {}
    468     _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {}
    469     function(const function&);
    470     template<class _Fp>
    471       function(_Fp,
    472                typename enable_if<!is_integral<_Fp>::value>::type* = 0);
    473 
    474     template<class _Alloc>
    475       _LIBCPP_INLINE_VISIBILITY
    476       function(allocator_arg_t, const _Alloc&) : __f_(0) {}
    477     template<class _Alloc>
    478       _LIBCPP_INLINE_VISIBILITY
    479       function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {}
    480     template<class _Alloc>
    481       function(allocator_arg_t, const _Alloc&, const function&);
    482     template<class _Fp, class _Alloc>
    483       function(allocator_arg_t, const _Alloc& __a, _Fp __f,
    484                typename enable_if<!is_integral<_Fp>::value>::type* = 0);
    485 
    486     function& operator=(const function&);
    487     function& operator=(nullptr_t);
    488     template<class _Fp>
    489       typename enable_if
    490       <
    491         !is_integral<_Fp>::value,
    492         function&
    493       >::type
    494       operator=(_Fp);
    495 
    496     ~function();
    497 
    498     // 20.7.16.2.2, function modifiers:
    499     void swap(function&);
    500     template<class _Fp, class _Alloc>
    501       _LIBCPP_INLINE_VISIBILITY
    502       void assign(_Fp __f, const _Alloc& __a)
    503         {function(allocator_arg, __a, __f).swap(*this);}
    504 
    505     // 20.7.16.2.3, function capacity:
    506     _LIBCPP_INLINE_VISIBILITY operator bool() const {return __f_;}
    507 
    508 private:
    509     // deleted overloads close possible hole in the type system
    510     template<class _R2>
    511       bool operator==(const function<_R2()>&) const;// = delete;
    512     template<class _R2>
    513       bool operator!=(const function<_R2()>&) const;// = delete;
    514 public:
    515     // 20.7.16.2.4, function invocation:
    516     _Rp operator()() const;
    517 
    518 #ifndef _LIBCPP_NO_RTTI
    519     // 20.7.16.2.5, function target access:
    520     const std::type_info& target_type() const;
    521     template <typename _Tp> _Tp* target();
    522     template <typename _Tp> const _Tp* target() const;
    523 #endif  // _LIBCPP_NO_RTTI
    524 };
    525 
    526 template<class _Rp>
    527 function<_Rp()>::function(const function& __f)
    528 {
    529     if (__f.__f_ == 0)
    530         __f_ = 0;
    531     else if (__f.__f_ == (const __base*)&__f.__buf_)
    532     {
    533         __f_ = (__base*)&__buf_;
    534         __f.__f_->__clone(__f_);
    535     }
    536     else
    537         __f_ = __f.__f_->__clone();
    538 }
    539 
    540 template<class _Rp>
    541 template<class _Alloc>
    542 function<_Rp()>::function(allocator_arg_t, const _Alloc&, const function& __f)
    543 {
    544     if (__f.__f_ == 0)
    545         __f_ = 0;
    546     else if (__f.__f_ == (const __base*)&__f.__buf_)
    547     {
    548         __f_ = (__base*)&__buf_;
    549         __f.__f_->__clone(__f_);
    550     }
    551     else
    552         __f_ = __f.__f_->__clone();
    553 }
    554 
    555 template<class _Rp>
    556 template <class _Fp>
    557 function<_Rp()>::function(_Fp __f,
    558                                      typename enable_if<!is_integral<_Fp>::value>::type*)
    559     : __f_(0)
    560 {
    561     if (__not_null(__f))
    562     {
    563         typedef __function::__func<_Fp, allocator<_Fp>, _Rp()> _FF;
    564         if (sizeof(_FF) <= sizeof(__buf_))
    565         {
    566             __f_ = (__base*)&__buf_;
    567             ::new (__f_) _FF(__f);
    568         }
    569         else
    570         {
    571             typedef allocator<_FF> _Ap;
    572             _Ap __a;
    573             typedef __allocator_destructor<_Ap> _Dp;
    574             unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
    575             ::new (__hold.get()) _FF(__f, allocator<_Fp>(__a));
    576             __f_ = __hold.release();
    577         }
    578     }
    579 }
    580 
    581 template<class _Rp>
    582 template <class _Fp, class _Alloc>
    583 function<_Rp()>::function(allocator_arg_t, const _Alloc& __a0, _Fp __f,
    584                                      typename enable_if<!is_integral<_Fp>::value>::type*)
    585     : __f_(0)
    586 {
    587     typedef allocator_traits<_Alloc> __alloc_traits;
    588     if (__not_null(__f))
    589     {
    590         typedef __function::__func<_Fp, _Alloc, _Rp()> _FF;
    591         if (sizeof(_FF) <= sizeof(__buf_))
    592         {
    593             __f_ = (__base*)&__buf_;
    594             ::new (__f_) _FF(__f, __a0);
    595         }
    596         else
    597         {
    598             typedef typename __rebind_alloc_helper<__alloc_traits, _FF>::type _Ap;
    599             _Ap __a(__a0);
    600             typedef __allocator_destructor<_Ap> _Dp;
    601             unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
    602             ::new (__hold.get()) _FF(__f, _Alloc(__a));
    603             __f_ = __hold.release();
    604         }
    605     }
    606 }
    607 
    608 template<class _Rp>
    609 function<_Rp()>&
    610 function<_Rp()>::operator=(const function& __f)
    611 {
    612     function(__f).swap(*this);
    613     return *this;
    614 }
    615 
    616 template<class _Rp>
    617 function<_Rp()>&
    618 function<_Rp()>::operator=(nullptr_t)
    619 {
    620     if (__f_ == (__base*)&__buf_)
    621         __f_->destroy();
    622     else if (__f_)
    623         __f_->destroy_deallocate();
    624     __f_ = 0;
    625     return *this;
    626 }
    627 
    628 template<class _Rp>
    629 template <class _Fp>
    630 typename enable_if
    631 <
    632     !is_integral<_Fp>::value,
    633     function<_Rp()>&
    634 >::type
    635 function<_Rp()>::operator=(_Fp __f)
    636 {
    637     function(_VSTD::move(__f)).swap(*this);
    638     return *this;
    639 }
    640 
    641 template<class _Rp>
    642 function<_Rp()>::~function()
    643 {
    644     if (__f_ == (__base*)&__buf_)
    645         __f_->destroy();
    646     else if (__f_)
    647         __f_->destroy_deallocate();
    648 }
    649 
    650 template<class _Rp>
    651 void
    652 function<_Rp()>::swap(function& __f)
    653 {
    654     if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
    655     {
    656         typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
    657         __base* __t = (__base*)&__tempbuf;
    658         __f_->__clone(__t);
    659         __f_->destroy();
    660         __f_ = 0;
    661         __f.__f_->__clone((__base*)&__buf_);
    662         __f.__f_->destroy();
    663         __f.__f_ = 0;
    664         __f_ = (__base*)&__buf_;
    665         __t->__clone((__base*)&__f.__buf_);
    666         __t->destroy();
    667         __f.__f_ = (__base*)&__f.__buf_;
    668     }
    669     else if (__f_ == (__base*)&__buf_)
    670     {
    671         __f_->__clone((__base*)&__f.__buf_);
    672         __f_->destroy();
    673         __f_ = __f.__f_;
    674         __f.__f_ = (__base*)&__f.__buf_;
    675     }
    676     else if (__f.__f_ == (__base*)&__f.__buf_)
    677     {
    678         __f.__f_->__clone((__base*)&__buf_);
    679         __f.__f_->destroy();
    680         __f.__f_ = __f_;
    681         __f_ = (__base*)&__buf_;
    682     }
    683     else
    684         _VSTD::swap(__f_, __f.__f_);
    685 }
    686 
    687 template<class _Rp>
    688 _Rp
    689 function<_Rp()>::operator()() const
    690 {
    691 #ifndef _LIBCPP_NO_EXCEPTIONS
    692     if (__f_ == 0)
    693         throw bad_function_call();
    694 #endif  // _LIBCPP_NO_EXCEPTIONS
    695     return (*__f_)();
    696 }
    697 
    698 #ifndef _LIBCPP_NO_RTTI
    699 
    700 template<class _Rp>
    701 const std::type_info&
    702 function<_Rp()>::target_type() const
    703 {
    704     if (__f_ == 0)
    705         return typeid(void);
    706     return __f_->target_type();
    707 }
    708 
    709 template<class _Rp>
    710 template <typename _Tp>
    711 _Tp*
    712 function<_Rp()>::target()
    713 {
    714     if (__f_ == 0)
    715         return (_Tp*)0;
    716     return (_Tp*)__f_->target(typeid(_Tp));
    717 }
    718 
    719 template<class _Rp>
    720 template <typename _Tp>
    721 const _Tp*
    722 function<_Rp()>::target() const
    723 {
    724     if (__f_ == 0)
    725         return (const _Tp*)0;
    726     return (const _Tp*)__f_->target(typeid(_Tp));
    727 }
    728 
    729 #endif  // _LIBCPP_NO_RTTI
    730 
    731 template<class _Rp, class _A0>
    732 class _LIBCPP_TYPE_VIS_ONLY function<_Rp(_A0)>
    733     : public unary_function<_A0, _Rp>
    734 {
    735     typedef __function::__base<_Rp(_A0)> __base;
    736     aligned_storage<3*sizeof(void*)>::type __buf_;
    737     __base* __f_;
    738 
    739     template <class _Fp>
    740         _LIBCPP_INLINE_VISIBILITY
    741         static bool __not_null(const _Fp&) {return true;}
    742     template <class _R2, class _B0>
    743         _LIBCPP_INLINE_VISIBILITY
    744         static bool __not_null(_R2 (*__p)(_B0)) {return __p;}
    745     template <class _R2, class _Cp>
    746         _LIBCPP_INLINE_VISIBILITY
    747         static bool __not_null(_R2 (_Cp::*__p)()) {return __p;}
    748     template <class _R2, class _Cp>
    749         _LIBCPP_INLINE_VISIBILITY
    750         static bool __not_null(_R2 (_Cp::*__p)() const) {return __p;}
    751     template <class _R2, class _Cp>
    752         _LIBCPP_INLINE_VISIBILITY
    753         static bool __not_null(_R2 (_Cp::*__p)() volatile) {return __p;}
    754     template <class _R2, class _Cp>
    755         _LIBCPP_INLINE_VISIBILITY
    756         static bool __not_null(_R2 (_Cp::*__p)() const volatile) {return __p;}
    757     template <class _R2, class _B0>
    758         _LIBCPP_INLINE_VISIBILITY
    759         static bool __not_null(const function<_R2(_B0)>& __p) {return __p;}
    760 public:
    761     typedef _Rp result_type;
    762 
    763     // 20.7.16.2.1, construct/copy/destroy:
    764     _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {}
    765     _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {}
    766     function(const function&);
    767     template<class _Fp>
    768       function(_Fp,
    769                typename enable_if<!is_integral<_Fp>::value>::type* = 0);
    770 
    771     template<class _Alloc>
    772       _LIBCPP_INLINE_VISIBILITY
    773       function(allocator_arg_t, const _Alloc&) : __f_(0) {}
    774     template<class _Alloc>
    775       _LIBCPP_INLINE_VISIBILITY
    776       function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {}
    777     template<class _Alloc>
    778       function(allocator_arg_t, const _Alloc&, const function&);
    779     template<class _Fp, class _Alloc>
    780       function(allocator_arg_t, const _Alloc& __a, _Fp __f,
    781                typename enable_if<!is_integral<_Fp>::value>::type* = 0);
    782 
    783     function& operator=(const function&);
    784     function& operator=(nullptr_t);
    785     template<class _Fp>
    786       typename enable_if
    787       <
    788         !is_integral<_Fp>::value,
    789         function&
    790       >::type
    791       operator=(_Fp);
    792 
    793     ~function();
    794 
    795     // 20.7.16.2.2, function modifiers:
    796     void swap(function&);
    797     template<class _Fp, class _Alloc>
    798       _LIBCPP_INLINE_VISIBILITY
    799       void assign(_Fp __f, const _Alloc& __a)
    800         {function(allocator_arg, __a, __f).swap(*this);}
    801 
    802     // 20.7.16.2.3, function capacity:
    803     _LIBCPP_INLINE_VISIBILITY operator bool() const {return __f_;}
    804 
    805 private:
    806     // deleted overloads close possible hole in the type system
    807     template<class _R2, class _B0>
    808       bool operator==(const function<_R2(_B0)>&) const;// = delete;
    809     template<class _R2, class _B0>
    810       bool operator!=(const function<_R2(_B0)>&) const;// = delete;
    811 public:
    812     // 20.7.16.2.4, function invocation:
    813     _Rp operator()(_A0) const;
    814 
    815 #ifndef _LIBCPP_NO_RTTI
    816     // 20.7.16.2.5, function target access:
    817     const std::type_info& target_type() const;
    818     template <typename _Tp> _Tp* target();
    819     template <typename _Tp> const _Tp* target() const;
    820 #endif  // _LIBCPP_NO_RTTI
    821 };
    822 
    823 template<class _Rp, class _A0>
    824 function<_Rp(_A0)>::function(const function& __f)
    825 {
    826     if (__f.__f_ == 0)
    827         __f_ = 0;
    828     else if (__f.__f_ == (const __base*)&__f.__buf_)
    829     {
    830         __f_ = (__base*)&__buf_;
    831         __f.__f_->__clone(__f_);
    832     }
    833     else
    834         __f_ = __f.__f_->__clone();
    835 }
    836 
    837 template<class _Rp, class _A0>
    838 template<class _Alloc>
    839 function<_Rp(_A0)>::function(allocator_arg_t, const _Alloc&, const function& __f)
    840 {
    841     if (__f.__f_ == 0)
    842         __f_ = 0;
    843     else if (__f.__f_ == (const __base*)&__f.__buf_)
    844     {
    845         __f_ = (__base*)&__buf_;
    846         __f.__f_->__clone(__f_);
    847     }
    848     else
    849         __f_ = __f.__f_->__clone();
    850 }
    851 
    852 template<class _Rp, class _A0>
    853 template <class _Fp>
    854 function<_Rp(_A0)>::function(_Fp __f,
    855                                      typename enable_if<!is_integral<_Fp>::value>::type*)
    856     : __f_(0)
    857 {
    858     if (__not_null(__f))
    859     {
    860         typedef __function::__func<_Fp, allocator<_Fp>, _Rp(_A0)> _FF;
    861         if (sizeof(_FF) <= sizeof(__buf_))
    862         {
    863             __f_ = (__base*)&__buf_;
    864             ::new (__f_) _FF(__f);
    865         }
    866         else
    867         {
    868             typedef allocator<_FF> _Ap;
    869             _Ap __a;
    870             typedef __allocator_destructor<_Ap> _Dp;
    871             unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
    872             ::new (__hold.get()) _FF(__f, allocator<_Fp>(__a));
    873             __f_ = __hold.release();
    874         }
    875     }
    876 }
    877 
    878 template<class _Rp, class _A0>
    879 template <class _Fp, class _Alloc>
    880 function<_Rp(_A0)>::function(allocator_arg_t, const _Alloc& __a0, _Fp __f,
    881                                      typename enable_if<!is_integral<_Fp>::value>::type*)
    882     : __f_(0)
    883 {
    884     typedef allocator_traits<_Alloc> __alloc_traits;
    885     if (__not_null(__f))
    886     {
    887         typedef __function::__func<_Fp, _Alloc, _Rp(_A0)> _FF;
    888         if (sizeof(_FF) <= sizeof(__buf_))
    889         {
    890             __f_ = (__base*)&__buf_;
    891             ::new (__f_) _FF(__f, __a0);
    892         }
    893         else
    894         {
    895             typedef typename __rebind_alloc_helper<__alloc_traits, _FF>::type _Ap;
    896             _Ap __a(__a0);
    897             typedef __allocator_destructor<_Ap> _Dp;
    898             unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
    899             ::new (__hold.get()) _FF(__f, _Alloc(__a));
    900             __f_ = __hold.release();
    901         }
    902     }
    903 }
    904 
    905 template<class _Rp, class _A0>
    906 function<_Rp(_A0)>&
    907 function<_Rp(_A0)>::operator=(const function& __f)
    908 {
    909     function(__f).swap(*this);
    910     return *this;
    911 }
    912 
    913 template<class _Rp, class _A0>
    914 function<_Rp(_A0)>&
    915 function<_Rp(_A0)>::operator=(nullptr_t)
    916 {
    917     if (__f_ == (__base*)&__buf_)
    918         __f_->destroy();
    919     else if (__f_)
    920         __f_->destroy_deallocate();
    921     __f_ = 0;
    922     return *this;
    923 }
    924 
    925 template<class _Rp, class _A0>
    926 template <class _Fp>
    927 typename enable_if
    928 <
    929     !is_integral<_Fp>::value,
    930     function<_Rp(_A0)>&
    931 >::type
    932 function<_Rp(_A0)>::operator=(_Fp __f)
    933 {
    934     function(_VSTD::move(__f)).swap(*this);
    935     return *this;
    936 }
    937 
    938 template<class _Rp, class _A0>
    939 function<_Rp(_A0)>::~function()
    940 {
    941     if (__f_ == (__base*)&__buf_)
    942         __f_->destroy();
    943     else if (__f_)
    944         __f_->destroy_deallocate();
    945 }
    946 
    947 template<class _Rp, class _A0>
    948 void
    949 function<_Rp(_A0)>::swap(function& __f)
    950 {
    951     if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
    952     {
    953         typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
    954         __base* __t = (__base*)&__tempbuf;
    955         __f_->__clone(__t);
    956         __f_->destroy();
    957         __f_ = 0;
    958         __f.__f_->__clone((__base*)&__buf_);
    959         __f.__f_->destroy();
    960         __f.__f_ = 0;
    961         __f_ = (__base*)&__buf_;
    962         __t->__clone((__base*)&__f.__buf_);
    963         __t->destroy();
    964         __f.__f_ = (__base*)&__f.__buf_;
    965     }
    966     else if (__f_ == (__base*)&__buf_)
    967     {
    968         __f_->__clone((__base*)&__f.__buf_);
    969         __f_->destroy();
    970         __f_ = __f.__f_;
    971         __f.__f_ = (__base*)&__f.__buf_;
    972     }
    973     else if (__f.__f_ == (__base*)&__f.__buf_)
    974     {
    975         __f.__f_->__clone((__base*)&__buf_);
    976         __f.__f_->destroy();
    977         __f.__f_ = __f_;
    978         __f_ = (__base*)&__buf_;
    979     }
    980     else
    981         _VSTD::swap(__f_, __f.__f_);
    982 }
    983 
    984 template<class _Rp, class _A0>
    985 _Rp
    986 function<_Rp(_A0)>::operator()(_A0 __a0) const
    987 {
    988 #ifndef _LIBCPP_NO_EXCEPTIONS
    989     if (__f_ == 0)
    990         throw bad_function_call();
    991 #endif  // _LIBCPP_NO_EXCEPTIONS
    992     return (*__f_)(__a0);
    993 }
    994 
    995 #ifndef _LIBCPP_NO_RTTI
    996 
    997 template<class _Rp, class _A0>
    998 const std::type_info&
    999 function<_Rp(_A0)>::target_type() const
   1000 {
   1001     if (__f_ == 0)
   1002         return typeid(void);
   1003     return __f_->target_type();
   1004 }
   1005 
   1006 template<class _Rp, class _A0>
   1007 template <typename _Tp>
   1008 _Tp*
   1009 function<_Rp(_A0)>::target()
   1010 {
   1011     if (__f_ == 0)
   1012         return (_Tp*)0;
   1013     return (_Tp*)__f_->target(typeid(_Tp));
   1014 }
   1015 
   1016 template<class _Rp, class _A0>
   1017 template <typename _Tp>
   1018 const _Tp*
   1019 function<_Rp(_A0)>::target() const
   1020 {
   1021     if (__f_ == 0)
   1022         return (const _Tp*)0;
   1023     return (const _Tp*)__f_->target(typeid(_Tp));
   1024 }
   1025 
   1026 #endif  // _LIBCPP_NO_RTTI
   1027 
   1028 template<class _Rp, class _A0, class _A1>
   1029 class _LIBCPP_TYPE_VIS_ONLY function<_Rp(_A0, _A1)>
   1030     : public binary_function<_A0, _A1, _Rp>
   1031 {
   1032     typedef __function::__base<_Rp(_A0, _A1)> __base;
   1033     aligned_storage<3*sizeof(void*)>::type __buf_;
   1034     __base* __f_;
   1035 
   1036     template <class _Fp>
   1037         _LIBCPP_INLINE_VISIBILITY
   1038         static bool __not_null(const _Fp&) {return true;}
   1039     template <class _R2, class _B0, class _B1>
   1040         _LIBCPP_INLINE_VISIBILITY
   1041         static bool __not_null(_R2 (*__p)(_B0, _B1)) {return __p;}
   1042     template <class _R2, class _Cp, class _B1>
   1043         _LIBCPP_INLINE_VISIBILITY
   1044         static bool __not_null(_R2 (_Cp::*__p)(_B1)) {return __p;}
   1045     template <class _R2, class _Cp, class _B1>
   1046         _LIBCPP_INLINE_VISIBILITY
   1047         static bool __not_null(_R2 (_Cp::*__p)(_B1) const) {return __p;}
   1048     template <class _R2, class _Cp, class _B1>
   1049         _LIBCPP_INLINE_VISIBILITY
   1050         static bool __not_null(_R2 (_Cp::*__p)(_B1) volatile) {return __p;}
   1051     template <class _R2, class _Cp, class _B1>
   1052         _LIBCPP_INLINE_VISIBILITY
   1053         static bool __not_null(_R2 (_Cp::*__p)(_B1) const volatile) {return __p;}
   1054     template <class _R2, class _B0, class _B1>
   1055         _LIBCPP_INLINE_VISIBILITY
   1056         static bool __not_null(const function<_R2(_B0, _B1)>& __p) {return __p;}
   1057 public:
   1058     typedef _Rp result_type;
   1059 
   1060     // 20.7.16.2.1, construct/copy/destroy:
   1061     _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {}
   1062     _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {}
   1063     function(const function&);
   1064     template<class _Fp>
   1065       function(_Fp,
   1066                typename enable_if<!is_integral<_Fp>::value>::type* = 0);
   1067 
   1068     template<class _Alloc>
   1069       _LIBCPP_INLINE_VISIBILITY
   1070       function(allocator_arg_t, const _Alloc&) : __f_(0) {}
   1071     template<class _Alloc>
   1072       _LIBCPP_INLINE_VISIBILITY
   1073       function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {}
   1074     template<class _Alloc>
   1075       function(allocator_arg_t, const _Alloc&, const function&);
   1076     template<class _Fp, class _Alloc>
   1077       function(allocator_arg_t, const _Alloc& __a, _Fp __f,
   1078                typename enable_if<!is_integral<_Fp>::value>::type* = 0);
   1079 
   1080     function& operator=(const function&);
   1081     function& operator=(nullptr_t);
   1082     template<class _Fp>
   1083       typename enable_if
   1084       <
   1085         !is_integral<_Fp>::value,
   1086         function&
   1087       >::type
   1088       operator=(_Fp);
   1089 
   1090     ~function();
   1091 
   1092     // 20.7.16.2.2, function modifiers:
   1093     void swap(function&);
   1094     template<class _Fp, class _Alloc>
   1095       _LIBCPP_INLINE_VISIBILITY
   1096       void assign(_Fp __f, const _Alloc& __a)
   1097         {function(allocator_arg, __a, __f).swap(*this);}
   1098 
   1099     // 20.7.16.2.3, function capacity:
   1100     operator bool() const {return __f_;}
   1101 
   1102 private:
   1103     // deleted overloads close possible hole in the type system
   1104     template<class _R2, class _B0, class _B1>
   1105       bool operator==(const function<_R2(_B0, _B1)>&) const;// = delete;
   1106     template<class _R2, class _B0, class _B1>
   1107       bool operator!=(const function<_R2(_B0, _B1)>&) const;// = delete;
   1108 public:
   1109     // 20.7.16.2.4, function invocation:
   1110     _Rp operator()(_A0, _A1) const;
   1111 
   1112 #ifndef _LIBCPP_NO_RTTI
   1113     // 20.7.16.2.5, function target access:
   1114     const std::type_info& target_type() const;
   1115     template <typename _Tp> _Tp* target();
   1116     template <typename _Tp> const _Tp* target() const;
   1117 #endif  // _LIBCPP_NO_RTTI
   1118 };
   1119 
   1120 template<class _Rp, class _A0, class _A1>
   1121 function<_Rp(_A0, _A1)>::function(const function& __f)
   1122 {
   1123     if (__f.__f_ == 0)
   1124         __f_ = 0;
   1125     else if (__f.__f_ == (const __base*)&__f.__buf_)
   1126     {
   1127         __f_ = (__base*)&__buf_;
   1128         __f.__f_->__clone(__f_);
   1129     }
   1130     else
   1131         __f_ = __f.__f_->__clone();
   1132 }
   1133 
   1134 template<class _Rp, class _A0, class _A1>
   1135 template<class _Alloc>
   1136 function<_Rp(_A0, _A1)>::function(allocator_arg_t, const _Alloc&, const function& __f)
   1137 {
   1138     if (__f.__f_ == 0)
   1139         __f_ = 0;
   1140     else if (__f.__f_ == (const __base*)&__f.__buf_)
   1141     {
   1142         __f_ = (__base*)&__buf_;
   1143         __f.__f_->__clone(__f_);
   1144     }
   1145     else
   1146         __f_ = __f.__f_->__clone();
   1147 }
   1148 
   1149 template<class _Rp, class _A0, class _A1>
   1150 template <class _Fp>
   1151 function<_Rp(_A0, _A1)>::function(_Fp __f,
   1152                                  typename enable_if<!is_integral<_Fp>::value>::type*)
   1153     : __f_(0)
   1154 {
   1155     if (__not_null(__f))
   1156     {
   1157         typedef __function::__func<_Fp, allocator<_Fp>, _Rp(_A0, _A1)> _FF;
   1158         if (sizeof(_FF) <= sizeof(__buf_))
   1159         {
   1160             __f_ = (__base*)&__buf_;
   1161             ::new (__f_) _FF(__f);
   1162         }
   1163         else
   1164         {
   1165             typedef allocator<_FF> _Ap;
   1166             _Ap __a;
   1167             typedef __allocator_destructor<_Ap> _Dp;
   1168             unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
   1169             ::new (__hold.get()) _FF(__f, allocator<_Fp>(__a));
   1170             __f_ = __hold.release();
   1171         }
   1172     }
   1173 }
   1174 
   1175 template<class _Rp, class _A0, class _A1>
   1176 template <class _Fp, class _Alloc>
   1177 function<_Rp(_A0, _A1)>::function(allocator_arg_t, const _Alloc& __a0, _Fp __f,
   1178                                  typename enable_if<!is_integral<_Fp>::value>::type*)
   1179     : __f_(0)
   1180 {
   1181     typedef allocator_traits<_Alloc> __alloc_traits;
   1182     if (__not_null(__f))
   1183     {
   1184         typedef __function::__func<_Fp, _Alloc, _Rp(_A0, _A1)> _FF;
   1185         if (sizeof(_FF) <= sizeof(__buf_))
   1186         {
   1187             __f_ = (__base*)&__buf_;
   1188             ::new (__f_) _FF(__f, __a0);
   1189         }
   1190         else
   1191         {
   1192             typedef typename __rebind_alloc_helper<__alloc_traits, _FF>::type _Ap;
   1193             _Ap __a(__a0);
   1194             typedef __allocator_destructor<_Ap> _Dp;
   1195             unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
   1196             ::new (__hold.get()) _FF(__f, _Alloc(__a));
   1197             __f_ = __hold.release();
   1198         }
   1199     }
   1200 }
   1201 
   1202 template<class _Rp, class _A0, class _A1>
   1203 function<_Rp(_A0, _A1)>&
   1204 function<_Rp(_A0, _A1)>::operator=(const function& __f)
   1205 {
   1206     function(__f).swap(*this);
   1207     return *this;
   1208 }
   1209 
   1210 template<class _Rp, class _A0, class _A1>
   1211 function<_Rp(_A0, _A1)>&
   1212 function<_Rp(_A0, _A1)>::operator=(nullptr_t)
   1213 {
   1214     if (__f_ == (__base*)&__buf_)
   1215         __f_->destroy();
   1216     else if (__f_)
   1217         __f_->destroy_deallocate();
   1218     __f_ = 0;
   1219     return *this;
   1220 }
   1221 
   1222 template<class _Rp, class _A0, class _A1>
   1223 template <class _Fp>
   1224 typename enable_if
   1225 <
   1226     !is_integral<_Fp>::value,
   1227     function<_Rp(_A0, _A1)>&
   1228 >::type
   1229 function<_Rp(_A0, _A1)>::operator=(_Fp __f)
   1230 {
   1231     function(_VSTD::move(__f)).swap(*this);
   1232     return *this;
   1233 }
   1234 
   1235 template<class _Rp, class _A0, class _A1>
   1236 function<_Rp(_A0, _A1)>::~function()
   1237 {
   1238     if (__f_ == (__base*)&__buf_)
   1239         __f_->destroy();
   1240     else if (__f_)
   1241         __f_->destroy_deallocate();
   1242 }
   1243 
   1244 template<class _Rp, class _A0, class _A1>
   1245 void
   1246 function<_Rp(_A0, _A1)>::swap(function& __f)
   1247 {
   1248     if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
   1249     {
   1250         typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
   1251         __base* __t = (__base*)&__tempbuf;
   1252         __f_->__clone(__t);
   1253         __f_->destroy();
   1254         __f_ = 0;
   1255         __f.__f_->__clone((__base*)&__buf_);
   1256         __f.__f_->destroy();
   1257         __f.__f_ = 0;
   1258         __f_ = (__base*)&__buf_;
   1259         __t->__clone((__base*)&__f.__buf_);
   1260         __t->destroy();
   1261         __f.__f_ = (__base*)&__f.__buf_;
   1262     }
   1263     else if (__f_ == (__base*)&__buf_)
   1264     {
   1265         __f_->__clone((__base*)&__f.__buf_);
   1266         __f_->destroy();
   1267         __f_ = __f.__f_;
   1268         __f.__f_ = (__base*)&__f.__buf_;
   1269     }
   1270     else if (__f.__f_ == (__base*)&__f.__buf_)
   1271     {
   1272         __f.__f_->__clone((__base*)&__buf_);
   1273         __f.__f_->destroy();
   1274         __f.__f_ = __f_;
   1275         __f_ = (__base*)&__buf_;
   1276     }
   1277     else
   1278         _VSTD::swap(__f_, __f.__f_);
   1279 }
   1280 
   1281 template<class _Rp, class _A0, class _A1>
   1282 _Rp
   1283 function<_Rp(_A0, _A1)>::operator()(_A0 __a0, _A1 __a1) const
   1284 {
   1285 #ifndef _LIBCPP_NO_EXCEPTIONS
   1286     if (__f_ == 0)
   1287         throw bad_function_call();
   1288 #endif  // _LIBCPP_NO_EXCEPTIONS
   1289     return (*__f_)(__a0, __a1);
   1290 }
   1291 
   1292 #ifndef _LIBCPP_NO_RTTI
   1293 
   1294 template<class _Rp, class _A0, class _A1>
   1295 const std::type_info&
   1296 function<_Rp(_A0, _A1)>::target_type() const
   1297 {
   1298     if (__f_ == 0)
   1299         return typeid(void);
   1300     return __f_->target_type();
   1301 }
   1302 
   1303 template<class _Rp, class _A0, class _A1>
   1304 template <typename _Tp>
   1305 _Tp*
   1306 function<_Rp(_A0, _A1)>::target()
   1307 {
   1308     if (__f_ == 0)
   1309         return (_Tp*)0;
   1310     return (_Tp*)__f_->target(typeid(_Tp));
   1311 }
   1312 
   1313 template<class _Rp, class _A0, class _A1>
   1314 template <typename _Tp>
   1315 const _Tp*
   1316 function<_Rp(_A0, _A1)>::target() const
   1317 {
   1318     if (__f_ == 0)
   1319         return (const _Tp*)0;
   1320     return (const _Tp*)__f_->target(typeid(_Tp));
   1321 }
   1322 
   1323 #endif  // _LIBCPP_NO_RTTI
   1324 
   1325 template<class _Rp, class _A0, class _A1, class _A2>
   1326 class _LIBCPP_TYPE_VIS_ONLY function<_Rp(_A0, _A1, _A2)>
   1327 {
   1328     typedef __function::__base<_Rp(_A0, _A1, _A2)> __base;
   1329     aligned_storage<3*sizeof(void*)>::type __buf_;
   1330     __base* __f_;
   1331 
   1332     template <class _Fp>
   1333         _LIBCPP_INLINE_VISIBILITY
   1334         static bool __not_null(const _Fp&) {return true;}
   1335     template <class _R2, class _B0, class _B1, class _B2>
   1336         _LIBCPP_INLINE_VISIBILITY
   1337         static bool __not_null(_R2 (*__p)(_B0, _B1, _B2)) {return __p;}
   1338     template <class _R2, class _Cp, class _B1, class _B2>
   1339         _LIBCPP_INLINE_VISIBILITY
   1340         static bool __not_null(_R2 (_Cp::*__p)(_B1, _B2)) {return __p;}
   1341     template <class _R2, class _Cp, class _B1, class _B2>
   1342         _LIBCPP_INLINE_VISIBILITY
   1343         static bool __not_null(_R2 (_Cp::*__p)(_B1, _B2) const) {return __p;}
   1344     template <class _R2, class _Cp, class _B1, class _B2>
   1345         _LIBCPP_INLINE_VISIBILITY
   1346         static bool __not_null(_R2 (_Cp::*__p)(_B1, _B2) volatile) {return __p;}
   1347     template <class _R2, class _Cp, class _B1, class _B2>
   1348         _LIBCPP_INLINE_VISIBILITY
   1349         static bool __not_null(_R2 (_Cp::*__p)(_B1, _B2) const volatile) {return __p;}
   1350     template <class _R2, class _B0, class _B1, class _B2>
   1351         _LIBCPP_INLINE_VISIBILITY
   1352         static bool __not_null(const function<_R2(_B0, _B1, _B2)>& __p) {return __p;}
   1353 public:
   1354     typedef _Rp result_type;
   1355 
   1356     // 20.7.16.2.1, construct/copy/destroy:
   1357     _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {}
   1358     _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {}
   1359     function(const function&);
   1360     template<class _Fp>
   1361       function(_Fp,
   1362                typename enable_if<!is_integral<_Fp>::value>::type* = 0);
   1363 
   1364     template<class _Alloc>
   1365       _LIBCPP_INLINE_VISIBILITY
   1366       function(allocator_arg_t, const _Alloc&) : __f_(0) {}
   1367     template<class _Alloc>
   1368       _LIBCPP_INLINE_VISIBILITY
   1369       function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {}
   1370     template<class _Alloc>
   1371       function(allocator_arg_t, const _Alloc&, const function&);
   1372     template<class _Fp, class _Alloc>
   1373       function(allocator_arg_t, const _Alloc& __a, _Fp __f,
   1374                typename enable_if<!is_integral<_Fp>::value>::type* = 0);
   1375 
   1376     function& operator=(const function&);
   1377     function& operator=(nullptr_t);
   1378     template<class _Fp>
   1379       typename enable_if
   1380       <
   1381         !is_integral<_Fp>::value,
   1382         function&
   1383       >::type
   1384       operator=(_Fp);
   1385 
   1386     ~function();
   1387 
   1388     // 20.7.16.2.2, function modifiers:
   1389     void swap(function&);
   1390     template<class _Fp, class _Alloc>
   1391       _LIBCPP_INLINE_VISIBILITY
   1392       void assign(_Fp __f, const _Alloc& __a)
   1393         {function(allocator_arg, __a, __f).swap(*this);}
   1394 
   1395     // 20.7.16.2.3, function capacity:
   1396     _LIBCPP_INLINE_VISIBILITY operator bool() const {return __f_;}
   1397 
   1398 private:
   1399     // deleted overloads close possible hole in the type system
   1400     template<class _R2, class _B0, class _B1, class _B2>
   1401       bool operator==(const function<_R2(_B0, _B1, _B2)>&) const;// = delete;
   1402     template<class _R2, class _B0, class _B1, class _B2>
   1403       bool operator!=(const function<_R2(_B0, _B1, _B2)>&) const;// = delete;
   1404 public:
   1405     // 20.7.16.2.4, function invocation:
   1406     _Rp operator()(_A0, _A1, _A2) const;
   1407 
   1408 #ifndef _LIBCPP_NO_RTTI
   1409     // 20.7.16.2.5, function target access:
   1410     const std::type_info& target_type() const;
   1411     template <typename _Tp> _Tp* target();
   1412     template <typename _Tp> const _Tp* target() const;
   1413 #endif  // _LIBCPP_NO_RTTI
   1414 };
   1415 
   1416 template<class _Rp, class _A0, class _A1, class _A2>
   1417 function<_Rp(_A0, _A1, _A2)>::function(const function& __f)
   1418 {
   1419     if (__f.__f_ == 0)
   1420         __f_ = 0;
   1421     else if (__f.__f_ == (const __base*)&__f.__buf_)
   1422     {
   1423         __f_ = (__base*)&__buf_;
   1424         __f.__f_->__clone(__f_);
   1425     }
   1426     else
   1427         __f_ = __f.__f_->__clone();
   1428 }
   1429 
   1430 template<class _Rp, class _A0, class _A1, class _A2>
   1431 template<class _Alloc>
   1432 function<_Rp(_A0, _A1, _A2)>::function(allocator_arg_t, const _Alloc&,
   1433                                       const function& __f)
   1434 {
   1435     if (__f.__f_ == 0)
   1436         __f_ = 0;
   1437     else if (__f.__f_ == (const __base*)&__f.__buf_)
   1438     {
   1439         __f_ = (__base*)&__buf_;
   1440         __f.__f_->__clone(__f_);
   1441     }
   1442     else
   1443         __f_ = __f.__f_->__clone();
   1444 }
   1445 
   1446 template<class _Rp, class _A0, class _A1, class _A2>
   1447 template <class _Fp>
   1448 function<_Rp(_A0, _A1, _A2)>::function(_Fp __f,
   1449                                      typename enable_if<!is_integral<_Fp>::value>::type*)
   1450     : __f_(0)
   1451 {
   1452     if (__not_null(__f))
   1453     {
   1454         typedef __function::__func<_Fp, allocator<_Fp>, _Rp(_A0, _A1, _A2)> _FF;
   1455         if (sizeof(_FF) <= sizeof(__buf_))
   1456         {
   1457             __f_ = (__base*)&__buf_;
   1458             ::new (__f_) _FF(__f);
   1459         }
   1460         else
   1461         {
   1462             typedef allocator<_FF> _Ap;
   1463             _Ap __a;
   1464             typedef __allocator_destructor<_Ap> _Dp;
   1465             unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
   1466             ::new (__hold.get()) _FF(__f, allocator<_Fp>(__a));
   1467             __f_ = __hold.release();
   1468         }
   1469     }
   1470 }
   1471 
   1472 template<class _Rp, class _A0, class _A1, class _A2>
   1473 template <class _Fp, class _Alloc>
   1474 function<_Rp(_A0, _A1, _A2)>::function(allocator_arg_t, const _Alloc& __a0, _Fp __f,
   1475                                      typename enable_if<!is_integral<_Fp>::value>::type*)
   1476     : __f_(0)
   1477 {
   1478     typedef allocator_traits<_Alloc> __alloc_traits;
   1479     if (__not_null(__f))
   1480     {
   1481         typedef __function::__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)> _FF;
   1482         if (sizeof(_FF) <= sizeof(__buf_))
   1483         {
   1484             __f_ = (__base*)&__buf_;
   1485             ::new (__f_) _FF(__f, __a0);
   1486         }
   1487         else
   1488         {
   1489             typedef typename __rebind_alloc_helper<__alloc_traits, _FF>::type _Ap;
   1490             _Ap __a(__a0);
   1491             typedef __allocator_destructor<_Ap> _Dp;
   1492             unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
   1493             ::new (__hold.get()) _FF(__f, _Alloc(__a));
   1494             __f_ = __hold.release();
   1495         }
   1496     }
   1497 }
   1498 
   1499 template<class _Rp, class _A0, class _A1, class _A2>
   1500 function<_Rp(_A0, _A1, _A2)>&
   1501 function<_Rp(_A0, _A1, _A2)>::operator=(const function& __f)
   1502 {
   1503     function(__f).swap(*this);
   1504     return *this;
   1505 }
   1506 
   1507 template<class _Rp, class _A0, class _A1, class _A2>
   1508 function<_Rp(_A0, _A1, _A2)>&
   1509 function<_Rp(_A0, _A1, _A2)>::operator=(nullptr_t)
   1510 {
   1511     if (__f_ == (__base*)&__buf_)
   1512         __f_->destroy();
   1513     else if (__f_)
   1514         __f_->destroy_deallocate();
   1515     __f_ = 0;
   1516     return *this;
   1517 }
   1518 
   1519 template<class _Rp, class _A0, class _A1, class _A2>
   1520 template <class _Fp>
   1521 typename enable_if
   1522 <
   1523     !is_integral<_Fp>::value,
   1524     function<_Rp(_A0, _A1, _A2)>&
   1525 >::type
   1526 function<_Rp(_A0, _A1, _A2)>::operator=(_Fp __f)
   1527 {
   1528     function(_VSTD::move(__f)).swap(*this);
   1529     return *this;
   1530 }
   1531 
   1532 template<class _Rp, class _A0, class _A1, class _A2>
   1533 function<_Rp(_A0, _A1, _A2)>::~function()
   1534 {
   1535     if (__f_ == (__base*)&__buf_)
   1536         __f_->destroy();
   1537     else if (__f_)
   1538         __f_->destroy_deallocate();
   1539 }
   1540 
   1541 template<class _Rp, class _A0, class _A1, class _A2>
   1542 void
   1543 function<_Rp(_A0, _A1, _A2)>::swap(function& __f)
   1544 {
   1545     if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
   1546     {
   1547         typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
   1548         __base* __t = (__base*)&__tempbuf;
   1549         __f_->__clone(__t);
   1550         __f_->destroy();
   1551         __f_ = 0;
   1552         __f.__f_->__clone((__base*)&__buf_);
   1553         __f.__f_->destroy();
   1554         __f.__f_ = 0;
   1555         __f_ = (__base*)&__buf_;
   1556         __t->__clone((__base*)&__f.__buf_);
   1557         __t->destroy();
   1558         __f.__f_ = (__base*)&__f.__buf_;
   1559     }
   1560     else if (__f_ == (__base*)&__buf_)
   1561     {
   1562         __f_->__clone((__base*)&__f.__buf_);
   1563         __f_->destroy();
   1564         __f_ = __f.__f_;
   1565         __f.__f_ = (__base*)&__f.__buf_;
   1566     }
   1567     else if (__f.__f_ == (__base*)&__f.__buf_)
   1568     {
   1569         __f.__f_->__clone((__base*)&__buf_);
   1570         __f.__f_->destroy();
   1571         __f.__f_ = __f_;
   1572         __f_ = (__base*)&__buf_;
   1573     }
   1574     else
   1575         _VSTD::swap(__f_, __f.__f_);
   1576 }
   1577 
   1578 template<class _Rp, class _A0, class _A1, class _A2>
   1579 _Rp
   1580 function<_Rp(_A0, _A1, _A2)>::operator()(_A0 __a0, _A1 __a1, _A2 __a2) const
   1581 {
   1582 #ifndef _LIBCPP_NO_EXCEPTIONS
   1583     if (__f_ == 0)
   1584         throw bad_function_call();
   1585 #endif  // _LIBCPP_NO_EXCEPTIONS
   1586     return (*__f_)(__a0, __a1, __a2);
   1587 }
   1588 
   1589 #ifndef _LIBCPP_NO_RTTI
   1590 
   1591 template<class _Rp, class _A0, class _A1, class _A2>
   1592 const std::type_info&
   1593 function<_Rp(_A0, _A1, _A2)>::target_type() const
   1594 {
   1595     if (__f_ == 0)
   1596         return typeid(void);
   1597     return __f_->target_type();
   1598 }
   1599 
   1600 template<class _Rp, class _A0, class _A1, class _A2>
   1601 template <typename _Tp>
   1602 _Tp*
   1603 function<_Rp(_A0, _A1, _A2)>::target()
   1604 {
   1605     if (__f_ == 0)
   1606         return (_Tp*)0;
   1607     return (_Tp*)__f_->target(typeid(_Tp));
   1608 }
   1609 
   1610 template<class _Rp, class _A0, class _A1, class _A2>
   1611 template <typename _Tp>
   1612 const _Tp*
   1613 function<_Rp(_A0, _A1, _A2)>::target() const
   1614 {
   1615     if (__f_ == 0)
   1616         return (const _Tp*)0;
   1617     return (const _Tp*)__f_->target(typeid(_Tp));
   1618 }
   1619 
   1620 #endif  // _LIBCPP_NO_RTTI
   1621 
   1622 template <class _Fp>
   1623 inline _LIBCPP_INLINE_VISIBILITY
   1624 bool
   1625 operator==(const function<_Fp>& __f, nullptr_t) {return !__f;}
   1626 
   1627 template <class _Fp>
   1628 inline _LIBCPP_INLINE_VISIBILITY
   1629 bool
   1630 operator==(nullptr_t, const function<_Fp>& __f) {return !__f;}
   1631 
   1632 template <class _Fp>
   1633 inline _LIBCPP_INLINE_VISIBILITY
   1634 bool
   1635 operator!=(const function<_Fp>& __f, nullptr_t) {return (bool)__f;}
   1636 
   1637 template <class _Fp>
   1638 inline _LIBCPP_INLINE_VISIBILITY
   1639 bool
   1640 operator!=(nullptr_t, const function<_Fp>& __f) {return (bool)__f;}
   1641 
   1642 template <class _Fp>
   1643 inline _LIBCPP_INLINE_VISIBILITY
   1644 void
   1645 swap(function<_Fp>& __x, function<_Fp>& __y)
   1646 {return __x.swap(__y);}
   1647 
   1648 #endif  // _LIBCPP_FUNCTIONAL_03
   1649