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