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