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