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