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   /**
01479    *  Trait identifying "location-invariant" types, meaning that the
01480    *  address of the object (or any of its members) will not escape.
01481    *  Also implies a trivial copy constructor and assignment operator.
01482    */
01483   template<typename _Tp>
01484     struct __is_location_invariant
01485     : integral_constant<bool, (is_pointer<_Tp>::value
01486                    || is_member_pointer<_Tp>::value)>
01487     { };
01488 
01489   class _Undefined_class;
01490 
01491   union _Nocopy_types
01492   {
01493     void*       _M_object;
01494     const void* _M_const_object;
01495     void (*_M_function_pointer)();
01496     void (_Undefined_class::*_M_member_pointer)();
01497   };
01498 
01499   union _Any_data
01500   {
01501     void*       _M_access()       { return &_M_pod_data[0]; }
01502     const void* _M_access() const { return &_M_pod_data[0]; }
01503 
01504     template<typename _Tp>
01505       _Tp&
01506       _M_access()
01507       { return *static_cast<_Tp*>(_M_access()); }
01508 
01509     template<typename _Tp>
01510       const _Tp&
01511       _M_access() const
01512       { return *static_cast<const _Tp*>(_M_access()); }
01513 
01514     _Nocopy_types _M_unused;
01515     char _M_pod_data[sizeof(_Nocopy_types)];
01516   };
01517 
01518   enum _Manager_operation
01519   {
01520     __get_type_info,
01521     __get_functor_ptr,
01522     __clone_functor,
01523     __destroy_functor
01524   };
01525 
01526   // Simple type wrapper that helps avoid annoying const problems
01527   // when casting between void pointers and pointers-to-pointers.
01528   template<typename _Tp>
01529     struct _Simple_type_wrapper
01530     {
01531       _Simple_type_wrapper(_Tp __value) : __value(__value) { }
01532 
01533       _Tp __value;
01534     };
01535 
01536   template<typename _Tp>
01537     struct __is_location_invariant<_Simple_type_wrapper<_Tp> >
01538     : __is_location_invariant<_Tp>
01539     { };
01540 
01541   // Converts a reference to a function object into a callable
01542   // function object.
01543   template<typename _Functor>
01544     inline _Functor&
01545     __callable_functor(_Functor& __f)
01546     { return __f; }
01547 
01548   template<typename _Member, typename _Class>
01549     inline _Mem_fn<_Member _Class::*>
01550     __callable_functor(_Member _Class::* &__p)
01551     { return mem_fn(__p); }
01552 
01553   template<typename _Member, typename _Class>
01554     inline _Mem_fn<_Member _Class::*>
01555     __callable_functor(_Member _Class::* const &__p)
01556     { return mem_fn(__p); }
01557 
01558   template<typename _Signature>
01559     class function;
01560 
01561   /// Base class of all polymorphic function object wrappers.
01562   class _Function_base
01563   {
01564   public:
01565     static const std::size_t _M_max_size = sizeof(_Nocopy_types);
01566     static const std::size_t _M_max_align = __alignof__(_Nocopy_types);
01567 
01568     template<typename _Functor>
01569       class _Base_manager
01570       {
01571       protected:
01572     static const bool __stored_locally =
01573         (__is_location_invariant<_Functor>::value
01574          && sizeof(_Functor) <= _M_max_size
01575          && __alignof__(_Functor) <= _M_max_align
01576          && (_M_max_align % __alignof__(_Functor) == 0));
01577     
01578     typedef integral_constant<bool, __stored_locally> _Local_storage;
01579 
01580     // Retrieve a pointer to the function object
01581     static _Functor*
01582     _M_get_pointer(const _Any_data& __source)
01583     {
01584       const _Functor* __ptr =
01585         __stored_locally? &__source._M_access<_Functor>()
01586         /* have stored a pointer */ : __source._M_access<_Functor*>();
01587       return const_cast<_Functor*>(__ptr);
01588     }
01589 
01590     // Clone a location-invariant function object that fits within
01591     // an _Any_data structure.
01592     static void
01593     _M_clone(_Any_data& __dest, const _Any_data& __source, true_type)
01594     {
01595       new (__dest._M_access()) _Functor(__source._M_access<_Functor>());
01596     }
01597 
01598     // Clone a function object that is not location-invariant or
01599     // that cannot fit into an _Any_data structure.
01600     static void
01601     _M_clone(_Any_data& __dest, const _Any_data& __source, false_type)
01602     {
01603       __dest._M_access<_Functor*>() =
01604         new _Functor(*__source._M_access<_Functor*>());
01605     }
01606 
01607     // Destroying a location-invariant object may still require
01608     // destruction.
01609     static void
01610     _M_destroy(_Any_data& __victim, true_type)
01611     {
01612       __victim._M_access<_Functor>().~_Functor();
01613     }
01614     
01615     // Destroying an object located on the heap.
01616     static void
01617     _M_destroy(_Any_data& __victim, false_type)
01618     {
01619       delete __victim._M_access<_Functor*>();
01620     }
01621     
01622       public:
01623     static bool
01624     _M_manager(_Any_data& __dest, const _Any_data& __source,
01625            _Manager_operation __op)
01626     {
01627       switch (__op)
01628         {
01629 #ifdef __GXX_RTTI
01630         case __get_type_info:
01631           __dest._M_access<const type_info*>() = &typeid(_Functor);
01632           break;
01633 #endif
01634         case __get_functor_ptr:
01635           __dest._M_access<_Functor*>() = _M_get_pointer(__source);
01636           break;
01637           
01638         case __clone_functor:
01639           _M_clone(__dest, __source, _Local_storage());
01640           break;
01641 
01642         case __destroy_functor:
01643           _M_destroy(__dest, _Local_storage());
01644           break;
01645         }
01646       return false;
01647     }
01648 
01649     static void
01650     _M_init_functor(_Any_data& __functor, _Functor&& __f)
01651     { _M_init_functor(__functor, std::move(__f), _Local_storage()); }
01652     
01653     template<typename _Signature>
01654       static bool
01655       _M_not_empty_function(const function<_Signature>& __f)
01656           { return static_cast<bool>(__f); }
01657 
01658     template<typename _Tp>
01659       static bool
01660       _M_not_empty_function(const _Tp*& __fp)
01661       { return __fp; }
01662 
01663     template<typename _Class, typename _Tp>
01664       static bool
01665       _M_not_empty_function(_Tp _Class::* const& __mp)
01666       { return __mp; }
01667 
01668     template<typename _Tp>
01669       static bool
01670       _M_not_empty_function(const _Tp&)
01671       { return true; }
01672 
01673       private:
01674         static void
01675     _M_init_functor(_Any_data& __functor, _Functor&& __f, true_type)
01676     { new (__functor._M_access()) _Functor(std::move(__f)); }
01677 
01678     static void
01679     _M_init_functor(_Any_data& __functor, _Functor&& __f, false_type)
01680     { __functor._M_access<_Functor*>() = new _Functor(std::move(__f)); }
01681       };
01682 
01683     template<typename _Functor>
01684       class _Ref_manager : public _Base_manager<_Functor*>
01685       {
01686     typedef _Function_base::_Base_manager<_Functor*> _Base;
01687 
01688     public:
01689     static bool
01690     _M_manager(_Any_data& __dest, const _Any_data& __source,
01691            _Manager_operation __op)
01692     {
01693       switch (__op)
01694         {
01695 #ifdef __GXX_RTTI
01696         case __get_type_info:
01697           __dest._M_access<const type_info*>() = &typeid(_Functor);
01698           break;
01699 #endif
01700         case __get_functor_ptr:
01701           __dest._M_access<_Functor*>() = *_Base::_M_get_pointer(__source);
01702           return is_const<_Functor>::value;
01703           break;
01704           
01705         default:
01706           _Base::_M_manager(__dest, __source, __op);
01707         }
01708       return false;
01709     }
01710 
01711     static void
01712     _M_init_functor(_Any_data& __functor, reference_wrapper<_Functor> __f)
01713     {
01714       // TBD: Use address_of function instead.
01715       _Base::_M_init_functor(__functor, &__f.get());
01716     }
01717       };
01718 
01719     _Function_base() : _M_manager(0) { }
01720     
01721     ~_Function_base()
01722     {
01723       if (_M_manager)
01724     _M_manager(_M_functor, _M_functor, __destroy_functor);
01725     }
01726 
01727 
01728     bool _M_empty() const { return !_M_manager; }
01729 
01730     typedef bool (*_Manager_type)(_Any_data&, const _Any_data&,
01731                                   _Manager_operation);
01732 
01733     _Any_data     _M_functor;
01734     _Manager_type _M_manager;
01735   };
01736 
01737   template<typename _Signature, typename _Functor>
01738     class _Function_handler;
01739 
01740   template<typename _Res, typename _Functor, typename... _ArgTypes>
01741     class _Function_handler<_Res(_ArgTypes...), _Functor>
01742     : public _Function_base::_Base_manager<_Functor>
01743     {
01744       typedef _Function_base::_Base_manager<_Functor> _Base;
01745 
01746     public:
01747       static _Res
01748       _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
01749       {
01750         return (*_Base::_M_get_pointer(__functor))(
01751             std::forward<_ArgTypes>(__args)...);
01752       }
01753     };
01754 
01755   template<typename _Functor, typename... _ArgTypes>
01756     class _Function_handler<void(_ArgTypes...), _Functor>
01757     : public _Function_base::_Base_manager<_Functor>
01758     {
01759       typedef _Function_base::_Base_manager<_Functor> _Base;
01760 
01761      public:
01762       static void
01763       _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
01764       {
01765         (*_Base::_M_get_pointer(__functor))(
01766             std::forward<_ArgTypes>(__args)...);
01767       }
01768     };
01769 
01770   template<typename _Res, typename _Functor, typename... _ArgTypes>
01771     class _Function_handler<_Res(_ArgTypes...), reference_wrapper<_Functor> >
01772     : public _Function_base::_Ref_manager<_Functor>
01773     {
01774       typedef _Function_base::_Ref_manager<_Functor> _Base;
01775 
01776      public:
01777       static _Res
01778       _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
01779       {
01780         return __callable_functor(**_Base::_M_get_pointer(__functor))(
01781               std::forward<_ArgTypes>(__args)...);
01782       }
01783     };
01784 
01785   template<typename _Functor, typename... _ArgTypes>
01786     class _Function_handler<void(_ArgTypes...), reference_wrapper<_Functor> >
01787     : public _Function_base::_Ref_manager<_Functor>
01788     {
01789       typedef _Function_base::_Ref_manager<_Functor> _Base;
01790 
01791      public:
01792       static void
01793       _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
01794       {
01795         __callable_functor(**_Base::_M_get_pointer(__functor))(
01796             std::forward<_ArgTypes>(__args)...);
01797       }
01798     };
01799 
01800   template<typename _Class, typename _Member, typename _Res, 
01801            typename... _ArgTypes>
01802     class _Function_handler<_Res(_ArgTypes...), _Member _Class::*>
01803     : public _Function_handler<void(_ArgTypes...), _Member _Class::*>
01804     {
01805       typedef _Function_handler<void(_ArgTypes...), _Member _Class::*>
01806         _Base;
01807 
01808      public:
01809       static _Res
01810       _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
01811       {
01812         return mem_fn(_Base::_M_get_pointer(__functor)->__value)(
01813             std::forward<_ArgTypes>(__args)...);
01814       }
01815     };
01816 
01817   template<typename _Class, typename _Member, typename... _ArgTypes>
01818     class _Function_handler<void(_ArgTypes...), _Member _Class::*>
01819     : public _Function_base::_Base_manager<
01820                  _Simple_type_wrapper< _Member _Class::* > >
01821     {
01822       typedef _Member _Class::* _Functor;
01823       typedef _Simple_type_wrapper<_Functor> _Wrapper;
01824       typedef _Function_base::_Base_manager<_Wrapper> _Base;
01825 
01826      public:
01827       static bool
01828       _M_manager(_Any_data& __dest, const _Any_data& __source,
01829                  _Manager_operation __op)
01830       {
01831         switch (__op)
01832       {
01833 #ifdef __GXX_RTTI
01834       case __get_type_info:
01835         __dest._M_access<const type_info*>() = &typeid(_Functor);
01836         break;
01837 #endif      
01838       case __get_functor_ptr:
01839         __dest._M_access<_Functor*>() =
01840           &_Base::_M_get_pointer(__source)->__value;
01841         break;
01842         
01843       default:
01844         _Base::_M_manager(__dest, __source, __op);
01845       }
01846         return false;
01847       }
01848 
01849       static void
01850       _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
01851       {
01852     mem_fn(_Base::_M_get_pointer(__functor)->__value)(
01853             std::forward<_ArgTypes>(__args)...);
01854       }
01855     };
01856 
01857   /**
01858    *  @brief Primary class template for std::function.
01859    *  @ingroup functors
01860    *
01861    *  Polymorphic function wrapper.
01862    */
01863   template<typename _Res, typename... _ArgTypes>
01864     class function<_Res(_ArgTypes...)>
01865     : public _Maybe_unary_or_binary_function<_Res, _ArgTypes...>,
01866       private _Function_base
01867     {
01868       typedef _Res _Signature_type(_ArgTypes...);
01869       
01870       struct _Useless { };
01871       
01872     public:
01873       typedef _Res result_type;
01874       
01875       // [3.7.2.1] construct/copy/destroy
01876       
01877       /**
01878        *  @brief Default construct creates an empty function call wrapper.
01879        *  @post @c !(bool)*this
01880        */
01881       function() : _Function_base() { }
01882       
01883       /**
01884        *  @brief Creates an empty function call wrapper.
01885        *  @post @c !(bool)*this
01886        */
01887       function(nullptr_t) : _Function_base() { }
01888       
01889       /**
01890        *  @brief %Function copy constructor.
01891        *  @param x A %function object with identical call signature.
01892        *  @post @c (bool)*this == (bool)x
01893        *
01894        *  The newly-created %function contains a copy of the target of @a
01895        *  x (if it has one).
01896        */
01897       function(const function& __x);
01898 
01899       /**
01900        *  @brief %Function move constructor.
01901        *  @param x A %function object rvalue with identical call signature.
01902        *
01903        *  The newly-created %function contains the target of @a x
01904        *  (if it has one).
01905        */
01906       function(function&& __x) : _Function_base()
01907       {
01908         __x.swap(*this);
01909       }
01910 
01911       // TODO: needs allocator_arg_t
01912       
01913       /**
01914        *  @brief Builds a %function that targets a copy of the incoming
01915        *  function object.
01916        *  @param f A %function object that is callable with parameters of
01917        *  type @c T1, @c T2, ..., @c TN and returns a value convertible
01918        *  to @c Res.
01919        *
01920        *  The newly-created %function object will target a copy of @a
01921        *  f. If @a f is @c reference_wrapper<F>, then this function
01922        *  object will contain a reference to the function object @c
01923        *  f.get(). If @a f is a NULL function pointer or NULL
01924        *  pointer-to-member, the newly-created object will be empty.
01925        *
01926        *  If @a f is a non-NULL function pointer or an object of type @c
01927        *  reference_wrapper<F>, this function will not throw.
01928        */
01929       template<typename _Functor>
01930         function(_Functor __f,
01931                  typename enable_if<
01932                            !is_integral<_Functor>::value, _Useless>::type
01933                    = _Useless());
01934 
01935       /**
01936        *  @brief %Function assignment operator.
01937        *  @param x A %function with identical call signature.
01938        *  @post @c (bool)*this == (bool)x
01939        *  @returns @c *this
01940        *
01941        *  The target of @a x is copied to @c *this. If @a x has no
01942        *  target, then @c *this will be empty.
01943        *
01944        *  If @a x targets a function pointer or a reference to a function
01945        *  object, then this operation will not throw an %exception.
01946        */
01947       function&
01948       operator=(const function& __x)
01949       {
01950         function(__x).swap(*this);
01951         return *this;
01952       }
01953 
01954       /**
01955        *  @brief %Function move-assignment operator.
01956        *  @param x A %function rvalue with identical call signature.
01957        *  @returns @c *this
01958        *
01959        *  The target of @a x is moved to @c *this. If @a x has no
01960        *  target, then @c *this will be empty.
01961        *
01962        *  If @a x targets a function pointer or a reference to a function
01963        *  object, then this operation will not throw an %exception.
01964        */
01965       function&
01966       operator=(function&& __x)
01967       {
01968         function(std::move(__x)).swap(*this);
01969         return *this;
01970       }
01971 
01972       /**
01973        *  @brief %Function assignment to zero.
01974        *  @post @c !(bool)*this
01975        *  @returns @c *this
01976        *
01977        *  The target of @c *this is deallocated, leaving it empty.
01978        */
01979       function&
01980       operator=(nullptr_t)
01981       {
01982         if (_M_manager)
01983       {
01984         _M_manager(_M_functor, _M_functor, __destroy_functor);
01985         _M_manager = 0;
01986         _M_invoker = 0;
01987       }
01988         return *this;
01989       }
01990 
01991       /**
01992        *  @brief %Function assignment to a new target.
01993        *  @param f A %function object that is callable with parameters of
01994        *  type @c T1, @c T2, ..., @c TN and returns a value convertible
01995        *  to @c Res.
01996        *  @return @c *this
01997        *
01998        *  This  %function object wrapper will target a copy of @a
01999        *  f. If @a f is @c reference_wrapper<F>, then this function
02000        *  object will contain a reference to the function object @c
02001        *  f.get(). If @a f is a NULL function pointer or NULL
02002        *  pointer-to-member, @c this object will be empty.
02003        *
02004        *  If @a f is a non-NULL function pointer or an object of type @c
02005        *  reference_wrapper<F>, this function will not throw.
02006        */
02007       template<typename _Functor>
02008         typename enable_if<!is_integral<_Functor>::value, function&>::type
02009     operator=(_Functor&& __f)
02010     {
02011       function(std::forward<_Functor>(__f)).swap(*this);
02012       return *this;
02013     }
02014 
02015       /// @overload
02016       template<typename _Functor>
02017         typename enable_if<!is_integral<_Functor>::value, function&>::type
02018     operator=(reference_wrapper<_Functor> __f)
02019     {
02020       function(__f).swap(*this);
02021       return *this;
02022     }
02023 
02024       // [3.7.2.2] function modifiers
02025       
02026       /**
02027        *  @brief Swap the targets of two %function objects.
02028        *  @param f A %function with identical call signature.
02029        *
02030        *  Swap the targets of @c this function object and @a f. This
02031        *  function will not throw an %exception.
02032        */
02033       void swap(function& __x)
02034       {
02035     std::swap(_M_functor, __x._M_functor);
02036     std::swap(_M_manager, __x._M_manager);
02037     std::swap(_M_invoker, __x._M_invoker);
02038       }
02039 
02040       // TODO: needs allocator_arg_t
02041       /*
02042       template<typename _Functor, typename _Alloc>
02043         void
02044         assign(_Functor&& __f, const _Alloc& __a)
02045         {
02046           function(allocator_arg, __a,
02047                    std::forward<_Functor>(__f)).swap(*this);
02048         }
02049       */
02050       
02051       // [3.7.2.3] function capacity
02052 
02053       /**
02054        *  @brief Determine if the %function wrapper has a target.
02055        *
02056        *  @return @c true when this %function object contains a target,
02057        *  or @c false when it is empty.
02058        *
02059        *  This function will not throw an %exception.
02060        */
02061       explicit operator bool() const
02062       { return !_M_empty(); }
02063 
02064       // [3.7.2.4] function invocation
02065 
02066       /**
02067        *  @brief Invokes the function targeted by @c *this.
02068        *  @returns the result of the target.
02069        *  @throws bad_function_call when @c !(bool)*this
02070        *
02071        *  The function call operator invokes the target function object
02072        *  stored by @c this.
02073        */
02074       _Res operator()(_ArgTypes... __args) const;
02075 
02076 #ifdef __GXX_RTTI
02077       // [3.7.2.5] function target access
02078       /**
02079        *  @brief Determine the type of the target of this function object
02080        *  wrapper.
02081        *
02082        *  @returns the type identifier of the target function object, or
02083        *  @c typeid(void) if @c !(bool)*this.
02084        *
02085        *  This function will not throw an %exception.
02086        */
02087       const type_info& target_type() const;
02088       
02089       /**
02090        *  @brief Access the stored target function object.
02091        *
02092        *  @return Returns a pointer to the stored target function object,
02093        *  if @c typeid(Functor).equals(target_type()); otherwise, a NULL
02094        *  pointer.
02095        *
02096        * This function will not throw an %exception.
02097        */
02098       template<typename _Functor>       _Functor* target();
02099       
02100       /// @overload
02101       template<typename _Functor> const _Functor* target() const;
02102 #endif
02103 
02104     private:
02105       typedef _Res (*_Invoker_type)(const _Any_data&, _ArgTypes...);
02106       _Invoker_type _M_invoker;
02107   };
02108 
02109   // Out-of-line member definitions.
02110   template<typename _Res, typename... _ArgTypes>
02111     function<_Res(_ArgTypes...)>::
02112     function(const function& __x)
02113     : _Function_base()
02114     {
02115       if (static_cast<bool>(__x))
02116     {
02117       _M_invoker = __x._M_invoker;
02118       _M_manager = __x._M_manager;
02119       __x._M_manager(_M_functor, __x._M_functor, __clone_functor);
02120     }
02121     }
02122 
02123   template<typename _Res, typename... _ArgTypes>
02124     template<typename _Functor>
02125       function<_Res(_ArgTypes...)>::
02126       function(_Functor __f,
02127            typename enable_if<
02128                     !is_integral<_Functor>::value, _Useless>::type)
02129       : _Function_base()
02130       {
02131     typedef _Function_handler<_Signature_type, _Functor> _My_handler;
02132 
02133     if (_My_handler::_M_not_empty_function(__f))
02134       {
02135         _M_invoker = &_My_handler::_M_invoke;
02136         _M_manager = &_My_handler::_M_manager;
02137         _My_handler::_M_init_functor(_M_functor, std::move(__f));
02138       }
02139       }
02140 
02141   template<typename _Res, typename... _ArgTypes>
02142     _Res
02143     function<_Res(_ArgTypes...)>::
02144     operator()(_ArgTypes... __args) const
02145     {
02146       if (_M_empty())
02147         __throw_bad_function_call();
02148       return _M_invoker(_M_functor, std::forward<_ArgTypes>(__args)...);
02149     }
02150 
02151 #ifdef __GXX_RTTI
02152   template<typename _Res, typename... _ArgTypes>
02153     const type_info&
02154     function<_Res(_ArgTypes...)>::
02155     target_type() const
02156     {
02157       if (_M_manager)
02158         {
02159           _Any_data __typeinfo_result;
02160           _M_manager(__typeinfo_result, _M_functor, __get_type_info);
02161           return *__typeinfo_result._M_access<const type_info*>();
02162         }
02163       else
02164     return typeid(void);
02165     }
02166 
02167   template<typename _Res, typename... _ArgTypes>
02168     template<typename _Functor>
02169       _Functor*
02170       function<_Res(_ArgTypes...)>::
02171       target()
02172       {
02173     if (typeid(_Functor) == target_type() && _M_manager)
02174       {
02175         _Any_data __ptr;
02176         if (_M_manager(__ptr, _M_functor, __get_functor_ptr)
02177         && !is_const<_Functor>::value)
02178           return 0;
02179         else
02180           return __ptr._M_access<_Functor*>();
02181       }
02182     else
02183       return 0;
02184       }
02185 
02186   template<typename _Res, typename... _ArgTypes>
02187     template<typename _Functor>
02188       const _Functor*
02189       function<_Res(_ArgTypes...)>::
02190       target() const
02191       {
02192     if (typeid(_Functor) == target_type() && _M_manager)
02193       {
02194         _Any_data __ptr;
02195         _M_manager(__ptr, _M_functor, __get_functor_ptr);
02196         return __ptr._M_access<const _Functor*>();
02197       }
02198     else
02199       return 0;
02200       }
02201 #endif
02202 
02203   // [20.7.15.2.6] null pointer comparisons
02204 
02205   /**
02206    *  @brief Compares a polymorphic function object wrapper against 0
02207    *  (the NULL pointer).
02208    *  @returns @c true if the wrapper has no target, @c false otherwise
02209    *
02210    *  This function will not throw an %exception.
02211    */
02212   template<typename _Res, typename... _Args>
02213     inline bool
02214     operator==(const function<_Res(_Args...)>& __f, nullptr_t)
02215     { return !static_cast<bool>(__f); }
02216 
02217   /// @overload
02218   template<typename _Res, typename... _Args>
02219     inline bool
02220     operator==(nullptr_t, const function<_Res(_Args...)>& __f)
02221     { return !static_cast<bool>(__f); }
02222 
02223   /**
02224    *  @brief Compares a polymorphic function object wrapper against 0
02225    *  (the NULL pointer).
02226    *  @returns @c false if the wrapper has no target, @c true otherwise
02227    *
02228    *  This function will not throw an %exception.
02229    */
02230   template<typename _Res, typename... _Args>
02231     inline bool
02232     operator!=(const function<_Res(_Args...)>& __f, nullptr_t)
02233     { return static_cast<bool>(__f); }
02234 
02235   /// @overload
02236   template<typename _Res, typename... _Args>
02237     inline bool
02238     operator!=(nullptr_t, const function<_Res(_Args...)>& __f)
02239     { return static_cast<bool>(__f); }
02240 
02241   // [20.7.15.2.7] specialized algorithms
02242 
02243   /**
02244    *  @brief Swap the targets of two polymorphic function object wrappers.
02245    *
02246    *  This function will not throw an %exception.
02247    */
02248   template<typename _Res, typename... _Args>
02249     inline void
02250     swap(function<_Res(_Args...)>& __x, function<_Res(_Args...)>& __y)
02251     { __x.swap(__y); }
02252 
02253 _GLIBCXX_END_NAMESPACE_VERSION
02254 } // namespace std
02255 
02256 #endif // __GXX_EXPERIMENTAL_CXX0X__
02257 
02258 #endif // _GLIBCXX_FUNCTIONAL