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

Generated on 9 Feb 2010 for libstdc++ by  doxygen 1.6.1