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