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