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