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

Generated on 12 Mar 2010 for libstdc++ by  doxygen 1.6.1