Home | History | Annotate | Line # | Download | only in tr1
      1 // TR1 functional header -*- C++ -*-
      2 
      3 // Copyright (C) 2004-2024 Free Software Foundation, Inc.
      4 //
      5 // This file is part of the GNU ISO C++ Library.  This library is free
      6 // software; you can redistribute it and/or modify it under the
      7 // terms of the GNU General Public License as published by the
      8 // Free Software Foundation; either version 3, or (at your option)
      9 // any later version.
     10 
     11 // This library is distributed in the hope that it will be useful,
     12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
     13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     14 // GNU General Public License for more details.
     15 
     16 // Under Section 7 of GPL version 3, you are granted additional
     17 // permissions described in the GCC Runtime Library Exception, version
     18 // 3.1, as published by the Free Software Foundation.
     19 
     20 // You should have received a copy of the GNU General Public License and
     21 // a copy of the GCC Runtime Library Exception along with this program;
     22 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
     23 // <http://www.gnu.org/licenses/>.
     24 
     25 /** @file tr1/functional
     26  *  This is a TR1 C++ Library header.
     27  */
     28 
     29 #ifndef _GLIBCXX_TR1_FUNCTIONAL
     30 #define _GLIBCXX_TR1_FUNCTIONAL 1
     31 
     32 #pragma GCC system_header
     33 
     34 #include <bits/requires_hosted.h> // TR1
     35 
     36 #include <functional> // for std::_Placeholder, std::_Bind, std::_Bind_result
     37 
     38 #include <typeinfo>
     39 #include <new>
     40 #include <tr1/tuple>
     41 #include <tr1/type_traits>
     42 #include <bits/stringfwd.h>
     43 #include <tr1/functional_hash.h>
     44 #include <ext/type_traits.h>
     45 #include <bits/move.h> // for std::__addressof
     46 
     47 namespace std _GLIBCXX_VISIBILITY(default)
     48 {
     49 _GLIBCXX_BEGIN_NAMESPACE_VERSION
     50 
     51 #if __cplusplus < 201103L
     52   // In C++98 mode, <functional> doesn't declare std::placeholders::_1 etc.
     53   // because they are not reserved names in C++98. However, they are reserved
     54   // by <tr1/functional> so we can declare them here, in order to redeclare
     55   // them in the std::tr1::placeholders namespace below.
     56   namespace placeholders
     57   {
     58     extern const _Placeholder<1> _1;
     59     extern const _Placeholder<2> _2;
     60     extern const _Placeholder<3> _3;
     61     extern const _Placeholder<4> _4;
     62     extern const _Placeholder<5> _5;
     63     extern const _Placeholder<6> _6;
     64     extern const _Placeholder<7> _7;
     65     extern const _Placeholder<8> _8;
     66     extern const _Placeholder<9> _9;
     67     extern const _Placeholder<10> _10;
     68     extern const _Placeholder<11> _11;
     69     extern const _Placeholder<12> _12;
     70     extern const _Placeholder<13> _13;
     71     extern const _Placeholder<14> _14;
     72     extern const _Placeholder<15> _15;
     73     extern const _Placeholder<16> _16;
     74     extern const _Placeholder<17> _17;
     75     extern const _Placeholder<18> _18;
     76     extern const _Placeholder<19> _19;
     77     extern const _Placeholder<20> _20;
     78     extern const _Placeholder<21> _21;
     79     extern const _Placeholder<22> _22;
     80     extern const _Placeholder<23> _23;
     81     extern const _Placeholder<24> _24;
     82     extern const _Placeholder<25> _25;
     83     extern const _Placeholder<26> _26;
     84     extern const _Placeholder<27> _27;
     85     extern const _Placeholder<28> _28;
     86     extern const _Placeholder<29> _29;
     87   }
     88 #endif // C++98
     89 
     90 namespace tr1
     91 {
     92   template<typename _MemberPointer>
     93     class _Mem_fn;
     94   template<typename _Tp, typename _Class>
     95     _Mem_fn<_Tp _Class::*>
     96     mem_fn(_Tp _Class::*);
     97 
     98   /**
     99    *  Actual implementation of _Has_result_type, which uses SFINAE to
    100    *  determine if the type _Tp has a publicly-accessible member type
    101    *  result_type.
    102   */
    103   template<typename _Tp>
    104     class _Has_result_type_helper : __sfinae_types
    105     {
    106       template<typename _Up>
    107         struct _Wrap_type
    108 	{ };
    109 
    110       template<typename _Up>
    111         static __one __test(_Wrap_type<typename _Up::result_type>*);
    112 
    113       template<typename _Up>
    114         static __two __test(...);
    115 
    116     public:
    117       static const bool value = sizeof(__test<_Tp>(0)) == 1;
    118     };
    119 
    120   template<typename _Tp>
    121     struct _Has_result_type
    122     : integral_constant<bool,
    123 	      _Has_result_type_helper<typename remove_cv<_Tp>::type>::value>
    124     { };
    125 
    126   /**
    127    *  
    128   */
    129   /// If we have found a result_type, extract it.
    130   template<bool _Has_result_type, typename _Functor>
    131     struct _Maybe_get_result_type
    132     { };
    133 
    134   template<typename _Functor>
    135     struct _Maybe_get_result_type<true, _Functor>
    136     {
    137       typedef typename _Functor::result_type result_type;
    138     };
    139 
    140   /**
    141    *  Base class for any function object that has a weak result type, as
    142    *  defined in 3.3/3 of TR1.
    143   */
    144   template<typename _Functor>
    145     struct _Weak_result_type_impl
    146     : _Maybe_get_result_type<_Has_result_type<_Functor>::value, _Functor>
    147     {
    148     };
    149 
    150   /// Retrieve the result type for a function type.
    151   template<typename _Res, typename... _ArgTypes> 
    152     struct _Weak_result_type_impl<_Res(_ArgTypes...)>
    153     {
    154       typedef _Res result_type;
    155     };
    156 
    157   /// Retrieve the result type for a function reference.
    158   template<typename _Res, typename... _ArgTypes> 
    159     struct _Weak_result_type_impl<_Res(&)(_ArgTypes...)>
    160     {
    161       typedef _Res result_type;
    162     };
    163 
    164   /// Retrieve the result type for a function pointer.
    165   template<typename _Res, typename... _ArgTypes> 
    166     struct _Weak_result_type_impl<_Res(*)(_ArgTypes...)>
    167     {
    168       typedef _Res result_type;
    169     };
    170 
    171   /// Retrieve result type for a member function pointer. 
    172   template<typename _Res, typename _Class, typename... _ArgTypes> 
    173     struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...)>
    174     {
    175       typedef _Res result_type;
    176     };
    177 
    178   /// Retrieve result type for a const member function pointer. 
    179   template<typename _Res, typename _Class, typename... _ArgTypes> 
    180     struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) const>
    181     {
    182       typedef _Res result_type;
    183     };
    184 
    185   /// Retrieve result type for a volatile member function pointer. 
    186   template<typename _Res, typename _Class, typename... _ArgTypes> 
    187     struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) volatile>
    188     {
    189       typedef _Res result_type;
    190     };
    191 
    192   /// Retrieve result type for a const volatile member function pointer. 
    193   template<typename _Res, typename _Class, typename... _ArgTypes> 
    194     struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...)const volatile>
    195     {
    196       typedef _Res result_type;
    197     };
    198 
    199   /**
    200    *  Strip top-level cv-qualifiers from the function object and let
    201    *  _Weak_result_type_impl perform the real work.
    202   */
    203   template<typename _Functor>
    204     struct _Weak_result_type
    205     : _Weak_result_type_impl<typename remove_cv<_Functor>::type>
    206     {
    207     };
    208 
    209   template<typename _Signature>
    210     class result_of;
    211 
    212   /**
    213    *  Actual implementation of result_of. When _Has_result_type is
    214    *  true, gets its result from _Weak_result_type. Otherwise, uses
    215    *  the function object's member template result to extract the
    216    *  result type.
    217   */
    218   template<bool _Has_result_type, typename _Signature>
    219     struct _Result_of_impl;
    220 
    221   // Handle member data pointers using _Mem_fn's logic
    222   template<typename _Res, typename _Class, typename _T1>
    223     struct _Result_of_impl<false, _Res _Class::*(_T1)>
    224     {
    225       typedef typename _Mem_fn<_Res _Class::*>
    226                 ::template _Result_type<_T1>::type type;
    227     };
    228 
    229   /**
    230    * Determine whether we can determine a result type from @c Functor 
    231    * alone.
    232    */ 
    233   template<typename _Functor, typename... _ArgTypes>
    234     class result_of<_Functor(_ArgTypes...)>
    235     : public _Result_of_impl<
    236                _Has_result_type<_Weak_result_type<_Functor> >::value,
    237                _Functor(_ArgTypes...)>
    238     {
    239     };
    240 
    241   /// We already know the result type for @c Functor; use it.
    242   template<typename _Functor, typename... _ArgTypes>
    243     struct _Result_of_impl<true, _Functor(_ArgTypes...)>
    244     {
    245       typedef typename _Weak_result_type<_Functor>::result_type type;
    246     };
    247 
    248   /**
    249    * We need to compute the result type for this invocation the hard 
    250    * way.
    251    */
    252   template<typename _Functor, typename... _ArgTypes>
    253     struct _Result_of_impl<false, _Functor(_ArgTypes...)>
    254     {
    255       typedef typename _Functor
    256                 ::template result<_Functor(_ArgTypes...)>::type type;
    257     };
    258 
    259   /**
    260    * It is unsafe to access ::result when there are zero arguments, so we 
    261    * return @c void instead.
    262    */
    263   template<typename _Functor>
    264     struct _Result_of_impl<false, _Functor()>
    265     {
    266       typedef void type;
    267     };
    268 
    269 // Ignore warnings about std::unary_function and std::binary_function.
    270 #pragma GCC diagnostic push
    271 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
    272 
    273   /// Determines if the type _Tp derives from unary_function.
    274   template<typename _Tp>
    275     struct _Derives_from_unary_function : __sfinae_types
    276     {
    277     private:
    278       template<typename _T1, typename _Res>
    279         static __one __test(const volatile unary_function<_T1, _Res>*);
    280 
    281       // It's tempting to change "..." to const volatile void*, but
    282       // that fails when _Tp is a function type.
    283       static __two __test(...);
    284 
    285     public:
    286       static const bool value = sizeof(__test((_Tp*)0)) == 1;
    287     };
    288 
    289   /// Determines if the type _Tp derives from binary_function.
    290   template<typename _Tp>
    291     struct _Derives_from_binary_function : __sfinae_types
    292     {
    293     private:
    294       template<typename _T1, typename _T2, typename _Res>
    295         static __one __test(const volatile binary_function<_T1, _T2, _Res>*);
    296 
    297       // It's tempting to change "..." to const volatile void*, but
    298       // that fails when _Tp is a function type.
    299       static __two __test(...);
    300 
    301     public:
    302       static const bool value = sizeof(__test((_Tp*)0)) == 1;
    303     };
    304 
    305   /// Turns a function type into a function pointer type
    306   template<typename _Tp, bool _IsFunctionType = is_function<_Tp>::value>
    307     struct _Function_to_function_pointer
    308     {
    309       typedef _Tp type;
    310     };
    311 
    312   template<typename _Tp>
    313     struct _Function_to_function_pointer<_Tp, true>
    314     {
    315       typedef _Tp* type;
    316     };
    317 
    318   /**
    319    * Invoke a function object, which may be either a member pointer or a
    320    * function object. The first parameter will tell which.
    321    */
    322   template<typename _Functor, typename... _Args>
    323     inline
    324     typename __gnu_cxx::__enable_if<
    325              (!is_member_pointer<_Functor>::value
    326               && !is_function<_Functor>::value
    327               && !is_function<typename remove_pointer<_Functor>::type>::value),
    328              typename result_of<_Functor(_Args...)>::type
    329            >::__type
    330     __invoke(_Functor& __f, _Args&... __args)
    331     {
    332       return __f(__args...);
    333     }
    334 
    335   template<typename _Functor, typename... _Args>
    336     inline
    337     typename __gnu_cxx::__enable_if<
    338              (is_member_pointer<_Functor>::value
    339               && !is_function<_Functor>::value
    340               && !is_function<typename remove_pointer<_Functor>::type>::value),
    341              typename result_of<_Functor(_Args...)>::type
    342            >::__type
    343     __invoke(_Functor& __f, _Args&... __args)
    344     {
    345       return mem_fn(__f)(__args...);
    346     }
    347 
    348   // To pick up function references (that will become function pointers)
    349   template<typename _Functor, typename... _Args>
    350     inline
    351     typename __gnu_cxx::__enable_if<
    352              (is_pointer<_Functor>::value
    353               && is_function<typename remove_pointer<_Functor>::type>::value),
    354              typename result_of<_Functor(_Args...)>::type
    355            >::__type
    356     __invoke(_Functor __f, _Args&... __args)
    357     {
    358       return __f(__args...);
    359     }
    360 
    361   /**
    362    *  Knowing which of unary_function and binary_function _Tp derives
    363    *  from, derives from the same and ensures that reference_wrapper
    364    *  will have a weak result type. See cases below.
    365    */
    366   template<bool _Unary, bool _Binary, typename _Tp>
    367     struct _Reference_wrapper_base_impl;
    368 
    369   // Not a unary_function or binary_function, so try a weak result type.
    370   template<typename _Tp>
    371     struct _Reference_wrapper_base_impl<false, false, _Tp>
    372     : _Weak_result_type<_Tp>
    373     { };
    374 
    375   // unary_function but not binary_function
    376   template<typename _Tp>
    377     struct _Reference_wrapper_base_impl<true, false, _Tp>
    378     : unary_function<typename _Tp::argument_type,
    379 		     typename _Tp::result_type>
    380     { };
    381 
    382   // binary_function but not unary_function
    383   template<typename _Tp>
    384     struct _Reference_wrapper_base_impl<false, true, _Tp>
    385     : binary_function<typename _Tp::first_argument_type,
    386 		      typename _Tp::second_argument_type,
    387 		      typename _Tp::result_type>
    388     { };
    389 
    390   // Both unary_function and binary_function. Import result_type to
    391   // avoid conflicts.
    392    template<typename _Tp>
    393     struct _Reference_wrapper_base_impl<true, true, _Tp>
    394     : unary_function<typename _Tp::argument_type,
    395 		     typename _Tp::result_type>,
    396       binary_function<typename _Tp::first_argument_type,
    397 		      typename _Tp::second_argument_type,
    398 		      typename _Tp::result_type>
    399     {
    400       typedef typename _Tp::result_type result_type;
    401     };
    402 
    403   /**
    404    *  Derives from unary_function or binary_function when it
    405    *  can. Specializations handle all of the easy cases. The primary
    406    *  template determines what to do with a class type, which may
    407    *  derive from both unary_function and binary_function.
    408   */
    409   template<typename _Tp>
    410     struct _Reference_wrapper_base
    411     : _Reference_wrapper_base_impl<
    412       _Derives_from_unary_function<_Tp>::value,
    413       _Derives_from_binary_function<_Tp>::value,
    414       _Tp>
    415     { };
    416 
    417   // - a function type (unary)
    418   template<typename _Res, typename _T1>
    419     struct _Reference_wrapper_base<_Res(_T1)>
    420     : unary_function<_T1, _Res>
    421     { };
    422 
    423   // - a function type (binary)
    424   template<typename _Res, typename _T1, typename _T2>
    425     struct _Reference_wrapper_base<_Res(_T1, _T2)>
    426     : binary_function<_T1, _T2, _Res>
    427     { };
    428 
    429   // - a function pointer type (unary)
    430   template<typename _Res, typename _T1>
    431     struct _Reference_wrapper_base<_Res(*)(_T1)>
    432     : unary_function<_T1, _Res>
    433     { };
    434 
    435   // - a function pointer type (binary)
    436   template<typename _Res, typename _T1, typename _T2>
    437     struct _Reference_wrapper_base<_Res(*)(_T1, _T2)>
    438     : binary_function<_T1, _T2, _Res>
    439     { };
    440 
    441   // - a pointer to member function type (unary, no qualifiers)
    442   template<typename _Res, typename _T1>
    443     struct _Reference_wrapper_base<_Res (_T1::*)()>
    444     : unary_function<_T1*, _Res>
    445     { };
    446 
    447   // - a pointer to member function type (binary, no qualifiers)
    448   template<typename _Res, typename _T1, typename _T2>
    449     struct _Reference_wrapper_base<_Res (_T1::*)(_T2)>
    450     : binary_function<_T1*, _T2, _Res>
    451     { };
    452 
    453   // - a pointer to member function type (unary, const)
    454   template<typename _Res, typename _T1>
    455     struct _Reference_wrapper_base<_Res (_T1::*)() const>
    456     : unary_function<const _T1*, _Res>
    457     { };
    458 
    459   // - a pointer to member function type (binary, const)
    460   template<typename _Res, typename _T1, typename _T2>
    461     struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const>
    462     : binary_function<const _T1*, _T2, _Res>
    463     { };
    464 
    465   // - a pointer to member function type (unary, volatile)
    466   template<typename _Res, typename _T1>
    467     struct _Reference_wrapper_base<_Res (_T1::*)() volatile>
    468     : unary_function<volatile _T1*, _Res>
    469     { };
    470 
    471   // - a pointer to member function type (binary, volatile)
    472   template<typename _Res, typename _T1, typename _T2>
    473     struct _Reference_wrapper_base<_Res (_T1::*)(_T2) volatile>
    474     : binary_function<volatile _T1*, _T2, _Res>
    475     { };
    476 
    477   // - a pointer to member function type (unary, const volatile)
    478   template<typename _Res, typename _T1>
    479     struct _Reference_wrapper_base<_Res (_T1::*)() const volatile>
    480     : unary_function<const volatile _T1*, _Res>
    481     { };
    482 
    483   // - a pointer to member function type (binary, const volatile)
    484   template<typename _Res, typename _T1, typename _T2>
    485     struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const volatile>
    486     : binary_function<const volatile _T1*, _T2, _Res>
    487     { };
    488 
    489   /// reference_wrapper
    490   template<typename _Tp>
    491     class reference_wrapper
    492     : public _Reference_wrapper_base<typename remove_cv<_Tp>::type>
    493     {
    494       // If _Tp is a function type, we can't form result_of<_Tp(...)>,
    495       // so turn it into a function pointer type.
    496       typedef typename _Function_to_function_pointer<_Tp>::type
    497         _M_func_type;
    498 
    499       _Tp* _M_data;
    500     public:
    501       typedef _Tp type;
    502 
    503       explicit
    504       reference_wrapper(_Tp& __indata)
    505       : _M_data(std::__addressof(__indata))
    506       { }
    507 
    508       reference_wrapper(const reference_wrapper<_Tp>& __inref):
    509       _M_data(__inref._M_data)
    510       { }
    511 
    512       reference_wrapper&
    513       operator=(const reference_wrapper<_Tp>& __inref)
    514       {
    515         _M_data = __inref._M_data;
    516         return *this;
    517       }
    518 
    519       operator _Tp&() const
    520       { return this->get(); }
    521 
    522       _Tp&
    523       get() const
    524       { return *_M_data; }
    525 
    526       template<typename... _Args>
    527         typename result_of<_M_func_type(_Args...)>::type
    528         operator()(_Args&... __args) const
    529         {
    530 	  return __invoke(get(), __args...);
    531 	}
    532     };
    533 
    534 
    535   // Denotes a reference should be taken to a variable.
    536   template<typename _Tp>
    537     inline reference_wrapper<_Tp>
    538     ref(_Tp& __t)
    539     { return reference_wrapper<_Tp>(__t); }
    540 
    541   // Denotes a const reference should be taken to a variable.
    542   template<typename _Tp>
    543     inline reference_wrapper<const _Tp>
    544     cref(const _Tp& __t)
    545     { return reference_wrapper<const _Tp>(__t); }
    546 
    547   template<typename _Tp>
    548     inline reference_wrapper<_Tp>
    549     ref(reference_wrapper<_Tp> __t)
    550     { return ref(__t.get()); }
    551 
    552   template<typename _Tp>
    553     inline reference_wrapper<const _Tp>
    554     cref(reference_wrapper<_Tp> __t)
    555     { return cref(__t.get()); }
    556 
    557   template<typename _Tp, bool>
    558     struct _Mem_fn_const_or_non
    559     {
    560       typedef const _Tp& type;
    561     };
    562 
    563   template<typename _Tp>
    564     struct _Mem_fn_const_or_non<_Tp, false>
    565     {
    566       typedef _Tp& type;
    567     };
    568 
    569   /**
    570    * Derives from @c unary_function or @c binary_function, or perhaps
    571    * nothing, depending on the number of arguments provided. The
    572    * primary template is the basis case, which derives nothing.
    573    */
    574   template<typename _Res, typename... _ArgTypes> 
    575     struct _Maybe_unary_or_binary_function { };
    576 
    577   /// Derives from @c unary_function, as appropriate. 
    578   template<typename _Res, typename _T1> 
    579     struct _Maybe_unary_or_binary_function<_Res, _T1>
    580     : std::unary_function<_T1, _Res> { };
    581 
    582   /// Derives from @c binary_function, as appropriate. 
    583   template<typename _Res, typename _T1, typename _T2> 
    584     struct _Maybe_unary_or_binary_function<_Res, _T1, _T2>
    585     : std::binary_function<_T1, _T2, _Res> { };
    586 
    587   /// Implementation of @c mem_fn for member function pointers.
    588   template<typename _Res, typename _Class, typename... _ArgTypes>
    589     class _Mem_fn<_Res (_Class::*)(_ArgTypes...)>
    590     : public _Maybe_unary_or_binary_function<_Res, _Class*, _ArgTypes...>
    591     {
    592       typedef _Res (_Class::*_Functor)(_ArgTypes...);
    593 
    594       template<typename _Tp>
    595         _Res
    596         _M_call(_Tp& __object, const volatile _Class *, 
    597                 _ArgTypes... __args) const
    598         { return (__object.*__pmf)(__args...); }
    599 
    600       template<typename _Tp>
    601         _Res
    602         _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
    603         { return ((*__ptr).*__pmf)(__args...); }
    604 
    605     public:
    606       typedef _Res result_type;
    607 
    608       explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
    609 
    610       // Handle objects
    611       _Res
    612       operator()(_Class& __object, _ArgTypes... __args) const
    613       { return (__object.*__pmf)(__args...); }
    614 
    615       // Handle pointers
    616       _Res
    617       operator()(_Class* __object, _ArgTypes... __args) const
    618       { return (__object->*__pmf)(__args...); }
    619 
    620       // Handle smart pointers, references and pointers to derived
    621       template<typename _Tp>
    622         _Res
    623 	operator()(_Tp& __object, _ArgTypes... __args) const
    624         { return _M_call(__object, &__object, __args...); }
    625 
    626     private:
    627       _Functor __pmf;
    628     };
    629 
    630   /// Implementation of @c mem_fn for const member function pointers.
    631   template<typename _Res, typename _Class, typename... _ArgTypes>
    632     class _Mem_fn<_Res (_Class::*)(_ArgTypes...) const>
    633     : public _Maybe_unary_or_binary_function<_Res, const _Class*, 
    634 					     _ArgTypes...>
    635     {
    636       typedef _Res (_Class::*_Functor)(_ArgTypes...) const;
    637 
    638       template<typename _Tp>
    639         _Res
    640         _M_call(_Tp& __object, const volatile _Class *, 
    641                 _ArgTypes... __args) const
    642         { return (__object.*__pmf)(__args...); }
    643 
    644       template<typename _Tp>
    645         _Res
    646         _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
    647         { return ((*__ptr).*__pmf)(__args...); }
    648 
    649     public:
    650       typedef _Res result_type;
    651 
    652       explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
    653 
    654       // Handle objects
    655       _Res
    656       operator()(const _Class& __object, _ArgTypes... __args) const
    657       { return (__object.*__pmf)(__args...); }
    658 
    659       // Handle pointers
    660       _Res
    661       operator()(const _Class* __object, _ArgTypes... __args) const
    662       { return (__object->*__pmf)(__args...); }
    663 
    664       // Handle smart pointers, references and pointers to derived
    665       template<typename _Tp>
    666         _Res operator()(_Tp& __object, _ArgTypes... __args) const
    667         { return _M_call(__object, &__object, __args...); }
    668 
    669     private:
    670       _Functor __pmf;
    671     };
    672 
    673   /// Implementation of @c mem_fn for volatile member function pointers.
    674   template<typename _Res, typename _Class, typename... _ArgTypes>
    675     class _Mem_fn<_Res (_Class::*)(_ArgTypes...) volatile>
    676     : public _Maybe_unary_or_binary_function<_Res, volatile _Class*, 
    677 					     _ArgTypes...>
    678     {
    679       typedef _Res (_Class::*_Functor)(_ArgTypes...) volatile;
    680 
    681       template<typename _Tp>
    682         _Res
    683         _M_call(_Tp& __object, const volatile _Class *, 
    684                 _ArgTypes... __args) const
    685         { return (__object.*__pmf)(__args...); }
    686 
    687       template<typename _Tp>
    688         _Res
    689         _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
    690         { return ((*__ptr).*__pmf)(__args...); }
    691 
    692     public:
    693       typedef _Res result_type;
    694 
    695       explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
    696 
    697       // Handle objects
    698       _Res
    699       operator()(volatile _Class& __object, _ArgTypes... __args) const
    700       { return (__object.*__pmf)(__args...); }
    701 
    702       // Handle pointers
    703       _Res
    704       operator()(volatile _Class* __object, _ArgTypes... __args) const
    705       { return (__object->*__pmf)(__args...); }
    706 
    707       // Handle smart pointers, references and pointers to derived
    708       template<typename _Tp>
    709         _Res
    710 	operator()(_Tp& __object, _ArgTypes... __args) const
    711         { return _M_call(__object, &__object, __args...); }
    712 
    713     private:
    714       _Functor __pmf;
    715     };
    716 
    717   /// Implementation of @c mem_fn for const volatile member function pointers.
    718   template<typename _Res, typename _Class, typename... _ArgTypes>
    719     class _Mem_fn<_Res (_Class::*)(_ArgTypes...) const volatile>
    720     : public _Maybe_unary_or_binary_function<_Res, const volatile _Class*, 
    721 					     _ArgTypes...>
    722     {
    723       typedef _Res (_Class::*_Functor)(_ArgTypes...) const volatile;
    724 
    725       template<typename _Tp>
    726         _Res
    727         _M_call(_Tp& __object, const volatile _Class *, 
    728                 _ArgTypes... __args) const
    729         { return (__object.*__pmf)(__args...); }
    730 
    731       template<typename _Tp>
    732         _Res
    733         _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
    734         { return ((*__ptr).*__pmf)(__args...); }
    735 
    736     public:
    737       typedef _Res result_type;
    738 
    739       explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
    740 
    741       // Handle objects
    742       _Res 
    743       operator()(const volatile _Class& __object, _ArgTypes... __args) const
    744       { return (__object.*__pmf)(__args...); }
    745 
    746       // Handle pointers
    747       _Res 
    748       operator()(const volatile _Class* __object, _ArgTypes... __args) const
    749       { return (__object->*__pmf)(__args...); }
    750 
    751       // Handle smart pointers, references and pointers to derived
    752       template<typename _Tp>
    753         _Res operator()(_Tp& __object, _ArgTypes... __args) const
    754         { return _M_call(__object, &__object, __args...); }
    755 
    756     private:
    757       _Functor __pmf;
    758     };
    759 
    760 
    761   template<typename _Res, typename _Class>
    762     class _Mem_fn<_Res _Class::*>
    763     {
    764       // This bit of genius is due to Peter Dimov, improved slightly by
    765       // Douglas Gregor.
    766       template<typename _Tp>
    767         _Res&
    768         _M_call(_Tp& __object, _Class *) const
    769         { return __object.*__pm; }
    770 
    771       template<typename _Tp, typename _Up>
    772         _Res&
    773         _M_call(_Tp& __object, _Up * const *) const
    774         { return (*__object).*__pm; }
    775 
    776       template<typename _Tp, typename _Up>
    777         const _Res&
    778         _M_call(_Tp& __object, const _Up * const *) const
    779         { return (*__object).*__pm; }
    780 
    781       template<typename _Tp>
    782         const _Res&
    783         _M_call(_Tp& __object, const _Class *) const
    784         { return __object.*__pm; }
    785 
    786       template<typename _Tp>
    787         const _Res&
    788         _M_call(_Tp& __ptr, const volatile void*) const
    789         { return (*__ptr).*__pm; }
    790 
    791       template<typename _Tp> static _Tp& __get_ref();
    792 
    793       template<typename _Tp>
    794         static __sfinae_types::__one __check_const(_Tp&, _Class*);
    795       template<typename _Tp, typename _Up>
    796         static __sfinae_types::__one __check_const(_Tp&, _Up * const *);
    797       template<typename _Tp, typename _Up>
    798         static __sfinae_types::__two __check_const(_Tp&, const _Up * const *);
    799       template<typename _Tp>
    800         static __sfinae_types::__two __check_const(_Tp&, const _Class*);
    801       template<typename _Tp>
    802         static __sfinae_types::__two __check_const(_Tp&, const volatile void*);
    803 
    804     public:
    805       template<typename _Tp>
    806         struct _Result_type
    807 	: _Mem_fn_const_or_non<_Res,
    808 	  (sizeof(__sfinae_types::__two)
    809 	   == sizeof(__check_const<_Tp>(__get_ref<_Tp>(), (_Tp*)0)))>
    810         { };
    811 
    812       template<typename _Signature>
    813         struct result;
    814 
    815       template<typename _CVMem, typename _Tp>
    816         struct result<_CVMem(_Tp)>
    817 	: public _Result_type<_Tp> { };
    818 
    819       template<typename _CVMem, typename _Tp>
    820         struct result<_CVMem(_Tp&)>
    821 	: public _Result_type<_Tp> { };
    822 
    823       explicit
    824       _Mem_fn(_Res _Class::*__pm) : __pm(__pm) { }
    825 
    826       // Handle objects
    827       _Res&
    828       operator()(_Class& __object) const
    829       { return __object.*__pm; }
    830 
    831       const _Res&
    832       operator()(const _Class& __object) const
    833       { return __object.*__pm; }
    834 
    835       // Handle pointers
    836       _Res&
    837       operator()(_Class* __object) const
    838       { return __object->*__pm; }
    839 
    840       const _Res&
    841       operator()(const _Class* __object) const
    842       { return __object->*__pm; }
    843 
    844       // Handle smart pointers and derived
    845       template<typename _Tp>
    846         typename _Result_type<_Tp>::type
    847         operator()(_Tp& __unknown) const
    848         { return _M_call(__unknown, &__unknown); }
    849 
    850     private:
    851       _Res _Class::*__pm;
    852     };
    853 
    854   /**
    855    *  @brief Returns a function object that forwards to the member
    856    *  pointer @a pm.
    857    */
    858   template<typename _Tp, typename _Class>
    859     inline _Mem_fn<_Tp _Class::*>
    860     mem_fn(_Tp _Class::* __pm)
    861     {
    862       return _Mem_fn<_Tp _Class::*>(__pm);
    863     }
    864 
    865   /**
    866    *  @brief Determines if the given type _Tp is a function object
    867    *  should be treated as a subexpression when evaluating calls to
    868    *  function objects returned by bind(). [TR1 3.6.1]
    869    */
    870   template<typename _Tp>
    871     struct is_bind_expression
    872     { static const bool value = false; };
    873 
    874   template<typename _Tp>
    875     const bool is_bind_expression<_Tp>::value;
    876 
    877   /**
    878    *  @brief Determines if the given type _Tp is a placeholder in a
    879    *  bind() expression and, if so, which placeholder it is. [TR1 3.6.2]
    880    */
    881   template<typename _Tp>
    882     struct is_placeholder
    883     { static const int value = 0; };
    884 
    885   template<typename _Tp>
    886     const int is_placeholder<_Tp>::value;
    887 
    888   /// The type of placeholder objects defined by libstdc++.
    889   using ::std::_Placeholder;
    890 
    891   /** @namespace std::tr1::placeholders
    892    *  @brief Sub-namespace for tr1/functional.
    893    */
    894   namespace placeholders
    895   {
    896     // The C++11 std::placeholders are already exported from the library.
    897     // Reusing them here avoids needing to export additional symbols for
    898     // the TR1 placeholders, and avoids ODR violations due to defining
    899     // them with internal linkage (as we used to do).
    900     using namespace ::std::placeholders;
    901   }
    902 
    903   /**
    904    *  Partial specialization of is_placeholder that provides the placeholder
    905    *  number for the placeholder objects defined by libstdc++.
    906    */
    907   template<int _Num>
    908     struct is_placeholder<_Placeholder<_Num> >
    909     : integral_constant<int, _Num>
    910     { };
    911 
    912   template<int _Num>
    913     struct is_placeholder<const _Placeholder<_Num> >
    914     : integral_constant<int, _Num>
    915     { };
    916 
    917   /**
    918    * Stores a tuple of indices. Used by bind() to extract the elements
    919    * in a tuple. 
    920    */
    921   template<int... _Indexes>
    922     struct _Index_tuple { };
    923 
    924   /// Builds an _Index_tuple<0, 1, 2, ..., _Num-1>.
    925   template<std::size_t _Num, typename _Tuple = _Index_tuple<> >
    926     struct _Build_index_tuple;
    927  
    928   template<std::size_t _Num, int... _Indexes> 
    929     struct _Build_index_tuple<_Num, _Index_tuple<_Indexes...> >
    930     : _Build_index_tuple<_Num - 1, 
    931                          _Index_tuple<_Indexes..., sizeof...(_Indexes)> >
    932     {
    933     };
    934 
    935   template<int... _Indexes>
    936     struct _Build_index_tuple<0, _Index_tuple<_Indexes...> >
    937     {
    938       typedef _Index_tuple<_Indexes...> __type;
    939     };
    940 
    941   /** 
    942    * Used by _Safe_tuple_element to indicate that there is no tuple
    943    * element at this position.
    944    */
    945   struct _No_tuple_element;
    946 
    947   /**
    948    * Implementation helper for _Safe_tuple_element. This primary
    949    * template handles the case where it is safe to use @c
    950    * tuple_element.
    951    */
    952   template<int __i, typename _Tuple, bool _IsSafe>
    953     struct _Safe_tuple_element_impl
    954     : tuple_element<__i, _Tuple> { };
    955 
    956   /**
    957    * Implementation helper for _Safe_tuple_element. This partial
    958    * specialization handles the case where it is not safe to use @c
    959    * tuple_element. We just return @c _No_tuple_element.
    960    */
    961   template<int __i, typename _Tuple>
    962     struct _Safe_tuple_element_impl<__i, _Tuple, false>
    963     {
    964       typedef _No_tuple_element type;
    965     };
    966 
    967   /**
    968    * Like tuple_element, but returns @c _No_tuple_element when
    969    * tuple_element would return an error.
    970    */
    971  template<int __i, typename _Tuple>
    972    struct _Safe_tuple_element
    973    : _Safe_tuple_element_impl<__i, _Tuple, 
    974                               (__i >= 0 && __i < tuple_size<_Tuple>::value)>
    975    {
    976    };
    977 
    978   /**
    979    *  Maps an argument to bind() into an actual argument to the bound
    980    *  function object [TR1 3.6.3/5]. Only the first parameter should
    981    *  be specified: the rest are used to determine among the various
    982    *  implementations. Note that, although this class is a function
    983    *  object, it isn't entirely normal because it takes only two
    984    *  parameters regardless of the number of parameters passed to the
    985    *  bind expression. The first parameter is the bound argument and
    986    *  the second parameter is a tuple containing references to the
    987    *  rest of the arguments.
    988    */
    989   template<typename _Arg,
    990            bool _IsBindExp = is_bind_expression<_Arg>::value,
    991            bool _IsPlaceholder = (is_placeholder<_Arg>::value > 0)>
    992     class _Mu;
    993 
    994   /**
    995    *  If the argument is reference_wrapper<_Tp>, returns the
    996    *  underlying reference. [TR1 3.6.3/5 bullet 1]
    997    */
    998   template<typename _Tp>
    999     class _Mu<reference_wrapper<_Tp>, false, false>
   1000     {
   1001     public:
   1002       typedef _Tp& result_type;
   1003 
   1004       /* Note: This won't actually work for const volatile
   1005        * reference_wrappers, because reference_wrapper::get() is const
   1006        * but not volatile-qualified. This might be a defect in the TR.
   1007        */
   1008       template<typename _CVRef, typename _Tuple>
   1009         result_type
   1010         operator()(_CVRef& __arg, const _Tuple&) const volatile
   1011         { return __arg.get(); }
   1012     };
   1013 
   1014   /**
   1015    *  If the argument is a bind expression, we invoke the underlying
   1016    *  function object with the same cv-qualifiers as we are given and
   1017    *  pass along all of our arguments (unwrapped). [TR1 3.6.3/5 bullet 2]
   1018    */
   1019   template<typename _Arg>
   1020     class _Mu<_Arg, true, false>
   1021     {
   1022     public:
   1023       template<typename _Signature> class result;
   1024 
   1025       // Determine the result type when we pass the arguments along. This
   1026       // involves passing along the cv-qualifiers placed on _Mu and
   1027       // unwrapping the argument bundle.
   1028       template<typename _CVMu, typename _CVArg, typename... _Args>
   1029         class result<_CVMu(_CVArg, tuple<_Args...>)>
   1030 	: public result_of<_CVArg(_Args...)> { };
   1031 
   1032       template<typename _CVArg, typename... _Args>
   1033         typename result_of<_CVArg(_Args...)>::type
   1034         operator()(_CVArg& __arg,
   1035 		   const tuple<_Args...>& __tuple) const volatile
   1036         {
   1037 	  // Construct an index tuple and forward to __call
   1038 	  typedef typename _Build_index_tuple<sizeof...(_Args)>::__type
   1039 	    _Indexes;
   1040 	  return this->__call(__arg, __tuple, _Indexes());
   1041 	}
   1042 
   1043     private:
   1044       // Invokes the underlying function object __arg by unpacking all
   1045       // of the arguments in the tuple. 
   1046       template<typename _CVArg, typename... _Args, int... _Indexes>
   1047         typename result_of<_CVArg(_Args...)>::type
   1048         __call(_CVArg& __arg, const tuple<_Args...>& __tuple,
   1049 	       const _Index_tuple<_Indexes...>&) const volatile
   1050         {
   1051 	  return __arg(tr1::get<_Indexes>(__tuple)...);
   1052 	}
   1053     };
   1054 
   1055   /**
   1056    *  If the argument is a placeholder for the Nth argument, returns
   1057    *  a reference to the Nth argument to the bind function object.
   1058    *  [TR1 3.6.3/5 bullet 3]
   1059    */
   1060   template<typename _Arg>
   1061     class _Mu<_Arg, false, true>
   1062     {
   1063     public:
   1064       template<typename _Signature> class result;
   1065 
   1066       template<typename _CVMu, typename _CVArg, typename _Tuple>
   1067         class result<_CVMu(_CVArg, _Tuple)>
   1068         {
   1069 	  // Add a reference, if it hasn't already been done for us.
   1070 	  // This allows us to be a little bit sloppy in constructing
   1071 	  // the tuple that we pass to result_of<...>.
   1072 	  typedef typename _Safe_tuple_element<(is_placeholder<_Arg>::value
   1073 						- 1), _Tuple>::type
   1074 	    __base_type;
   1075 
   1076 	public:
   1077 	  typedef typename add_reference<__base_type>::type type;
   1078 	};
   1079 
   1080       template<typename _Tuple>
   1081         typename result<_Mu(_Arg, _Tuple)>::type
   1082         operator()(const volatile _Arg&, const _Tuple& __tuple) const volatile
   1083         {
   1084 	  return ::std::tr1::get<(is_placeholder<_Arg>::value - 1)>(__tuple);
   1085 	}
   1086     };
   1087 
   1088   /**
   1089    *  If the argument is just a value, returns a reference to that
   1090    *  value. The cv-qualifiers on the reference are the same as the
   1091    *  cv-qualifiers on the _Mu object. [TR1 3.6.3/5 bullet 4]
   1092    */
   1093   template<typename _Arg>
   1094     class _Mu<_Arg, false, false>
   1095     {
   1096     public:
   1097       template<typename _Signature> struct result;
   1098 
   1099       template<typename _CVMu, typename _CVArg, typename _Tuple>
   1100         struct result<_CVMu(_CVArg, _Tuple)>
   1101         {
   1102 	  typedef typename add_reference<_CVArg>::type type;
   1103 	};
   1104 
   1105       // Pick up the cv-qualifiers of the argument
   1106       template<typename _CVArg, typename _Tuple>
   1107         _CVArg&
   1108         operator()(_CVArg& __arg, const _Tuple&) const volatile
   1109         { return __arg; }
   1110     };
   1111 
   1112   /**
   1113    *  Maps member pointers into instances of _Mem_fn but leaves all
   1114    *  other function objects untouched. Used by tr1::bind(). The
   1115    *  primary template handles the non--member-pointer case.
   1116    */
   1117   template<typename _Tp>
   1118     struct _Maybe_wrap_member_pointer
   1119     {
   1120       typedef _Tp type;
   1121       
   1122       static const _Tp&
   1123       __do_wrap(const _Tp& __x)
   1124       { return __x; }
   1125     };
   1126 
   1127   /**
   1128    *  Maps member pointers into instances of _Mem_fn but leaves all
   1129    *  other function objects untouched. Used by tr1::bind(). This
   1130    *  partial specialization handles the member pointer case.
   1131    */
   1132   template<typename _Tp, typename _Class>
   1133     struct _Maybe_wrap_member_pointer<_Tp _Class::*>
   1134     {
   1135       typedef _Mem_fn<_Tp _Class::*> type;
   1136       
   1137       static type
   1138       __do_wrap(_Tp _Class::* __pm)
   1139       { return type(__pm); }
   1140     };
   1141 
   1142   /// Type of the function object returned from bind().
   1143   template<typename _Signature>
   1144     struct _Bind;
   1145 
   1146    template<typename _Functor, typename... _Bound_args>
   1147     class _Bind<_Functor(_Bound_args...)>
   1148     : public _Weak_result_type<_Functor>
   1149     {
   1150       typedef _Bind __self_type;
   1151       typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type 
   1152         _Bound_indexes;
   1153 
   1154       _Functor _M_f;
   1155       tuple<_Bound_args...> _M_bound_args;
   1156 
   1157       // Call unqualified
   1158       template<typename... _Args, int... _Indexes>
   1159         typename result_of<
   1160                    _Functor(typename result_of<_Mu<_Bound_args> 
   1161                             (_Bound_args, tuple<_Args...>)>::type...)
   1162                  >::type
   1163         __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>)
   1164         {
   1165           return _M_f(_Mu<_Bound_args>()
   1166                       (tr1::get<_Indexes>(_M_bound_args), __args)...);
   1167         }
   1168 
   1169       // Call as const
   1170       template<typename... _Args, int... _Indexes>
   1171         typename result_of<
   1172                    const _Functor(typename result_of<_Mu<_Bound_args> 
   1173                                     (const _Bound_args, tuple<_Args...>)
   1174                                   >::type...)>::type
   1175         __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>) const
   1176         {
   1177           return _M_f(_Mu<_Bound_args>()
   1178                       (tr1::get<_Indexes>(_M_bound_args), __args)...);
   1179         }
   1180 
   1181       // Call as volatile
   1182       template<typename... _Args, int... _Indexes>
   1183         typename result_of<
   1184                    volatile _Functor(typename result_of<_Mu<_Bound_args> 
   1185                                     (volatile _Bound_args, tuple<_Args...>)
   1186                                   >::type...)>::type
   1187         __call(const tuple<_Args...>& __args, 
   1188                _Index_tuple<_Indexes...>) volatile
   1189         {
   1190           return _M_f(_Mu<_Bound_args>()
   1191                       (tr1::get<_Indexes>(_M_bound_args), __args)...);
   1192         }
   1193 
   1194       // Call as const volatile
   1195       template<typename... _Args, int... _Indexes>
   1196         typename result_of<
   1197                    const volatile _Functor(typename result_of<_Mu<_Bound_args> 
   1198                                     (const volatile _Bound_args, 
   1199                                      tuple<_Args...>)
   1200                                   >::type...)>::type
   1201         __call(const tuple<_Args...>& __args, 
   1202                _Index_tuple<_Indexes...>) const volatile
   1203         {
   1204           return _M_f(_Mu<_Bound_args>()
   1205                       (tr1::get<_Indexes>(_M_bound_args), __args)...);
   1206         }
   1207 
   1208      public:
   1209       explicit _Bind(_Functor __f, _Bound_args... __bound_args)
   1210         : _M_f(__f), _M_bound_args(__bound_args...) { }
   1211 
   1212       // Call unqualified
   1213       template<typename... _Args>
   1214         typename result_of<
   1215                    _Functor(typename result_of<_Mu<_Bound_args> 
   1216                             (_Bound_args, tuple<_Args...>)>::type...)
   1217                  >::type
   1218         operator()(_Args&... __args)
   1219         {
   1220           return this->__call(tr1::tie(__args...), _Bound_indexes());
   1221         }
   1222 
   1223       // Call as const
   1224       template<typename... _Args>
   1225         typename result_of<
   1226                    const _Functor(typename result_of<_Mu<_Bound_args> 
   1227                             (const _Bound_args, tuple<_Args...>)>::type...)
   1228                  >::type
   1229         operator()(_Args&... __args) const
   1230         {
   1231           return this->__call(tr1::tie(__args...), _Bound_indexes());
   1232         }
   1233 
   1234 
   1235       // Call as volatile
   1236       template<typename... _Args>
   1237         typename result_of<
   1238                    volatile _Functor(typename result_of<_Mu<_Bound_args> 
   1239                             (volatile _Bound_args, tuple<_Args...>)>::type...)
   1240                  >::type
   1241         operator()(_Args&... __args) volatile
   1242         {
   1243           return this->__call(tr1::tie(__args...), _Bound_indexes());
   1244         }
   1245 
   1246 
   1247       // Call as const volatile
   1248       template<typename... _Args>
   1249         typename result_of<
   1250                    const volatile _Functor(typename result_of<_Mu<_Bound_args> 
   1251                             (const volatile _Bound_args, 
   1252                              tuple<_Args...>)>::type...)
   1253                  >::type
   1254         operator()(_Args&... __args) const volatile
   1255         {
   1256           return this->__call(tr1::tie(__args...), _Bound_indexes());
   1257         }
   1258     };
   1259 
   1260   /// Type of the function object returned from bind<R>().
   1261   template<typename _Result, typename _Signature>
   1262     struct _Bind_result;
   1263 
   1264   template<typename _Result, typename _Functor, typename... _Bound_args>
   1265     class _Bind_result<_Result, _Functor(_Bound_args...)>
   1266     {
   1267       typedef _Bind_result __self_type;
   1268       typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type 
   1269         _Bound_indexes;
   1270 
   1271       _Functor _M_f;
   1272       tuple<_Bound_args...> _M_bound_args;
   1273 
   1274       // Call unqualified
   1275       template<typename... _Args, int... _Indexes>
   1276         _Result
   1277         __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>)
   1278         {
   1279           return _M_f(_Mu<_Bound_args>()
   1280                       (tr1::get<_Indexes>(_M_bound_args), __args)...);
   1281         }
   1282 
   1283       // Call as const
   1284       template<typename... _Args, int... _Indexes>
   1285         _Result
   1286         __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>) const
   1287         {
   1288           return _M_f(_Mu<_Bound_args>()
   1289                       (tr1::get<_Indexes>(_M_bound_args), __args)...);
   1290         }
   1291 
   1292       // Call as volatile
   1293       template<typename... _Args, int... _Indexes>
   1294         _Result
   1295         __call(const tuple<_Args...>& __args, 
   1296                _Index_tuple<_Indexes...>) volatile
   1297         {
   1298           return _M_f(_Mu<_Bound_args>()
   1299                       (tr1::get<_Indexes>(_M_bound_args), __args)...);
   1300         }
   1301 
   1302       // Call as const volatile
   1303       template<typename... _Args, int... _Indexes>
   1304         _Result
   1305         __call(const tuple<_Args...>& __args, 
   1306                _Index_tuple<_Indexes...>) const volatile
   1307         {
   1308           return _M_f(_Mu<_Bound_args>()
   1309                       (tr1::get<_Indexes>(_M_bound_args), __args)...);
   1310         }
   1311 
   1312     public:
   1313       typedef _Result result_type;
   1314 
   1315       explicit
   1316       _Bind_result(_Functor __f, _Bound_args... __bound_args)
   1317       : _M_f(__f), _M_bound_args(__bound_args...) { }
   1318 
   1319       // Call unqualified
   1320       template<typename... _Args>
   1321         result_type
   1322         operator()(_Args&... __args)
   1323         {
   1324           return this->__call(tr1::tie(__args...), _Bound_indexes());
   1325         }
   1326 
   1327       // Call as const
   1328       template<typename... _Args>
   1329         result_type
   1330         operator()(_Args&... __args) const
   1331         {
   1332           return this->__call(tr1::tie(__args...), _Bound_indexes());
   1333         }
   1334 
   1335       // Call as volatile
   1336       template<typename... _Args>
   1337         result_type
   1338         operator()(_Args&... __args) volatile
   1339         {
   1340           return this->__call(tr1::tie(__args...), _Bound_indexes());
   1341         }
   1342 
   1343       // Call as const volatile
   1344       template<typename... _Args>
   1345         result_type
   1346         operator()(_Args&... __args) const volatile
   1347         {
   1348           return this->__call(tr1::tie(__args...), _Bound_indexes());
   1349         }
   1350     };
   1351 
   1352   /// Class template _Bind is always a bind expression.
   1353   template<typename _Signature>
   1354     struct is_bind_expression<_Bind<_Signature> >
   1355     { static const bool value = true; };
   1356 
   1357   template<typename _Signature>
   1358     const bool is_bind_expression<_Bind<_Signature> >::value;
   1359 
   1360   /// Class template _Bind is always a bind expression.
   1361   template<typename _Signature>
   1362     struct is_bind_expression<const _Bind<_Signature> >
   1363     { static const bool value = true; };
   1364 
   1365   template<typename _Signature>
   1366     const bool is_bind_expression<const _Bind<_Signature> >::value;
   1367 
   1368   /// Class template _Bind is always a bind expression.
   1369   template<typename _Signature>
   1370     struct is_bind_expression<volatile _Bind<_Signature> >
   1371     { static const bool value = true; };
   1372 
   1373   template<typename _Signature>
   1374     const bool is_bind_expression<volatile _Bind<_Signature> >::value;
   1375 
   1376   /// Class template _Bind is always a bind expression.
   1377   template<typename _Signature>
   1378     struct is_bind_expression<const volatile _Bind<_Signature> >
   1379     { static const bool value = true; };
   1380 
   1381   template<typename _Signature>
   1382     const bool is_bind_expression<const volatile _Bind<_Signature> >::value;
   1383 
   1384   /// Class template _Bind_result is always a bind expression.
   1385   template<typename _Result, typename _Signature>
   1386     struct is_bind_expression<_Bind_result<_Result, _Signature> >
   1387     { static const bool value = true; };
   1388 
   1389   template<typename _Result, typename _Signature>
   1390     const bool is_bind_expression<_Bind_result<_Result, _Signature> >::value;
   1391 
   1392   /// Class template _Bind_result is always a bind expression.
   1393   template<typename _Result, typename _Signature>
   1394     struct is_bind_expression<const _Bind_result<_Result, _Signature> >
   1395     { static const bool value = true; };
   1396 
   1397   template<typename _Result, typename _Signature>
   1398     const bool
   1399     is_bind_expression<const _Bind_result<_Result, _Signature> >::value;
   1400 
   1401   /// Class template _Bind_result is always a bind expression.
   1402   template<typename _Result, typename _Signature>
   1403     struct is_bind_expression<volatile _Bind_result<_Result, _Signature> >
   1404     { static const bool value = true; };
   1405 
   1406   template<typename _Result, typename _Signature>
   1407     const bool
   1408     is_bind_expression<volatile _Bind_result<_Result, _Signature> >::value;
   1409 
   1410   /// Class template _Bind_result is always a bind expression.
   1411   template<typename _Result, typename _Signature>
   1412     struct
   1413     is_bind_expression<const volatile _Bind_result<_Result, _Signature> >
   1414     { static const bool value = true; };
   1415 
   1416   template<typename _Result, typename _Signature>
   1417     const bool
   1418     is_bind_expression<const volatile _Bind_result<_Result,
   1419                                                    _Signature> >::value;
   1420 
   1421 #if __cplusplus >= 201103L
   1422   // Specialize tr1::is_bind_expression for std::bind closure types,
   1423   // so that they can also work with tr1::bind.
   1424 
   1425   template<typename _Signature>
   1426     struct is_bind_expression<std::_Bind<_Signature>>
   1427     : true_type { };
   1428 
   1429   template<typename _Signature>
   1430     struct is_bind_expression<const std::_Bind<_Signature>>
   1431     : true_type { };
   1432 
   1433   template<typename _Signature>
   1434     struct is_bind_expression<volatile std::_Bind<_Signature>>
   1435     : true_type { };
   1436 
   1437   template<typename _Signature>
   1438     struct is_bind_expression<const volatile std::_Bind<_Signature>>
   1439     : true_type { };
   1440 
   1441   template<typename _Result, typename _Signature>
   1442     struct is_bind_expression<std::_Bind_result<_Result, _Signature>>
   1443     : true_type { };
   1444 
   1445   template<typename _Result, typename _Signature>
   1446     struct is_bind_expression<const std::_Bind_result<_Result, _Signature>>
   1447     : true_type { };
   1448 
   1449   template<typename _Result, typename _Signature>
   1450     struct is_bind_expression<volatile std::_Bind_result<_Result, _Signature>>
   1451     : true_type { };
   1452 
   1453   template<typename _Result, typename _Signature>
   1454     struct is_bind_expression<const volatile std::_Bind_result<_Result,
   1455                                                                _Signature>>
   1456     : true_type { };
   1457 #endif
   1458 
   1459   /// bind
   1460   template<typename _Functor, typename... _ArgTypes>
   1461     inline
   1462     _Bind<typename _Maybe_wrap_member_pointer<_Functor>::type(_ArgTypes...)>
   1463     bind(_Functor __f, _ArgTypes... __args)
   1464     {
   1465       typedef _Maybe_wrap_member_pointer<_Functor> __maybe_type;
   1466       typedef typename __maybe_type::type __functor_type;
   1467       typedef _Bind<__functor_type(_ArgTypes...)> __result_type;
   1468       return __result_type(__maybe_type::__do_wrap(__f), __args...);
   1469     } 
   1470 
   1471   template<typename _Result, typename _Functor, typename... _ArgTypes>
   1472     inline
   1473     _Bind_result<_Result,
   1474 		 typename _Maybe_wrap_member_pointer<_Functor>::type
   1475                             (_ArgTypes...)>
   1476     bind(_Functor __f, _ArgTypes... __args)
   1477     {
   1478       typedef _Maybe_wrap_member_pointer<_Functor> __maybe_type;
   1479       typedef typename __maybe_type::type __functor_type;
   1480       typedef _Bind_result<_Result, __functor_type(_ArgTypes...)>
   1481 	__result_type;
   1482       return __result_type(__maybe_type::__do_wrap(__f), __args...);
   1483     }
   1484 
   1485   /**
   1486    *  @brief Exception class thrown when class template function's
   1487    *  operator() is called with an empty target.
   1488    *  @ingroup exceptions
   1489    */
   1490   class bad_function_call : public std::exception { };
   1491 
   1492   /**
   1493    *  The integral constant expression 0 can be converted into a
   1494    *  pointer to this type. It is used by the function template to
   1495    *  accept NULL pointers.
   1496    */
   1497   struct _M_clear_type;
   1498 
   1499   /**
   1500    *  Trait identifying @a location-invariant types, meaning that the
   1501    *  address of the object (or any of its members) will not escape.
   1502    *  Also implies a trivial copy constructor and assignment operator.
   1503    */
   1504   template<typename _Tp>
   1505     struct __is_location_invariant
   1506     : integral_constant<bool,
   1507                         (is_pointer<_Tp>::value
   1508                          || is_member_pointer<_Tp>::value)>
   1509     {
   1510     };
   1511 
   1512   class _Undefined_class;
   1513 
   1514   union _Nocopy_types
   1515   {
   1516     void*       _M_object;
   1517     const void* _M_const_object;
   1518     void (*_M_function_pointer)();
   1519     void (_Undefined_class::*_M_member_pointer)();
   1520   };
   1521 
   1522   union _Any_data
   1523   {
   1524     void*       _M_access()       { return &_M_pod_data[0]; }
   1525     const void* _M_access() const { return &_M_pod_data[0]; }
   1526 
   1527     template<typename _Tp>
   1528       _Tp&
   1529       _M_access()
   1530       { return *static_cast<_Tp*>(_M_access()); }
   1531 
   1532     template<typename _Tp>
   1533       const _Tp&
   1534       _M_access() const
   1535       { return *static_cast<const _Tp*>(_M_access()); }
   1536 
   1537     _Nocopy_types _M_unused;
   1538     char _M_pod_data[sizeof(_Nocopy_types)];
   1539   };
   1540 
   1541   enum _Manager_operation
   1542   {
   1543     __get_type_info,
   1544     __get_functor_ptr,
   1545     __clone_functor,
   1546     __destroy_functor
   1547   };
   1548 
   1549   // Simple type wrapper that helps avoid annoying const problems
   1550   // when casting between void pointers and pointers-to-pointers.
   1551   template<typename _Tp>
   1552     struct _Simple_type_wrapper
   1553     {
   1554       _Simple_type_wrapper(_Tp __value) : __value(__value) { }
   1555 
   1556       _Tp __value;
   1557     };
   1558 
   1559   template<typename _Tp>
   1560     struct __is_location_invariant<_Simple_type_wrapper<_Tp> >
   1561     : __is_location_invariant<_Tp>
   1562     {
   1563     };
   1564 
   1565   // Converts a reference to a function object into a callable
   1566   // function object.
   1567   template<typename _Functor>
   1568     inline _Functor&
   1569     __callable_functor(_Functor& __f)
   1570     { return __f; }
   1571 
   1572   template<typename _Member, typename _Class>
   1573     inline _Mem_fn<_Member _Class::*>
   1574     __callable_functor(_Member _Class::* &__p)
   1575     { return mem_fn(__p); }
   1576 
   1577   template<typename _Member, typename _Class>
   1578     inline _Mem_fn<_Member _Class::*>
   1579     __callable_functor(_Member _Class::* const &__p)
   1580     { return mem_fn(__p); }
   1581 
   1582   template<typename _Signature>
   1583     class function;
   1584 
   1585   /// Base class of all polymorphic function object wrappers.
   1586   class _Function_base
   1587   {
   1588   public:
   1589     static const std::size_t _M_max_size = sizeof(_Nocopy_types);
   1590     static const std::size_t _M_max_align = __alignof__(_Nocopy_types);
   1591 
   1592     template<typename _Functor>
   1593       class _Base_manager
   1594       {
   1595       protected:
   1596 	static const bool __stored_locally =
   1597         (__is_location_invariant<_Functor>::value
   1598          && sizeof(_Functor) <= _M_max_size
   1599          && __alignof__(_Functor) <= _M_max_align
   1600          && (_M_max_align % __alignof__(_Functor) == 0));
   1601 	
   1602 	typedef integral_constant<bool, __stored_locally> _Local_storage;
   1603 
   1604 	// Retrieve a pointer to the function object
   1605 	static _Functor*
   1606 	_M_get_pointer(const _Any_data& __source)
   1607 	{
   1608 	  const _Functor* __ptr =
   1609 	    __stored_locally? std::__addressof(__source._M_access<_Functor>())
   1610 	    /* have stored a pointer */ : __source._M_access<_Functor*>();
   1611 	  return const_cast<_Functor*>(__ptr);
   1612 	}
   1613 
   1614 	// Clone a location-invariant function object that fits within
   1615 	// an _Any_data structure.
   1616 	static void
   1617 	_M_clone(_Any_data& __dest, const _Any_data& __source, true_type)
   1618 	{
   1619 	  new (__dest._M_access()) _Functor(__source._M_access<_Functor>());
   1620 	}
   1621 
   1622 	// Clone a function object that is not location-invariant or
   1623 	// that cannot fit into an _Any_data structure.
   1624 	static void
   1625 	_M_clone(_Any_data& __dest, const _Any_data& __source, false_type)
   1626 	{
   1627 	  __dest._M_access<_Functor*>() =
   1628 	    new _Functor(*__source._M_access<_Functor*>());
   1629 	}
   1630 
   1631 	// Destroying a location-invariant object may still require
   1632 	// destruction.
   1633 	static void
   1634 	_M_destroy(_Any_data& __victim, true_type)
   1635 	{
   1636 	  __victim._M_access<_Functor>().~_Functor();
   1637 	}
   1638 	
   1639 	// Destroying an object located on the heap.
   1640 	static void
   1641 	_M_destroy(_Any_data& __victim, false_type)
   1642 	{
   1643 	  delete __victim._M_access<_Functor*>();
   1644 	}
   1645 	
   1646       public:
   1647 	static bool
   1648 	_M_manager(_Any_data& __dest, const _Any_data& __source,
   1649 		   _Manager_operation __op)
   1650 	{
   1651 	  switch (__op)
   1652 	    {
   1653 #if __cpp_rtti
   1654 	    case __get_type_info:
   1655 	      __dest._M_access<const type_info*>() = &typeid(_Functor);
   1656 	      break;
   1657 #endif
   1658 	    case __get_functor_ptr:
   1659 	      __dest._M_access<_Functor*>() = _M_get_pointer(__source);
   1660 	      break;
   1661 	      
   1662 	    case __clone_functor:
   1663 	      _M_clone(__dest, __source, _Local_storage());
   1664 	      break;
   1665 
   1666 	    case __destroy_functor:
   1667 	      _M_destroy(__dest, _Local_storage());
   1668 	      break;
   1669 	    }
   1670 	  return false;
   1671 	}
   1672 
   1673 	static void
   1674 	_M_init_functor(_Any_data& __functor, const _Functor& __f)
   1675 	{ _M_init_functor(__functor, __f, _Local_storage()); }
   1676 	
   1677 	template<typename _Signature>
   1678 	  static bool
   1679 	  _M_not_empty_function(const function<_Signature>& __f)
   1680           { return static_cast<bool>(__f); }
   1681 
   1682 	template<typename _Tp>
   1683 	  static bool
   1684 	  _M_not_empty_function(const _Tp*& __fp)
   1685 	  { return __fp; }
   1686 
   1687 	template<typename _Class, typename _Tp>
   1688 	  static bool
   1689 	  _M_not_empty_function(_Tp _Class::* const& __mp)
   1690 	  { return __mp; }
   1691 
   1692 	template<typename _Tp>
   1693 	  static bool
   1694 	  _M_not_empty_function(const _Tp&)
   1695 	  { return true; }
   1696 
   1697       private:
   1698 	static void
   1699 	_M_init_functor(_Any_data& __functor, const _Functor& __f, true_type)
   1700 	{ new (__functor._M_access()) _Functor(__f); }
   1701 
   1702 	static void
   1703 	_M_init_functor(_Any_data& __functor, const _Functor& __f, false_type)
   1704 	{ __functor._M_access<_Functor*>() = new _Functor(__f); }
   1705       };
   1706 
   1707     template<typename _Functor>
   1708       class _Ref_manager : public _Base_manager<_Functor*>
   1709       {
   1710 	typedef _Function_base::_Base_manager<_Functor*> _Base;
   1711 
   1712     public:
   1713 	static bool
   1714 	_M_manager(_Any_data& __dest, const _Any_data& __source,
   1715 		   _Manager_operation __op)
   1716 	{
   1717 	  switch (__op)
   1718 	    {
   1719 #if __cpp_rtti
   1720 	    case __get_type_info:
   1721 	      __dest._M_access<const type_info*>() = &typeid(_Functor);
   1722 	      break;
   1723 #endif
   1724 	    case __get_functor_ptr:
   1725 	      __dest._M_access<_Functor*>() = *_Base::_M_get_pointer(__source);
   1726 	      return is_const<_Functor>::value;
   1727 	      break;
   1728 	      
   1729 	    default:
   1730 	      _Base::_M_manager(__dest, __source, __op);
   1731 	    }
   1732 	  return false;
   1733 	}
   1734 
   1735 	static void
   1736 	_M_init_functor(_Any_data& __functor, reference_wrapper<_Functor> __f)
   1737 	{
   1738 	  _Base::_M_init_functor(__functor, std::__addressof(__f.get()));
   1739 	}
   1740       };
   1741 
   1742     _Function_base() : _M_manager(0) { }
   1743     
   1744     ~_Function_base()
   1745     {
   1746       if (_M_manager)
   1747 	_M_manager(_M_functor, _M_functor, __destroy_functor);
   1748     }
   1749 
   1750 
   1751     bool _M_empty() const { return !_M_manager; }
   1752 
   1753     typedef bool (*_Manager_type)(_Any_data&, const _Any_data&,
   1754                                   _Manager_operation);
   1755 
   1756     _Any_data     _M_functor;
   1757     _Manager_type _M_manager;
   1758   };
   1759 
   1760   template<typename _Signature, typename _Functor>
   1761     class _Function_handler;
   1762 
   1763   template<typename _Res, typename _Functor, typename... _ArgTypes>
   1764     class _Function_handler<_Res(_ArgTypes...), _Functor>
   1765     : public _Function_base::_Base_manager<_Functor>
   1766     {
   1767       typedef _Function_base::_Base_manager<_Functor> _Base;
   1768 
   1769     public:
   1770       static _Res
   1771       _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
   1772       {
   1773         return (*_Base::_M_get_pointer(__functor))(__args...);
   1774       }
   1775     };
   1776 
   1777   template<typename _Functor, typename... _ArgTypes>
   1778     class _Function_handler<void(_ArgTypes...), _Functor>
   1779     : public _Function_base::_Base_manager<_Functor>
   1780     {
   1781       typedef _Function_base::_Base_manager<_Functor> _Base;
   1782 
   1783      public:
   1784       static void
   1785       _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
   1786       {
   1787         (*_Base::_M_get_pointer(__functor))(__args...);
   1788       }
   1789     };
   1790 
   1791   template<typename _Res, typename _Functor, typename... _ArgTypes>
   1792     class _Function_handler<_Res(_ArgTypes...), reference_wrapper<_Functor> >
   1793     : public _Function_base::_Ref_manager<_Functor>
   1794     {
   1795       typedef _Function_base::_Ref_manager<_Functor> _Base;
   1796 
   1797      public:
   1798       static _Res
   1799       _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
   1800       {
   1801         return 
   1802           __callable_functor(**_Base::_M_get_pointer(__functor))(__args...);
   1803       }
   1804     };
   1805 
   1806   template<typename _Functor, typename... _ArgTypes>
   1807     class _Function_handler<void(_ArgTypes...), reference_wrapper<_Functor> >
   1808     : public _Function_base::_Ref_manager<_Functor>
   1809     {
   1810       typedef _Function_base::_Ref_manager<_Functor> _Base;
   1811 
   1812      public:
   1813       static void
   1814       _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
   1815       {
   1816         __callable_functor(**_Base::_M_get_pointer(__functor))(__args...);
   1817       }
   1818     };
   1819 
   1820   template<typename _Class, typename _Member, typename _Res, 
   1821            typename... _ArgTypes>
   1822     class _Function_handler<_Res(_ArgTypes...), _Member _Class::*>
   1823     : public _Function_handler<void(_ArgTypes...), _Member _Class::*>
   1824     {
   1825       typedef _Function_handler<void(_ArgTypes...), _Member _Class::*>
   1826         _Base;
   1827 
   1828      public:
   1829       static _Res
   1830       _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
   1831       {
   1832         return tr1::
   1833 	  mem_fn(_Base::_M_get_pointer(__functor)->__value)(__args...);
   1834       }
   1835     };
   1836 
   1837   template<typename _Class, typename _Member, typename... _ArgTypes>
   1838     class _Function_handler<void(_ArgTypes...), _Member _Class::*>
   1839     : public _Function_base::_Base_manager<
   1840                  _Simple_type_wrapper< _Member _Class::* > >
   1841     {
   1842       typedef _Member _Class::* _Functor;
   1843       typedef _Simple_type_wrapper<_Functor> _Wrapper;
   1844       typedef _Function_base::_Base_manager<_Wrapper> _Base;
   1845 
   1846      public:
   1847       static bool
   1848       _M_manager(_Any_data& __dest, const _Any_data& __source,
   1849                  _Manager_operation __op)
   1850       {
   1851         switch (__op)
   1852 	  {
   1853 #if __cpp_rtti
   1854 	  case __get_type_info:
   1855 	    __dest._M_access<const type_info*>() = &typeid(_Functor);
   1856 	    break;
   1857 #endif	    
   1858 	  case __get_functor_ptr:
   1859 	    __dest._M_access<_Functor*>() =
   1860 	      &_Base::_M_get_pointer(__source)->__value;
   1861 	    break;
   1862 	    
   1863 	  default:
   1864 	    _Base::_M_manager(__dest, __source, __op);
   1865 	  }
   1866         return false;
   1867       }
   1868 
   1869       static void
   1870       _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
   1871       {
   1872 	tr1::mem_fn(_Base::_M_get_pointer(__functor)->__value)(__args...);
   1873       }
   1874     };
   1875 
   1876   /// class function
   1877   template<typename _Res, typename... _ArgTypes>
   1878     class function<_Res(_ArgTypes...)>
   1879     : public _Maybe_unary_or_binary_function<_Res, _ArgTypes...>,
   1880       private _Function_base
   1881     {
   1882 #if __cplusplus < 201103L
   1883       /// This class is used to implement the safe_bool idiom.
   1884       struct _Hidden_type
   1885       {
   1886 	_Hidden_type* _M_bool;
   1887       };
   1888 
   1889       /// This typedef is used to implement the safe_bool idiom.
   1890       typedef _Hidden_type* _Hidden_type::* _Safe_bool;
   1891 #endif
   1892 
   1893       typedef _Res _Signature_type(_ArgTypes...);
   1894       
   1895       struct _Useless { };
   1896       
   1897     public:
   1898       typedef _Res result_type;
   1899       
   1900       // [3.7.2.1] construct/copy/destroy
   1901       
   1902       /**
   1903        *  @brief Default construct creates an empty function call wrapper.
   1904        *  @post @c !(bool)*this
   1905        */
   1906       function() : _Function_base() { }
   1907       
   1908       /**
   1909        *  @brief Default construct creates an empty function call wrapper.
   1910        *  @post @c !(bool)*this
   1911        */
   1912       function(_M_clear_type*) : _Function_base() { }
   1913       
   1914       /**
   1915        *  @brief %Function copy constructor.
   1916        *  @param x A %function object with identical call signature.
   1917        *  @post @c (bool)*this == (bool)x
   1918        *
   1919        *  The newly-created %function contains a copy of the target of @a
   1920        *  x (if it has one).
   1921        */
   1922       function(const function& __x);
   1923 
   1924       /**
   1925        *  @brief Builds a %function that targets a copy of the incoming
   1926        *  function object.
   1927        *  @param f A %function object that is callable with parameters of
   1928        *  type @c T1, @c T2, ..., @c TN and returns a value convertible
   1929        *  to @c Res.
   1930        *
   1931        *  The newly-created %function object will target a copy of @a
   1932        *  f. If @a f is @c reference_wrapper<F>, then this function
   1933        *  object will contain a reference to the function object @c
   1934        *  f.get(). If @a f is a NULL function pointer or NULL
   1935        *  pointer-to-member, the newly-created object will be empty.
   1936        *
   1937        *  If @a f is a non-NULL function pointer or an object of type @c
   1938        *  reference_wrapper<F>, this function will not throw.
   1939        */
   1940       template<typename _Functor>
   1941         function(_Functor __f,
   1942                  typename __gnu_cxx::__enable_if<
   1943                            !is_integral<_Functor>::value, _Useless>::__type
   1944                    = _Useless());
   1945 
   1946       /**
   1947        *  @brief %Function assignment operator.
   1948        *  @param x A %function with identical call signature.
   1949        *  @post @c (bool)*this == (bool)x
   1950        *  @returns @c *this
   1951        *
   1952        *  The target of @a x is copied to @c *this. If @a x has no
   1953        *  target, then @c *this will be empty.
   1954        *
   1955        *  If @a x targets a function pointer or a reference to a function
   1956        *  object, then this operation will not throw an %exception.
   1957        */
   1958       function&
   1959       operator=(const function& __x)
   1960       {
   1961         function(__x).swap(*this);
   1962         return *this;
   1963       }
   1964 
   1965       /**
   1966        *  @brief %Function assignment to zero.
   1967        *  @post @c !(bool)*this
   1968        *  @returns @c *this
   1969        *
   1970        *  The target of @c *this is deallocated, leaving it empty.
   1971        */
   1972       function&
   1973       operator=(_M_clear_type*)
   1974       {
   1975         if (_M_manager)
   1976 	  {
   1977 	    _M_manager(_M_functor, _M_functor, __destroy_functor);
   1978 	    _M_manager = 0;
   1979 	    _M_invoker = 0;
   1980 	  }
   1981         return *this;
   1982       }
   1983 
   1984       /**
   1985        *  @brief %Function assignment to a new target.
   1986        *  @param f A %function object that is callable with parameters of
   1987        *  type @c T1, @c T2, ..., @c TN and returns a value convertible
   1988        *  to @c Res.
   1989        *  @return @c *this
   1990        *
   1991        *  This  %function object wrapper will target a copy of @a
   1992        *  f. If @a f is @c reference_wrapper<F>, then this function
   1993        *  object will contain a reference to the function object @c
   1994        *  f.get(). If @a f is a NULL function pointer or NULL
   1995        *  pointer-to-member, @c this object will be empty.
   1996        *
   1997        *  If @a f is a non-NULL function pointer or an object of type @c
   1998        *  reference_wrapper<F>, this function will not throw.
   1999        */
   2000       template<typename _Functor>
   2001         typename __gnu_cxx::__enable_if<!is_integral<_Functor>::value,
   2002 	                                function&>::__type
   2003 	operator=(_Functor __f)
   2004 	{
   2005 	  function(__f).swap(*this);
   2006 	  return *this;
   2007 	}
   2008 
   2009       // [3.7.2.2] function modifiers
   2010       
   2011       /**
   2012        *  @brief Swap the targets of two %function objects.
   2013        *  @param f A %function with identical call signature.
   2014        *
   2015        *  Swap the targets of @c this function object and @a f. This
   2016        *  function will not throw an %exception.
   2017        */
   2018       void swap(function& __x)
   2019       {
   2020 	std::swap(_M_functor, __x._M_functor);
   2021 	std::swap(_M_manager, __x._M_manager);
   2022 	std::swap(_M_invoker, __x._M_invoker);
   2023       }
   2024 
   2025       // [3.7.2.3] function capacity
   2026 
   2027       /**
   2028        *  @brief Determine if the %function wrapper has a target.
   2029        *
   2030        *  @return @c true when this %function object contains a target,
   2031        *  or @c false when it is empty.
   2032        *
   2033        *  This function will not throw an %exception.
   2034        */
   2035 #if __cplusplus >= 201103L
   2036       explicit operator bool() const
   2037       { return !_M_empty(); }
   2038 #else
   2039       operator _Safe_bool() const
   2040       {
   2041         if (_M_empty())
   2042 	  return 0;
   2043 	else
   2044 	  return &_Hidden_type::_M_bool;
   2045       }
   2046 #endif
   2047 
   2048       // [3.7.2.4] function invocation
   2049 
   2050       /**
   2051        *  @brief Invokes the function targeted by @c *this.
   2052        *  @returns the result of the target.
   2053        *  @throws bad_function_call when @c !(bool)*this
   2054        *
   2055        *  The function call operator invokes the target function object
   2056        *  stored by @c this.
   2057        */
   2058       _Res operator()(_ArgTypes... __args) const;
   2059 
   2060 #if __cpp_rtti
   2061       // [3.7.2.5] function target access
   2062       /**
   2063        *  @brief Determine the type of the target of this function object
   2064        *  wrapper.
   2065        *
   2066        *  @returns the type identifier of the target function object, or
   2067        *  @c typeid(void) if @c !(bool)*this.
   2068        *
   2069        *  This function will not throw an %exception.
   2070        */
   2071       const type_info& target_type() const;
   2072       
   2073       /**
   2074        *  @brief Access the stored target function object.
   2075        *
   2076        *  @return Returns a pointer to the stored target function object,
   2077        *  if @c typeid(Functor).equals(target_type()); otherwise, a NULL
   2078        *  pointer.
   2079        *
   2080        * This function will not throw an %exception.
   2081        */
   2082       template<typename _Functor>       _Functor* target();
   2083       
   2084       /// @overload
   2085       template<typename _Functor> const _Functor* target() const;
   2086 #endif
   2087 
   2088     private:
   2089       // [3.7.2.6] undefined operators
   2090       template<typename _Function>
   2091 	void operator==(const function<_Function>&) const;
   2092       template<typename _Function>
   2093 	void operator!=(const function<_Function>&) const;
   2094 
   2095       typedef _Res (*_Invoker_type)(const _Any_data&, _ArgTypes...);
   2096       _Invoker_type _M_invoker;
   2097   };
   2098 #pragma GCC diagnostic pop
   2099 
   2100   template<typename _Res, typename... _ArgTypes>
   2101     function<_Res(_ArgTypes...)>::
   2102     function(const function& __x)
   2103     : _Function_base()
   2104     {
   2105       if (static_cast<bool>(__x))
   2106 	{
   2107 	  __x._M_manager(_M_functor, __x._M_functor, __clone_functor);
   2108 	  _M_invoker = __x._M_invoker;
   2109 	  _M_manager = __x._M_manager;
   2110 	}
   2111     }
   2112 
   2113   template<typename _Res, typename... _ArgTypes>
   2114     template<typename _Functor>
   2115       function<_Res(_ArgTypes...)>::
   2116       function(_Functor __f,
   2117 	       typename __gnu_cxx::__enable_if<
   2118                        !is_integral<_Functor>::value, _Useless>::__type)
   2119       : _Function_base()
   2120       {
   2121 	typedef _Function_handler<_Signature_type, _Functor> _My_handler;
   2122 
   2123 	if (_My_handler::_M_not_empty_function(__f))
   2124 	  {
   2125 	    _My_handler::_M_init_functor(_M_functor, __f);
   2126 	    _M_invoker = &_My_handler::_M_invoke;
   2127 	    _M_manager = &_My_handler::_M_manager;
   2128 	  }
   2129       }
   2130 
   2131   template<typename _Res, typename... _ArgTypes>
   2132     _Res
   2133     function<_Res(_ArgTypes...)>::
   2134     operator()(_ArgTypes... __args) const
   2135     {
   2136       if (_M_empty())
   2137 	_GLIBCXX_THROW_OR_ABORT(bad_function_call());
   2138       return _M_invoker(_M_functor, __args...);
   2139     }
   2140 
   2141 #if __cpp_rtti
   2142   template<typename _Res, typename... _ArgTypes>
   2143     const type_info&
   2144     function<_Res(_ArgTypes...)>::
   2145     target_type() const
   2146     {
   2147       if (_M_manager)
   2148         {
   2149           _Any_data __typeinfo_result;
   2150           _M_manager(__typeinfo_result, _M_functor, __get_type_info);
   2151           return *__typeinfo_result._M_access<const type_info*>();
   2152         }
   2153       else
   2154 	return typeid(void);
   2155     }
   2156 
   2157   template<typename _Res, typename... _ArgTypes>
   2158     template<typename _Functor>
   2159       _Functor*
   2160       function<_Res(_ArgTypes...)>::
   2161       target()
   2162       {
   2163 	if (typeid(_Functor) == target_type() && _M_manager)
   2164 	  {
   2165 	    _Any_data __ptr;
   2166 	    if (_M_manager(__ptr, _M_functor, __get_functor_ptr)
   2167 		&& !is_const<_Functor>::value)
   2168 	      return 0;
   2169 	    else
   2170 	      return __ptr._M_access<_Functor*>();
   2171 	  }
   2172 	else
   2173 	  return 0;
   2174       }
   2175 
   2176   template<typename _Res, typename... _ArgTypes>
   2177     template<typename _Functor>
   2178       const _Functor*
   2179       function<_Res(_ArgTypes...)>::
   2180       target() const
   2181       {
   2182 	if (typeid(_Functor) == target_type() && _M_manager)
   2183 	  {
   2184 	    _Any_data __ptr;
   2185 	    _M_manager(__ptr, _M_functor, __get_functor_ptr);
   2186 	    return __ptr._M_access<const _Functor*>();
   2187 	  }
   2188 	else
   2189 	  return 0;
   2190       }
   2191 #endif
   2192 
   2193   // [3.7.2.7] null pointer comparisons
   2194 
   2195   /**
   2196    *  @brief Compares a polymorphic function object wrapper against 0
   2197    *  (the NULL pointer).
   2198    *  @returns @c true if the wrapper has no target, @c false otherwise
   2199    *
   2200    *  This function will not throw an %exception.
   2201    */
   2202   template<typename _Signature>
   2203     inline bool
   2204     operator==(const function<_Signature>& __f, _M_clear_type*)
   2205     { return !static_cast<bool>(__f); }
   2206 
   2207   /// @overload
   2208   template<typename _Signature>
   2209     inline bool
   2210     operator==(_M_clear_type*, const function<_Signature>& __f)
   2211     { return !static_cast<bool>(__f); }
   2212 
   2213   /**
   2214    *  @brief Compares a polymorphic function object wrapper against 0
   2215    *  (the NULL pointer).
   2216    *  @returns @c false if the wrapper has no target, @c true otherwise
   2217    *
   2218    *  This function will not throw an %exception.
   2219    */
   2220   template<typename _Signature>
   2221     inline bool
   2222     operator!=(const function<_Signature>& __f, _M_clear_type*)
   2223     { return static_cast<bool>(__f); }
   2224 
   2225   /// @overload
   2226   template<typename _Signature>
   2227     inline bool
   2228     operator!=(_M_clear_type*, const function<_Signature>& __f)
   2229     { return static_cast<bool>(__f); }
   2230 
   2231   // [3.7.2.8] specialized algorithms
   2232 
   2233   /**
   2234    *  @brief Swap the targets of two polymorphic function object wrappers.
   2235    *
   2236    *  This function will not throw an %exception.
   2237    */
   2238   template<typename _Signature>
   2239     inline void
   2240     swap(function<_Signature>& __x, function<_Signature>& __y)
   2241     { __x.swap(__y); }
   2242 }
   2243 
   2244 #if __cplusplus >= 201103L
   2245   // Specialize std::is_bind_expression for tr1::bind closure types,
   2246   // so that they can also work with std::bind.
   2247 
   2248   template<typename _Signature>
   2249     struct is_bind_expression<tr1::_Bind<_Signature>>
   2250     : true_type { };
   2251 
   2252   template<typename _Signature>
   2253     struct is_bind_expression<const tr1::_Bind<_Signature>>
   2254     : true_type { };
   2255 
   2256   template<typename _Signature>
   2257     struct is_bind_expression<volatile tr1::_Bind<_Signature>>
   2258     : true_type { };
   2259 
   2260   template<typename _Signature>
   2261     struct is_bind_expression<const volatile tr1::_Bind<_Signature>>
   2262     : true_type { };
   2263 
   2264   template<typename _Result, typename _Signature>
   2265     struct is_bind_expression<tr1::_Bind_result<_Result, _Signature>>
   2266     : true_type { };
   2267 
   2268   template<typename _Result, typename _Signature>
   2269     struct is_bind_expression<const tr1::_Bind_result<_Result, _Signature>>
   2270     : true_type { };
   2271 
   2272   template<typename _Result, typename _Signature>
   2273     struct is_bind_expression<volatile tr1::_Bind_result<_Result, _Signature>>
   2274     : true_type { };
   2275 
   2276   template<typename _Result, typename _Signature>
   2277     struct is_bind_expression<const volatile tr1::_Bind_result<_Result,
   2278                                                                _Signature>>
   2279     : true_type { };
   2280 
   2281 #endif // C++11
   2282 _GLIBCXX_END_NAMESPACE_VERSION
   2283 }
   2284 
   2285 #endif // _GLIBCXX_TR1_FUNCTIONAL
   2286