PR libstdc++/64276
[official-gcc.git] / libstdc++-v3 / include / std / functional
blob5f76766490a7019fb631454166fb37b1df1ef931
1 // <functional> -*- C++ -*-
3 // Copyright (C) 2001-2014 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library.  This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
23 // <http://www.gnu.org/licenses/>.
26  * Copyright (c) 1997
27  * Silicon Graphics Computer Systems, Inc.
28  *
29  * Permission to use, copy, modify, distribute and sell this software
30  * and its documentation for any purpose is hereby granted without fee,
31  * provided that the above copyright notice appear in all copies and
32  * that both that copyright notice and this permission notice appear
33  * in supporting documentation.  Silicon Graphics makes no
34  * representations about the suitability of this software for any
35  * purpose.  It is provided "as is" without express or implied warranty.
36  *
37  */
39 /** @file include/functional
40  *  This is a Standard C++ Library header.
41  */
43 #ifndef _GLIBCXX_FUNCTIONAL
44 #define _GLIBCXX_FUNCTIONAL 1
46 #pragma GCC system_header
48 #include <bits/c++config.h>
49 #include <bits/stl_function.h>
51 #if __cplusplus >= 201103L
53 #include <typeinfo>
54 #include <new>
55 #include <tuple>
56 #include <type_traits>
57 #include <bits/functexcept.h>
58 #include <bits/functional_hash.h>
60 namespace std _GLIBCXX_VISIBILITY(default)
62 _GLIBCXX_BEGIN_NAMESPACE_VERSION
64   template<typename _MemberPointer>
65     class _Mem_fn;
66   template<typename _Tp, typename _Class>
67     _Mem_fn<_Tp _Class::*>
68     mem_fn(_Tp _Class::*) noexcept;
70   /// If we have found a result_type, extract it.
71   template<typename _Functor, typename = __void_t<>>
72     struct _Maybe_get_result_type
73     { };
75   template<typename _Functor>
76     struct _Maybe_get_result_type<_Functor,
77                                   __void_t<typename _Functor::result_type>>
78     { typedef typename _Functor::result_type result_type; };
80   /**
81    *  Base class for any function object that has a weak result type, as
82    *  defined in 20.8.2 [func.require] of C++11.
83   */
84   template<typename _Functor>
85     struct _Weak_result_type_impl
86     : _Maybe_get_result_type<_Functor>
87     { };
89   /// Retrieve the result type for a function type.
90   template<typename _Res, typename... _ArgTypes>
91     struct _Weak_result_type_impl<_Res(_ArgTypes...)>
92     { typedef _Res result_type; };
94   template<typename _Res, typename... _ArgTypes>
95     struct _Weak_result_type_impl<_Res(_ArgTypes......)>
96     { typedef _Res result_type; };
98   template<typename _Res, typename... _ArgTypes>
99     struct _Weak_result_type_impl<_Res(_ArgTypes...) const>
100     { typedef _Res result_type; };
102   template<typename _Res, typename... _ArgTypes>
103     struct _Weak_result_type_impl<_Res(_ArgTypes......) const>
104     { typedef _Res result_type; };
106   template<typename _Res, typename... _ArgTypes>
107     struct _Weak_result_type_impl<_Res(_ArgTypes...) volatile>
108     { typedef _Res result_type; };
110   template<typename _Res, typename... _ArgTypes>
111     struct _Weak_result_type_impl<_Res(_ArgTypes......) volatile>
112     { typedef _Res result_type; };
114   template<typename _Res, typename... _ArgTypes>
115     struct _Weak_result_type_impl<_Res(_ArgTypes...) const volatile>
116     { typedef _Res result_type; };
118   template<typename _Res, typename... _ArgTypes>
119     struct _Weak_result_type_impl<_Res(_ArgTypes......) const volatile>
120     { typedef _Res result_type; };
122   /// Retrieve the result type for a function reference.
123   template<typename _Res, typename... _ArgTypes>
124     struct _Weak_result_type_impl<_Res(&)(_ArgTypes...)>
125     { typedef _Res result_type; };
127   template<typename _Res, typename... _ArgTypes>
128     struct _Weak_result_type_impl<_Res(&)(_ArgTypes......)>
129     { typedef _Res result_type; };
131   /// Retrieve the result type for a function pointer.
132   template<typename _Res, typename... _ArgTypes>
133     struct _Weak_result_type_impl<_Res(*)(_ArgTypes...)>
134     { typedef _Res result_type; };
136   template<typename _Res, typename... _ArgTypes>
137     struct _Weak_result_type_impl<_Res(*)(_ArgTypes......)>
138     { typedef _Res result_type; };
140   /// Retrieve result type for a member function pointer.
141   template<typename _Res, typename _Class, typename... _ArgTypes>
142     struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...)>
143     { typedef _Res result_type; };
145   template<typename _Res, typename _Class, typename... _ArgTypes>
146     struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes......)>
147     { typedef _Res result_type; };
149   /// Retrieve result type for a const member function pointer.
150   template<typename _Res, typename _Class, typename... _ArgTypes>
151     struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) const>
152     { typedef _Res result_type; };
154   template<typename _Res, typename _Class, typename... _ArgTypes>
155     struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes......) const>
156     { typedef _Res result_type; };
158   /// Retrieve result type for a volatile member function pointer.
159   template<typename _Res, typename _Class, typename... _ArgTypes>
160     struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) volatile>
161     { typedef _Res result_type; };
163   template<typename _Res, typename _Class, typename... _ArgTypes>
164     struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes......) volatile>
165     { typedef _Res result_type; };
167   /// Retrieve result type for a const volatile member function pointer.
168   template<typename _Res, typename _Class, typename... _ArgTypes>
169     struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...)
170                                   const volatile>
171     { typedef _Res result_type; };
173   template<typename _Res, typename _Class, typename... _ArgTypes>
174     struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes......)
175                                   const volatile>
176     { typedef _Res result_type; };
178   /**
179    *  Strip top-level cv-qualifiers from the function object and let
180    *  _Weak_result_type_impl perform the real work.
181   */
182   template<typename _Functor>
183     struct _Weak_result_type
184     : _Weak_result_type_impl<typename remove_cv<_Functor>::type>
185     { };
187   /**
188    * Invoke a function object, which may be either a member pointer or a
189    * function object. The first parameter will tell which.
190    */
191   template<typename _Functor, typename... _Args>
192     inline
193     typename enable_if<
194              (!is_member_pointer<_Functor>::value
195               && !is_function<_Functor>::value
196               && !is_function<typename remove_pointer<_Functor>::type>::value),
197              typename result_of<_Functor&(_Args&&...)>::type
198            >::type
199     __invoke(_Functor& __f, _Args&&... __args)
200     {
201       return __f(std::forward<_Args>(__args)...);
202     }
204   template<typename _Functor, typename... _Args>
205     inline
206     typename enable_if<
207              (is_member_pointer<_Functor>::value
208               && !is_function<_Functor>::value
209               && !is_function<typename remove_pointer<_Functor>::type>::value),
210              typename result_of<_Functor(_Args&&...)>::type
211            >::type
212     __invoke(_Functor& __f, _Args&&... __args)
213     {
214       return std::mem_fn(__f)(std::forward<_Args>(__args)...);
215     }
217   // To pick up function references (that will become function pointers)
218   template<typename _Functor, typename... _Args>
219     inline
220     typename enable_if<
221              (is_pointer<_Functor>::value
222               && is_function<typename remove_pointer<_Functor>::type>::value),
223              typename result_of<_Functor(_Args&&...)>::type
224            >::type
225     __invoke(_Functor __f, _Args&&... __args)
226     {
227       return __f(std::forward<_Args>(__args)...);
228     }
230   /**
231    *  Knowing which of unary_function and binary_function _Tp derives
232    *  from, derives from the same and ensures that reference_wrapper
233    *  will have a weak result type. See cases below.
234    */
235   template<bool _Unary, bool _Binary, typename _Tp>
236     struct _Reference_wrapper_base_impl;
238   // None of the nested argument types.
239   template<typename _Tp>
240     struct _Reference_wrapper_base_impl<false, false, _Tp>
241     : _Weak_result_type<_Tp>
242     { };
244   // Nested argument_type only.
245   template<typename _Tp>
246     struct _Reference_wrapper_base_impl<true, false, _Tp>
247     : _Weak_result_type<_Tp>
248     {
249       typedef typename _Tp::argument_type argument_type;
250     };
252   // Nested first_argument_type and second_argument_type only.
253   template<typename _Tp>
254     struct _Reference_wrapper_base_impl<false, true, _Tp>
255     : _Weak_result_type<_Tp>
256     {
257       typedef typename _Tp::first_argument_type first_argument_type;
258       typedef typename _Tp::second_argument_type second_argument_type;
259     };
261   // All the nested argument types.
262    template<typename _Tp>
263     struct _Reference_wrapper_base_impl<true, true, _Tp>
264     : _Weak_result_type<_Tp>
265     {
266       typedef typename _Tp::argument_type argument_type;
267       typedef typename _Tp::first_argument_type first_argument_type;
268       typedef typename _Tp::second_argument_type second_argument_type;
269     };
271   _GLIBCXX_HAS_NESTED_TYPE(argument_type)
272   _GLIBCXX_HAS_NESTED_TYPE(first_argument_type)
273   _GLIBCXX_HAS_NESTED_TYPE(second_argument_type)
275   /**
276    *  Derives from unary_function or binary_function when it
277    *  can. Specializations handle all of the easy cases. The primary
278    *  template determines what to do with a class type, which may
279    *  derive from both unary_function and binary_function.
280   */
281   template<typename _Tp>
282     struct _Reference_wrapper_base
283     : _Reference_wrapper_base_impl<
284       __has_argument_type<_Tp>::value,
285       __has_first_argument_type<_Tp>::value
286       && __has_second_argument_type<_Tp>::value,
287       _Tp>
288     { };
290   // - a function type (unary)
291   template<typename _Res, typename _T1>
292     struct _Reference_wrapper_base<_Res(_T1)>
293     : unary_function<_T1, _Res>
294     { };
296   template<typename _Res, typename _T1>
297     struct _Reference_wrapper_base<_Res(_T1) const>
298     : unary_function<_T1, _Res>
299     { };
301   template<typename _Res, typename _T1>
302     struct _Reference_wrapper_base<_Res(_T1) volatile>
303     : unary_function<_T1, _Res>
304     { };
306   template<typename _Res, typename _T1>
307     struct _Reference_wrapper_base<_Res(_T1) const volatile>
308     : unary_function<_T1, _Res>
309     { };
311   // - a function type (binary)
312   template<typename _Res, typename _T1, typename _T2>
313     struct _Reference_wrapper_base<_Res(_T1, _T2)>
314     : binary_function<_T1, _T2, _Res>
315     { };
317   template<typename _Res, typename _T1, typename _T2>
318     struct _Reference_wrapper_base<_Res(_T1, _T2) const>
319     : binary_function<_T1, _T2, _Res>
320     { };
322   template<typename _Res, typename _T1, typename _T2>
323     struct _Reference_wrapper_base<_Res(_T1, _T2) volatile>
324     : binary_function<_T1, _T2, _Res>
325     { };
327   template<typename _Res, typename _T1, typename _T2>
328     struct _Reference_wrapper_base<_Res(_T1, _T2) const volatile>
329     : binary_function<_T1, _T2, _Res>
330     { };
332   // - a function pointer type (unary)
333   template<typename _Res, typename _T1>
334     struct _Reference_wrapper_base<_Res(*)(_T1)>
335     : unary_function<_T1, _Res>
336     { };
338   // - a function pointer type (binary)
339   template<typename _Res, typename _T1, typename _T2>
340     struct _Reference_wrapper_base<_Res(*)(_T1, _T2)>
341     : binary_function<_T1, _T2, _Res>
342     { };
344   // - a pointer to member function type (unary, no qualifiers)
345   template<typename _Res, typename _T1>
346     struct _Reference_wrapper_base<_Res (_T1::*)()>
347     : unary_function<_T1*, _Res>
348     { };
350   // - a pointer to member function type (binary, no qualifiers)
351   template<typename _Res, typename _T1, typename _T2>
352     struct _Reference_wrapper_base<_Res (_T1::*)(_T2)>
353     : binary_function<_T1*, _T2, _Res>
354     { };
356   // - a pointer to member function type (unary, const)
357   template<typename _Res, typename _T1>
358     struct _Reference_wrapper_base<_Res (_T1::*)() const>
359     : unary_function<const _T1*, _Res>
360     { };
362   // - a pointer to member function type (binary, const)
363   template<typename _Res, typename _T1, typename _T2>
364     struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const>
365     : binary_function<const _T1*, _T2, _Res>
366     { };
368   // - a pointer to member function type (unary, volatile)
369   template<typename _Res, typename _T1>
370     struct _Reference_wrapper_base<_Res (_T1::*)() volatile>
371     : unary_function<volatile _T1*, _Res>
372     { };
374   // - a pointer to member function type (binary, volatile)
375   template<typename _Res, typename _T1, typename _T2>
376     struct _Reference_wrapper_base<_Res (_T1::*)(_T2) volatile>
377     : binary_function<volatile _T1*, _T2, _Res>
378     { };
380   // - a pointer to member function type (unary, const volatile)
381   template<typename _Res, typename _T1>
382     struct _Reference_wrapper_base<_Res (_T1::*)() const volatile>
383     : unary_function<const volatile _T1*, _Res>
384     { };
386   // - a pointer to member function type (binary, const volatile)
387   template<typename _Res, typename _T1, typename _T2>
388     struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const volatile>
389     : binary_function<const volatile _T1*, _T2, _Res>
390     { };
392   /**
393    *  @brief Primary class template for reference_wrapper.
394    *  @ingroup functors
395    *  @{
396    */
397   template<typename _Tp>
398     class reference_wrapper
399     : public _Reference_wrapper_base<typename remove_cv<_Tp>::type>
400     {
401       _Tp* _M_data;
403     public:
404       typedef _Tp type;
406       reference_wrapper(_Tp& __indata) noexcept
407       : _M_data(std::__addressof(__indata))
408       { }
410       reference_wrapper(_Tp&&) = delete;
412       reference_wrapper(const reference_wrapper&) = default;
414       reference_wrapper&
415       operator=(const reference_wrapper&) = default;
417       operator _Tp&() const noexcept
418       { return this->get(); }
420       _Tp&
421       get() const noexcept
422       { return *_M_data; }
424       template<typename... _Args>
425         typename result_of<_Tp&(_Args&&...)>::type
426         operator()(_Args&&... __args) const
427         {
428           return __invoke(get(), std::forward<_Args>(__args)...);
429         }
430     };
433   /// Denotes a reference should be taken to a variable.
434   template<typename _Tp>
435     inline reference_wrapper<_Tp>
436     ref(_Tp& __t) noexcept
437     { return reference_wrapper<_Tp>(__t); }
439   /// Denotes a const reference should be taken to a variable.
440   template<typename _Tp>
441     inline reference_wrapper<const _Tp>
442     cref(const _Tp& __t) noexcept
443     { return reference_wrapper<const _Tp>(__t); }
445   template<typename _Tp>
446     void ref(const _Tp&&) = delete;
448   template<typename _Tp>
449     void cref(const _Tp&&) = delete;
451   /// Partial specialization.
452   template<typename _Tp>
453     inline reference_wrapper<_Tp>
454     ref(reference_wrapper<_Tp> __t) noexcept
455     { return ref(__t.get()); }
457   /// Partial specialization.
458   template<typename _Tp>
459     inline reference_wrapper<const _Tp>
460     cref(reference_wrapper<_Tp> __t) noexcept
461     { return cref(__t.get()); }
463   // @} group functors
465   template<typename... _Types>
466     struct _Pack : integral_constant<size_t, sizeof...(_Types)>
467     { };
469   template<typename _From, typename _To, bool = _From::value == _To::value>
470     struct _AllConvertible : false_type
471     { };
473   template<typename... _From, typename... _To>
474     struct _AllConvertible<_Pack<_From...>, _Pack<_To...>, true>
475     : __and_<is_convertible<_From, _To>...>
476     { };
478   template<typename _Tp1, typename _Tp2>
479     using _NotSame = __not_<is_same<typename std::decay<_Tp1>::type,
480                                     typename std::decay<_Tp2>::type>>;
482   /**
483    * Derives from @c unary_function or @c binary_function, or perhaps
484    * nothing, depending on the number of arguments provided. The
485    * primary template is the basis case, which derives nothing.
486    */
487   template<typename _Res, typename... _ArgTypes>
488     struct _Maybe_unary_or_binary_function { };
490   /// Derives from @c unary_function, as appropriate.
491   template<typename _Res, typename _T1>
492     struct _Maybe_unary_or_binary_function<_Res, _T1>
493     : std::unary_function<_T1, _Res> { };
495   /// Derives from @c binary_function, as appropriate.
496   template<typename _Res, typename _T1, typename _T2>
497     struct _Maybe_unary_or_binary_function<_Res, _T1, _T2>
498     : std::binary_function<_T1, _T2, _Res> { };
500   template<typename _Signature>
501     struct _Mem_fn_traits;
503   template<typename _Res, typename _Class, typename... _ArgTypes>
504     struct _Mem_fn_traits_base
505     {
506       using __result_type = _Res;
507       using __class_type =  _Class;
508       using __arg_types = _Pack<_ArgTypes...>;
509       using __maybe_type
510         = _Maybe_unary_or_binary_function<_Res, _Class*, _ArgTypes...>;
511       using __arity = integral_constant<size_t, sizeof...(_ArgTypes)>;
512     };
514 #define _GLIBCXX_MEM_FN_TRAITS2(_CV, _REF, _LVAL, _RVAL)                \
515   template<typename _Res, typename _Class, typename... _ArgTypes>       \
516     struct _Mem_fn_traits<_Res (_Class::*)(_ArgTypes...) _CV _REF>      \
517     : _Mem_fn_traits_base<_Res, _CV _Class, _ArgTypes...>               \
518     {                                                                   \
519       using __pmf_type  = _Res (_Class::*)(_ArgTypes...) _CV _REF;      \
520       using __lvalue = _LVAL;                                           \
521       using __rvalue = _RVAL;                                           \
522       using __vararg = false_type;                                      \
523     };                                                                  \
524   template<typename _Res, typename _Class, typename... _ArgTypes>       \
525     struct _Mem_fn_traits<_Res (_Class::*)(_ArgTypes... ...) _CV _REF>  \
526     : _Mem_fn_traits_base<_Res, _CV _Class, _ArgTypes...>               \
527     {                                                                   \
528       using __pmf_type  = _Res (_Class::*)(_ArgTypes... ...) _CV _REF;  \
529       using __lvalue = _LVAL;                                           \
530       using __rvalue = _RVAL;                                           \
531       using __vararg = true_type;                                       \
532     };
534 #define _GLIBCXX_MEM_FN_TRAITS(_REF, _LVAL, _RVAL)              \
535   _GLIBCXX_MEM_FN_TRAITS2(              , _REF, _LVAL, _RVAL)   \
536   _GLIBCXX_MEM_FN_TRAITS2(const         , _REF, _LVAL, _RVAL)   \
537   _GLIBCXX_MEM_FN_TRAITS2(volatile      , _REF, _LVAL, _RVAL)   \
538   _GLIBCXX_MEM_FN_TRAITS2(const volatile, _REF, _LVAL, _RVAL)
540 _GLIBCXX_MEM_FN_TRAITS( , true_type, true_type)
541 _GLIBCXX_MEM_FN_TRAITS(&, true_type, false_type)
542 _GLIBCXX_MEM_FN_TRAITS(&&, false_type, true_type)
544 #undef _GLIBCXX_MEM_FN_TRAITS
545 #undef _GLIBCXX_MEM_FN_TRAITS2
547   template<typename _MemFunPtr,
548            bool __is_mem_fn = is_member_function_pointer<_MemFunPtr>::value>
549     class _Mem_fn_base
550     : public _Mem_fn_traits<_MemFunPtr>::__maybe_type
551     {
552       using _Traits = _Mem_fn_traits<_MemFunPtr>;
554       using _Class = typename _Traits::__class_type;
555       using _ArgTypes = typename _Traits::__arg_types;
556       using _Pmf = typename _Traits::__pmf_type;
558       using _Arity = typename _Traits::__arity;
559       using _Varargs = typename _Traits::__vararg;
561       template<typename _Func, typename... _BoundArgs>
562         friend struct _Bind_check_arity;
564       // for varargs functions we just check the number of arguments,
565       // otherwise we also check they are convertible.
566       template<typename _Args>
567         using _CheckArgs = typename conditional<_Varargs::value,
568           __bool_constant<(_Args::value >= _ArgTypes::value)>,
569           _AllConvertible<_Args, _ArgTypes>
570         >::type;
572     public:
573       using result_type = typename _Traits::__result_type;
575       explicit _Mem_fn_base(_Pmf __pmf) : _M_pmf(__pmf) { }
577       // Handle objects
578       template<typename... _Args, typename _Req
579                = _Require<typename _Traits::__lvalue,
580                           _CheckArgs<_Pack<_Args...>>>>
581         result_type
582         operator()(_Class& __object, _Args&&... __args) const
583         { return (__object.*_M_pmf)(std::forward<_Args>(__args)...); }
585       template<typename... _Args, typename _Req
586                = _Require<typename _Traits::__rvalue,
587                           _CheckArgs<_Pack<_Args...>>>>
588         result_type
589         operator()(_Class&& __object, _Args&&... __args) const
590         {
591           return (std::move(__object).*_M_pmf)(std::forward<_Args>(__args)...);
592         }
594       // Handle pointers
595       template<typename... _Args, typename _Req
596                = _Require<typename _Traits::__lvalue,
597                           _CheckArgs<_Pack<_Args...>>>>
598         result_type
599         operator()(_Class* __object, _Args&&... __args) const
600         { return (__object->*_M_pmf)(std::forward<_Args>(__args)...); }
602       // Handle smart pointers, references and pointers to derived
603       template<typename _Tp, typename... _Args, typename _Req
604                = _Require<_NotSame<_Class, _Tp>, _NotSame<_Class*, _Tp>,
605                           _CheckArgs<_Pack<_Args...>>>>
606         result_type
607         operator()(_Tp&& __object, _Args&&... __args) const
608         {
609           return _M_call(std::forward<_Tp>(__object), &__object,
610               std::forward<_Args>(__args)...);
611         }
613       // Handle reference wrappers
614       template<typename _Tp, typename... _Args, typename _Req
615                = _Require<is_base_of<_Class, _Tp>, typename _Traits::__lvalue,
616                           _CheckArgs<_Pack<_Args...>>>>
617         result_type
618         operator()(reference_wrapper<_Tp> __ref, _Args&&... __args) const
619         { return operator()(__ref.get(), std::forward<_Args>(__args)...); }
621     private:
622       template<typename _Tp, typename... _Args>
623         result_type
624         _M_call(_Tp&& __object, const volatile _Class *,
625                 _Args&&... __args) const
626         {
627           return (std::forward<_Tp>(__object).*_M_pmf)
628             (std::forward<_Args>(__args)...);
629         }
631       template<typename _Tp, typename... _Args>
632         result_type
633         _M_call(_Tp&& __ptr, const volatile void *, _Args&&... __args) const
634         { return ((*__ptr).*_M_pmf)(std::forward<_Args>(__args)...); }
636       _Pmf _M_pmf;
637     };
639   // Partial specialization for member object pointers.
640   template<typename _Res, typename _Class>
641     class _Mem_fn_base<_Res _Class::*, false>
642     {
643       using __pm_type = _Res _Class::*;
645       // This bit of genius is due to Peter Dimov, improved slightly by
646       // Douglas Gregor.
647       // Made less elegant to support perfect forwarding and noexcept.
648       template<typename _Tp>
649         auto
650         _M_call(_Tp&& __object, const _Class *) const noexcept
651         -> decltype(std::forward<_Tp>(__object).*std::declval<__pm_type&>())
652         { return std::forward<_Tp>(__object).*_M_pm; }
654       template<typename _Tp, typename _Up>
655         auto
656         _M_call(_Tp&& __object, _Up * const *) const noexcept
657         -> decltype((*std::forward<_Tp>(__object)).*std::declval<__pm_type&>())
658         { return (*std::forward<_Tp>(__object)).*_M_pm; }
660       template<typename _Tp>
661         auto
662         _M_call(_Tp&& __ptr, const volatile void*) const
663         noexcept(noexcept((*__ptr).*std::declval<__pm_type&>()))
664         -> decltype((*__ptr).*std::declval<__pm_type&>())
665         { return (*__ptr).*_M_pm; }
667       using _Arity = integral_constant<size_t, 0>;
668       using _Varargs = false_type;
670       template<typename _Func, typename... _BoundArgs>
671         friend struct _Bind_check_arity;
673     public:
674       explicit
675       _Mem_fn_base(_Res _Class::*__pm) noexcept : _M_pm(__pm) { }
677       // Handle objects
678       _Res&
679       operator()(_Class& __object) const noexcept
680       { return __object.*_M_pm; }
682       const _Res&
683       operator()(const _Class& __object) const noexcept
684       { return __object.*_M_pm; }
686       _Res&&
687       operator()(_Class&& __object) const noexcept
688       { return std::forward<_Class>(__object).*_M_pm; }
690       const _Res&&
691       operator()(const _Class&& __object) const noexcept
692       { return std::forward<const _Class>(__object).*_M_pm; }
694       // Handle pointers
695       _Res&
696       operator()(_Class* __object) const noexcept
697       { return __object->*_M_pm; }
699       const _Res&
700       operator()(const _Class* __object) const noexcept
701       { return __object->*_M_pm; }
703       // Handle smart pointers and derived
704       template<typename _Tp, typename _Req = _Require<_NotSame<_Class*, _Tp>>>
705         auto
706         operator()(_Tp&& __unknown) const
707         noexcept(noexcept(std::declval<_Mem_fn_base*>()->_M_call
708                           (std::forward<_Tp>(__unknown), &__unknown)))
709         -> decltype(this->_M_call(std::forward<_Tp>(__unknown), &__unknown))
710         { return _M_call(std::forward<_Tp>(__unknown), &__unknown); }
712       template<typename _Tp, typename _Req = _Require<is_base_of<_Class, _Tp>>>
713         auto
714         operator()(reference_wrapper<_Tp> __ref) const
715         noexcept(noexcept(std::declval<_Mem_fn_base&>()(__ref.get())))
716         -> decltype((*this)(__ref.get()))
717         { return (*this)(__ref.get()); }
719     private:
720       _Res _Class::*_M_pm;
721     };
723   template<typename _Res, typename _Class>
724     struct _Mem_fn<_Res _Class::*>
725     : _Mem_fn_base<_Res _Class::*>
726     {
727       using _Mem_fn_base<_Res _Class::*>::_Mem_fn_base;
728     };
730   // _GLIBCXX_RESOLVE_LIB_DEFECTS
731   // 2048.  Unnecessary mem_fn overloads
732   /**
733    *  @brief Returns a function object that forwards to the member
734    *  pointer @a pm.
735    *  @ingroup functors
736    */
737   template<typename _Tp, typename _Class>
738     inline _Mem_fn<_Tp _Class::*>
739     mem_fn(_Tp _Class::* __pm) noexcept
740     {
741       return _Mem_fn<_Tp _Class::*>(__pm);
742     }
744   /**
745    *  @brief Determines if the given type _Tp is a function object
746    *  should be treated as a subexpression when evaluating calls to
747    *  function objects returned by bind(). [TR1 3.6.1]
748    *  @ingroup binders
749    */
750   template<typename _Tp>
751     struct is_bind_expression
752     : public false_type { };
754   /**
755    *  @brief Determines if the given type _Tp is a placeholder in a
756    *  bind() expression and, if so, which placeholder it is. [TR1 3.6.2]
757    *  @ingroup binders
758    */
759   template<typename _Tp>
760     struct is_placeholder
761     : public integral_constant<int, 0>
762     { };
764   /** @brief The type of placeholder objects defined by libstdc++.
765    *  @ingroup binders
766    */
767   template<int _Num> struct _Placeholder { };
769   _GLIBCXX_END_NAMESPACE_VERSION
771   /** @namespace std::placeholders
772    *  @brief ISO C++11 entities sub-namespace for functional.
773    *  @ingroup binders
774    */
775   namespace placeholders
776   {
777   _GLIBCXX_BEGIN_NAMESPACE_VERSION
778   /* Define a large number of placeholders. There is no way to
779    * simplify this with variadic templates, because we're introducing
780    * unique names for each.
781    */
782     extern const _Placeholder<1> _1;
783     extern const _Placeholder<2> _2;
784     extern const _Placeholder<3> _3;
785     extern const _Placeholder<4> _4;
786     extern const _Placeholder<5> _5;
787     extern const _Placeholder<6> _6;
788     extern const _Placeholder<7> _7;
789     extern const _Placeholder<8> _8;
790     extern const _Placeholder<9> _9;
791     extern const _Placeholder<10> _10;
792     extern const _Placeholder<11> _11;
793     extern const _Placeholder<12> _12;
794     extern const _Placeholder<13> _13;
795     extern const _Placeholder<14> _14;
796     extern const _Placeholder<15> _15;
797     extern const _Placeholder<16> _16;
798     extern const _Placeholder<17> _17;
799     extern const _Placeholder<18> _18;
800     extern const _Placeholder<19> _19;
801     extern const _Placeholder<20> _20;
802     extern const _Placeholder<21> _21;
803     extern const _Placeholder<22> _22;
804     extern const _Placeholder<23> _23;
805     extern const _Placeholder<24> _24;
806     extern const _Placeholder<25> _25;
807     extern const _Placeholder<26> _26;
808     extern const _Placeholder<27> _27;
809     extern const _Placeholder<28> _28;
810     extern const _Placeholder<29> _29;
811   _GLIBCXX_END_NAMESPACE_VERSION
812   }
814   _GLIBCXX_BEGIN_NAMESPACE_VERSION
816   /**
817    *  Partial specialization of is_placeholder that provides the placeholder
818    *  number for the placeholder objects defined by libstdc++.
819    *  @ingroup binders
820    */
821   template<int _Num>
822     struct is_placeholder<_Placeholder<_Num> >
823     : public integral_constant<int, _Num>
824     { };
826   template<int _Num>
827     struct is_placeholder<const _Placeholder<_Num> >
828     : public integral_constant<int, _Num>
829     { };
831   /**
832    * Used by _Safe_tuple_element to indicate that there is no tuple
833    * element at this position.
834    */
835   struct _No_tuple_element;
837   /**
838    * Implementation helper for _Safe_tuple_element. This primary
839    * template handles the case where it is safe to use @c
840    * tuple_element.
841    */
842   template<std::size_t __i, typename _Tuple, bool _IsSafe>
843     struct _Safe_tuple_element_impl
844     : tuple_element<__i, _Tuple> { };
846   /**
847    * Implementation helper for _Safe_tuple_element. This partial
848    * specialization handles the case where it is not safe to use @c
849    * tuple_element. We just return @c _No_tuple_element.
850    */
851   template<std::size_t __i, typename _Tuple>
852     struct _Safe_tuple_element_impl<__i, _Tuple, false>
853     {
854       typedef _No_tuple_element type;
855     };
857   /**
858    * Like tuple_element, but returns @c _No_tuple_element when
859    * tuple_element would return an error.
860    */
861  template<std::size_t __i, typename _Tuple>
862    struct _Safe_tuple_element
863    : _Safe_tuple_element_impl<__i, _Tuple,
864                               (__i < tuple_size<_Tuple>::value)>
865    { };
867   /**
868    *  Maps an argument to bind() into an actual argument to the bound
869    *  function object [TR1 3.6.3/5]. Only the first parameter should
870    *  be specified: the rest are used to determine among the various
871    *  implementations. Note that, although this class is a function
872    *  object, it isn't entirely normal because it takes only two
873    *  parameters regardless of the number of parameters passed to the
874    *  bind expression. The first parameter is the bound argument and
875    *  the second parameter is a tuple containing references to the
876    *  rest of the arguments.
877    */
878   template<typename _Arg,
879            bool _IsBindExp = is_bind_expression<_Arg>::value,
880            bool _IsPlaceholder = (is_placeholder<_Arg>::value > 0)>
881     class _Mu;
883   /**
884    *  If the argument is reference_wrapper<_Tp>, returns the
885    *  underlying reference. [TR1 3.6.3/5 bullet 1]
886    */
887   template<typename _Tp>
888     class _Mu<reference_wrapper<_Tp>, false, false>
889     {
890     public:
891       typedef _Tp& result_type;
893       /* Note: This won't actually work for const volatile
894        * reference_wrappers, because reference_wrapper::get() is const
895        * but not volatile-qualified. This might be a defect in the TR.
896        */
897       template<typename _CVRef, typename _Tuple>
898         result_type
899         operator()(_CVRef& __arg, _Tuple&) const volatile
900         { return __arg.get(); }
901     };
903   /**
904    *  If the argument is a bind expression, we invoke the underlying
905    *  function object with the same cv-qualifiers as we are given and
906    *  pass along all of our arguments (unwrapped). [TR1 3.6.3/5 bullet 2]
907    */
908   template<typename _Arg>
909     class _Mu<_Arg, true, false>
910     {
911     public:
912       template<typename _CVArg, typename... _Args>
913         auto
914         operator()(_CVArg& __arg,
915                    tuple<_Args...>& __tuple) const volatile
916         -> decltype(__arg(declval<_Args>()...))
917         {
918           // Construct an index tuple and forward to __call
919           typedef typename _Build_index_tuple<sizeof...(_Args)>::__type
920             _Indexes;
921           return this->__call(__arg, __tuple, _Indexes());
922         }
924     private:
925       // Invokes the underlying function object __arg by unpacking all
926       // of the arguments in the tuple.
927       template<typename _CVArg, typename... _Args, std::size_t... _Indexes>
928         auto
929         __call(_CVArg& __arg, tuple<_Args...>& __tuple,
930                const _Index_tuple<_Indexes...>&) const volatile
931         -> decltype(__arg(declval<_Args>()...))
932         {
933           return __arg(std::forward<_Args>(std::get<_Indexes>(__tuple))...);
934         }
935     };
937   /**
938    *  If the argument is a placeholder for the Nth argument, returns
939    *  a reference to the Nth argument to the bind function object.
940    *  [TR1 3.6.3/5 bullet 3]
941    */
942   template<typename _Arg>
943     class _Mu<_Arg, false, true>
944     {
945     public:
946       template<typename _Signature> class result;
948       template<typename _CVMu, typename _CVArg, typename _Tuple>
949         class result<_CVMu(_CVArg, _Tuple)>
950         {
951           // Add a reference, if it hasn't already been done for us.
952           // This allows us to be a little bit sloppy in constructing
953           // the tuple that we pass to result_of<...>.
954           typedef typename _Safe_tuple_element<(is_placeholder<_Arg>::value
955                                                 - 1), _Tuple>::type
956             __base_type;
958         public:
959           typedef typename add_rvalue_reference<__base_type>::type type;
960         };
962       template<typename _Tuple>
963         typename result<_Mu(_Arg, _Tuple)>::type
964         operator()(const volatile _Arg&, _Tuple& __tuple) const volatile
965         {
966           return std::forward<typename result<_Mu(_Arg, _Tuple)>::type>(
967               ::std::get<(is_placeholder<_Arg>::value - 1)>(__tuple));
968         }
969     };
971   /**
972    *  If the argument is just a value, returns a reference to that
973    *  value. The cv-qualifiers on the reference are the same as the
974    *  cv-qualifiers on the _Mu object. [TR1 3.6.3/5 bullet 4]
975    */
976   template<typename _Arg>
977     class _Mu<_Arg, false, false>
978     {
979     public:
980       template<typename _Signature> struct result;
982       template<typename _CVMu, typename _CVArg, typename _Tuple>
983         struct result<_CVMu(_CVArg, _Tuple)>
984         {
985           typedef typename add_lvalue_reference<_CVArg>::type type;
986         };
988       // Pick up the cv-qualifiers of the argument
989       template<typename _CVArg, typename _Tuple>
990         _CVArg&&
991         operator()(_CVArg&& __arg, _Tuple&) const volatile
992         { return std::forward<_CVArg>(__arg); }
993     };
995   /**
996    *  Maps member pointers into instances of _Mem_fn but leaves all
997    *  other function objects untouched. Used by std::bind(). The
998    *  primary template handles the non-member-pointer case.
999    */
1000   template<typename _Tp>
1001     struct _Maybe_wrap_member_pointer
1002     {
1003       typedef _Tp type;
1005       static const _Tp&
1006       __do_wrap(const _Tp& __x)
1007       { return __x; }
1009       static _Tp&&
1010       __do_wrap(_Tp&& __x)
1011       { return static_cast<_Tp&&>(__x); }
1012     };
1014   /**
1015    *  Maps member pointers into instances of _Mem_fn but leaves all
1016    *  other function objects untouched. Used by std::bind(). This
1017    *  partial specialization handles the member pointer case.
1018    */
1019   template<typename _Tp, typename _Class>
1020     struct _Maybe_wrap_member_pointer<_Tp _Class::*>
1021     {
1022       typedef _Mem_fn<_Tp _Class::*> type;
1024       static type
1025       __do_wrap(_Tp _Class::* __pm)
1026       { return type(__pm); }
1027     };
1029   // Specialization needed to prevent "forming reference to void" errors when
1030   // bind<void>() is called, because argument deduction instantiates
1031   // _Maybe_wrap_member_pointer<void> outside the immediate context where
1032   // SFINAE applies.
1033   template<>
1034     struct _Maybe_wrap_member_pointer<void>
1035     {
1036       typedef void type;
1037     };
1039   // std::get<I> for volatile-qualified tuples
1040   template<std::size_t _Ind, typename... _Tp>
1041     inline auto
1042     __volget(volatile tuple<_Tp...>& __tuple)
1043     -> __tuple_element_t<_Ind, tuple<_Tp...>> volatile&
1044     { return std::get<_Ind>(const_cast<tuple<_Tp...>&>(__tuple)); }
1046   // std::get<I> for const-volatile-qualified tuples
1047   template<std::size_t _Ind, typename... _Tp>
1048     inline auto
1049     __volget(const volatile tuple<_Tp...>& __tuple)
1050     -> __tuple_element_t<_Ind, tuple<_Tp...>> const volatile&
1051     { return std::get<_Ind>(const_cast<const tuple<_Tp...>&>(__tuple)); }
1053   /// Type of the function object returned from bind().
1054   template<typename _Signature>
1055     struct _Bind;
1057    template<typename _Functor, typename... _Bound_args>
1058     class _Bind<_Functor(_Bound_args...)>
1059     : public _Weak_result_type<_Functor>
1060     {
1061       typedef _Bind __self_type;
1062       typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
1063         _Bound_indexes;
1065       _Functor _M_f;
1066       tuple<_Bound_args...> _M_bound_args;
1068       // Call unqualified
1069       template<typename _Result, typename... _Args, std::size_t... _Indexes>
1070         _Result
1071         __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>)
1072         {
1073           return _M_f(_Mu<_Bound_args>()
1074                       (std::get<_Indexes>(_M_bound_args), __args)...);
1075         }
1077       // Call as const
1078       template<typename _Result, typename... _Args, std::size_t... _Indexes>
1079         _Result
1080         __call_c(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>) const
1081         {
1082           return _M_f(_Mu<_Bound_args>()
1083                       (std::get<_Indexes>(_M_bound_args), __args)...);
1084         }
1086       // Call as volatile
1087       template<typename _Result, typename... _Args, std::size_t... _Indexes>
1088         _Result
1089         __call_v(tuple<_Args...>&& __args,
1090                  _Index_tuple<_Indexes...>) volatile
1091         {
1092           return _M_f(_Mu<_Bound_args>()
1093                       (__volget<_Indexes>(_M_bound_args), __args)...);
1094         }
1096       // Call as const volatile
1097       template<typename _Result, typename... _Args, std::size_t... _Indexes>
1098         _Result
1099         __call_c_v(tuple<_Args...>&& __args,
1100                    _Index_tuple<_Indexes...>) const volatile
1101         {
1102           return _M_f(_Mu<_Bound_args>()
1103                       (__volget<_Indexes>(_M_bound_args), __args)...);
1104         }
1106      public:
1107       template<typename... _Args>
1108         explicit _Bind(const _Functor& __f, _Args&&... __args)
1109         : _M_f(__f), _M_bound_args(std::forward<_Args>(__args)...)
1110         { }
1112       template<typename... _Args>
1113         explicit _Bind(_Functor&& __f, _Args&&... __args)
1114         : _M_f(std::move(__f)), _M_bound_args(std::forward<_Args>(__args)...)
1115         { }
1117       _Bind(const _Bind&) = default;
1119       _Bind(_Bind&& __b)
1120       : _M_f(std::move(__b._M_f)), _M_bound_args(std::move(__b._M_bound_args))
1121       { }
1123       // Call unqualified
1124       template<typename... _Args, typename _Result
1125         = decltype( std::declval<_Functor>()(
1126               _Mu<_Bound_args>()( std::declval<_Bound_args&>(),
1127                                   std::declval<tuple<_Args...>&>() )... ) )>
1128         _Result
1129         operator()(_Args&&... __args)
1130         {
1131           return this->__call<_Result>(
1132               std::forward_as_tuple(std::forward<_Args>(__args)...),
1133               _Bound_indexes());
1134         }
1136       // Call as const
1137       template<typename... _Args, typename _Result
1138         = decltype( std::declval<typename enable_if<(sizeof...(_Args) >= 0),
1139                        typename add_const<_Functor>::type>::type>()(
1140               _Mu<_Bound_args>()( std::declval<const _Bound_args&>(),
1141                                   std::declval<tuple<_Args...>&>() )... ) )>
1142         _Result
1143         operator()(_Args&&... __args) const
1144         {
1145           return this->__call_c<_Result>(
1146               std::forward_as_tuple(std::forward<_Args>(__args)...),
1147               _Bound_indexes());
1148         }
1150       // Call as volatile
1151       template<typename... _Args, typename _Result
1152         = decltype( std::declval<typename enable_if<(sizeof...(_Args) >= 0),
1153                        typename add_volatile<_Functor>::type>::type>()(
1154               _Mu<_Bound_args>()( std::declval<volatile _Bound_args&>(),
1155                                   std::declval<tuple<_Args...>&>() )... ) )>
1156         _Result
1157         operator()(_Args&&... __args) volatile
1158         {
1159           return this->__call_v<_Result>(
1160               std::forward_as_tuple(std::forward<_Args>(__args)...),
1161               _Bound_indexes());
1162         }
1164       // Call as const volatile
1165       template<typename... _Args, typename _Result
1166         = decltype( std::declval<typename enable_if<(sizeof...(_Args) >= 0),
1167                        typename add_cv<_Functor>::type>::type>()(
1168               _Mu<_Bound_args>()( std::declval<const volatile _Bound_args&>(),
1169                                   std::declval<tuple<_Args...>&>() )... ) )>
1170         _Result
1171         operator()(_Args&&... __args) const volatile
1172         {
1173           return this->__call_c_v<_Result>(
1174               std::forward_as_tuple(std::forward<_Args>(__args)...),
1175               _Bound_indexes());
1176         }
1177     };
1179   /// Type of the function object returned from bind<R>().
1180   template<typename _Result, typename _Signature>
1181     struct _Bind_result;
1183   template<typename _Result, typename _Functor, typename... _Bound_args>
1184     class _Bind_result<_Result, _Functor(_Bound_args...)>
1185     {
1186       typedef _Bind_result __self_type;
1187       typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
1188         _Bound_indexes;
1190       _Functor _M_f;
1191       tuple<_Bound_args...> _M_bound_args;
1193       // sfinae types
1194       template<typename _Res>
1195         struct __enable_if_void : enable_if<is_void<_Res>::value, int> { };
1196       template<typename _Res>
1197         struct __disable_if_void : enable_if<!is_void<_Res>::value, int> { };
1199       // Call unqualified
1200       template<typename _Res, typename... _Args, std::size_t... _Indexes>
1201         _Result
1202         __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1203             typename __disable_if_void<_Res>::type = 0)
1204         {
1205           return _M_f(_Mu<_Bound_args>()
1206                       (std::get<_Indexes>(_M_bound_args), __args)...);
1207         }
1209       // Call unqualified, return void
1210       template<typename _Res, typename... _Args, std::size_t... _Indexes>
1211         void
1212         __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1213             typename __enable_if_void<_Res>::type = 0)
1214         {
1215           _M_f(_Mu<_Bound_args>()
1216                (std::get<_Indexes>(_M_bound_args), __args)...);
1217         }
1219       // Call as const
1220       template<typename _Res, typename... _Args, std::size_t... _Indexes>
1221         _Result
1222         __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1223             typename __disable_if_void<_Res>::type = 0) const
1224         {
1225           return _M_f(_Mu<_Bound_args>()
1226                       (std::get<_Indexes>(_M_bound_args), __args)...);
1227         }
1229       // Call as const, return void
1230       template<typename _Res, typename... _Args, std::size_t... _Indexes>
1231         void
1232         __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1233             typename __enable_if_void<_Res>::type = 0) const
1234         {
1235           _M_f(_Mu<_Bound_args>()
1236                (std::get<_Indexes>(_M_bound_args),  __args)...);
1237         }
1239       // Call as volatile
1240       template<typename _Res, typename... _Args, std::size_t... _Indexes>
1241         _Result
1242         __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1243             typename __disable_if_void<_Res>::type = 0) volatile
1244         {
1245           return _M_f(_Mu<_Bound_args>()
1246                       (__volget<_Indexes>(_M_bound_args), __args)...);
1247         }
1249       // Call as volatile, return void
1250       template<typename _Res, typename... _Args, std::size_t... _Indexes>
1251         void
1252         __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1253             typename __enable_if_void<_Res>::type = 0) volatile
1254         {
1255           _M_f(_Mu<_Bound_args>()
1256                (__volget<_Indexes>(_M_bound_args), __args)...);
1257         }
1259       // Call as const volatile
1260       template<typename _Res, typename... _Args, std::size_t... _Indexes>
1261         _Result
1262         __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1263             typename __disable_if_void<_Res>::type = 0) const volatile
1264         {
1265           return _M_f(_Mu<_Bound_args>()
1266                       (__volget<_Indexes>(_M_bound_args), __args)...);
1267         }
1269       // Call as const volatile, return void
1270       template<typename _Res, typename... _Args, std::size_t... _Indexes>
1271         void
1272         __call(tuple<_Args...>&& __args,
1273                _Index_tuple<_Indexes...>,
1274             typename __enable_if_void<_Res>::type = 0) const volatile
1275         {
1276           _M_f(_Mu<_Bound_args>()
1277                (__volget<_Indexes>(_M_bound_args), __args)...);
1278         }
1280     public:
1281       typedef _Result result_type;
1283       template<typename... _Args>
1284         explicit _Bind_result(const _Functor& __f, _Args&&... __args)
1285         : _M_f(__f), _M_bound_args(std::forward<_Args>(__args)...)
1286         { }
1288       template<typename... _Args>
1289         explicit _Bind_result(_Functor&& __f, _Args&&... __args)
1290         : _M_f(std::move(__f)), _M_bound_args(std::forward<_Args>(__args)...)
1291         { }
1293       _Bind_result(const _Bind_result&) = default;
1295       _Bind_result(_Bind_result&& __b)
1296       : _M_f(std::move(__b._M_f)), _M_bound_args(std::move(__b._M_bound_args))
1297       { }
1299       // Call unqualified
1300       template<typename... _Args>
1301         result_type
1302         operator()(_Args&&... __args)
1303         {
1304           return this->__call<_Result>(
1305               std::forward_as_tuple(std::forward<_Args>(__args)...),
1306               _Bound_indexes());
1307         }
1309       // Call as const
1310       template<typename... _Args>
1311         result_type
1312         operator()(_Args&&... __args) const
1313         {
1314           return this->__call<_Result>(
1315               std::forward_as_tuple(std::forward<_Args>(__args)...),
1316               _Bound_indexes());
1317         }
1319       // Call as volatile
1320       template<typename... _Args>
1321         result_type
1322         operator()(_Args&&... __args) volatile
1323         {
1324           return this->__call<_Result>(
1325               std::forward_as_tuple(std::forward<_Args>(__args)...),
1326               _Bound_indexes());
1327         }
1329       // Call as const volatile
1330       template<typename... _Args>
1331         result_type
1332         operator()(_Args&&... __args) const volatile
1333         {
1334           return this->__call<_Result>(
1335               std::forward_as_tuple(std::forward<_Args>(__args)...),
1336               _Bound_indexes());
1337         }
1338     };
1340   /**
1341    *  @brief Class template _Bind is always a bind expression.
1342    *  @ingroup binders
1343    */
1344   template<typename _Signature>
1345     struct is_bind_expression<_Bind<_Signature> >
1346     : public true_type { };
1348   /**
1349    *  @brief Class template _Bind is always a bind expression.
1350    *  @ingroup binders
1351    */
1352   template<typename _Signature>
1353     struct is_bind_expression<const _Bind<_Signature> >
1354     : public true_type { };
1356   /**
1357    *  @brief Class template _Bind is always a bind expression.
1358    *  @ingroup binders
1359    */
1360   template<typename _Signature>
1361     struct is_bind_expression<volatile _Bind<_Signature> >
1362     : public true_type { };
1364   /**
1365    *  @brief Class template _Bind is always a bind expression.
1366    *  @ingroup binders
1367    */
1368   template<typename _Signature>
1369     struct is_bind_expression<const volatile _Bind<_Signature>>
1370     : public true_type { };
1372   /**
1373    *  @brief Class template _Bind_result is always a bind expression.
1374    *  @ingroup binders
1375    */
1376   template<typename _Result, typename _Signature>
1377     struct is_bind_expression<_Bind_result<_Result, _Signature>>
1378     : public true_type { };
1380   /**
1381    *  @brief Class template _Bind_result is always a bind expression.
1382    *  @ingroup binders
1383    */
1384   template<typename _Result, typename _Signature>
1385     struct is_bind_expression<const _Bind_result<_Result, _Signature>>
1386     : public true_type { };
1388   /**
1389    *  @brief Class template _Bind_result is always a bind expression.
1390    *  @ingroup binders
1391    */
1392   template<typename _Result, typename _Signature>
1393     struct is_bind_expression<volatile _Bind_result<_Result, _Signature>>
1394     : public true_type { };
1396   /**
1397    *  @brief Class template _Bind_result is always a bind expression.
1398    *  @ingroup binders
1399    */
1400   template<typename _Result, typename _Signature>
1401     struct is_bind_expression<const volatile _Bind_result<_Result, _Signature>>
1402     : public true_type { };
1404   template<typename _Func, typename... _BoundArgs>
1405     struct _Bind_check_arity { };
1407   template<typename _Ret, typename... _Args, typename... _BoundArgs>
1408     struct _Bind_check_arity<_Ret (*)(_Args...), _BoundArgs...>
1409     {
1410       static_assert(sizeof...(_BoundArgs) == sizeof...(_Args),
1411                    "Wrong number of arguments for function");
1412     };
1414   template<typename _Ret, typename... _Args, typename... _BoundArgs>
1415     struct _Bind_check_arity<_Ret (*)(_Args......), _BoundArgs...>
1416     {
1417       static_assert(sizeof...(_BoundArgs) >= sizeof...(_Args),
1418                    "Wrong number of arguments for function");
1419     };
1421   template<typename _Tp, typename _Class, typename... _BoundArgs>
1422     struct _Bind_check_arity<_Tp _Class::*, _BoundArgs...>
1423     {
1424       using _Arity = typename _Mem_fn<_Tp _Class::*>::_Arity;
1425       using _Varargs = typename _Mem_fn<_Tp _Class::*>::_Varargs;
1426       static_assert(_Varargs::value
1427                     ? sizeof...(_BoundArgs) >= _Arity::value + 1
1428                     : sizeof...(_BoundArgs) == _Arity::value + 1,
1429                     "Wrong number of arguments for pointer-to-member");
1430     };
1432   // Trait type used to remove std::bind() from overload set via SFINAE
1433   // when first argument has integer type, so that std::bind() will
1434   // not be a better match than ::bind() from the BSD Sockets API.
1435   template<typename _Tp, typename _Tp2 = typename decay<_Tp>::type>
1436     using __is_socketlike = __or_<is_integral<_Tp2>, is_enum<_Tp2>>;
1438   template<bool _SocketLike, typename _Func, typename... _BoundArgs>
1439     struct _Bind_helper
1440     : _Bind_check_arity<typename decay<_Func>::type, _BoundArgs...>
1441     {
1442       typedef _Maybe_wrap_member_pointer<typename decay<_Func>::type>
1443         __maybe_type;
1444       typedef typename __maybe_type::type __func_type;
1445       typedef _Bind<__func_type(typename decay<_BoundArgs>::type...)> type;
1446     };
1448   // Partial specialization for is_socketlike == true, does not define
1449   // nested type so std::bind() will not participate in overload resolution
1450   // when the first argument might be a socket file descriptor.
1451   template<typename _Func, typename... _BoundArgs>
1452     struct _Bind_helper<true, _Func, _BoundArgs...>
1453     { };
1455   /**
1456    *  @brief Function template for std::bind.
1457    *  @ingroup binders
1458    */
1459   template<typename _Func, typename... _BoundArgs>
1460     inline typename
1461     _Bind_helper<__is_socketlike<_Func>::value, _Func, _BoundArgs...>::type
1462     bind(_Func&& __f, _BoundArgs&&... __args)
1463     {
1464       typedef _Bind_helper<false, _Func, _BoundArgs...> __helper_type;
1465       typedef typename __helper_type::__maybe_type __maybe_type;
1466       typedef typename __helper_type::type __result_type;
1467       return __result_type(__maybe_type::__do_wrap(std::forward<_Func>(__f)),
1468                            std::forward<_BoundArgs>(__args)...);
1469     }
1471   template<typename _Result, typename _Func, typename... _BoundArgs>
1472     struct _Bindres_helper
1473     : _Bind_check_arity<typename decay<_Func>::type, _BoundArgs...>
1474     {
1475       typedef _Maybe_wrap_member_pointer<typename decay<_Func>::type>
1476         __maybe_type;
1477       typedef typename __maybe_type::type __functor_type;
1478       typedef _Bind_result<_Result,
1479                            __functor_type(typename decay<_BoundArgs>::type...)>
1480         type;
1481     };
1483   /**
1484    *  @brief Function template for std::bind<R>.
1485    *  @ingroup binders
1486    */
1487   template<typename _Result, typename _Func, typename... _BoundArgs>
1488     inline
1489     typename _Bindres_helper<_Result, _Func, _BoundArgs...>::type
1490     bind(_Func&& __f, _BoundArgs&&... __args)
1491     {
1492       typedef _Bindres_helper<_Result, _Func, _BoundArgs...> __helper_type;
1493       typedef typename __helper_type::__maybe_type __maybe_type;
1494       typedef typename __helper_type::type __result_type;
1495       return __result_type(__maybe_type::__do_wrap(std::forward<_Func>(__f)),
1496                            std::forward<_BoundArgs>(__args)...);
1497     }
1499   template<typename _Signature>
1500     struct _Bind_simple;
1502   template<typename _Callable, typename... _Args>
1503     struct _Bind_simple<_Callable(_Args...)>
1504     {
1505       typedef typename result_of<_Callable(_Args...)>::type result_type;
1507       template<typename _Tp, typename... _Up>
1508         explicit
1509         _Bind_simple(_Tp&& __f, _Up&&... __args)
1510         : _M_bound(std::forward<_Tp>(__f), std::forward<_Up>(__args)...)
1511         { }
1513       _Bind_simple(const _Bind_simple&) = default;
1514       _Bind_simple(_Bind_simple&&) = default;
1516       result_type
1517       operator()()
1518       {
1519         typedef typename _Build_index_tuple<sizeof...(_Args)>::__type _Indices;
1520         return _M_invoke(_Indices());
1521       }
1523     private:
1524       template<std::size_t... _Indices>
1525         typename result_of<_Callable(_Args...)>::type
1526         _M_invoke(_Index_tuple<_Indices...>)
1527         {
1528           // std::bind always forwards bound arguments as lvalues,
1529           // but this type can call functions which only accept rvalues.
1530           return std::forward<_Callable>(std::get<0>(_M_bound))(
1531               std::forward<_Args>(std::get<_Indices+1>(_M_bound))...);
1532         }
1534       std::tuple<_Callable, _Args...> _M_bound;
1535     };
1537   template<typename _Func, typename... _BoundArgs>
1538     struct _Bind_simple_helper
1539     : _Bind_check_arity<typename decay<_Func>::type, _BoundArgs...>
1540     {
1541       typedef _Maybe_wrap_member_pointer<typename decay<_Func>::type>
1542         __maybe_type;
1543       typedef typename __maybe_type::type __func_type;
1544       typedef _Bind_simple<__func_type(typename decay<_BoundArgs>::type...)>
1545         __type;
1546     };
1548   // Simplified version of std::bind for internal use, without support for
1549   // unbound arguments, placeholders or nested bind expressions.
1550   template<typename _Callable, typename... _Args>
1551     typename _Bind_simple_helper<_Callable, _Args...>::__type
1552     __bind_simple(_Callable&& __callable, _Args&&... __args)
1553     {
1554       typedef _Bind_simple_helper<_Callable, _Args...> __helper_type;
1555       typedef typename __helper_type::__maybe_type __maybe_type;
1556       typedef typename __helper_type::__type __result_type;
1557       return __result_type(
1558           __maybe_type::__do_wrap( std::forward<_Callable>(__callable)),
1559           std::forward<_Args>(__args)...);
1560     }
1562   /**
1563    *  @brief Exception class thrown when class template function's
1564    *  operator() is called with an empty target.
1565    *  @ingroup exceptions
1566    */
1567   class bad_function_call : public std::exception
1568   {
1569   public:
1570     virtual ~bad_function_call() noexcept;
1572     const char* what() const noexcept;
1573   };
1575   /**
1576    *  Trait identifying "location-invariant" types, meaning that the
1577    *  address of the object (or any of its members) will not escape.
1578    *  Trivially copyable types are location-invariant and users can
1579    *  specialize this trait for other types.
1580    */
1581   template<typename _Tp>
1582     struct __is_location_invariant
1583     : is_trivially_copyable<_Tp>::type
1584     { };
1586   class _Undefined_class;
1588   union _Nocopy_types
1589   {
1590     void*       _M_object;
1591     const void* _M_const_object;
1592     void (*_M_function_pointer)();
1593     void (_Undefined_class::*_M_member_pointer)();
1594   };
1596   union _Any_data
1597   {
1598     void*       _M_access()       { return &_M_pod_data[0]; }
1599     const void* _M_access() const { return &_M_pod_data[0]; }
1601     template<typename _Tp>
1602       _Tp&
1603       _M_access()
1604       { return *static_cast<_Tp*>(_M_access()); }
1606     template<typename _Tp>
1607       const _Tp&
1608       _M_access() const
1609       { return *static_cast<const _Tp*>(_M_access()); }
1611     _Nocopy_types _M_unused;
1612     char _M_pod_data[sizeof(_Nocopy_types)];
1613   };
1615   enum _Manager_operation
1616   {
1617     __get_type_info,
1618     __get_functor_ptr,
1619     __clone_functor,
1620     __destroy_functor
1621   };
1623   // Simple type wrapper that helps avoid annoying const problems
1624   // when casting between void pointers and pointers-to-pointers.
1625   template<typename _Tp>
1626     struct _Simple_type_wrapper
1627     {
1628       _Simple_type_wrapper(_Tp __value) : __value(__value) { }
1630       _Tp __value;
1631     };
1633   template<typename _Tp>
1634     struct __is_location_invariant<_Simple_type_wrapper<_Tp> >
1635     : __is_location_invariant<_Tp>
1636     { };
1638   // Converts a reference to a function object into a callable
1639   // function object.
1640   template<typename _Functor>
1641     inline _Functor&
1642     __callable_functor(_Functor& __f)
1643     { return __f; }
1645   template<typename _Member, typename _Class>
1646     inline _Mem_fn<_Member _Class::*>
1647     __callable_functor(_Member _Class::* &__p)
1648     { return std::mem_fn(__p); }
1650   template<typename _Member, typename _Class>
1651     inline _Mem_fn<_Member _Class::*>
1652     __callable_functor(_Member _Class::* const &__p)
1653     { return std::mem_fn(__p); }
1655   template<typename _Member, typename _Class>
1656     inline _Mem_fn<_Member _Class::*>
1657     __callable_functor(_Member _Class::* volatile &__p)
1658     { return std::mem_fn(__p); }
1660   template<typename _Member, typename _Class>
1661     inline _Mem_fn<_Member _Class::*>
1662     __callable_functor(_Member _Class::* const volatile &__p)
1663     { return std::mem_fn(__p); }
1665   template<typename _Signature>
1666     class function;
1668   /// Base class of all polymorphic function object wrappers.
1669   class _Function_base
1670   {
1671   public:
1672     static const std::size_t _M_max_size = sizeof(_Nocopy_types);
1673     static const std::size_t _M_max_align = __alignof__(_Nocopy_types);
1675     template<typename _Functor>
1676       class _Base_manager
1677       {
1678       protected:
1679         static const bool __stored_locally =
1680         (__is_location_invariant<_Functor>::value
1681          && sizeof(_Functor) <= _M_max_size
1682          && __alignof__(_Functor) <= _M_max_align
1683          && (_M_max_align % __alignof__(_Functor) == 0));
1685         typedef integral_constant<bool, __stored_locally> _Local_storage;
1687         // Retrieve a pointer to the function object
1688         static _Functor*
1689         _M_get_pointer(const _Any_data& __source)
1690         {
1691           const _Functor* __ptr =
1692             __stored_locally? std::__addressof(__source._M_access<_Functor>())
1693             /* have stored a pointer */ : __source._M_access<_Functor*>();
1694           return const_cast<_Functor*>(__ptr);
1695         }
1697         // Clone a location-invariant function object that fits within
1698         // an _Any_data structure.
1699         static void
1700         _M_clone(_Any_data& __dest, const _Any_data& __source, true_type)
1701         {
1702           new (__dest._M_access()) _Functor(__source._M_access<_Functor>());
1703         }
1705         // Clone a function object that is not location-invariant or
1706         // that cannot fit into an _Any_data structure.
1707         static void
1708         _M_clone(_Any_data& __dest, const _Any_data& __source, false_type)
1709         {
1710           __dest._M_access<_Functor*>() =
1711             new _Functor(*__source._M_access<_Functor*>());
1712         }
1714         // Destroying a location-invariant object may still require
1715         // destruction.
1716         static void
1717         _M_destroy(_Any_data& __victim, true_type)
1718         {
1719           __victim._M_access<_Functor>().~_Functor();
1720         }
1722         // Destroying an object located on the heap.
1723         static void
1724         _M_destroy(_Any_data& __victim, false_type)
1725         {
1726           delete __victim._M_access<_Functor*>();
1727         }
1729       public:
1730         static bool
1731         _M_manager(_Any_data& __dest, const _Any_data& __source,
1732                    _Manager_operation __op)
1733         {
1734           switch (__op)
1735             {
1736 #if __cpp_rtti
1737             case __get_type_info:
1738               __dest._M_access<const type_info*>() = &typeid(_Functor);
1739               break;
1740 #endif
1741             case __get_functor_ptr:
1742               __dest._M_access<_Functor*>() = _M_get_pointer(__source);
1743               break;
1745             case __clone_functor:
1746               _M_clone(__dest, __source, _Local_storage());
1747               break;
1749             case __destroy_functor:
1750               _M_destroy(__dest, _Local_storage());
1751               break;
1752             }
1753           return false;
1754         }
1756         static void
1757         _M_init_functor(_Any_data& __functor, _Functor&& __f)
1758         { _M_init_functor(__functor, std::move(__f), _Local_storage()); }
1760         template<typename _Signature>
1761           static bool
1762           _M_not_empty_function(const function<_Signature>& __f)
1763           { return static_cast<bool>(__f); }
1765         template<typename _Tp>
1766           static bool
1767           _M_not_empty_function(_Tp* const& __fp)
1768           { return __fp; }
1770         template<typename _Class, typename _Tp>
1771           static bool
1772           _M_not_empty_function(_Tp _Class::* const& __mp)
1773           { return __mp; }
1775         template<typename _Tp>
1776           static bool
1777           _M_not_empty_function(const _Tp&)
1778           { return true; }
1780       private:
1781         static void
1782         _M_init_functor(_Any_data& __functor, _Functor&& __f, true_type)
1783         { new (__functor._M_access()) _Functor(std::move(__f)); }
1785         static void
1786         _M_init_functor(_Any_data& __functor, _Functor&& __f, false_type)
1787         { __functor._M_access<_Functor*>() = new _Functor(std::move(__f)); }
1788       };
1790     template<typename _Functor>
1791       class _Ref_manager : public _Base_manager<_Functor*>
1792       {
1793         typedef _Function_base::_Base_manager<_Functor*> _Base;
1795       public:
1796         static bool
1797         _M_manager(_Any_data& __dest, const _Any_data& __source,
1798                    _Manager_operation __op)
1799         {
1800           switch (__op)
1801             {
1802 #if __cpp_rtti
1803             case __get_type_info:
1804               __dest._M_access<const type_info*>() = &typeid(_Functor);
1805               break;
1806 #endif
1807             case __get_functor_ptr:
1808               __dest._M_access<_Functor*>() = *_Base::_M_get_pointer(__source);
1809               return is_const<_Functor>::value;
1810               break;
1812             default:
1813               _Base::_M_manager(__dest, __source, __op);
1814             }
1815           return false;
1816         }
1818         static void
1819         _M_init_functor(_Any_data& __functor, reference_wrapper<_Functor> __f)
1820         {
1821           _Base::_M_init_functor(__functor, std::__addressof(__f.get()));
1822         }
1823       };
1825     _Function_base() : _M_manager(nullptr) { }
1827     ~_Function_base()
1828     {
1829       if (_M_manager)
1830         _M_manager(_M_functor, _M_functor, __destroy_functor);
1831     }
1834     bool _M_empty() const { return !_M_manager; }
1836     typedef bool (*_Manager_type)(_Any_data&, const _Any_data&,
1837                                   _Manager_operation);
1839     _Any_data     _M_functor;
1840     _Manager_type _M_manager;
1841   };
1843   template<typename _Signature, typename _Functor>
1844     class _Function_handler;
1846   template<typename _Res, typename _Functor, typename... _ArgTypes>
1847     class _Function_handler<_Res(_ArgTypes...), _Functor>
1848     : public _Function_base::_Base_manager<_Functor>
1849     {
1850       typedef _Function_base::_Base_manager<_Functor> _Base;
1852     public:
1853       static _Res
1854       _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args)
1855       {
1856         return (*_Base::_M_get_pointer(__functor))(
1857             std::forward<_ArgTypes>(__args)...);
1858       }
1859     };
1861   template<typename _Functor, typename... _ArgTypes>
1862     class _Function_handler<void(_ArgTypes...), _Functor>
1863     : public _Function_base::_Base_manager<_Functor>
1864     {
1865       typedef _Function_base::_Base_manager<_Functor> _Base;
1867      public:
1868       static void
1869       _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args)
1870       {
1871         (*_Base::_M_get_pointer(__functor))(
1872             std::forward<_ArgTypes>(__args)...);
1873       }
1874     };
1876   template<typename _Res, typename _Functor, typename... _ArgTypes>
1877     class _Function_handler<_Res(_ArgTypes...), reference_wrapper<_Functor> >
1878     : public _Function_base::_Ref_manager<_Functor>
1879     {
1880       typedef _Function_base::_Ref_manager<_Functor> _Base;
1882      public:
1883       static _Res
1884       _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args)
1885       {
1886         return __callable_functor(**_Base::_M_get_pointer(__functor))(
1887               std::forward<_ArgTypes>(__args)...);
1888       }
1889     };
1891   template<typename _Functor, typename... _ArgTypes>
1892     class _Function_handler<void(_ArgTypes...), reference_wrapper<_Functor> >
1893     : public _Function_base::_Ref_manager<_Functor>
1894     {
1895       typedef _Function_base::_Ref_manager<_Functor> _Base;
1897      public:
1898       static void
1899       _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args)
1900       {
1901         __callable_functor(**_Base::_M_get_pointer(__functor))(
1902             std::forward<_ArgTypes>(__args)...);
1903       }
1904     };
1906   template<typename _Class, typename _Member, typename _Res,
1907            typename... _ArgTypes>
1908     class _Function_handler<_Res(_ArgTypes...), _Member _Class::*>
1909     : public _Function_handler<void(_ArgTypes...), _Member _Class::*>
1910     {
1911       typedef _Function_handler<void(_ArgTypes...), _Member _Class::*>
1912         _Base;
1914      public:
1915       static _Res
1916       _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args)
1917       {
1918         return std::mem_fn(_Base::_M_get_pointer(__functor)->__value)(
1919             std::forward<_ArgTypes>(__args)...);
1920       }
1921     };
1923   template<typename _Class, typename _Member, typename... _ArgTypes>
1924     class _Function_handler<void(_ArgTypes...), _Member _Class::*>
1925     : public _Function_base::_Base_manager<
1926                  _Simple_type_wrapper< _Member _Class::* > >
1927     {
1928       typedef _Member _Class::* _Functor;
1929       typedef _Simple_type_wrapper<_Functor> _Wrapper;
1930       typedef _Function_base::_Base_manager<_Wrapper> _Base;
1932     public:
1933       static bool
1934       _M_manager(_Any_data& __dest, const _Any_data& __source,
1935                  _Manager_operation __op)
1936       {
1937         switch (__op)
1938           {
1939 #if __cpp_rtti
1940           case __get_type_info:
1941             __dest._M_access<const type_info*>() = &typeid(_Functor);
1942             break;
1943 #endif
1944           case __get_functor_ptr:
1945             __dest._M_access<_Functor*>() =
1946               &_Base::_M_get_pointer(__source)->__value;
1947             break;
1949           default:
1950             _Base::_M_manager(__dest, __source, __op);
1951           }
1952         return false;
1953       }
1955       static void
1956       _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args)
1957       {
1958         std::mem_fn(_Base::_M_get_pointer(__functor)->__value)(
1959             std::forward<_ArgTypes>(__args)...);
1960       }
1961     };
1963   template<typename _From, typename _To>
1964     using __check_func_return_type
1965       = __or_<is_void<_To>, is_convertible<_From, _To>>;
1967   /**
1968    *  @brief Primary class template for std::function.
1969    *  @ingroup functors
1970    *
1971    *  Polymorphic function wrapper.
1972    */
1973   template<typename _Res, typename... _ArgTypes>
1974     class function<_Res(_ArgTypes...)>
1975     : public _Maybe_unary_or_binary_function<_Res, _ArgTypes...>,
1976       private _Function_base
1977     {
1978       typedef _Res _Signature_type(_ArgTypes...);
1980       template<typename _Functor>
1981         using _Invoke = decltype(__callable_functor(std::declval<_Functor&>())
1982                                  (std::declval<_ArgTypes>()...) );
1984       // Used so the return type convertibility checks aren't done when
1985       // performing overload resolution for copy construction/assignment.
1986       template<typename _Tp>
1987         using _NotSelf = __not_<is_same<_Tp, function>>;
1989       template<typename _Functor>
1990         using _Callable
1991           = __and_<_NotSelf<_Functor>,
1992                    __check_func_return_type<_Invoke<_Functor>, _Res>>;
1994       template<typename _Cond, typename _Tp>
1995         using _Requires = typename enable_if<_Cond::value, _Tp>::type;
1997     public:
1998       typedef _Res result_type;
2000       // [3.7.2.1] construct/copy/destroy
2002       /**
2003        *  @brief Default construct creates an empty function call wrapper.
2004        *  @post @c !(bool)*this
2005        */
2006       function() noexcept
2007       : _Function_base() { }
2009       /**
2010        *  @brief Creates an empty function call wrapper.
2011        *  @post @c !(bool)*this
2012        */
2013       function(nullptr_t) noexcept
2014       : _Function_base() { }
2016       /**
2017        *  @brief %Function copy constructor.
2018        *  @param __x A %function object with identical call signature.
2019        *  @post @c bool(*this) == bool(__x)
2020        *
2021        *  The newly-created %function contains a copy of the target of @a
2022        *  __x (if it has one).
2023        */
2024       function(const function& __x);
2026       /**
2027        *  @brief %Function move constructor.
2028        *  @param __x A %function object rvalue with identical call signature.
2029        *
2030        *  The newly-created %function contains the target of @a __x
2031        *  (if it has one).
2032        */
2033       function(function&& __x) : _Function_base()
2034       {
2035         __x.swap(*this);
2036       }
2038       // TODO: needs allocator_arg_t
2040       /**
2041        *  @brief Builds a %function that targets a copy of the incoming
2042        *  function object.
2043        *  @param __f A %function object that is callable with parameters of
2044        *  type @c T1, @c T2, ..., @c TN and returns a value convertible
2045        *  to @c Res.
2046        *
2047        *  The newly-created %function object will target a copy of 
2048        *  @a __f. If @a __f is @c reference_wrapper<F>, then this function
2049        *  object will contain a reference to the function object @c
2050        *  __f.get(). If @a __f is a NULL function pointer or NULL
2051        *  pointer-to-member, the newly-created object will be empty.
2052        *
2053        *  If @a __f is a non-NULL function pointer or an object of type @c
2054        *  reference_wrapper<F>, this function will not throw.
2055        */
2056       template<typename _Functor,
2057                typename = _Requires<_Callable<_Functor>, void>>
2058         function(_Functor);
2060       /**
2061        *  @brief %Function assignment operator.
2062        *  @param __x A %function with identical call signature.
2063        *  @post @c (bool)*this == (bool)x
2064        *  @returns @c *this
2065        *
2066        *  The target of @a __x is copied to @c *this. If @a __x has no
2067        *  target, then @c *this will be empty.
2068        *
2069        *  If @a __x targets a function pointer or a reference to a function
2070        *  object, then this operation will not throw an %exception.
2071        */
2072       function&
2073       operator=(const function& __x)
2074       {
2075         function(__x).swap(*this);
2076         return *this;
2077       }
2079       /**
2080        *  @brief %Function move-assignment operator.
2081        *  @param __x A %function rvalue with identical call signature.
2082        *  @returns @c *this
2083        *
2084        *  The target of @a __x is moved to @c *this. If @a __x has no
2085        *  target, then @c *this will be empty.
2086        *
2087        *  If @a __x targets a function pointer or a reference to a function
2088        *  object, then this operation will not throw an %exception.
2089        */
2090       function&
2091       operator=(function&& __x)
2092       {
2093         function(std::move(__x)).swap(*this);
2094         return *this;
2095       }
2097       /**
2098        *  @brief %Function assignment to zero.
2099        *  @post @c !(bool)*this
2100        *  @returns @c *this
2101        *
2102        *  The target of @c *this is deallocated, leaving it empty.
2103        */
2104       function&
2105       operator=(nullptr_t) noexcept
2106       {
2107         if (_M_manager)
2108           {
2109             _M_manager(_M_functor, _M_functor, __destroy_functor);
2110             _M_manager = nullptr;
2111             _M_invoker = nullptr;
2112           }
2113         return *this;
2114       }
2116       /**
2117        *  @brief %Function assignment to a new target.
2118        *  @param __f A %function object that is callable with parameters of
2119        *  type @c T1, @c T2, ..., @c TN and returns a value convertible
2120        *  to @c Res.
2121        *  @return @c *this
2122        *
2123        *  This  %function object wrapper will target a copy of @a
2124        *  __f. If @a __f is @c reference_wrapper<F>, then this function
2125        *  object will contain a reference to the function object @c
2126        *  __f.get(). If @a __f is a NULL function pointer or NULL
2127        *  pointer-to-member, @c this object will be empty.
2128        *
2129        *  If @a __f is a non-NULL function pointer or an object of type @c
2130        *  reference_wrapper<F>, this function will not throw.
2131        */
2132       template<typename _Functor>
2133         _Requires<_Callable<typename decay<_Functor>::type>, function&>
2134         operator=(_Functor&& __f)
2135         {
2136           function(std::forward<_Functor>(__f)).swap(*this);
2137           return *this;
2138         }
2140       /// @overload
2141       template<typename _Functor>
2142         function&
2143         operator=(reference_wrapper<_Functor> __f) noexcept
2144         {
2145           function(__f).swap(*this);
2146           return *this;
2147         }
2149       // [3.7.2.2] function modifiers
2151       /**
2152        *  @brief Swap the targets of two %function objects.
2153        *  @param __x A %function with identical call signature.
2154        *
2155        *  Swap the targets of @c this function object and @a __f. This
2156        *  function will not throw an %exception.
2157        */
2158       void swap(function& __x)
2159       {
2160         std::swap(_M_functor, __x._M_functor);
2161         std::swap(_M_manager, __x._M_manager);
2162         std::swap(_M_invoker, __x._M_invoker);
2163       }
2165       // TODO: needs allocator_arg_t
2166       /*
2167       template<typename _Functor, typename _Alloc>
2168         void
2169         assign(_Functor&& __f, const _Alloc& __a)
2170         {
2171           function(allocator_arg, __a,
2172                    std::forward<_Functor>(__f)).swap(*this);
2173         }
2174       */
2176       // [3.7.2.3] function capacity
2178       /**
2179        *  @brief Determine if the %function wrapper has a target.
2180        *
2181        *  @return @c true when this %function object contains a target,
2182        *  or @c false when it is empty.
2183        *
2184        *  This function will not throw an %exception.
2185        */
2186       explicit operator bool() const noexcept
2187       { return !_M_empty(); }
2189       // [3.7.2.4] function invocation
2191       /**
2192        *  @brief Invokes the function targeted by @c *this.
2193        *  @returns the result of the target.
2194        *  @throws bad_function_call when @c !(bool)*this
2195        *
2196        *  The function call operator invokes the target function object
2197        *  stored by @c this.
2198        */
2199       _Res operator()(_ArgTypes... __args) const;
2201 #if __cpp_rtti
2202       // [3.7.2.5] function target access
2203       /**
2204        *  @brief Determine the type of the target of this function object
2205        *  wrapper.
2206        *
2207        *  @returns the type identifier of the target function object, or
2208        *  @c typeid(void) if @c !(bool)*this.
2209        *
2210        *  This function will not throw an %exception.
2211        */
2212       const type_info& target_type() const noexcept;
2214       /**
2215        *  @brief Access the stored target function object.
2216        *
2217        *  @return Returns a pointer to the stored target function object,
2218        *  if @c typeid(Functor).equals(target_type()); otherwise, a NULL
2219        *  pointer.
2220        *
2221        * This function will not throw an %exception.
2222        */
2223       template<typename _Functor>       _Functor* target() noexcept;
2225       /// @overload
2226       template<typename _Functor> const _Functor* target() const noexcept;
2227 #endif
2229     private:
2230       using _Invoker_type = _Res (*)(const _Any_data&, _ArgTypes&&...);
2231       _Invoker_type _M_invoker;
2232   };
2234   // Out-of-line member definitions.
2235   template<typename _Res, typename... _ArgTypes>
2236     function<_Res(_ArgTypes...)>::
2237     function(const function& __x)
2238     : _Function_base()
2239     {
2240       if (static_cast<bool>(__x))
2241         {
2242           __x._M_manager(_M_functor, __x._M_functor, __clone_functor);
2243           _M_invoker = __x._M_invoker;
2244           _M_manager = __x._M_manager;
2245         }
2246     }
2248   template<typename _Res, typename... _ArgTypes>
2249     template<typename _Functor, typename>
2250       function<_Res(_ArgTypes...)>::
2251       function(_Functor __f)
2252       : _Function_base()
2253       {
2254         typedef _Function_handler<_Signature_type, _Functor> _My_handler;
2256         if (_My_handler::_M_not_empty_function(__f))
2257           {
2258             _My_handler::_M_init_functor(_M_functor, std::move(__f));
2259             _M_invoker = &_My_handler::_M_invoke;
2260             _M_manager = &_My_handler::_M_manager;
2261           }
2262       }
2264   template<typename _Res, typename... _ArgTypes>
2265     _Res
2266     function<_Res(_ArgTypes...)>::
2267     operator()(_ArgTypes... __args) const
2268     {
2269       if (_M_empty())
2270         __throw_bad_function_call();
2271       return _M_invoker(_M_functor, std::forward<_ArgTypes>(__args)...);
2272     }
2274 #if __cpp_rtti
2275   template<typename _Res, typename... _ArgTypes>
2276     const type_info&
2277     function<_Res(_ArgTypes...)>::
2278     target_type() const noexcept
2279     {
2280       if (_M_manager)
2281         {
2282           _Any_data __typeinfo_result;
2283           _M_manager(__typeinfo_result, _M_functor, __get_type_info);
2284           return *__typeinfo_result._M_access<const type_info*>();
2285         }
2286       else
2287         return typeid(void);
2288     }
2290   template<typename _Res, typename... _ArgTypes>
2291     template<typename _Functor>
2292       _Functor*
2293       function<_Res(_ArgTypes...)>::
2294       target() noexcept
2295       {
2296         if (typeid(_Functor) == target_type() && _M_manager)
2297           {
2298             _Any_data __ptr;
2299             if (_M_manager(__ptr, _M_functor, __get_functor_ptr)
2300                 && !is_const<_Functor>::value)
2301               return 0;
2302             else
2303               return __ptr._M_access<_Functor*>();
2304           }
2305         else
2306           return 0;
2307       }
2309   template<typename _Res, typename... _ArgTypes>
2310     template<typename _Functor>
2311       const _Functor*
2312       function<_Res(_ArgTypes...)>::
2313       target() const noexcept
2314       {
2315         if (typeid(_Functor) == target_type() && _M_manager)
2316           {
2317             _Any_data __ptr;
2318             _M_manager(__ptr, _M_functor, __get_functor_ptr);
2319             return __ptr._M_access<const _Functor*>();
2320           }
2321         else
2322           return 0;
2323       }
2324 #endif
2326   // [20.7.15.2.6] null pointer comparisons
2328   /**
2329    *  @brief Compares a polymorphic function object wrapper against 0
2330    *  (the NULL pointer).
2331    *  @returns @c true if the wrapper has no target, @c false otherwise
2332    *
2333    *  This function will not throw an %exception.
2334    */
2335   template<typename _Res, typename... _Args>
2336     inline bool
2337     operator==(const function<_Res(_Args...)>& __f, nullptr_t) noexcept
2338     { return !static_cast<bool>(__f); }
2340   /// @overload
2341   template<typename _Res, typename... _Args>
2342     inline bool
2343     operator==(nullptr_t, const function<_Res(_Args...)>& __f) noexcept
2344     { return !static_cast<bool>(__f); }
2346   /**
2347    *  @brief Compares a polymorphic function object wrapper against 0
2348    *  (the NULL pointer).
2349    *  @returns @c false if the wrapper has no target, @c true otherwise
2350    *
2351    *  This function will not throw an %exception.
2352    */
2353   template<typename _Res, typename... _Args>
2354     inline bool
2355     operator!=(const function<_Res(_Args...)>& __f, nullptr_t) noexcept
2356     { return static_cast<bool>(__f); }
2358   /// @overload
2359   template<typename _Res, typename... _Args>
2360     inline bool
2361     operator!=(nullptr_t, const function<_Res(_Args...)>& __f) noexcept
2362     { return static_cast<bool>(__f); }
2364   // [20.7.15.2.7] specialized algorithms
2366   /**
2367    *  @brief Swap the targets of two polymorphic function object wrappers.
2368    *
2369    *  This function will not throw an %exception.
2370    */
2371   template<typename _Res, typename... _Args>
2372     inline void
2373     swap(function<_Res(_Args...)>& __x, function<_Res(_Args...)>& __y)
2374     { __x.swap(__y); }
2376 _GLIBCXX_END_NAMESPACE_VERSION
2377 } // namespace std
2379 #endif // C++11
2381 #endif // _GLIBCXX_FUNCTIONAL