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