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