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