libstdc++
functional
Go to the documentation of this file.
00001 // <functional> -*- C++ -*-
00002 
00003 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
00004 // 2011 Free Software Foundation, Inc.
00005 //
00006 // This file is part of the GNU ISO C++ Library.  This library is free
00007 // software; you can redistribute it and/or modify it under the
00008 // terms of the GNU General Public License as published by the
00009 // Free Software Foundation; either version 3, or (at your option)
00010 // any later version.
00011 
00012 // This library is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 
00017 // Under Section 7 of GPL version 3, you are granted additional
00018 // permissions described in the GCC Runtime Library Exception, version
00019 // 3.1, as published by the Free Software Foundation.
00020 
00021 // You should have received a copy of the GNU General Public License and
00022 // a copy of the GCC Runtime Library Exception along with this program;
00023 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00024 // <http://www.gnu.org/licenses/>.
00025 
00026 /*
00027  * Copyright (c) 1997
00028  * Silicon Graphics Computer Systems, Inc.
00029  *
00030  * Permission to use, copy, modify, distribute and sell this software
00031  * and its documentation for any purpose is hereby granted without fee,
00032  * provided that the above copyright notice appear in all copies and
00033  * that both that copyright notice and this permission notice appear
00034  * in supporting documentation.  Silicon Graphics makes no
00035  * representations about the suitability of this software for any
00036  * purpose.  It is provided "as is" without express or implied warranty.
00037  *
00038  */
00039 
00040 /** @file include/functional
00041  *  This is a Standard C++ Library header.
00042  */
00043 
00044 #ifndef _GLIBCXX_FUNCTIONAL
00045 #define _GLIBCXX_FUNCTIONAL 1
00046 
00047 #pragma GCC system_header
00048 
00049 #include <bits/c++config.h>
00050 #include <bits/stl_function.h>
00051 
00052 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00053 
00054 #include <typeinfo>
00055 #include <new>
00056 #include <tuple>
00057 #include <type_traits>
00058 #include <bits/functexcept.h>
00059 #include <bits/functional_hash.h>
00060 
00061 namespace std _GLIBCXX_VISIBILITY(default)
00062 {
00063 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00064 
00065 _GLIBCXX_HAS_NESTED_TYPE(result_type)
00066 
00067   /// If we have found a result_type, extract it.
00068   template<bool _Has_result_type, typename _Functor>
00069     struct _Maybe_get_result_type
00070     { };
00071 
00072   template<typename _Functor>
00073     struct _Maybe_get_result_type<true, _Functor>
00074     { typedef typename _Functor::result_type result_type; };
00075 
00076   /**
00077    *  Base class for any function object that has a weak result type, as
00078    *  defined in 3.3/3 of TR1.
00079   */
00080   template<typename _Functor>
00081     struct _Weak_result_type_impl
00082     : _Maybe_get_result_type<__has_result_type<_Functor>::value, _Functor>
00083     { };
00084 
00085   /// Retrieve the result type for a function type.
00086   template<typename _Res, typename... _ArgTypes>
00087     struct _Weak_result_type_impl<_Res(_ArgTypes...)>
00088     { typedef _Res result_type; };
00089 
00090   template<typename _Res, typename... _ArgTypes>
00091     struct _Weak_result_type_impl<_Res(_ArgTypes......)>
00092     { typedef _Res result_type; };
00093 
00094   template<typename _Res, typename... _ArgTypes>
00095     struct _Weak_result_type_impl<_Res(_ArgTypes...) const>
00096     { typedef _Res result_type; };
00097 
00098   template<typename _Res, typename... _ArgTypes>
00099     struct _Weak_result_type_impl<_Res(_ArgTypes......) const>
00100     { typedef _Res result_type; };
00101 
00102   template<typename _Res, typename... _ArgTypes>
00103     struct _Weak_result_type_impl<_Res(_ArgTypes...) volatile>
00104     { typedef _Res result_type; };
00105 
00106   template<typename _Res, typename... _ArgTypes>
00107     struct _Weak_result_type_impl<_Res(_ArgTypes......) volatile>
00108     { typedef _Res result_type; };
00109 
00110   template<typename _Res, typename... _ArgTypes>
00111     struct _Weak_result_type_impl<_Res(_ArgTypes...) const volatile>
00112     { typedef _Res result_type; };
00113 
00114   template<typename _Res, typename... _ArgTypes>
00115     struct _Weak_result_type_impl<_Res(_ArgTypes......) const volatile>
00116     { typedef _Res result_type; };
00117 
00118   /// Retrieve the result type for a function reference.
00119   template<typename _Res, typename... _ArgTypes>
00120     struct _Weak_result_type_impl<_Res(&)(_ArgTypes...)>
00121     { typedef _Res result_type; };
00122 
00123   template<typename _Res, typename... _ArgTypes>
00124     struct _Weak_result_type_impl<_Res(&)(_ArgTypes......)>
00125     { typedef _Res result_type; };
00126 
00127   /// Retrieve the result type for a function pointer.
00128   template<typename _Res, typename... _ArgTypes>
00129     struct _Weak_result_type_impl<_Res(*)(_ArgTypes...)>
00130     { typedef _Res result_type; };
00131 
00132   template<typename _Res, typename... _ArgTypes>
00133     struct _Weak_result_type_impl<_Res(*)(_ArgTypes......)>
00134     { typedef _Res result_type; };
00135 
00136   /// Retrieve result type for a member function pointer.
00137   template<typename _Res, typename _Class, typename... _ArgTypes>
00138     struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...)>
00139     { typedef _Res result_type; };
00140 
00141   template<typename _Res, typename _Class, typename... _ArgTypes>
00142     struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes......)>
00143     { typedef _Res result_type; };
00144 
00145   /// Retrieve result type for a const member function pointer.
00146   template<typename _Res, typename _Class, typename... _ArgTypes>
00147     struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) const>
00148     { typedef _Res result_type; };
00149 
00150   template<typename _Res, typename _Class, typename... _ArgTypes>
00151     struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes......) const>
00152     { typedef _Res result_type; };
00153 
00154   /// Retrieve result type for a volatile member function pointer.
00155   template<typename _Res, typename _Class, typename... _ArgTypes>
00156     struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) volatile>
00157     { typedef _Res result_type; };
00158 
00159   template<typename _Res, typename _Class, typename... _ArgTypes>
00160     struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes......) volatile>
00161     { typedef _Res result_type; };
00162 
00163   /// Retrieve result type for a const volatile member function pointer.
00164   template<typename _Res, typename _Class, typename... _ArgTypes>
00165     struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...)
00166                   const volatile>
00167     { typedef _Res result_type; };
00168 
00169   template<typename _Res, typename _Class, typename... _ArgTypes>
00170     struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes......)
00171                   const volatile>
00172     { typedef _Res result_type; };
00173 
00174   /**
00175    *  Strip top-level cv-qualifiers from the function object and let
00176    *  _Weak_result_type_impl perform the real work.
00177   */
00178   template<typename _Functor>
00179     struct _Weak_result_type
00180     : _Weak_result_type_impl<typename remove_cv<_Functor>::type>
00181     { };
00182 
00183   /// Determines if the type _Tp derives from unary_function.
00184   template<typename _Tp>
00185     struct _Derives_from_unary_function : __sfinae_types
00186     {
00187     private:
00188       template<typename _T1, typename _Res>
00189     static __one __test(const volatile unary_function<_T1, _Res>*);
00190 
00191       // It's tempting to change "..." to const volatile void*, but
00192       // that fails when _Tp is a function type.
00193       static __two __test(...);
00194 
00195     public:
00196       static const bool value = sizeof(__test((_Tp*)0)) == 1;
00197     };
00198 
00199   /// Determines if the type _Tp derives from binary_function.
00200   template<typename _Tp>
00201     struct _Derives_from_binary_function : __sfinae_types
00202     {
00203     private:
00204       template<typename _T1, typename _T2, typename _Res>
00205     static __one __test(const volatile binary_function<_T1, _T2, _Res>*);
00206 
00207       // It's tempting to change "..." to const volatile void*, but
00208       // that fails when _Tp is a function type.
00209       static __two __test(...);
00210 
00211     public:
00212       static const bool value = sizeof(__test((_Tp*)0)) == 1;
00213     };
00214 
00215   /**
00216    * Invoke a function object, which may be either a member pointer or a
00217    * function object. The first parameter will tell which.
00218    */
00219   template<typename _Functor, typename... _Args>
00220     inline
00221     typename enable_if<
00222          (!is_member_pointer<_Functor>::value
00223           && !is_function<_Functor>::value
00224           && !is_function<typename remove_pointer<_Functor>::type>::value),
00225          typename result_of<_Functor(_Args&&...)>::type
00226        >::type
00227     __invoke(_Functor& __f, _Args&&... __args)
00228     {
00229       return __f(std::forward<_Args>(__args)...);
00230     }
00231 
00232   template<typename _Functor, typename... _Args>
00233     inline
00234     typename enable_if<
00235              (is_member_pointer<_Functor>::value
00236               && !is_function<_Functor>::value
00237               && !is_function<typename remove_pointer<_Functor>::type>::value),
00238              typename result_of<_Functor(_Args&&...)>::type
00239            >::type
00240     __invoke(_Functor& __f, _Args&&... __args)
00241     {
00242       return mem_fn(__f)(std::forward<_Args>(__args)...);
00243     }
00244 
00245   // To pick up function references (that will become function pointers)
00246   template<typename _Functor, typename... _Args>
00247     inline
00248     typename enable_if<
00249          (is_pointer<_Functor>::value
00250           && is_function<typename remove_pointer<_Functor>::type>::value),
00251          typename result_of<_Functor(_Args&&...)>::type
00252        >::type
00253     __invoke(_Functor __f, _Args&&... __args)
00254     {
00255       return __f(std::forward<_Args>(__args)...);
00256     }
00257 
00258   /**
00259    *  Knowing which of unary_function and binary_function _Tp derives
00260    *  from, derives from the same and ensures that reference_wrapper
00261    *  will have a weak result type. See cases below.
00262    */
00263   template<bool _Unary, bool _Binary, typename _Tp>
00264     struct _Reference_wrapper_base_impl;
00265 
00266   // None of the nested argument types.
00267   template<typename _Tp>
00268     struct _Reference_wrapper_base_impl<false, false, _Tp>
00269     : _Weak_result_type<_Tp>
00270     { };
00271 
00272   // Nested argument_type only.
00273   template<typename _Tp>
00274     struct _Reference_wrapper_base_impl<true, false, _Tp>
00275     : _Weak_result_type<_Tp>
00276     {
00277       typedef typename _Tp::argument_type argument_type;
00278     };
00279 
00280   // Nested first_argument_type and second_argument_type only.
00281   template<typename _Tp>
00282     struct _Reference_wrapper_base_impl<false, true, _Tp>
00283     : _Weak_result_type<_Tp>
00284     {
00285       typedef typename _Tp::first_argument_type first_argument_type;
00286       typedef typename _Tp::second_argument_type second_argument_type;
00287     };
00288 
00289   // All the nested argument types.
00290    template<typename _Tp>
00291     struct _Reference_wrapper_base_impl<true, true, _Tp>
00292     : _Weak_result_type<_Tp>
00293     {
00294       typedef typename _Tp::argument_type argument_type;
00295       typedef typename _Tp::first_argument_type first_argument_type;
00296       typedef typename _Tp::second_argument_type second_argument_type;
00297     };
00298 
00299   _GLIBCXX_HAS_NESTED_TYPE(argument_type)
00300   _GLIBCXX_HAS_NESTED_TYPE(first_argument_type)
00301   _GLIBCXX_HAS_NESTED_TYPE(second_argument_type)
00302 
00303   /**
00304    *  Derives from unary_function or binary_function when it
00305    *  can. Specializations handle all of the easy cases. The primary
00306    *  template determines what to do with a class type, which may
00307    *  derive from both unary_function and binary_function.
00308   */
00309   template<typename _Tp>
00310     struct _Reference_wrapper_base
00311     : _Reference_wrapper_base_impl<
00312       __has_argument_type<_Tp>::value,
00313       __has_first_argument_type<_Tp>::value
00314       && __has_second_argument_type<_Tp>::value,
00315       _Tp>
00316     { };
00317 
00318   // - a function type (unary)
00319   template<typename _Res, typename _T1>
00320     struct _Reference_wrapper_base<_Res(_T1)>
00321     : unary_function<_T1, _Res>
00322     { };
00323 
00324   template<typename _Res, typename _T1>
00325     struct _Reference_wrapper_base<_Res(_T1) const>
00326     : unary_function<_T1, _Res>
00327     { };
00328 
00329   template<typename _Res, typename _T1>
00330     struct _Reference_wrapper_base<_Res(_T1) volatile>
00331     : unary_function<_T1, _Res>
00332     { };
00333 
00334   template<typename _Res, typename _T1>
00335     struct _Reference_wrapper_base<_Res(_T1) const volatile>
00336     : unary_function<_T1, _Res>
00337     { };
00338 
00339   // - a function type (binary)
00340   template<typename _Res, typename _T1, typename _T2>
00341     struct _Reference_wrapper_base<_Res(_T1, _T2)>
00342     : binary_function<_T1, _T2, _Res>
00343     { };
00344 
00345   template<typename _Res, typename _T1, typename _T2>
00346     struct _Reference_wrapper_base<_Res(_T1, _T2) const>
00347     : binary_function<_T1, _T2, _Res>
00348     { };
00349 
00350   template<typename _Res, typename _T1, typename _T2>
00351     struct _Reference_wrapper_base<_Res(_T1, _T2) volatile>
00352     : binary_function<_T1, _T2, _Res>
00353     { };
00354 
00355   template<typename _Res, typename _T1, typename _T2>
00356     struct _Reference_wrapper_base<_Res(_T1, _T2) const volatile>
00357     : binary_function<_T1, _T2, _Res>
00358     { };
00359 
00360   // - a function pointer type (unary)
00361   template<typename _Res, typename _T1>
00362     struct _Reference_wrapper_base<_Res(*)(_T1)>
00363     : unary_function<_T1, _Res>
00364     { };
00365 
00366   // - a function pointer type (binary)
00367   template<typename _Res, typename _T1, typename _T2>
00368     struct _Reference_wrapper_base<_Res(*)(_T1, _T2)>
00369     : binary_function<_T1, _T2, _Res>
00370     { };
00371 
00372   // - a pointer to member function type (unary, no qualifiers)
00373   template<typename _Res, typename _T1>
00374     struct _Reference_wrapper_base<_Res (_T1::*)()>
00375     : unary_function<_T1*, _Res>
00376     { };
00377 
00378   // - a pointer to member function type (binary, no qualifiers)
00379   template<typename _Res, typename _T1, typename _T2>
00380     struct _Reference_wrapper_base<_Res (_T1::*)(_T2)>
00381     : binary_function<_T1*, _T2, _Res>
00382     { };
00383 
00384   // - a pointer to member function type (unary, const)
00385   template<typename _Res, typename _T1>
00386     struct _Reference_wrapper_base<_Res (_T1::*)() const>
00387     : unary_function<const _T1*, _Res>
00388     { };
00389 
00390   // - a pointer to member function type (binary, const)
00391   template<typename _Res, typename _T1, typename _T2>
00392     struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const>
00393     : binary_function<const _T1*, _T2, _Res>
00394     { };
00395 
00396   // - a pointer to member function type (unary, volatile)
00397   template<typename _Res, typename _T1>
00398     struct _Reference_wrapper_base<_Res (_T1::*)() volatile>
00399     : unary_function<volatile _T1*, _Res>
00400     { };
00401 
00402   // - a pointer to member function type (binary, volatile)
00403   template<typename _Res, typename _T1, typename _T2>
00404     struct _Reference_wrapper_base<_Res (_T1::*)(_T2) volatile>
00405     : binary_function<volatile _T1*, _T2, _Res>
00406     { };
00407 
00408   // - a pointer to member function type (unary, const volatile)
00409   template<typename _Res, typename _T1>
00410     struct _Reference_wrapper_base<_Res (_T1::*)() const volatile>
00411     : unary_function<const volatile _T1*, _Res>
00412     { };
00413 
00414   // - a pointer to member function type (binary, const volatile)
00415   template<typename _Res, typename _T1, typename _T2>
00416     struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const volatile>
00417     : binary_function<const volatile _T1*, _T2, _Res>
00418     { };
00419 
00420   /**
00421    *  @brief Primary class template for reference_wrapper.
00422    *  @ingroup functors
00423    *  @{
00424    */
00425   template<typename _Tp>
00426     class reference_wrapper
00427     : public _Reference_wrapper_base<typename remove_cv<_Tp>::type>
00428     {
00429       _Tp* _M_data;
00430 
00431     public:
00432       typedef _Tp type;
00433 
00434       reference_wrapper(_Tp& __indata)
00435       : _M_data(std::__addressof(__indata))
00436       { }
00437 
00438       reference_wrapper(_Tp&&) = delete;
00439 
00440       reference_wrapper(const reference_wrapper<_Tp>& __inref):
00441       _M_data(__inref._M_data)
00442       { }
00443 
00444       reference_wrapper&
00445       operator=(const reference_wrapper<_Tp>& __inref)
00446       {
00447     _M_data = __inref._M_data;
00448     return *this;
00449       }
00450 
00451       operator _Tp&() const
00452       { return this->get(); }
00453 
00454       _Tp&
00455       get() const
00456       { return *_M_data; }
00457 
00458       template<typename... _Args>
00459     typename result_of<_Tp&(_Args&&...)>::type
00460     operator()(_Args&&... __args) const
00461     {
00462       return __invoke(get(), std::forward<_Args>(__args)...);
00463     }
00464     };
00465 
00466 
00467   /// Denotes a reference should be taken to a variable.
00468   template<typename _Tp>
00469     inline reference_wrapper<_Tp>
00470     ref(_Tp& __t)
00471     { return reference_wrapper<_Tp>(__t); }
00472 
00473   /// Denotes a const reference should be taken to a variable.
00474   template<typename _Tp>
00475     inline reference_wrapper<const _Tp>
00476     cref(const _Tp& __t)
00477     { return reference_wrapper<const _Tp>(__t); }
00478 
00479   template<typename _Tp>
00480     void ref(const _Tp&&) = delete;
00481 
00482   template<typename _Tp>
00483     void cref(const _Tp&&) = delete;
00484 
00485   /// Partial specialization.
00486   template<typename _Tp>
00487     inline reference_wrapper<_Tp>
00488     ref(reference_wrapper<_Tp> __t)
00489     { return ref(__t.get()); }
00490 
00491   /// Partial specialization.
00492   template<typename _Tp>
00493     inline reference_wrapper<const _Tp>
00494     cref(reference_wrapper<_Tp> __t)
00495     { return cref(__t.get()); }
00496 
00497   // @} group functors
00498 
00499   template<typename _MemberPointer>
00500     class _Mem_fn;
00501 
00502   /**
00503    * Derives from @c unary_function or @c binary_function, or perhaps
00504    * nothing, depending on the number of arguments provided. The
00505    * primary template is the basis case, which derives nothing.
00506    */
00507   template<typename _Res, typename... _ArgTypes>
00508     struct _Maybe_unary_or_binary_function { };
00509 
00510   /// Derives from @c unary_function, as appropriate.
00511   template<typename _Res, typename _T1>
00512     struct _Maybe_unary_or_binary_function<_Res, _T1>
00513     : std::unary_function<_T1, _Res> { };
00514 
00515   /// Derives from @c binary_function, as appropriate.
00516   template<typename _Res, typename _T1, typename _T2>
00517     struct _Maybe_unary_or_binary_function<_Res, _T1, _T2>
00518     : std::binary_function<_T1, _T2, _Res> { };
00519 
00520   /// Implementation of @c mem_fn for member function pointers.
00521   template<typename _Res, typename _Class, typename... _ArgTypes>
00522     class _Mem_fn<_Res (_Class::*)(_ArgTypes...)>
00523     : public _Maybe_unary_or_binary_function<_Res, _Class*, _ArgTypes...>
00524     {
00525       typedef _Res (_Class::*_Functor)(_ArgTypes...);
00526 
00527       template<typename _Tp>
00528     _Res
00529     _M_call(_Tp& __object, const volatile _Class *,
00530         _ArgTypes... __args) const
00531     { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); }
00532 
00533       template<typename _Tp>
00534     _Res
00535     _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
00536     { return ((*__ptr).*__pmf)(std::forward<_ArgTypes>(__args)...); }
00537 
00538     public:
00539       typedef _Res result_type;
00540 
00541       explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
00542 
00543       // Handle objects
00544       _Res
00545       operator()(_Class& __object, _ArgTypes... __args) const
00546       { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); }
00547 
00548       // Handle pointers
00549       _Res
00550       operator()(_Class* __object, _ArgTypes... __args) const
00551       { return (__object->*__pmf)(std::forward<_ArgTypes>(__args)...); }
00552 
00553       // Handle smart pointers, references and pointers to derived
00554       template<typename _Tp>
00555     _Res
00556     operator()(_Tp& __object, _ArgTypes... __args) const
00557     {
00558       return _M_call(__object, &__object,
00559           std::forward<_ArgTypes>(__args)...);
00560     }
00561 
00562     private:
00563       _Functor __pmf;
00564     };
00565 
00566   /// Implementation of @c mem_fn for const member function pointers.
00567   template<typename _Res, typename _Class, typename... _ArgTypes>
00568     class _Mem_fn<_Res (_Class::*)(_ArgTypes...) const>
00569     : public _Maybe_unary_or_binary_function<_Res, const _Class*,
00570                          _ArgTypes...>
00571     {
00572       typedef _Res (_Class::*_Functor)(_ArgTypes...) const;
00573 
00574       template<typename _Tp>
00575     _Res
00576     _M_call(_Tp& __object, const volatile _Class *,
00577         _ArgTypes... __args) const
00578     { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); }
00579 
00580       template<typename _Tp>
00581     _Res
00582     _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
00583     { return ((*__ptr).*__pmf)(std::forward<_ArgTypes>(__args)...); }
00584 
00585     public:
00586       typedef _Res result_type;
00587 
00588       explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
00589 
00590       // Handle objects
00591       _Res
00592       operator()(const _Class& __object, _ArgTypes... __args) const
00593       { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); }
00594 
00595       // Handle pointers
00596       _Res
00597       operator()(const _Class* __object, _ArgTypes... __args) const
00598       { return (__object->*__pmf)(std::forward<_ArgTypes>(__args)...); }
00599 
00600       // Handle smart pointers, references and pointers to derived
00601       template<typename _Tp>
00602     _Res operator()(_Tp& __object, _ArgTypes... __args) const
00603     {
00604       return _M_call(__object, &__object,
00605           std::forward<_ArgTypes>(__args)...);
00606     }
00607 
00608     private:
00609       _Functor __pmf;
00610     };
00611 
00612   /// Implementation of @c mem_fn for volatile member function pointers.
00613   template<typename _Res, typename _Class, typename... _ArgTypes>
00614     class _Mem_fn<_Res (_Class::*)(_ArgTypes...) volatile>
00615     : public _Maybe_unary_or_binary_function<_Res, volatile _Class*,
00616                          _ArgTypes...>
00617     {
00618       typedef _Res (_Class::*_Functor)(_ArgTypes...) volatile;
00619 
00620       template<typename _Tp>
00621     _Res
00622     _M_call(_Tp& __object, const volatile _Class *,
00623         _ArgTypes... __args) const
00624     { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); }
00625 
00626       template<typename _Tp>
00627     _Res
00628     _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
00629     { return ((*__ptr).*__pmf)(std::forward<_ArgTypes>(__args)...); }
00630 
00631     public:
00632       typedef _Res result_type;
00633 
00634       explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
00635 
00636       // Handle objects
00637       _Res
00638       operator()(volatile _Class& __object, _ArgTypes... __args) const
00639       { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); }
00640 
00641       // Handle pointers
00642       _Res
00643       operator()(volatile _Class* __object, _ArgTypes... __args) const
00644       { return (__object->*__pmf)(std::forward<_ArgTypes>(__args)...); }
00645 
00646       // Handle smart pointers, references and pointers to derived
00647       template<typename _Tp>
00648     _Res
00649     operator()(_Tp& __object, _ArgTypes... __args) const
00650     {
00651       return _M_call(__object, &__object,
00652           std::forward<_ArgTypes>(__args)...);
00653     }
00654 
00655     private:
00656       _Functor __pmf;
00657     };
00658 
00659   /// Implementation of @c mem_fn for const volatile member function pointers.
00660   template<typename _Res, typename _Class, typename... _ArgTypes>
00661     class _Mem_fn<_Res (_Class::*)(_ArgTypes...) const volatile>
00662     : public _Maybe_unary_or_binary_function<_Res, const volatile _Class*,
00663                          _ArgTypes...>
00664     {
00665       typedef _Res (_Class::*_Functor)(_ArgTypes...) const volatile;
00666 
00667       template<typename _Tp>
00668     _Res
00669     _M_call(_Tp& __object, const volatile _Class *,
00670         _ArgTypes... __args) const
00671     { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); }
00672 
00673       template<typename _Tp>
00674     _Res
00675     _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
00676     { return ((*__ptr).*__pmf)(std::forward<_ArgTypes>(__args)...); }
00677 
00678     public:
00679       typedef _Res result_type;
00680 
00681       explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
00682 
00683       // Handle objects
00684       _Res
00685       operator()(const volatile _Class& __object, _ArgTypes... __args) const
00686       { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); }
00687 
00688       // Handle pointers
00689       _Res
00690       operator()(const volatile _Class* __object, _ArgTypes... __args) const
00691       { return (__object->*__pmf)(std::forward<_ArgTypes>(__args)...); }
00692 
00693       // Handle smart pointers, references and pointers to derived
00694       template<typename _Tp>
00695     _Res operator()(_Tp& __object, _ArgTypes... __args) const
00696     {
00697       return _M_call(__object, &__object,
00698           std::forward<_ArgTypes>(__args)...);
00699     }
00700 
00701     private:
00702       _Functor __pmf;
00703     };
00704 
00705 
00706   template<typename _Tp, bool>
00707     struct _Mem_fn_const_or_non
00708     {
00709       typedef const _Tp& type;
00710     };
00711 
00712   template<typename _Tp>
00713     struct _Mem_fn_const_or_non<_Tp, false>
00714     {
00715       typedef _Tp& type;
00716     };
00717 
00718   template<typename _Res, typename _Class>
00719     class _Mem_fn<_Res _Class::*>
00720     {
00721       // This bit of genius is due to Peter Dimov, improved slightly by
00722       // Douglas Gregor.
00723       template<typename _Tp>
00724     _Res&
00725     _M_call(_Tp& __object, _Class *) const
00726     { return __object.*__pm; }
00727 
00728       template<typename _Tp, typename _Up>
00729     _Res&
00730     _M_call(_Tp& __object, _Up * const *) const
00731     { return (*__object).*__pm; }
00732 
00733       template<typename _Tp, typename _Up>
00734     const _Res&
00735     _M_call(_Tp& __object, const _Up * const *) const
00736     { return (*__object).*__pm; }
00737 
00738       template<typename _Tp>
00739     const _Res&
00740     _M_call(_Tp& __object, const _Class *) const
00741     { return __object.*__pm; }
00742 
00743       template<typename _Tp>
00744     const _Res&
00745     _M_call(_Tp& __ptr, const volatile void*) const
00746     { return (*__ptr).*__pm; }
00747 
00748       template<typename _Tp> static _Tp& __get_ref();
00749 
00750       template<typename _Tp>
00751     static __sfinae_types::__one __check_const(_Tp&, _Class*);
00752       template<typename _Tp, typename _Up>
00753     static __sfinae_types::__one __check_const(_Tp&, _Up * const *);
00754       template<typename _Tp, typename _Up>
00755     static __sfinae_types::__two __check_const(_Tp&, const _Up * const *);
00756       template<typename _Tp>
00757     static __sfinae_types::__two __check_const(_Tp&, const _Class*);
00758       template<typename _Tp>
00759     static __sfinae_types::__two __check_const(_Tp&, const volatile void*);
00760 
00761     public:
00762       template<typename _Tp>
00763     struct _Result_type
00764     : _Mem_fn_const_or_non<_Res,
00765       (sizeof(__sfinae_types::__two)
00766        == sizeof(__check_const<_Tp>(__get_ref<_Tp>(), (_Tp*)0)))>
00767     { };
00768 
00769       template<typename _Signature>
00770     struct result;
00771 
00772       template<typename _CVMem, typename _Tp>
00773     struct result<_CVMem(_Tp)>
00774     : public _Result_type<_Tp> { };
00775 
00776       template<typename _CVMem, typename _Tp>
00777     struct result<_CVMem(_Tp&)>
00778     : public _Result_type<_Tp> { };
00779 
00780       explicit
00781       _Mem_fn(_Res _Class::*__pm) : __pm(__pm) { }
00782 
00783       // Handle objects
00784       _Res&
00785       operator()(_Class& __object) const
00786       { return __object.*__pm; }
00787 
00788       const _Res&
00789       operator()(const _Class& __object) const
00790       { return __object.*__pm; }
00791 
00792       // Handle pointers
00793       _Res&
00794       operator()(_Class* __object) const
00795       { return __object->*__pm; }
00796 
00797       const _Res&
00798       operator()(const _Class* __object) const
00799       { return __object->*__pm; }
00800 
00801       // Handle smart pointers and derived
00802       template<typename _Tp>
00803     typename _Result_type<_Tp>::type
00804     operator()(_Tp& __unknown) const
00805     { return _M_call(__unknown, &__unknown); }
00806 
00807     private:
00808       _Res _Class::*__pm;
00809     };
00810 
00811   /**
00812    *  @brief Returns a function object that forwards to the member
00813    *  pointer @a pm.
00814    *  @ingroup functors
00815    */
00816   template<typename _Tp, typename _Class>
00817     inline _Mem_fn<_Tp _Class::*>
00818     mem_fn(_Tp _Class::* __pm)
00819     {
00820       return _Mem_fn<_Tp _Class::*>(__pm);
00821     }
00822 
00823   /**
00824    *  @brief Determines if the given type _Tp is a function object
00825    *  should be treated as a subexpression when evaluating calls to
00826    *  function objects returned by bind(). [TR1 3.6.1]
00827    *  @ingroup binders
00828    */
00829   template<typename _Tp>
00830     struct is_bind_expression
00831     : public false_type { };
00832 
00833   /**
00834    *  @brief Determines if the given type _Tp is a placeholder in a
00835    *  bind() expression and, if so, which placeholder it is. [TR1 3.6.2]
00836    *  @ingroup binders
00837    */
00838   template<typename _Tp>
00839     struct is_placeholder
00840     : public integral_constant<int, 0>
00841     { };
00842 
00843   /// The type of placeholder objects defined by libstdc++.
00844   template<int _Num> struct _Placeholder { };
00845 
00846   _GLIBCXX_END_NAMESPACE_VERSION
00847 
00848   /** @namespace std::placeholders
00849    *  @brief ISO C++ 0x entities sub namespace for functional.
00850    *  @ingroup binders
00851    *
00852    *  Define a large number of placeholders. There is no way to
00853    *  simplify this with variadic templates, because we're introducing
00854    *  unique names for each.
00855    */
00856   namespace placeholders
00857   {
00858   _GLIBCXX_BEGIN_NAMESPACE_VERSION
00859     extern const _Placeholder<1> _1;
00860     extern const _Placeholder<2> _2;
00861     extern const _Placeholder<3> _3;
00862     extern const _Placeholder<4> _4;
00863     extern const _Placeholder<5> _5;
00864     extern const _Placeholder<6> _6;
00865     extern const _Placeholder<7> _7;
00866     extern const _Placeholder<8> _8;
00867     extern const _Placeholder<9> _9;
00868     extern const _Placeholder<10> _10;
00869     extern const _Placeholder<11> _11;
00870     extern const _Placeholder<12> _12;
00871     extern const _Placeholder<13> _13;
00872     extern const _Placeholder<14> _14;
00873     extern const _Placeholder<15> _15;
00874     extern const _Placeholder<16> _16;
00875     extern const _Placeholder<17> _17;
00876     extern const _Placeholder<18> _18;
00877     extern const _Placeholder<19> _19;
00878     extern const _Placeholder<20> _20;
00879     extern const _Placeholder<21> _21;
00880     extern const _Placeholder<22> _22;
00881     extern const _Placeholder<23> _23;
00882     extern const _Placeholder<24> _24;
00883     extern const _Placeholder<25> _25;
00884     extern const _Placeholder<26> _26;
00885     extern const _Placeholder<27> _27;
00886     extern const _Placeholder<28> _28;
00887     extern const _Placeholder<29> _29;
00888   _GLIBCXX_END_NAMESPACE_VERSION
00889   }
00890 
00891   _GLIBCXX_BEGIN_NAMESPACE_VERSION
00892 
00893   /**
00894    *  Partial specialization of is_placeholder that provides the placeholder
00895    *  number for the placeholder objects defined by libstdc++.
00896    *  @ingroup binders
00897    */
00898   template<int _Num>
00899     struct is_placeholder<_Placeholder<_Num> >
00900     : public integral_constant<int, _Num>
00901     { };
00902 
00903   /**
00904    * Used by _Safe_tuple_element to indicate that there is no tuple
00905    * element at this position.
00906    */
00907   struct _No_tuple_element;
00908 
00909   /**
00910    * Implementation helper for _Safe_tuple_element. This primary
00911    * template handles the case where it is safe to use @c
00912    * tuple_element.
00913    */
00914   template<int __i, typename _Tuple, bool _IsSafe>
00915     struct _Safe_tuple_element_impl
00916     : tuple_element<__i, _Tuple> { };
00917 
00918   /**
00919    * Implementation helper for _Safe_tuple_element. This partial
00920    * specialization handles the case where it is not safe to use @c
00921    * tuple_element. We just return @c _No_tuple_element.
00922    */
00923   template<int __i, typename _Tuple>
00924     struct _Safe_tuple_element_impl<__i, _Tuple, false>
00925     {
00926       typedef _No_tuple_element type;
00927     };
00928 
00929   /**
00930    * Like tuple_element, but returns @c _No_tuple_element when
00931    * tuple_element would return an error.
00932    */
00933  template<int __i, typename _Tuple>
00934    struct _Safe_tuple_element
00935    : _Safe_tuple_element_impl<__i, _Tuple,
00936                   (__i >= 0 && __i < tuple_size<_Tuple>::value)>
00937    { };
00938 
00939   /**
00940    *  Maps an argument to bind() into an actual argument to the bound
00941    *  function object [TR1 3.6.3/5]. Only the first parameter should
00942    *  be specified: the rest are used to determine among the various
00943    *  implementations. Note that, although this class is a function
00944    *  object, it isn't entirely normal because it takes only two
00945    *  parameters regardless of the number of parameters passed to the
00946    *  bind expression. The first parameter is the bound argument and
00947    *  the second parameter is a tuple containing references to the
00948    *  rest of the arguments.
00949    */
00950   template<typename _Arg,
00951        bool _IsBindExp = is_bind_expression<_Arg>::value,
00952        bool _IsPlaceholder = (is_placeholder<_Arg>::value > 0)>
00953     class _Mu;
00954 
00955   /**
00956    *  If the argument is reference_wrapper<_Tp>, returns the
00957    *  underlying reference. [TR1 3.6.3/5 bullet 1]
00958    */
00959   template<typename _Tp>
00960     class _Mu<reference_wrapper<_Tp>, false, false>
00961     {
00962     public:
00963       typedef _Tp& result_type;
00964 
00965       /* Note: This won't actually work for const volatile
00966        * reference_wrappers, because reference_wrapper::get() is const
00967        * but not volatile-qualified. This might be a defect in the TR.
00968        */
00969       template<typename _CVRef, typename _Tuple>
00970     result_type
00971     operator()(_CVRef& __arg, _Tuple&) const volatile
00972     { return __arg.get(); }
00973     };
00974 
00975   /**
00976    *  If the argument is a bind expression, we invoke the underlying
00977    *  function object with the same cv-qualifiers as we are given and
00978    *  pass along all of our arguments (unwrapped). [TR1 3.6.3/5 bullet 2]
00979    */
00980   template<typename _Arg>
00981     class _Mu<_Arg, true, false>
00982     {
00983     public:
00984       template<typename _CVArg, typename... _Args>
00985     auto
00986     operator()(_CVArg& __arg,
00987            tuple<_Args...>& __tuple) const volatile
00988     -> decltype(__arg(declval<_Args>()...))
00989     {
00990       // Construct an index tuple and forward to __call
00991       typedef typename _Build_index_tuple<sizeof...(_Args)>::__type
00992         _Indexes;
00993       return this->__call(__arg, __tuple, _Indexes());
00994     }
00995 
00996     private:
00997       // Invokes the underlying function object __arg by unpacking all
00998       // of the arguments in the tuple.
00999       template<typename _CVArg, typename... _Args, int... _Indexes>
01000     auto
01001     __call(_CVArg& __arg, tuple<_Args...>& __tuple,
01002            const _Index_tuple<_Indexes...>&) const volatile
01003     -> decltype(__arg(declval<_Args>()...))
01004     {
01005       return __arg(std::forward<_Args>(get<_Indexes>(__tuple))...);
01006     }
01007     };
01008 
01009   /**
01010    *  If the argument is a placeholder for the Nth argument, returns
01011    *  a reference to the Nth argument to the bind function object.
01012    *  [TR1 3.6.3/5 bullet 3]
01013    */
01014   template<typename _Arg>
01015     class _Mu<_Arg, false, true>
01016     {
01017     public:
01018       template<typename _Signature> class result;
01019 
01020       template<typename _CVMu, typename _CVArg, typename _Tuple>
01021     class result<_CVMu(_CVArg, _Tuple)>
01022     {
01023       // Add a reference, if it hasn't already been done for us.
01024       // This allows us to be a little bit sloppy in constructing
01025       // the tuple that we pass to result_of<...>.
01026       typedef typename _Safe_tuple_element<(is_placeholder<_Arg>::value
01027                         - 1), _Tuple>::type
01028         __base_type;
01029 
01030     public:
01031       typedef typename add_rvalue_reference<__base_type>::type type;
01032     };
01033 
01034       template<typename _Tuple>
01035     typename result<_Mu(_Arg, _Tuple)>::type
01036     operator()(const volatile _Arg&, _Tuple& __tuple) const volatile
01037     {
01038       return std::forward<typename result<_Mu(_Arg, _Tuple)>::type>(
01039           ::std::get<(is_placeholder<_Arg>::value - 1)>(__tuple));
01040     }
01041     };
01042 
01043   /**
01044    *  If the argument is just a value, returns a reference to that
01045    *  value. The cv-qualifiers on the reference are the same as the
01046    *  cv-qualifiers on the _Mu object. [TR1 3.6.3/5 bullet 4]
01047    */
01048   template<typename _Arg>
01049     class _Mu<_Arg, false, false>
01050     {
01051     public:
01052       template<typename _Signature> struct result;
01053 
01054       template<typename _CVMu, typename _CVArg, typename _Tuple>
01055     struct result<_CVMu(_CVArg, _Tuple)>
01056     {
01057       typedef typename add_lvalue_reference<_CVArg>::type type;
01058     };
01059 
01060       // Pick up the cv-qualifiers of the argument
01061       template<typename _CVArg, typename _Tuple>
01062     _CVArg&&
01063     operator()(_CVArg&& __arg, _Tuple&) const volatile
01064     { return std::forward<_CVArg>(__arg); }
01065     };
01066 
01067   /**
01068    *  Maps member pointers into instances of _Mem_fn but leaves all
01069    *  other function objects untouched. Used by tr1::bind(). The
01070    *  primary template handles the non--member-pointer case.
01071    */
01072   template<typename _Tp>
01073     struct _Maybe_wrap_member_pointer
01074     {
01075       typedef _Tp type;
01076 
01077       static const _Tp&
01078       __do_wrap(const _Tp& __x)
01079       { return __x; }
01080 
01081       static _Tp&&
01082       __do_wrap(_Tp&& __x)
01083       { return static_cast<_Tp&&>(__x); }
01084     };
01085 
01086   /**
01087    *  Maps member pointers into instances of _Mem_fn but leaves all
01088    *  other function objects untouched. Used by tr1::bind(). This
01089    *  partial specialization handles the member pointer case.
01090    */
01091   template<typename _Tp, typename _Class>
01092     struct _Maybe_wrap_member_pointer<_Tp _Class::*>
01093     {
01094       typedef _Mem_fn<_Tp _Class::*> type;
01095 
01096       static type
01097       __do_wrap(_Tp _Class::* __pm)
01098       { return type(__pm); }
01099     };
01100 
01101   // Specialization needed to prevent "forming reference to void" errors when
01102   // bind<void>() is called, because argument deduction instantiates
01103   // _Maybe_wrap_member_pointer<void> outside the immediate context where
01104   // SFINAE applies.
01105   template<>
01106     struct _Maybe_wrap_member_pointer<void>
01107     {
01108       typedef void type;
01109     };
01110 
01111   // std::get<I> for volatile-qualified tuples
01112   template<size_t _Ind, typename... _Tp>
01113     inline auto
01114     __volget(volatile tuple<_Tp...>& __tuple)
01115     -> typename tuple_element<_Ind, tuple<_Tp...>>::type volatile&
01116     { return std::get<_Ind>(const_cast<tuple<_Tp...>&>(__tuple)); }
01117 
01118   // std::get<I> for const-volatile-qualified tuples
01119   template<size_t _Ind, typename... _Tp>
01120     inline auto
01121     __volget(const volatile tuple<_Tp...>& __tuple)
01122     -> typename tuple_element<_Ind, tuple<_Tp...>>::type const volatile&
01123     { return std::get<_Ind>(const_cast<const tuple<_Tp...>&>(__tuple)); }
01124 
01125   /// Type of the function object returned from bind().
01126   template<typename _Signature>
01127     struct _Bind;
01128 
01129    template<typename _Functor, typename... _Bound_args>
01130     class _Bind<_Functor(_Bound_args...)>
01131     : public _Weak_result_type<_Functor>
01132     {
01133       typedef _Bind __self_type;
01134       typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
01135     _Bound_indexes;
01136 
01137       _Functor _M_f;
01138       tuple<_Bound_args...> _M_bound_args;
01139 
01140       // Call unqualified
01141       template<typename _Result, typename... _Args, int... _Indexes>
01142     _Result
01143     __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>)
01144     {
01145       return _M_f(_Mu<_Bound_args>()
01146               (get<_Indexes>(_M_bound_args), __args)...);
01147     }
01148 
01149       // Call as const
01150       template<typename _Result, typename... _Args, int... _Indexes>
01151     _Result
01152     __call_c(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>) const
01153     {
01154       return _M_f(_Mu<_Bound_args>()
01155               (get<_Indexes>(_M_bound_args), __args)...);
01156     }
01157 
01158       // Call as volatile
01159       template<typename _Result, typename... _Args, int... _Indexes>
01160     _Result
01161     __call_v(tuple<_Args...>&& __args,
01162          _Index_tuple<_Indexes...>) volatile
01163     {
01164       return _M_f(_Mu<_Bound_args>()
01165               (__volget<_Indexes>(_M_bound_args), __args)...);
01166     }
01167 
01168       // Call as const volatile
01169       template<typename _Result, typename... _Args, int... _Indexes>
01170     _Result
01171     __call_c_v(tuple<_Args...>&& __args,
01172            _Index_tuple<_Indexes...>) const volatile
01173     {
01174       return _M_f(_Mu<_Bound_args>()
01175               (__volget<_Indexes>(_M_bound_args), __args)...);
01176     }
01177 
01178      public:
01179       template<typename... _Args>
01180     explicit _Bind(const _Functor& __f, _Args&&... __args)
01181     : _M_f(__f), _M_bound_args(std::forward<_Args>(__args)...)
01182     { }
01183 
01184       template<typename... _Args>
01185     explicit _Bind(_Functor&& __f, _Args&&... __args)
01186     : _M_f(std::move(__f)), _M_bound_args(std::forward<_Args>(__args)...)
01187     { }
01188 
01189       _Bind(const _Bind&) = default;
01190 
01191       _Bind(_Bind&& __b)
01192       : _M_f(std::move(__b._M_f)), _M_bound_args(std::move(__b._M_bound_args))
01193       { }
01194 
01195       // Call unqualified
01196       template<typename... _Args, typename _Result
01197     = decltype( std::declval<_Functor>()(
01198           _Mu<_Bound_args>()( std::declval<_Bound_args&>(),
01199                   std::declval<tuple<_Args...>&>() )... ) )>
01200     _Result
01201     operator()(_Args&&... __args)
01202     {
01203       return this->__call<_Result>(
01204           std::forward_as_tuple(std::forward<_Args>(__args)...),
01205           _Bound_indexes());
01206     }
01207 
01208       // Call as const
01209       template<typename... _Args, typename _Result
01210     = decltype( std::declval<const _Functor>()(
01211           _Mu<_Bound_args>()( std::declval<const _Bound_args&>(),
01212                   std::declval<tuple<_Args...>&>() )... ) )>
01213     _Result
01214     operator()(_Args&&... __args) const
01215     {
01216       return this->__call_c<_Result>(
01217           std::forward_as_tuple(std::forward<_Args>(__args)...),
01218           _Bound_indexes());
01219     }
01220 
01221       // Call as volatile
01222       template<typename... _Args, typename _Result
01223     = decltype( std::declval<volatile _Functor>()(
01224           _Mu<_Bound_args>()( std::declval<volatile _Bound_args&>(),
01225                   std::declval<tuple<_Args...>&>() )... ) )>
01226     _Result
01227     operator()(_Args&&... __args) volatile
01228     {
01229       return this->__call_v<_Result>(
01230           std::forward_as_tuple(std::forward<_Args>(__args)...),
01231           _Bound_indexes());
01232     }
01233 
01234       // Call as const volatile
01235       template<typename... _Args, typename _Result
01236     = decltype( std::declval<const volatile _Functor>()(
01237           _Mu<_Bound_args>()( std::declval<const volatile _Bound_args&>(),
01238                   std::declval<tuple<_Args...>&>() )... ) )>
01239     _Result
01240     operator()(_Args&&... __args) const volatile
01241     {
01242       return this->__call_c_v<_Result>(
01243           std::forward_as_tuple(std::forward<_Args>(__args)...),
01244           _Bound_indexes());
01245     }
01246     };
01247 
01248   /// Type of the function object returned from bind<R>().
01249   template<typename _Result, typename _Signature>
01250     struct _Bind_result;
01251 
01252   template<typename _Result, typename _Functor, typename... _Bound_args>
01253     class _Bind_result<_Result, _Functor(_Bound_args...)>
01254     {
01255       typedef _Bind_result __self_type;
01256       typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
01257     _Bound_indexes;
01258 
01259       _Functor _M_f;
01260       tuple<_Bound_args...> _M_bound_args;
01261 
01262       // sfinae types
01263       template<typename _Res>
01264     struct __enable_if_void : enable_if<is_void<_Res>::value, int> { };
01265       template<typename _Res>
01266     struct __disable_if_void : enable_if<!is_void<_Res>::value, int> { };
01267 
01268       // Call unqualified
01269       template<typename _Res, typename... _Args, int... _Indexes>
01270     _Result
01271     __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
01272         typename __disable_if_void<_Res>::type = 0)
01273     {
01274       return _M_f(_Mu<_Bound_args>()
01275               (get<_Indexes>(_M_bound_args), __args)...);
01276     }
01277 
01278       // Call unqualified, return void
01279       template<typename _Res, typename... _Args, int... _Indexes>
01280     void
01281     __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
01282         typename __enable_if_void<_Res>::type = 0)
01283     {
01284       _M_f(_Mu<_Bound_args>()
01285            (get<_Indexes>(_M_bound_args), __args)...);
01286     }
01287 
01288       // Call as const
01289       template<typename _Res, typename... _Args, int... _Indexes>
01290     _Result
01291     __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
01292         typename __disable_if_void<_Res>::type = 0) const
01293     {
01294       return _M_f(_Mu<_Bound_args>()
01295               (get<_Indexes>(_M_bound_args), __args)...);
01296     }
01297 
01298       // Call as const, return void
01299       template<typename _Res, typename... _Args, int... _Indexes>
01300     void
01301     __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
01302         typename __enable_if_void<_Res>::type = 0) const
01303     {
01304       _M_f(_Mu<_Bound_args>()
01305            (get<_Indexes>(_M_bound_args),  __args)...);
01306     }
01307 
01308       // Call as volatile
01309       template<typename _Res, typename... _Args, int... _Indexes>
01310     _Result
01311     __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
01312         typename __disable_if_void<_Res>::type = 0) volatile
01313     {
01314       return _M_f(_Mu<_Bound_args>()
01315               (__volget<_Indexes>(_M_bound_args), __args)...);
01316     }
01317 
01318       // Call as volatile, return void
01319       template<typename _Res, typename... _Args, int... _Indexes>
01320     void
01321     __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
01322         typename __enable_if_void<_Res>::type = 0) volatile
01323     {
01324       _M_f(_Mu<_Bound_args>()
01325            (__volget<_Indexes>(_M_bound_args), __args)...);
01326     }
01327 
01328       // Call as const volatile
01329       template<typename _Res, typename... _Args, int... _Indexes>
01330     _Result
01331     __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
01332         typename __disable_if_void<_Res>::type = 0) const volatile
01333     {
01334       return _M_f(_Mu<_Bound_args>()
01335               (__volget<_Indexes>(_M_bound_args), __args)...);
01336     }
01337 
01338       // Call as const volatile, return void
01339       template<typename _Res, typename... _Args, int... _Indexes>
01340     void
01341     __call(tuple<_Args...>&& __args,
01342            _Index_tuple<_Indexes...>,
01343         typename __enable_if_void<_Res>::type = 0) const volatile
01344     {
01345       _M_f(_Mu<_Bound_args>()
01346            (__volget<_Indexes>(_M_bound_args), __args)...);
01347     }
01348 
01349     public:
01350       typedef _Result result_type;
01351 
01352       template<typename... _Args>
01353     explicit _Bind_result(const _Functor& __f, _Args&&... __args)
01354     : _M_f(__f), _M_bound_args(std::forward<_Args>(__args)...)
01355     { }
01356 
01357       template<typename... _Args>
01358     explicit _Bind_result(_Functor&& __f, _Args&&... __args)
01359     : _M_f(std::move(__f)), _M_bound_args(std::forward<_Args>(__args)...)
01360     { }
01361 
01362       _Bind_result(const _Bind_result&) = default;
01363 
01364       _Bind_result(_Bind_result&& __b)
01365       : _M_f(std::move(__b._M_f)), _M_bound_args(std::move(__b._M_bound_args))
01366       { }
01367 
01368       // Call unqualified
01369       template<typename... _Args>
01370     result_type
01371     operator()(_Args&&... __args)
01372     {
01373       return this->__call<_Result>(
01374           std::forward_as_tuple(std::forward<_Args>(__args)...),
01375           _Bound_indexes());
01376     }
01377 
01378       // Call as const
01379       template<typename... _Args>
01380     result_type
01381     operator()(_Args&&... __args) const
01382     {
01383       return this->__call<_Result>(
01384           std::forward_as_tuple(std::forward<_Args>(__args)...),
01385           _Bound_indexes());
01386     }
01387 
01388       // Call as volatile
01389       template<typename... _Args>
01390     result_type
01391     operator()(_Args&&... __args) volatile
01392     {
01393       return this->__call<_Result>(
01394           std::forward_as_tuple(std::forward<_Args>(__args)...),
01395           _Bound_indexes());
01396     }
01397 
01398       // Call as const volatile
01399       template<typename... _Args>
01400     result_type
01401     operator()(_Args&&... __args) const volatile
01402     {
01403       return this->__call<_Result>(
01404           std::forward_as_tuple(std::forward<_Args>(__args)...),
01405           _Bound_indexes());
01406     }
01407     };
01408 
01409   /**
01410    *  @brief Class template _Bind is always a bind expression.
01411    *  @ingroup binders
01412    */
01413   template<typename _Signature>
01414     struct is_bind_expression<_Bind<_Signature> >
01415     : public true_type { };
01416 
01417   /**
01418    *  @brief Class template _Bind is always a bind expression.
01419    *  @ingroup binders
01420    */
01421   template<typename _Result, typename _Signature>
01422     struct is_bind_expression<_Bind_result<_Result, _Signature> >
01423     : public true_type { };
01424 
01425   template<typename _Functor, typename... _ArgTypes>
01426     struct _Bind_helper
01427     {
01428       typedef _Maybe_wrap_member_pointer<typename decay<_Functor>::type>
01429     __maybe_type;
01430       typedef typename __maybe_type::type __functor_type;
01431       typedef _Bind<__functor_type(typename decay<_ArgTypes>::type...)> type;
01432     };
01433 
01434   /**
01435    *  @brief Function template for std::bind.
01436    *  @ingroup binders
01437    */
01438   template<typename _Functor, typename... _ArgTypes>
01439     inline
01440     typename _Bind_helper<_Functor, _ArgTypes...>::type
01441     bind(_Functor&& __f, _ArgTypes&&... __args)
01442     {
01443       typedef _Bind_helper<_Functor, _ArgTypes...> __helper_type;
01444       typedef typename __helper_type::__maybe_type __maybe_type;
01445       typedef typename __helper_type::type __result_type;
01446       return __result_type(__maybe_type::__do_wrap(std::forward<_Functor>(__f)),
01447                std::forward<_ArgTypes>(__args)...);
01448     }
01449 
01450   template<typename _Result, typename _Functor, typename... _ArgTypes>
01451     struct _Bindres_helper
01452     {
01453       typedef _Maybe_wrap_member_pointer<typename decay<_Functor>::type>
01454     __maybe_type;
01455       typedef typename __maybe_type::type __functor_type;
01456       typedef _Bind_result<_Result,
01457                __functor_type(typename decay<_ArgTypes>::type...)>
01458     type;
01459     };
01460 
01461   /**
01462    *  @brief Function template for std::bind<R>.
01463    *  @ingroup binders
01464    */
01465   template<typename _Result, typename _Functor, typename... _ArgTypes>
01466     inline
01467     typename _Bindres_helper<_Result, _Functor, _ArgTypes...>::type
01468     bind(_Functor&& __f, _ArgTypes&&... __args)
01469     {
01470       typedef _Bindres_helper<_Result, _Functor, _ArgTypes...> __helper_type;
01471       typedef typename __helper_type::__maybe_type __maybe_type;
01472       typedef typename __helper_type::type __result_type;
01473       return __result_type(__maybe_type::__do_wrap(std::forward<_Functor>(__f)),
01474                std::forward<_ArgTypes>(__args)...);
01475     }
01476 
01477   /**
01478    *  @brief Exception class thrown when class template function's
01479    *  operator() is called with an empty target.
01480    *  @ingroup exceptions
01481    */
01482   class bad_function_call : public std::exception
01483   {
01484   public:
01485     virtual ~bad_function_call() throw();
01486   };
01487 
01488   /**
01489    *  Trait identifying "location-invariant" types, meaning that the
01490    *  address of the object (or any of its members) will not escape.
01491    *  Also implies a trivial copy constructor and assignment operator.
01492    */
01493   template<typename _Tp>
01494     struct __is_location_invariant
01495     : integral_constant<bool, (is_pointer<_Tp>::value
01496                    || is_member_pointer<_Tp>::value)>
01497     { };
01498 
01499   class _Undefined_class;
01500 
01501   union _Nocopy_types
01502   {
01503     void*       _M_object;
01504     const void* _M_const_object;
01505     void (*_M_function_pointer)();
01506     void (_Undefined_class::*_M_member_pointer)();
01507   };
01508 
01509   union _Any_data
01510   {
01511     void*       _M_access()       { return &_M_pod_data[0]; }
01512     const void* _M_access() const { return &_M_pod_data[0]; }
01513 
01514     template<typename _Tp>
01515       _Tp&
01516       _M_access()
01517       { return *static_cast<_Tp*>(_M_access()); }
01518 
01519     template<typename _Tp>
01520       const _Tp&
01521       _M_access() const
01522       { return *static_cast<const _Tp*>(_M_access()); }
01523 
01524     _Nocopy_types _M_unused;
01525     char _M_pod_data[sizeof(_Nocopy_types)];
01526   };
01527 
01528   enum _Manager_operation
01529   {
01530     __get_type_info,
01531     __get_functor_ptr,
01532     __clone_functor,
01533     __destroy_functor
01534   };
01535 
01536   // Simple type wrapper that helps avoid annoying const problems
01537   // when casting between void pointers and pointers-to-pointers.
01538   template<typename _Tp>
01539     struct _Simple_type_wrapper
01540     {
01541       _Simple_type_wrapper(_Tp __value) : __value(__value) { }
01542 
01543       _Tp __value;
01544     };
01545 
01546   template<typename _Tp>
01547     struct __is_location_invariant<_Simple_type_wrapper<_Tp> >
01548     : __is_location_invariant<_Tp>
01549     { };
01550 
01551   // Converts a reference to a function object into a callable
01552   // function object.
01553   template<typename _Functor>
01554     inline _Functor&
01555     __callable_functor(_Functor& __f)
01556     { return __f; }
01557 
01558   template<typename _Member, typename _Class>
01559     inline _Mem_fn<_Member _Class::*>
01560     __callable_functor(_Member _Class::* &__p)
01561     { return mem_fn(__p); }
01562 
01563   template<typename _Member, typename _Class>
01564     inline _Mem_fn<_Member _Class::*>
01565     __callable_functor(_Member _Class::* const &__p)
01566     { return mem_fn(__p); }
01567 
01568   template<typename _Signature>
01569     class function;
01570 
01571   /// Base class of all polymorphic function object wrappers.
01572   class _Function_base
01573   {
01574   public:
01575     static const std::size_t _M_max_size = sizeof(_Nocopy_types);
01576     static const std::size_t _M_max_align = __alignof__(_Nocopy_types);
01577 
01578     template<typename _Functor>
01579       class _Base_manager
01580       {
01581       protected:
01582     static const bool __stored_locally =
01583     (__is_location_invariant<_Functor>::value
01584      && sizeof(_Functor) <= _M_max_size
01585      && __alignof__(_Functor) <= _M_max_align
01586      && (_M_max_align % __alignof__(_Functor) == 0));
01587 
01588     typedef integral_constant<bool, __stored_locally> _Local_storage;
01589 
01590     // Retrieve a pointer to the function object
01591     static _Functor*
01592     _M_get_pointer(const _Any_data& __source)
01593     {
01594       const _Functor* __ptr =
01595         __stored_locally? std::__addressof(__source._M_access<_Functor>())
01596         /* have stored a pointer */ : __source._M_access<_Functor*>();
01597       return const_cast<_Functor*>(__ptr);
01598     }
01599 
01600     // Clone a location-invariant function object that fits within
01601     // an _Any_data structure.
01602     static void
01603     _M_clone(_Any_data& __dest, const _Any_data& __source, true_type)
01604     {
01605       new (__dest._M_access()) _Functor(__source._M_access<_Functor>());
01606     }
01607 
01608     // Clone a function object that is not location-invariant or
01609     // that cannot fit into an _Any_data structure.
01610     static void
01611     _M_clone(_Any_data& __dest, const _Any_data& __source, false_type)
01612     {
01613       __dest._M_access<_Functor*>() =
01614         new _Functor(*__source._M_access<_Functor*>());
01615     }
01616 
01617     // Destroying a location-invariant object may still require
01618     // destruction.
01619     static void
01620     _M_destroy(_Any_data& __victim, true_type)
01621     {
01622       __victim._M_access<_Functor>().~_Functor();
01623     }
01624 
01625     // Destroying an object located on the heap.
01626     static void
01627     _M_destroy(_Any_data& __victim, false_type)
01628     {
01629       delete __victim._M_access<_Functor*>();
01630     }
01631 
01632       public:
01633     static bool
01634     _M_manager(_Any_data& __dest, const _Any_data& __source,
01635            _Manager_operation __op)
01636     {
01637       switch (__op)
01638         {
01639 #ifdef __GXX_RTTI
01640         case __get_type_info:
01641           __dest._M_access<const type_info*>() = &typeid(_Functor);
01642           break;
01643 #endif
01644         case __get_functor_ptr:
01645           __dest._M_access<_Functor*>() = _M_get_pointer(__source);
01646           break;
01647 
01648         case __clone_functor:
01649           _M_clone(__dest, __source, _Local_storage());
01650           break;
01651 
01652         case __destroy_functor:
01653           _M_destroy(__dest, _Local_storage());
01654           break;
01655         }
01656       return false;
01657     }
01658 
01659     static void
01660     _M_init_functor(_Any_data& __functor, _Functor&& __f)
01661     { _M_init_functor(__functor, std::move(__f), _Local_storage()); }
01662 
01663     template<typename _Signature>
01664       static bool
01665       _M_not_empty_function(const function<_Signature>& __f)
01666       { return static_cast<bool>(__f); }
01667 
01668     template<typename _Tp>
01669       static bool
01670       _M_not_empty_function(const _Tp*& __fp)
01671       { return __fp; }
01672 
01673     template<typename _Class, typename _Tp>
01674       static bool
01675       _M_not_empty_function(_Tp _Class::* const& __mp)
01676       { return __mp; }
01677 
01678     template<typename _Tp>
01679       static bool
01680       _M_not_empty_function(const _Tp&)
01681       { return true; }
01682 
01683       private:
01684     static void
01685     _M_init_functor(_Any_data& __functor, _Functor&& __f, true_type)
01686     { new (__functor._M_access()) _Functor(std::move(__f)); }
01687 
01688     static void
01689     _M_init_functor(_Any_data& __functor, _Functor&& __f, false_type)
01690     { __functor._M_access<_Functor*>() = new _Functor(std::move(__f)); }
01691       };
01692 
01693     template<typename _Functor>
01694       class _Ref_manager : public _Base_manager<_Functor*>
01695       {
01696     typedef _Function_base::_Base_manager<_Functor*> _Base;
01697 
01698     public:
01699     static bool
01700     _M_manager(_Any_data& __dest, const _Any_data& __source,
01701            _Manager_operation __op)
01702     {
01703       switch (__op)
01704         {
01705 #ifdef __GXX_RTTI
01706         case __get_type_info:
01707           __dest._M_access<const type_info*>() = &typeid(_Functor);
01708           break;
01709 #endif
01710         case __get_functor_ptr:
01711           __dest._M_access<_Functor*>() = *_Base::_M_get_pointer(__source);
01712           return is_const<_Functor>::value;
01713           break;
01714 
01715         default:
01716           _Base::_M_manager(__dest, __source, __op);
01717         }
01718       return false;
01719     }
01720 
01721     static void
01722     _M_init_functor(_Any_data& __functor, reference_wrapper<_Functor> __f)
01723     {
01724       // TBD: Use address_of function instead.
01725       _Base::_M_init_functor(__functor, &__f.get());
01726     }
01727       };
01728 
01729     _Function_base() : _M_manager(0) { }
01730 
01731     ~_Function_base()
01732     {
01733       if (_M_manager)
01734     _M_manager(_M_functor, _M_functor, __destroy_functor);
01735     }
01736 
01737 
01738     bool _M_empty() const { return !_M_manager; }
01739 
01740     typedef bool (*_Manager_type)(_Any_data&, const _Any_data&,
01741                   _Manager_operation);
01742 
01743     _Any_data     _M_functor;
01744     _Manager_type _M_manager;
01745   };
01746 
01747   template<typename _Signature, typename _Functor>
01748     class _Function_handler;
01749 
01750   template<typename _Res, typename _Functor, typename... _ArgTypes>
01751     class _Function_handler<_Res(_ArgTypes...), _Functor>
01752     : public _Function_base::_Base_manager<_Functor>
01753     {
01754       typedef _Function_base::_Base_manager<_Functor> _Base;
01755 
01756     public:
01757       static _Res
01758       _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
01759       {
01760     return (*_Base::_M_get_pointer(__functor))(
01761         std::forward<_ArgTypes>(__args)...);
01762       }
01763     };
01764 
01765   template<typename _Functor, typename... _ArgTypes>
01766     class _Function_handler<void(_ArgTypes...), _Functor>
01767     : public _Function_base::_Base_manager<_Functor>
01768     {
01769       typedef _Function_base::_Base_manager<_Functor> _Base;
01770 
01771      public:
01772       static void
01773       _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
01774       {
01775     (*_Base::_M_get_pointer(__functor))(
01776         std::forward<_ArgTypes>(__args)...);
01777       }
01778     };
01779 
01780   template<typename _Res, typename _Functor, typename... _ArgTypes>
01781     class _Function_handler<_Res(_ArgTypes...), reference_wrapper<_Functor> >
01782     : public _Function_base::_Ref_manager<_Functor>
01783     {
01784       typedef _Function_base::_Ref_manager<_Functor> _Base;
01785 
01786      public:
01787       static _Res
01788       _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
01789       {
01790     return __callable_functor(**_Base::_M_get_pointer(__functor))(
01791           std::forward<_ArgTypes>(__args)...);
01792       }
01793     };
01794 
01795   template<typename _Functor, typename... _ArgTypes>
01796     class _Function_handler<void(_ArgTypes...), reference_wrapper<_Functor> >
01797     : public _Function_base::_Ref_manager<_Functor>
01798     {
01799       typedef _Function_base::_Ref_manager<_Functor> _Base;
01800 
01801      public:
01802       static void
01803       _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
01804       {
01805     __callable_functor(**_Base::_M_get_pointer(__functor))(
01806         std::forward<_ArgTypes>(__args)...);
01807       }
01808     };
01809 
01810   template<typename _Class, typename _Member, typename _Res,
01811        typename... _ArgTypes>
01812     class _Function_handler<_Res(_ArgTypes...), _Member _Class::*>
01813     : public _Function_handler<void(_ArgTypes...), _Member _Class::*>
01814     {
01815       typedef _Function_handler<void(_ArgTypes...), _Member _Class::*>
01816     _Base;
01817 
01818      public:
01819       static _Res
01820       _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
01821       {
01822     return mem_fn(_Base::_M_get_pointer(__functor)->__value)(
01823         std::forward<_ArgTypes>(__args)...);
01824       }
01825     };
01826 
01827   template<typename _Class, typename _Member, typename... _ArgTypes>
01828     class _Function_handler<void(_ArgTypes...), _Member _Class::*>
01829     : public _Function_base::_Base_manager<
01830          _Simple_type_wrapper< _Member _Class::* > >
01831     {
01832       typedef _Member _Class::* _Functor;
01833       typedef _Simple_type_wrapper<_Functor> _Wrapper;
01834       typedef _Function_base::_Base_manager<_Wrapper> _Base;
01835 
01836      public:
01837       static bool
01838       _M_manager(_Any_data& __dest, const _Any_data& __source,
01839          _Manager_operation __op)
01840       {
01841     switch (__op)
01842       {
01843 #ifdef __GXX_RTTI
01844       case __get_type_info:
01845         __dest._M_access<const type_info*>() = &typeid(_Functor);
01846         break;
01847 #endif
01848       case __get_functor_ptr:
01849         __dest._M_access<_Functor*>() =
01850           &_Base::_M_get_pointer(__source)->__value;
01851         break;
01852 
01853       default:
01854         _Base::_M_manager(__dest, __source, __op);
01855       }
01856     return false;
01857       }
01858 
01859       static void
01860       _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
01861       {
01862     mem_fn(_Base::_M_get_pointer(__functor)->__value)(
01863         std::forward<_ArgTypes>(__args)...);
01864       }
01865     };
01866 
01867   /**
01868    *  @brief Primary class template for std::function.
01869    *  @ingroup functors
01870    *
01871    *  Polymorphic function wrapper.
01872    */
01873   template<typename _Res, typename... _ArgTypes>
01874     class function<_Res(_ArgTypes...)>
01875     : public _Maybe_unary_or_binary_function<_Res, _ArgTypes...>,
01876       private _Function_base
01877     {
01878       typedef _Res _Signature_type(_ArgTypes...);
01879 
01880       struct _Useless { };
01881 
01882     public:
01883       typedef _Res result_type;
01884 
01885       // [3.7.2.1] construct/copy/destroy
01886 
01887       /**
01888        *  @brief Default construct creates an empty function call wrapper.
01889        *  @post @c !(bool)*this
01890        */
01891       function() : _Function_base() { }
01892 
01893       /**
01894        *  @brief Creates an empty function call wrapper.
01895        *  @post @c !(bool)*this
01896        */
01897       function(nullptr_t) : _Function_base() { }
01898 
01899       /**
01900        *  @brief %Function copy constructor.
01901        *  @param x A %function object with identical call signature.
01902        *  @post @c (bool)*this == (bool)x
01903        *
01904        *  The newly-created %function contains a copy of the target of @a
01905        *  x (if it has one).
01906        */
01907       function(const function& __x);
01908 
01909       /**
01910        *  @brief %Function move constructor.
01911        *  @param x A %function object rvalue with identical call signature.
01912        *
01913        *  The newly-created %function contains the target of @a x
01914        *  (if it has one).
01915        */
01916       function(function&& __x) : _Function_base()
01917       {
01918     __x.swap(*this);
01919       }
01920 
01921       // TODO: needs allocator_arg_t
01922 
01923       /**
01924        *  @brief Builds a %function that targets a copy of the incoming
01925        *  function object.
01926        *  @param f A %function object that is callable with parameters of
01927        *  type @c T1, @c T2, ..., @c TN and returns a value convertible
01928        *  to @c Res.
01929        *
01930        *  The newly-created %function object will target a copy of @a
01931        *  f. If @a f is @c reference_wrapper<F>, then this function
01932        *  object will contain a reference to the function object @c
01933        *  f.get(). If @a f is a NULL function pointer or NULL
01934        *  pointer-to-member, the newly-created object will be empty.
01935        *
01936        *  If @a f is a non-NULL function pointer or an object of type @c
01937        *  reference_wrapper<F>, this function will not throw.
01938        */
01939       template<typename _Functor>
01940     function(_Functor __f,
01941          typename enable_if<
01942                !is_integral<_Functor>::value, _Useless>::type
01943            = _Useless());
01944 
01945       /**
01946        *  @brief %Function assignment operator.
01947        *  @param x A %function with identical call signature.
01948        *  @post @c (bool)*this == (bool)x
01949        *  @returns @c *this
01950        *
01951        *  The target of @a x is copied to @c *this. If @a x has no
01952        *  target, then @c *this will be empty.
01953        *
01954        *  If @a x targets a function pointer or a reference to a function
01955        *  object, then this operation will not throw an %exception.
01956        */
01957       function&
01958       operator=(const function& __x)
01959       {
01960     function(__x).swap(*this);
01961     return *this;
01962       }
01963 
01964       /**
01965        *  @brief %Function move-assignment operator.
01966        *  @param x A %function rvalue with identical call signature.
01967        *  @returns @c *this
01968        *
01969        *  The target of @a x is moved to @c *this. If @a x has no
01970        *  target, then @c *this will be empty.
01971        *
01972        *  If @a x targets a function pointer or a reference to a function
01973        *  object, then this operation will not throw an %exception.
01974        */
01975       function&
01976       operator=(function&& __x)
01977       {
01978     function(std::move(__x)).swap(*this);
01979     return *this;
01980       }
01981 
01982       /**
01983        *  @brief %Function assignment to zero.
01984        *  @post @c !(bool)*this
01985        *  @returns @c *this
01986        *
01987        *  The target of @c *this is deallocated, leaving it empty.
01988        */
01989       function&
01990       operator=(nullptr_t)
01991       {
01992     if (_M_manager)
01993       {
01994         _M_manager(_M_functor, _M_functor, __destroy_functor);
01995         _M_manager = 0;
01996         _M_invoker = 0;
01997       }
01998     return *this;
01999       }
02000 
02001       /**
02002        *  @brief %Function assignment to a new target.
02003        *  @param f A %function object that is callable with parameters of
02004        *  type @c T1, @c T2, ..., @c TN and returns a value convertible
02005        *  to @c Res.
02006        *  @return @c *this
02007        *
02008        *  This  %function object wrapper will target a copy of @a
02009        *  f. If @a f is @c reference_wrapper<F>, then this function
02010        *  object will contain a reference to the function object @c
02011        *  f.get(). If @a f is a NULL function pointer or NULL
02012        *  pointer-to-member, @c this object will be empty.
02013        *
02014        *  If @a f is a non-NULL function pointer or an object of type @c
02015        *  reference_wrapper<F>, this function will not throw.
02016        */
02017       template<typename _Functor>
02018     typename enable_if<!is_integral<_Functor>::value, function&>::type
02019     operator=(_Functor&& __f)
02020     {
02021       function(std::forward<_Functor>(__f)).swap(*this);
02022       return *this;
02023     }
02024 
02025       /// @overload
02026       template<typename _Functor>
02027     typename enable_if<!is_integral<_Functor>::value, function&>::type
02028     operator=(reference_wrapper<_Functor> __f)
02029     {
02030       function(__f).swap(*this);
02031       return *this;
02032     }
02033 
02034       // [3.7.2.2] function modifiers
02035 
02036       /**
02037        *  @brief Swap the targets of two %function objects.
02038        *  @param f A %function with identical call signature.
02039        *
02040        *  Swap the targets of @c this function object and @a f. This
02041        *  function will not throw an %exception.
02042        */
02043       void swap(function& __x)
02044       {
02045     std::swap(_M_functor, __x._M_functor);
02046     std::swap(_M_manager, __x._M_manager);
02047     std::swap(_M_invoker, __x._M_invoker);
02048       }
02049 
02050       // TODO: needs allocator_arg_t
02051       /*
02052       template<typename _Functor, typename _Alloc>
02053     void
02054     assign(_Functor&& __f, const _Alloc& __a)
02055     {
02056       function(allocator_arg, __a,
02057            std::forward<_Functor>(__f)).swap(*this);
02058     }
02059       */
02060 
02061       // [3.7.2.3] function capacity
02062 
02063       /**
02064        *  @brief Determine if the %function wrapper has a target.
02065        *
02066        *  @return @c true when this %function object contains a target,
02067        *  or @c false when it is empty.
02068        *
02069        *  This function will not throw an %exception.
02070        */
02071       explicit operator bool() const
02072       { return !_M_empty(); }
02073 
02074       // [3.7.2.4] function invocation
02075 
02076       /**
02077        *  @brief Invokes the function targeted by @c *this.
02078        *  @returns the result of the target.
02079        *  @throws bad_function_call when @c !(bool)*this
02080        *
02081        *  The function call operator invokes the target function object
02082        *  stored by @c this.
02083        */
02084       _Res operator()(_ArgTypes... __args) const;
02085 
02086 #ifdef __GXX_RTTI
02087       // [3.7.2.5] function target access
02088       /**
02089        *  @brief Determine the type of the target of this function object
02090        *  wrapper.
02091        *
02092        *  @returns the type identifier of the target function object, or
02093        *  @c typeid(void) if @c !(bool)*this.
02094        *
02095        *  This function will not throw an %exception.
02096        */
02097       const type_info& target_type() const;
02098 
02099       /**
02100        *  @brief Access the stored target function object.
02101        *
02102        *  @return Returns a pointer to the stored target function object,
02103        *  if @c typeid(Functor).equals(target_type()); otherwise, a NULL
02104        *  pointer.
02105        *
02106        * This function will not throw an %exception.
02107        */
02108       template<typename _Functor>       _Functor* target();
02109 
02110       /// @overload
02111       template<typename _Functor> const _Functor* target() const;
02112 #endif
02113 
02114     private:
02115       typedef _Res (*_Invoker_type)(const _Any_data&, _ArgTypes...);
02116       _Invoker_type _M_invoker;
02117   };
02118 
02119   // Out-of-line member definitions.
02120   template<typename _Res, typename... _ArgTypes>
02121     function<_Res(_ArgTypes...)>::
02122     function(const function& __x)
02123     : _Function_base()
02124     {
02125       if (static_cast<bool>(__x))
02126     {
02127       _M_invoker = __x._M_invoker;
02128       _M_manager = __x._M_manager;
02129       __x._M_manager(_M_functor, __x._M_functor, __clone_functor);
02130     }
02131     }
02132 
02133   template<typename _Res, typename... _ArgTypes>
02134     template<typename _Functor>
02135       function<_Res(_ArgTypes...)>::
02136       function(_Functor __f,
02137            typename enable_if<
02138             !is_integral<_Functor>::value, _Useless>::type)
02139       : _Function_base()
02140       {
02141     typedef _Function_handler<_Signature_type, _Functor> _My_handler;
02142 
02143     if (_My_handler::_M_not_empty_function(__f))
02144       {
02145         _M_invoker = &_My_handler::_M_invoke;
02146         _M_manager = &_My_handler::_M_manager;
02147         _My_handler::_M_init_functor(_M_functor, std::move(__f));
02148       }
02149       }
02150 
02151   template<typename _Res, typename... _ArgTypes>
02152     _Res
02153     function<_Res(_ArgTypes...)>::
02154     operator()(_ArgTypes... __args) const
02155     {
02156       if (_M_empty())
02157     __throw_bad_function_call();
02158       return _M_invoker(_M_functor, std::forward<_ArgTypes>(__args)...);
02159     }
02160 
02161 #ifdef __GXX_RTTI
02162   template<typename _Res, typename... _ArgTypes>
02163     const type_info&
02164     function<_Res(_ArgTypes...)>::
02165     target_type() const
02166     {
02167       if (_M_manager)
02168     {
02169       _Any_data __typeinfo_result;
02170       _M_manager(__typeinfo_result, _M_functor, __get_type_info);
02171       return *__typeinfo_result._M_access<const type_info*>();
02172     }
02173       else
02174     return typeid(void);
02175     }
02176 
02177   template<typename _Res, typename... _ArgTypes>
02178     template<typename _Functor>
02179       _Functor*
02180       function<_Res(_ArgTypes...)>::
02181       target()
02182       {
02183     if (typeid(_Functor) == target_type() && _M_manager)
02184       {
02185         _Any_data __ptr;
02186         if (_M_manager(__ptr, _M_functor, __get_functor_ptr)
02187         && !is_const<_Functor>::value)
02188           return 0;
02189         else
02190           return __ptr._M_access<_Functor*>();
02191       }
02192     else
02193       return 0;
02194       }
02195 
02196   template<typename _Res, typename... _ArgTypes>
02197     template<typename _Functor>
02198       const _Functor*
02199       function<_Res(_ArgTypes...)>::
02200       target() const
02201       {
02202     if (typeid(_Functor) == target_type() && _M_manager)
02203       {
02204         _Any_data __ptr;
02205         _M_manager(__ptr, _M_functor, __get_functor_ptr);
02206         return __ptr._M_access<const _Functor*>();
02207       }
02208     else
02209       return 0;
02210       }
02211 #endif
02212 
02213   // [20.7.15.2.6] null pointer comparisons
02214 
02215   /**
02216    *  @brief Compares a polymorphic function object wrapper against 0
02217    *  (the NULL pointer).
02218    *  @returns @c true if the wrapper has no target, @c false otherwise
02219    *
02220    *  This function will not throw an %exception.
02221    */
02222   template<typename _Res, typename... _Args>
02223     inline bool
02224     operator==(const function<_Res(_Args...)>& __f, nullptr_t)
02225     { return !static_cast<bool>(__f); }
02226 
02227   /// @overload
02228   template<typename _Res, typename... _Args>
02229     inline bool
02230     operator==(nullptr_t, const function<_Res(_Args...)>& __f)
02231     { return !static_cast<bool>(__f); }
02232 
02233   /**
02234    *  @brief Compares a polymorphic function object wrapper against 0
02235    *  (the NULL pointer).
02236    *  @returns @c false if the wrapper has no target, @c true otherwise
02237    *
02238    *  This function will not throw an %exception.
02239    */
02240   template<typename _Res, typename... _Args>
02241     inline bool
02242     operator!=(const function<_Res(_Args...)>& __f, nullptr_t)
02243     { return static_cast<bool>(__f); }
02244 
02245   /// @overload
02246   template<typename _Res, typename... _Args>
02247     inline bool
02248     operator!=(nullptr_t, const function<_Res(_Args...)>& __f)
02249     { return static_cast<bool>(__f); }
02250 
02251   // [20.7.15.2.7] specialized algorithms
02252 
02253   /**
02254    *  @brief Swap the targets of two polymorphic function object wrappers.
02255    *
02256    *  This function will not throw an %exception.
02257    */
02258   template<typename _Res, typename... _Args>
02259     inline void
02260     swap(function<_Res(_Args...)>& __x, function<_Res(_Args...)>& __y)
02261     { __x.swap(__y); }
02262 
02263 _GLIBCXX_END_NAMESPACE_VERSION
02264 } // namespace std
02265 
02266 #endif // __GXX_EXPERIMENTAL_CXX0X__
02267 
02268 #endif // _GLIBCXX_FUNCTIONAL