* include/std/functional (__is_location_invariant): Use __or_ helper.
[official-gcc.git] / libstdc++-v3 / include / std / functional
blob295022de8ee3f4f58c2d7486e99b10b15ca6deb7
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 _GLIBCXX_HAS_NESTED_TYPE(result_type)
72   /// If we have found a result_type, extract it.
73   template<bool _Has_result_type, typename _Functor>
74     struct _Maybe_get_result_type
75     { };
77   template<typename _Functor>
78     struct _Maybe_get_result_type<true, _Functor>
79     { typedef typename _Functor::result_type result_type; };
81   /**
82    *  Base class for any function object that has a weak result type, as
83    *  defined in 3.3/3 of TR1.
84   */
85   template<typename _Functor>
86     struct _Weak_result_type_impl
87     : _Maybe_get_result_type<__has_result_type<_Functor>::value, _Functor>
88     { };
90   /// Retrieve the result type for a function type.
91   template<typename _Res, typename... _ArgTypes>
92     struct _Weak_result_type_impl<_Res(_ArgTypes...)>
93     { typedef _Res result_type; };
95   template<typename _Res, typename... _ArgTypes>
96     struct _Weak_result_type_impl<_Res(_ArgTypes......)>
97     { typedef _Res result_type; };
99   template<typename _Res, typename... _ArgTypes>
100     struct _Weak_result_type_impl<_Res(_ArgTypes...) const>
101     { typedef _Res result_type; };
103   template<typename _Res, typename... _ArgTypes>
104     struct _Weak_result_type_impl<_Res(_ArgTypes......) const>
105     { typedef _Res result_type; };
107   template<typename _Res, typename... _ArgTypes>
108     struct _Weak_result_type_impl<_Res(_ArgTypes...) volatile>
109     { typedef _Res result_type; };
111   template<typename _Res, typename... _ArgTypes>
112     struct _Weak_result_type_impl<_Res(_ArgTypes......) volatile>
113     { typedef _Res result_type; };
115   template<typename _Res, typename... _ArgTypes>
116     struct _Weak_result_type_impl<_Res(_ArgTypes...) const volatile>
117     { typedef _Res result_type; };
119   template<typename _Res, typename... _ArgTypes>
120     struct _Weak_result_type_impl<_Res(_ArgTypes......) const volatile>
121     { typedef _Res result_type; };
123   /// Retrieve the result type for a function reference.
124   template<typename _Res, typename... _ArgTypes>
125     struct _Weak_result_type_impl<_Res(&)(_ArgTypes...)>
126     { typedef _Res result_type; };
128   template<typename _Res, typename... _ArgTypes>
129     struct _Weak_result_type_impl<_Res(&)(_ArgTypes......)>
130     { typedef _Res result_type; };
132   /// Retrieve the result type for a function pointer.
133   template<typename _Res, typename... _ArgTypes>
134     struct _Weak_result_type_impl<_Res(*)(_ArgTypes...)>
135     { typedef _Res result_type; };
137   template<typename _Res, typename... _ArgTypes>
138     struct _Weak_result_type_impl<_Res(*)(_ArgTypes......)>
139     { typedef _Res result_type; };
141   /// Retrieve result type for a member function pointer.
142   template<typename _Res, typename _Class, typename... _ArgTypes>
143     struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...)>
144     { typedef _Res result_type; };
146   template<typename _Res, typename _Class, typename... _ArgTypes>
147     struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes......)>
148     { typedef _Res result_type; };
150   /// Retrieve result type for a const member function pointer.
151   template<typename _Res, typename _Class, typename... _ArgTypes>
152     struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) const>
153     { typedef _Res result_type; };
155   template<typename _Res, typename _Class, typename... _ArgTypes>
156     struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes......) const>
157     { typedef _Res result_type; };
159   /// Retrieve result type for a volatile member function pointer.
160   template<typename _Res, typename _Class, typename... _ArgTypes>
161     struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) volatile>
162     { typedef _Res result_type; };
164   template<typename _Res, typename _Class, typename... _ArgTypes>
165     struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes......) volatile>
166     { typedef _Res result_type; };
168   /// Retrieve result type for a const volatile member function pointer.
169   template<typename _Res, typename _Class, typename... _ArgTypes>
170     struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...)
171                                   const volatile>
172     { typedef _Res result_type; };
174   template<typename _Res, typename _Class, typename... _ArgTypes>
175     struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes......)
176                                   const volatile>
177     { typedef _Res result_type; };
179   /**
180    *  Strip top-level cv-qualifiers from the function object and let
181    *  _Weak_result_type_impl perform the real work.
182   */
183   template<typename _Functor>
184     struct _Weak_result_type
185     : _Weak_result_type_impl<typename remove_cv<_Functor>::type>
186     { };
188   /**
189    * Invoke a function object, which may be either a member pointer or a
190    * function object. The first parameter will tell which.
191    */
192   template<typename _Functor, typename... _Args>
193     inline
194     typename enable_if<
195              (!is_member_pointer<_Functor>::value
196               && !is_function<_Functor>::value
197               && !is_function<typename remove_pointer<_Functor>::type>::value),
198              typename result_of<_Functor&(_Args&&...)>::type
199            >::type
200     __invoke(_Functor& __f, _Args&&... __args)
201     {
202       return __f(std::forward<_Args>(__args)...);
203     }
205   template<typename _Functor, typename... _Args>
206     inline
207     typename enable_if<
208              (is_member_pointer<_Functor>::value
209               && !is_function<_Functor>::value
210               && !is_function<typename remove_pointer<_Functor>::type>::value),
211              typename result_of<_Functor(_Args&&...)>::type
212            >::type
213     __invoke(_Functor& __f, _Args&&... __args)
214     {
215       return std::mem_fn(__f)(std::forward<_Args>(__args)...);
216     }
218   // To pick up function references (that will become function pointers)
219   template<typename _Functor, typename... _Args>
220     inline
221     typename enable_if<
222              (is_pointer<_Functor>::value
223               && is_function<typename remove_pointer<_Functor>::type>::value),
224              typename result_of<_Functor(_Args&&...)>::type
225            >::type
226     __invoke(_Functor __f, _Args&&... __args)
227     {
228       return __f(std::forward<_Args>(__args)...);
229     }
231   /**
232    *  Knowing which of unary_function and binary_function _Tp derives
233    *  from, derives from the same and ensures that reference_wrapper
234    *  will have a weak result type. See cases below.
235    */
236   template<bool _Unary, bool _Binary, typename _Tp>
237     struct _Reference_wrapper_base_impl;
239   // None of the nested argument types.
240   template<typename _Tp>
241     struct _Reference_wrapper_base_impl<false, false, _Tp>
242     : _Weak_result_type<_Tp>
243     { };
245   // Nested argument_type only.
246   template<typename _Tp>
247     struct _Reference_wrapper_base_impl<true, false, _Tp>
248     : _Weak_result_type<_Tp>
249     {
250       typedef typename _Tp::argument_type argument_type;
251     };
253   // Nested first_argument_type and second_argument_type only.
254   template<typename _Tp>
255     struct _Reference_wrapper_base_impl<false, true, _Tp>
256     : _Weak_result_type<_Tp>
257     {
258       typedef typename _Tp::first_argument_type first_argument_type;
259       typedef typename _Tp::second_argument_type second_argument_type;
260     };
262   // All the nested argument types.
263    template<typename _Tp>
264     struct _Reference_wrapper_base_impl<true, true, _Tp>
265     : _Weak_result_type<_Tp>
266     {
267       typedef typename _Tp::argument_type argument_type;
268       typedef typename _Tp::first_argument_type first_argument_type;
269       typedef typename _Tp::second_argument_type second_argument_type;
270     };
272   _GLIBCXX_HAS_NESTED_TYPE(argument_type)
273   _GLIBCXX_HAS_NESTED_TYPE(first_argument_type)
274   _GLIBCXX_HAS_NESTED_TYPE(second_argument_type)
276   /**
277    *  Derives from unary_function or binary_function when it
278    *  can. Specializations handle all of the easy cases. The primary
279    *  template determines what to do with a class type, which may
280    *  derive from both unary_function and binary_function.
281   */
282   template<typename _Tp>
283     struct _Reference_wrapper_base
284     : _Reference_wrapper_base_impl<
285       __has_argument_type<_Tp>::value,
286       __has_first_argument_type<_Tp>::value
287       && __has_second_argument_type<_Tp>::value,
288       _Tp>
289     { };
291   // - a function type (unary)
292   template<typename _Res, typename _T1>
293     struct _Reference_wrapper_base<_Res(_T1)>
294     : unary_function<_T1, _Res>
295     { };
297   template<typename _Res, typename _T1>
298     struct _Reference_wrapper_base<_Res(_T1) const>
299     : unary_function<_T1, _Res>
300     { };
302   template<typename _Res, typename _T1>
303     struct _Reference_wrapper_base<_Res(_T1) volatile>
304     : unary_function<_T1, _Res>
305     { };
307   template<typename _Res, typename _T1>
308     struct _Reference_wrapper_base<_Res(_T1) const volatile>
309     : unary_function<_T1, _Res>
310     { };
312   // - a function type (binary)
313   template<typename _Res, typename _T1, typename _T2>
314     struct _Reference_wrapper_base<_Res(_T1, _T2)>
315     : binary_function<_T1, _T2, _Res>
316     { };
318   template<typename _Res, typename _T1, typename _T2>
319     struct _Reference_wrapper_base<_Res(_T1, _T2) const>
320     : binary_function<_T1, _T2, _Res>
321     { };
323   template<typename _Res, typename _T1, typename _T2>
324     struct _Reference_wrapper_base<_Res(_T1, _T2) volatile>
325     : binary_function<_T1, _T2, _Res>
326     { };
328   template<typename _Res, typename _T1, typename _T2>
329     struct _Reference_wrapper_base<_Res(_T1, _T2) const volatile>
330     : binary_function<_T1, _T2, _Res>
331     { };
333   // - a function pointer type (unary)
334   template<typename _Res, typename _T1>
335     struct _Reference_wrapper_base<_Res(*)(_T1)>
336     : unary_function<_T1, _Res>
337     { };
339   // - a function pointer type (binary)
340   template<typename _Res, typename _T1, typename _T2>
341     struct _Reference_wrapper_base<_Res(*)(_T1, _T2)>
342     : binary_function<_T1, _T2, _Res>
343     { };
345   // - a pointer to member function type (unary, no qualifiers)
346   template<typename _Res, typename _T1>
347     struct _Reference_wrapper_base<_Res (_T1::*)()>
348     : unary_function<_T1*, _Res>
349     { };
351   // - a pointer to member function type (binary, no qualifiers)
352   template<typename _Res, typename _T1, typename _T2>
353     struct _Reference_wrapper_base<_Res (_T1::*)(_T2)>
354     : binary_function<_T1*, _T2, _Res>
355     { };
357   // - a pointer to member function type (unary, const)
358   template<typename _Res, typename _T1>
359     struct _Reference_wrapper_base<_Res (_T1::*)() const>
360     : unary_function<const _T1*, _Res>
361     { };
363   // - a pointer to member function type (binary, const)
364   template<typename _Res, typename _T1, typename _T2>
365     struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const>
366     : binary_function<const _T1*, _T2, _Res>
367     { };
369   // - a pointer to member function type (unary, volatile)
370   template<typename _Res, typename _T1>
371     struct _Reference_wrapper_base<_Res (_T1::*)() volatile>
372     : unary_function<volatile _T1*, _Res>
373     { };
375   // - a pointer to member function type (binary, volatile)
376   template<typename _Res, typename _T1, typename _T2>
377     struct _Reference_wrapper_base<_Res (_T1::*)(_T2) volatile>
378     : binary_function<volatile _T1*, _T2, _Res>
379     { };
381   // - a pointer to member function type (unary, const volatile)
382   template<typename _Res, typename _T1>
383     struct _Reference_wrapper_base<_Res (_T1::*)() const volatile>
384     : unary_function<const volatile _T1*, _Res>
385     { };
387   // - a pointer to member function type (binary, const volatile)
388   template<typename _Res, typename _T1, typename _T2>
389     struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const volatile>
390     : binary_function<const volatile _T1*, _T2, _Res>
391     { };
393   /**
394    *  @brief Primary class template for reference_wrapper.
395    *  @ingroup functors
396    *  @{
397    */
398   template<typename _Tp>
399     class reference_wrapper
400     : public _Reference_wrapper_base<typename remove_cv<_Tp>::type>
401     {
402       _Tp* _M_data;
404     public:
405       typedef _Tp type;
407       reference_wrapper(_Tp& __indata) noexcept
408       : _M_data(std::__addressof(__indata))
409       { }
411       reference_wrapper(_Tp&&) = delete;
413       reference_wrapper(const reference_wrapper<_Tp>& __inref) noexcept
414       : _M_data(__inref._M_data)
415       { }
417       reference_wrapper&
418       operator=(const reference_wrapper<_Tp>& __inref) noexcept
419       {
420         _M_data = __inref._M_data;
421         return *this;
422       }
424       operator _Tp&() const noexcept
425       { return this->get(); }
427       _Tp&
428       get() const noexcept
429       { return *_M_data; }
431       template<typename... _Args>
432         typename result_of<_Tp&(_Args&&...)>::type
433         operator()(_Args&&... __args) const
434         {
435           return __invoke(get(), std::forward<_Args>(__args)...);
436         }
437     };
440   /// Denotes a reference should be taken to a variable.
441   template<typename _Tp>
442     inline reference_wrapper<_Tp>
443     ref(_Tp& __t) noexcept
444     { return reference_wrapper<_Tp>(__t); }
446   /// Denotes a const reference should be taken to a variable.
447   template<typename _Tp>
448     inline reference_wrapper<const _Tp>
449     cref(const _Tp& __t) noexcept
450     { return reference_wrapper<const _Tp>(__t); }
452   template<typename _Tp>
453     void ref(const _Tp&&) = delete;
455   template<typename _Tp>
456     void cref(const _Tp&&) = delete;
458   /// Partial specialization.
459   template<typename _Tp>
460     inline reference_wrapper<_Tp>
461     ref(reference_wrapper<_Tp> __t) noexcept
462     { return ref(__t.get()); }
464   /// Partial specialization.
465   template<typename _Tp>
466     inline reference_wrapper<const _Tp>
467     cref(reference_wrapper<_Tp> __t) noexcept
468     { return cref(__t.get()); }
470   // @} group functors
472   template<typename... _Types>
473     struct _Pack : integral_constant<size_t, sizeof...(_Types)>
474     { };
476   template<typename _From, typename _To, bool = _From::value == _To::value>
477     struct _AllConvertible : false_type
478     { };
480   template<typename... _From, typename... _To>
481     struct _AllConvertible<_Pack<_From...>, _Pack<_To...>, true>
482     : __and_<is_convertible<_From, _To>...>
483     { };
485   template<typename _Tp1, typename _Tp2>
486     using _NotSame = __not_<is_same<typename std::decay<_Tp1>::type,
487                                     typename std::decay<_Tp2>::type>>;
489   /**
490    * Derives from @c unary_function or @c binary_function, or perhaps
491    * nothing, depending on the number of arguments provided. The
492    * primary template is the basis case, which derives nothing.
493    */
494   template<typename _Res, typename... _ArgTypes>
495     struct _Maybe_unary_or_binary_function { };
497   /// Derives from @c unary_function, as appropriate.
498   template<typename _Res, typename _T1>
499     struct _Maybe_unary_or_binary_function<_Res, _T1>
500     : std::unary_function<_T1, _Res> { };
502   /// Derives from @c binary_function, as appropriate.
503   template<typename _Res, typename _T1, typename _T2>
504     struct _Maybe_unary_or_binary_function<_Res, _T1, _T2>
505     : std::binary_function<_T1, _T2, _Res> { };
507   /// Implementation of @c mem_fn for member function pointers.
508   template<typename _Res, typename _Class, typename... _ArgTypes>
509     class _Mem_fn<_Res (_Class::*)(_ArgTypes...)>
510     : public _Maybe_unary_or_binary_function<_Res, _Class*, _ArgTypes...>
511     {
512       typedef _Res (_Class::*_Functor)(_ArgTypes...);
514       template<typename _Tp, typename... _Args>
515         _Res
516         _M_call(_Tp&& __object, const volatile _Class *,
517                 _Args&&... __args) const
518         {
519           return (std::forward<_Tp>(__object).*__pmf)
520             (std::forward<_Args>(__args)...);
521         }
523       template<typename _Tp, typename... _Args>
524         _Res
525         _M_call(_Tp&& __ptr, const volatile void *, _Args&&... __args) const
526         { return ((*__ptr).*__pmf)(std::forward<_Args>(__args)...); }
528       // Require each _Args to be convertible to corresponding _ArgTypes
529       template<typename... _Args>
530         using _RequireValidArgs
531           = _Require<_AllConvertible<_Pack<_Args...>, _Pack<_ArgTypes...>>>;
533       // Require each _Args to be convertible to corresponding _ArgTypes
534       // and require _Tp is not _Class, _Class& or _Class*
535       template<typename _Tp, typename... _Args>
536         using _RequireValidArgs2
537           = _Require<_NotSame<_Class, _Tp>, _NotSame<_Class*, _Tp>,
538                      _AllConvertible<_Pack<_Args...>, _Pack<_ArgTypes...>>>;
540       // Require each _Args to be convertible to corresponding _ArgTypes
541       // and require _Tp is _Class or derived from _Class
542       template<typename _Tp, typename... _Args>
543         using _RequireValidArgs3
544           = _Require<is_base_of<_Class, _Tp>,
545                      _AllConvertible<_Pack<_Args...>, _Pack<_ArgTypes...>>>;
547     public:
548       typedef _Res result_type;
550       explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
552       // Handle objects
553       template<typename... _Args, typename _Req = _RequireValidArgs<_Args...>>
554         _Res
555         operator()(_Class& __object, _Args&&... __args) const
556         { return (__object.*__pmf)(std::forward<_Args>(__args)...); }
558       template<typename... _Args, typename _Req = _RequireValidArgs<_Args...>>
559         _Res
560         operator()(_Class&& __object, _Args&&... __args) const
561         {
562           return (std::move(__object).*__pmf)(std::forward<_Args>(__args)...);
563         }
565       // Handle pointers
566       template<typename... _Args, typename _Req = _RequireValidArgs<_Args...>>
567         _Res
568         operator()(_Class* __object, _Args&&... __args) const
569         { return (__object->*__pmf)(std::forward<_Args>(__args)...); }
571       // Handle smart pointers, references and pointers to derived
572       template<typename _Tp, typename... _Args,
573                typename _Req = _RequireValidArgs2<_Tp, _Args...>>
574         _Res
575         operator()(_Tp&& __object, _Args&&... __args) const
576         {
577           return _M_call(std::forward<_Tp>(__object), &__object,
578               std::forward<_Args>(__args)...);
579         }
581       template<typename _Tp, typename... _Args,
582                typename _Req = _RequireValidArgs3<_Tp, _Args...>>
583         _Res
584         operator()(reference_wrapper<_Tp> __ref, _Args&&... __args) const
585         { return operator()(__ref.get(), std::forward<_Args>(__args)...); }
587     private:
588       _Functor __pmf;
589     };
591   /// Implementation of @c mem_fn for const member function pointers.
592   template<typename _Res, typename _Class, typename... _ArgTypes>
593     class _Mem_fn<_Res (_Class::*)(_ArgTypes...) const>
594     : public _Maybe_unary_or_binary_function<_Res, const _Class*,
595                                              _ArgTypes...>
596     {
597       typedef _Res (_Class::*_Functor)(_ArgTypes...) const;
599       template<typename _Tp, typename... _Args>
600         _Res
601         _M_call(_Tp&& __object, const volatile _Class *,
602                 _Args&&... __args) const
603         {
604           return (std::forward<_Tp>(__object).*__pmf)
605             (std::forward<_Args>(__args)...);
606         }
608       template<typename _Tp, typename... _Args>
609         _Res
610         _M_call(_Tp&& __ptr, const volatile void *, _Args&&... __args) const
611         { return ((*__ptr).*__pmf)(std::forward<_Args>(__args)...); }
613       template<typename... _Args>
614         using _RequireValidArgs
615           = _Require<_AllConvertible<_Pack<_Args...>, _Pack<_ArgTypes...>>>;
617       template<typename _Tp, typename... _Args>
618         using _RequireValidArgs2
619           = _Require<_NotSame<_Class, _Tp>, _NotSame<const _Class*, _Tp>,
620                      _AllConvertible<_Pack<_Args...>, _Pack<_ArgTypes...>>>;
622       template<typename _Tp, typename... _Args>
623         using _RequireValidArgs3
624           = _Require<is_base_of<_Class, _Tp>,
625                      _AllConvertible<_Pack<_Args...>, _Pack<_ArgTypes...>>>;
627     public:
628       typedef _Res result_type;
630       explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
632       // Handle objects
633       template<typename... _Args, typename _Req = _RequireValidArgs<_Args...>>
634         _Res
635         operator()(const _Class& __object, _Args&&... __args) const
636         { return (__object.*__pmf)(std::forward<_Args>(__args)...); }
638       template<typename... _Args, typename _Req = _RequireValidArgs<_Args...>>
639         _Res
640         operator()(const _Class&& __object, _Args&&... __args) const
641         {
642           return (std::move(__object).*__pmf)(std::forward<_Args>(__args)...);
643         }
645       // Handle pointers
646       template<typename... _Args, typename _Req = _RequireValidArgs<_Args...>>
647         _Res
648         operator()(const _Class* __object, _Args&&... __args) const
649         { return (__object->*__pmf)(std::forward<_Args>(__args)...); }
651       // Handle smart pointers, references and pointers to derived
652       template<typename _Tp, typename... _Args,
653                typename _Req = _RequireValidArgs2<_Tp, _Args...>>
654         _Res operator()(_Tp&& __object, _Args&&... __args) const
655         {
656           return _M_call(std::forward<_Tp>(__object), &__object,
657               std::forward<_Args>(__args)...);
658         }
660       template<typename _Tp, typename... _Args,
661                typename _Req = _RequireValidArgs3<_Tp, _Args...>>
662         _Res
663         operator()(reference_wrapper<_Tp> __ref, _Args&&... __args) const
664         { return operator()(__ref.get(), std::forward<_Args>(__args)...); }
666     private:
667       _Functor __pmf;
668     };
670   /// Implementation of @c mem_fn for volatile member function pointers.
671   template<typename _Res, typename _Class, typename... _ArgTypes>
672     class _Mem_fn<_Res (_Class::*)(_ArgTypes...) volatile>
673     : public _Maybe_unary_or_binary_function<_Res, volatile _Class*,
674                                              _ArgTypes...>
675     {
676       typedef _Res (_Class::*_Functor)(_ArgTypes...) volatile;
678       template<typename _Tp, typename... _Args>
679         _Res
680         _M_call(_Tp&& __object, const volatile _Class *,
681                 _Args&&... __args) const
682         {
683           return (std::forward<_Tp>(__object).*__pmf)
684             (std::forward<_Args>(__args)...);
685         }
687       template<typename _Tp, typename... _Args>
688         _Res
689         _M_call(_Tp&& __ptr, const volatile void *, _Args&&... __args) const
690         { return ((*__ptr).*__pmf)(std::forward<_Args>(__args)...); }
692       template<typename... _Args>
693         using _RequireValidArgs
694           = _Require<_AllConvertible<_Pack<_Args...>, _Pack<_ArgTypes...>>>;
696       template<typename _Tp, typename... _Args>
697         using _RequireValidArgs2
698           = _Require<_NotSame<_Class, _Tp>, _NotSame<volatile _Class*, _Tp>,
699                      _AllConvertible<_Pack<_Args...>, _Pack<_ArgTypes...>>>;
701       template<typename _Tp, typename... _Args>
702         using _RequireValidArgs3
703           = _Require<is_base_of<_Class, _Tp>,
704                      _AllConvertible<_Pack<_Args...>, _Pack<_ArgTypes...>>>;
706     public:
707       typedef _Res result_type;
709       explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
711       // Handle objects
712       template<typename... _Args, typename _Req = _RequireValidArgs<_Args...>>
713         _Res
714         operator()(volatile _Class& __object, _Args&&... __args) const
715         { return (__object.*__pmf)(std::forward<_Args>(__args)...); }
717       template<typename... _Args, typename _Req = _RequireValidArgs<_Args...>>
718         _Res
719         operator()(volatile _Class&& __object, _Args&&... __args) const
720         {
721           return (std::move(__object).*__pmf)(std::forward<_Args>(__args)...);
722         }
724       // Handle pointers
725       template<typename... _Args, typename _Req = _RequireValidArgs<_Args...>>
726         _Res
727         operator()(volatile _Class* __object, _Args&&... __args) const
728         { return (__object->*__pmf)(std::forward<_Args>(__args)...); }
730       // Handle smart pointers, references and pointers to derived
731       template<typename _Tp, typename... _Args,
732                typename _Req = _RequireValidArgs2<_Tp, _Args...>>
733         _Res
734         operator()(_Tp&& __object, _Args&&... __args) const
735         {
736           return _M_call(std::forward<_Tp>(__object), &__object,
737               std::forward<_Args>(__args)...);
738         }
740       template<typename _Tp, typename... _Args,
741                typename _Req = _RequireValidArgs3<_Tp, _Args...>>
742         _Res
743         operator()(reference_wrapper<_Tp> __ref, _Args&&... __args) const
744         { return operator()(__ref.get(), std::forward<_Args>(__args)...); }
746     private:
747       _Functor __pmf;
748     };
750   /// Implementation of @c mem_fn for const volatile member function pointers.
751   template<typename _Res, typename _Class, typename... _ArgTypes>
752     class _Mem_fn<_Res (_Class::*)(_ArgTypes...) const volatile>
753     : public _Maybe_unary_or_binary_function<_Res, const volatile _Class*,
754                                              _ArgTypes...>
755     {
756       typedef _Res (_Class::*_Functor)(_ArgTypes...) const volatile;
758       template<typename _Tp, typename... _Args>
759         _Res
760         _M_call(_Tp&& __object, const volatile _Class *,
761                 _Args&&... __args) const
762         {
763           return (std::forward<_Tp>(__object).*__pmf)
764             (std::forward<_Args>(__args)...);
765         }
767       template<typename _Tp, typename... _Args>
768         _Res
769         _M_call(_Tp&& __ptr, const volatile void *, _Args&&... __args) const
770         { return ((*__ptr).*__pmf)(std::forward<_Args>(__args)...); }
772       template<typename... _Args>
773         using _RequireValidArgs
774           = _Require<_AllConvertible<_Pack<_Args...>, _Pack<_ArgTypes...>>>;
776       template<typename _Tp, typename... _Args>
777         using _RequireValidArgs2
778           = _Require<_NotSame<_Class, _Tp>,
779                      _NotSame<const volatile _Class*, _Tp>,
780                      _AllConvertible<_Pack<_Args...>, _Pack<_ArgTypes...>>>;
782       template<typename _Tp, typename... _Args>
783         using _RequireValidArgs3
784           = _Require<is_base_of<_Class, _Tp>,
785                      _AllConvertible<_Pack<_Args...>, _Pack<_ArgTypes...>>>;
787     public:
788       typedef _Res result_type;
790       explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
792       // Handle objects
793       template<typename... _Args, typename _Req = _RequireValidArgs<_Args...>>
794         _Res
795         operator()(const volatile _Class& __object, _Args&&... __args) const
796         { return (__object.*__pmf)(std::forward<_Args>(__args)...); }
798       template<typename... _Args, typename _Req = _RequireValidArgs<_Args...>>
799         _Res
800         operator()(const volatile _Class&& __object, _Args&&... __args) const
801         {
802           return (std::move(__object).*__pmf)(std::forward<_Args>(__args)...);
803         }
805       // Handle pointers
806       template<typename... _Args, typename _Req = _RequireValidArgs<_Args...>>
807         _Res
808         operator()(const volatile _Class* __object, _Args&&... __args) const
809         { return (__object->*__pmf)(std::forward<_Args>(__args)...); }
811       // Handle smart pointers, references and pointers to derived
812       template<typename _Tp, typename... _Args,
813                typename _Req = _RequireValidArgs2<_Tp, _Args...>>
814         _Res operator()(_Tp&& __object, _Args&&... __args) const
815         {
816           return _M_call(std::forward<_Tp>(__object), &__object,
817               std::forward<_Args>(__args)...);
818         }
820       template<typename _Tp, typename... _Args,
821                typename _Req = _RequireValidArgs3<_Tp, _Args...>>
822         _Res
823         operator()(reference_wrapper<_Tp> __ref, _Args&&... __args) const
824         { return operator()(__ref.get(), std::forward<_Args>(__args)...); }
826     private:
827       _Functor __pmf;
828     };
831   template<typename _Tp, bool>
832     struct _Mem_fn_const_or_non
833     {
834       typedef const _Tp& type;
835     };
837   template<typename _Tp>
838     struct _Mem_fn_const_or_non<_Tp, false>
839     {
840       typedef _Tp& type;
841     };
843   template<typename _Res, typename _Class>
844     class _Mem_fn<_Res _Class::*>
845     {
846       using __pm_type = _Res _Class::*;
848       // This bit of genius is due to Peter Dimov, improved slightly by
849       // Douglas Gregor.
850       // Made less elegant to support perfect forwarding and noexcept.
851       template<typename _Tp>
852         auto
853         _M_call(_Tp&& __object, const _Class *) const noexcept
854         -> decltype(std::forward<_Tp>(__object).*std::declval<__pm_type&>())
855         { return std::forward<_Tp>(__object).*__pm; }
857       template<typename _Tp, typename _Up>
858         auto
859         _M_call(_Tp&& __object, _Up * const *) const noexcept
860         -> decltype((*std::forward<_Tp>(__object)).*std::declval<__pm_type&>())
861         { return (*std::forward<_Tp>(__object)).*__pm; }
863       template<typename _Tp>
864         auto
865         _M_call(_Tp&& __ptr, const volatile void*) const
866         noexcept(noexcept((*__ptr).*std::declval<__pm_type&>()))
867         -> decltype((*__ptr).*std::declval<__pm_type&>())
868         { return (*__ptr).*__pm; }
870     public:
871       explicit
872       _Mem_fn(_Res _Class::*__pm) noexcept : __pm(__pm) { }
874       // Handle objects
875       _Res&
876       operator()(_Class& __object) const noexcept
877       { return __object.*__pm; }
879       const _Res&
880       operator()(const _Class& __object) const noexcept
881       { return __object.*__pm; }
883       _Res&&
884       operator()(_Class&& __object) const noexcept
885       { return std::forward<_Class>(__object).*__pm; }
887       const _Res&&
888       operator()(const _Class&& __object) const noexcept
889       { return std::forward<const _Class>(__object).*__pm; }
891       // Handle pointers
892       _Res&
893       operator()(_Class* __object) const noexcept
894       { return __object->*__pm; }
896       const _Res&
897       operator()(const _Class* __object) const noexcept
898       { return __object->*__pm; }
900       // Handle smart pointers and derived
901       template<typename _Tp, typename _Req = _Require<_NotSame<_Class*, _Tp>>>
902         auto
903         operator()(_Tp&& __unknown) const
904         noexcept(noexcept(std::declval<_Mem_fn*>()->_M_call
905                           (std::forward<_Tp>(__unknown), &__unknown)))
906         -> decltype(this->_M_call(std::forward<_Tp>(__unknown), &__unknown))
907         { return _M_call(std::forward<_Tp>(__unknown), &__unknown); }
909       template<typename _Tp, typename _Req = _Require<is_base_of<_Class, _Tp>>>
910         auto
911         operator()(reference_wrapper<_Tp> __ref) const
912         noexcept(noexcept(std::declval<_Mem_fn&>()(__ref.get())))
913         -> decltype((*this)(__ref.get()))
914         { return (*this)(__ref.get()); }
916     private:
917       _Res _Class::*__pm;
918     };
920   // _GLIBCXX_RESOLVE_LIB_DEFECTS
921   // 2048.  Unnecessary mem_fn overloads
922   /**
923    *  @brief Returns a function object that forwards to the member
924    *  pointer @a pm.
925    *  @ingroup functors
926    */
927   template<typename _Tp, typename _Class>
928     inline _Mem_fn<_Tp _Class::*>
929     mem_fn(_Tp _Class::* __pm) noexcept
930     {
931       return _Mem_fn<_Tp _Class::*>(__pm);
932     }
934   /**
935    *  @brief Determines if the given type _Tp is a function object
936    *  should be treated as a subexpression when evaluating calls to
937    *  function objects returned by bind(). [TR1 3.6.1]
938    *  @ingroup binders
939    */
940   template<typename _Tp>
941     struct is_bind_expression
942     : public false_type { };
944   /**
945    *  @brief Determines if the given type _Tp is a placeholder in a
946    *  bind() expression and, if so, which placeholder it is. [TR1 3.6.2]
947    *  @ingroup binders
948    */
949   template<typename _Tp>
950     struct is_placeholder
951     : public integral_constant<int, 0>
952     { };
954   /** @brief The type of placeholder objects defined by libstdc++.
955    *  @ingroup binders
956    */
957   template<int _Num> struct _Placeholder { };
959   _GLIBCXX_END_NAMESPACE_VERSION
961   /** @namespace std::placeholders
962    *  @brief ISO C++11 entities sub-namespace for functional.
963    *  @ingroup binders
964    */
965   namespace placeholders
966   {
967   _GLIBCXX_BEGIN_NAMESPACE_VERSION
968   /* Define a large number of placeholders. There is no way to
969    * simplify this with variadic templates, because we're introducing
970    * unique names for each.
971    */
972     extern const _Placeholder<1> _1;
973     extern const _Placeholder<2> _2;
974     extern const _Placeholder<3> _3;
975     extern const _Placeholder<4> _4;
976     extern const _Placeholder<5> _5;
977     extern const _Placeholder<6> _6;
978     extern const _Placeholder<7> _7;
979     extern const _Placeholder<8> _8;
980     extern const _Placeholder<9> _9;
981     extern const _Placeholder<10> _10;
982     extern const _Placeholder<11> _11;
983     extern const _Placeholder<12> _12;
984     extern const _Placeholder<13> _13;
985     extern const _Placeholder<14> _14;
986     extern const _Placeholder<15> _15;
987     extern const _Placeholder<16> _16;
988     extern const _Placeholder<17> _17;
989     extern const _Placeholder<18> _18;
990     extern const _Placeholder<19> _19;
991     extern const _Placeholder<20> _20;
992     extern const _Placeholder<21> _21;
993     extern const _Placeholder<22> _22;
994     extern const _Placeholder<23> _23;
995     extern const _Placeholder<24> _24;
996     extern const _Placeholder<25> _25;
997     extern const _Placeholder<26> _26;
998     extern const _Placeholder<27> _27;
999     extern const _Placeholder<28> _28;
1000     extern const _Placeholder<29> _29;
1001   _GLIBCXX_END_NAMESPACE_VERSION
1002   }
1004   _GLIBCXX_BEGIN_NAMESPACE_VERSION
1006   /**
1007    *  Partial specialization of is_placeholder that provides the placeholder
1008    *  number for the placeholder objects defined by libstdc++.
1009    *  @ingroup binders
1010    */
1011   template<int _Num>
1012     struct is_placeholder<_Placeholder<_Num> >
1013     : public integral_constant<int, _Num>
1014     { };
1016   template<int _Num>
1017     struct is_placeholder<const _Placeholder<_Num> >
1018     : public integral_constant<int, _Num>
1019     { };
1021   /**
1022    * Used by _Safe_tuple_element to indicate that there is no tuple
1023    * element at this position.
1024    */
1025   struct _No_tuple_element;
1027   /**
1028    * Implementation helper for _Safe_tuple_element. This primary
1029    * template handles the case where it is safe to use @c
1030    * tuple_element.
1031    */
1032   template<std::size_t __i, typename _Tuple, bool _IsSafe>
1033     struct _Safe_tuple_element_impl
1034     : tuple_element<__i, _Tuple> { };
1036   /**
1037    * Implementation helper for _Safe_tuple_element. This partial
1038    * specialization handles the case where it is not safe to use @c
1039    * tuple_element. We just return @c _No_tuple_element.
1040    */
1041   template<std::size_t __i, typename _Tuple>
1042     struct _Safe_tuple_element_impl<__i, _Tuple, false>
1043     {
1044       typedef _No_tuple_element type;
1045     };
1047   /**
1048    * Like tuple_element, but returns @c _No_tuple_element when
1049    * tuple_element would return an error.
1050    */
1051  template<std::size_t __i, typename _Tuple>
1052    struct _Safe_tuple_element
1053    : _Safe_tuple_element_impl<__i, _Tuple,
1054                               (__i < tuple_size<_Tuple>::value)>
1055    { };
1057   /**
1058    *  Maps an argument to bind() into an actual argument to the bound
1059    *  function object [TR1 3.6.3/5]. Only the first parameter should
1060    *  be specified: the rest are used to determine among the various
1061    *  implementations. Note that, although this class is a function
1062    *  object, it isn't entirely normal because it takes only two
1063    *  parameters regardless of the number of parameters passed to the
1064    *  bind expression. The first parameter is the bound argument and
1065    *  the second parameter is a tuple containing references to the
1066    *  rest of the arguments.
1067    */
1068   template<typename _Arg,
1069            bool _IsBindExp = is_bind_expression<_Arg>::value,
1070            bool _IsPlaceholder = (is_placeholder<_Arg>::value > 0)>
1071     class _Mu;
1073   /**
1074    *  If the argument is reference_wrapper<_Tp>, returns the
1075    *  underlying reference. [TR1 3.6.3/5 bullet 1]
1076    */
1077   template<typename _Tp>
1078     class _Mu<reference_wrapper<_Tp>, false, false>
1079     {
1080     public:
1081       typedef _Tp& result_type;
1083       /* Note: This won't actually work for const volatile
1084        * reference_wrappers, because reference_wrapper::get() is const
1085        * but not volatile-qualified. This might be a defect in the TR.
1086        */
1087       template<typename _CVRef, typename _Tuple>
1088         result_type
1089         operator()(_CVRef& __arg, _Tuple&) const volatile
1090         { return __arg.get(); }
1091     };
1093   /**
1094    *  If the argument is a bind expression, we invoke the underlying
1095    *  function object with the same cv-qualifiers as we are given and
1096    *  pass along all of our arguments (unwrapped). [TR1 3.6.3/5 bullet 2]
1097    */
1098   template<typename _Arg>
1099     class _Mu<_Arg, true, false>
1100     {
1101     public:
1102       template<typename _CVArg, typename... _Args>
1103         auto
1104         operator()(_CVArg& __arg,
1105                    tuple<_Args...>& __tuple) const volatile
1106         -> decltype(__arg(declval<_Args>()...))
1107         {
1108           // Construct an index tuple and forward to __call
1109           typedef typename _Build_index_tuple<sizeof...(_Args)>::__type
1110             _Indexes;
1111           return this->__call(__arg, __tuple, _Indexes());
1112         }
1114     private:
1115       // Invokes the underlying function object __arg by unpacking all
1116       // of the arguments in the tuple.
1117       template<typename _CVArg, typename... _Args, std::size_t... _Indexes>
1118         auto
1119         __call(_CVArg& __arg, tuple<_Args...>& __tuple,
1120                const _Index_tuple<_Indexes...>&) const volatile
1121         -> decltype(__arg(declval<_Args>()...))
1122         {
1123           return __arg(std::forward<_Args>(get<_Indexes>(__tuple))...);
1124         }
1125     };
1127   /**
1128    *  If the argument is a placeholder for the Nth argument, returns
1129    *  a reference to the Nth argument to the bind function object.
1130    *  [TR1 3.6.3/5 bullet 3]
1131    */
1132   template<typename _Arg>
1133     class _Mu<_Arg, false, true>
1134     {
1135     public:
1136       template<typename _Signature> class result;
1138       template<typename _CVMu, typename _CVArg, typename _Tuple>
1139         class result<_CVMu(_CVArg, _Tuple)>
1140         {
1141           // Add a reference, if it hasn't already been done for us.
1142           // This allows us to be a little bit sloppy in constructing
1143           // the tuple that we pass to result_of<...>.
1144           typedef typename _Safe_tuple_element<(is_placeholder<_Arg>::value
1145                                                 - 1), _Tuple>::type
1146             __base_type;
1148         public:
1149           typedef typename add_rvalue_reference<__base_type>::type type;
1150         };
1152       template<typename _Tuple>
1153         typename result<_Mu(_Arg, _Tuple)>::type
1154         operator()(const volatile _Arg&, _Tuple& __tuple) const volatile
1155         {
1156           return std::forward<typename result<_Mu(_Arg, _Tuple)>::type>(
1157               ::std::get<(is_placeholder<_Arg>::value - 1)>(__tuple));
1158         }
1159     };
1161   /**
1162    *  If the argument is just a value, returns a reference to that
1163    *  value. The cv-qualifiers on the reference are the same as the
1164    *  cv-qualifiers on the _Mu object. [TR1 3.6.3/5 bullet 4]
1165    */
1166   template<typename _Arg>
1167     class _Mu<_Arg, false, false>
1168     {
1169     public:
1170       template<typename _Signature> struct result;
1172       template<typename _CVMu, typename _CVArg, typename _Tuple>
1173         struct result<_CVMu(_CVArg, _Tuple)>
1174         {
1175           typedef typename add_lvalue_reference<_CVArg>::type type;
1176         };
1178       // Pick up the cv-qualifiers of the argument
1179       template<typename _CVArg, typename _Tuple>
1180         _CVArg&&
1181         operator()(_CVArg&& __arg, _Tuple&) const volatile
1182         { return std::forward<_CVArg>(__arg); }
1183     };
1185   /**
1186    *  Maps member pointers into instances of _Mem_fn but leaves all
1187    *  other function objects untouched. Used by tr1::bind(). The
1188    *  primary template handles the non--member-pointer case.
1189    */
1190   template<typename _Tp>
1191     struct _Maybe_wrap_member_pointer
1192     {
1193       typedef _Tp type;
1195       static const _Tp&
1196       __do_wrap(const _Tp& __x)
1197       { return __x; }
1199       static _Tp&&
1200       __do_wrap(_Tp&& __x)
1201       { return static_cast<_Tp&&>(__x); }
1202     };
1204   /**
1205    *  Maps member pointers into instances of _Mem_fn but leaves all
1206    *  other function objects untouched. Used by tr1::bind(). This
1207    *  partial specialization handles the member pointer case.
1208    */
1209   template<typename _Tp, typename _Class>
1210     struct _Maybe_wrap_member_pointer<_Tp _Class::*>
1211     {
1212       typedef _Mem_fn<_Tp _Class::*> type;
1214       static type
1215       __do_wrap(_Tp _Class::* __pm)
1216       { return type(__pm); }
1217     };
1219   // Specialization needed to prevent "forming reference to void" errors when
1220   // bind<void>() is called, because argument deduction instantiates
1221   // _Maybe_wrap_member_pointer<void> outside the immediate context where
1222   // SFINAE applies.
1223   template<>
1224     struct _Maybe_wrap_member_pointer<void>
1225     {
1226       typedef void type;
1227     };
1229   // std::get<I> for volatile-qualified tuples
1230   template<std::size_t _Ind, typename... _Tp>
1231     inline auto
1232     __volget(volatile tuple<_Tp...>& __tuple)
1233     -> typename tuple_element<_Ind, tuple<_Tp...>>::type volatile&
1234     { return std::get<_Ind>(const_cast<tuple<_Tp...>&>(__tuple)); }
1236   // std::get<I> for const-volatile-qualified tuples
1237   template<std::size_t _Ind, typename... _Tp>
1238     inline auto
1239     __volget(const volatile tuple<_Tp...>& __tuple)
1240     -> typename tuple_element<_Ind, tuple<_Tp...>>::type const volatile&
1241     { return std::get<_Ind>(const_cast<const tuple<_Tp...>&>(__tuple)); }
1243   /// Type of the function object returned from bind().
1244   template<typename _Signature>
1245     struct _Bind;
1247    template<typename _Functor, typename... _Bound_args>
1248     class _Bind<_Functor(_Bound_args...)>
1249     : public _Weak_result_type<_Functor>
1250     {
1251       typedef _Bind __self_type;
1252       typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
1253         _Bound_indexes;
1255       _Functor _M_f;
1256       tuple<_Bound_args...> _M_bound_args;
1258       // Call unqualified
1259       template<typename _Result, typename... _Args, std::size_t... _Indexes>
1260         _Result
1261         __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>)
1262         {
1263           return _M_f(_Mu<_Bound_args>()
1264                       (get<_Indexes>(_M_bound_args), __args)...);
1265         }
1267       // Call as const
1268       template<typename _Result, typename... _Args, std::size_t... _Indexes>
1269         _Result
1270         __call_c(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>) const
1271         {
1272           return _M_f(_Mu<_Bound_args>()
1273                       (get<_Indexes>(_M_bound_args), __args)...);
1274         }
1276       // Call as volatile
1277       template<typename _Result, typename... _Args, std::size_t... _Indexes>
1278         _Result
1279         __call_v(tuple<_Args...>&& __args,
1280                  _Index_tuple<_Indexes...>) volatile
1281         {
1282           return _M_f(_Mu<_Bound_args>()
1283                       (__volget<_Indexes>(_M_bound_args), __args)...);
1284         }
1286       // Call as const volatile
1287       template<typename _Result, typename... _Args, std::size_t... _Indexes>
1288         _Result
1289         __call_c_v(tuple<_Args...>&& __args,
1290                    _Index_tuple<_Indexes...>) const volatile
1291         {
1292           return _M_f(_Mu<_Bound_args>()
1293                       (__volget<_Indexes>(_M_bound_args), __args)...);
1294         }
1296      public:
1297       template<typename... _Args>
1298         explicit _Bind(const _Functor& __f, _Args&&... __args)
1299         : _M_f(__f), _M_bound_args(std::forward<_Args>(__args)...)
1300         { }
1302       template<typename... _Args>
1303         explicit _Bind(_Functor&& __f, _Args&&... __args)
1304         : _M_f(std::move(__f)), _M_bound_args(std::forward<_Args>(__args)...)
1305         { }
1307       _Bind(const _Bind&) = default;
1309       _Bind(_Bind&& __b)
1310       : _M_f(std::move(__b._M_f)), _M_bound_args(std::move(__b._M_bound_args))
1311       { }
1313       // Call unqualified
1314       template<typename... _Args, typename _Result
1315         = decltype( std::declval<_Functor>()(
1316               _Mu<_Bound_args>()( std::declval<_Bound_args&>(),
1317                                   std::declval<tuple<_Args...>&>() )... ) )>
1318         _Result
1319         operator()(_Args&&... __args)
1320         {
1321           return this->__call<_Result>(
1322               std::forward_as_tuple(std::forward<_Args>(__args)...),
1323               _Bound_indexes());
1324         }
1326       // Call as const
1327       template<typename... _Args, typename _Result
1328         = decltype( std::declval<typename enable_if<(sizeof...(_Args) >= 0),
1329                        typename add_const<_Functor>::type>::type>()(
1330               _Mu<_Bound_args>()( std::declval<const _Bound_args&>(),
1331                                   std::declval<tuple<_Args...>&>() )... ) )>
1332         _Result
1333         operator()(_Args&&... __args) const
1334         {
1335           return this->__call_c<_Result>(
1336               std::forward_as_tuple(std::forward<_Args>(__args)...),
1337               _Bound_indexes());
1338         }
1340       // Call as volatile
1341       template<typename... _Args, typename _Result
1342         = decltype( std::declval<typename enable_if<(sizeof...(_Args) >= 0),
1343                        typename add_volatile<_Functor>::type>::type>()(
1344               _Mu<_Bound_args>()( std::declval<volatile _Bound_args&>(),
1345                                   std::declval<tuple<_Args...>&>() )... ) )>
1346         _Result
1347         operator()(_Args&&... __args) volatile
1348         {
1349           return this->__call_v<_Result>(
1350               std::forward_as_tuple(std::forward<_Args>(__args)...),
1351               _Bound_indexes());
1352         }
1354       // Call as const volatile
1355       template<typename... _Args, typename _Result
1356         = decltype( std::declval<typename enable_if<(sizeof...(_Args) >= 0),
1357                        typename add_cv<_Functor>::type>::type>()(
1358               _Mu<_Bound_args>()( std::declval<const volatile _Bound_args&>(),
1359                                   std::declval<tuple<_Args...>&>() )... ) )>
1360         _Result
1361         operator()(_Args&&... __args) const volatile
1362         {
1363           return this->__call_c_v<_Result>(
1364               std::forward_as_tuple(std::forward<_Args>(__args)...),
1365               _Bound_indexes());
1366         }
1367     };
1369   /// Type of the function object returned from bind<R>().
1370   template<typename _Result, typename _Signature>
1371     struct _Bind_result;
1373   template<typename _Result, typename _Functor, typename... _Bound_args>
1374     class _Bind_result<_Result, _Functor(_Bound_args...)>
1375     {
1376       typedef _Bind_result __self_type;
1377       typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
1378         _Bound_indexes;
1380       _Functor _M_f;
1381       tuple<_Bound_args...> _M_bound_args;
1383       // sfinae types
1384       template<typename _Res>
1385         struct __enable_if_void : enable_if<is_void<_Res>::value, int> { };
1386       template<typename _Res>
1387         struct __disable_if_void : enable_if<!is_void<_Res>::value, int> { };
1389       // Call unqualified
1390       template<typename _Res, typename... _Args, std::size_t... _Indexes>
1391         _Result
1392         __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1393             typename __disable_if_void<_Res>::type = 0)
1394         {
1395           return _M_f(_Mu<_Bound_args>()
1396                       (get<_Indexes>(_M_bound_args), __args)...);
1397         }
1399       // Call unqualified, return void
1400       template<typename _Res, typename... _Args, std::size_t... _Indexes>
1401         void
1402         __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1403             typename __enable_if_void<_Res>::type = 0)
1404         {
1405           _M_f(_Mu<_Bound_args>()
1406                (get<_Indexes>(_M_bound_args), __args)...);
1407         }
1409       // Call as const
1410       template<typename _Res, typename... _Args, std::size_t... _Indexes>
1411         _Result
1412         __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1413             typename __disable_if_void<_Res>::type = 0) const
1414         {
1415           return _M_f(_Mu<_Bound_args>()
1416                       (get<_Indexes>(_M_bound_args), __args)...);
1417         }
1419       // Call as const, return void
1420       template<typename _Res, typename... _Args, std::size_t... _Indexes>
1421         void
1422         __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1423             typename __enable_if_void<_Res>::type = 0) const
1424         {
1425           _M_f(_Mu<_Bound_args>()
1426                (get<_Indexes>(_M_bound_args),  __args)...);
1427         }
1429       // Call as volatile
1430       template<typename _Res, typename... _Args, std::size_t... _Indexes>
1431         _Result
1432         __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1433             typename __disable_if_void<_Res>::type = 0) volatile
1434         {
1435           return _M_f(_Mu<_Bound_args>()
1436                       (__volget<_Indexes>(_M_bound_args), __args)...);
1437         }
1439       // Call as volatile, return void
1440       template<typename _Res, typename... _Args, std::size_t... _Indexes>
1441         void
1442         __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1443             typename __enable_if_void<_Res>::type = 0) volatile
1444         {
1445           _M_f(_Mu<_Bound_args>()
1446                (__volget<_Indexes>(_M_bound_args), __args)...);
1447         }
1449       // Call as const volatile
1450       template<typename _Res, typename... _Args, std::size_t... _Indexes>
1451         _Result
1452         __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1453             typename __disable_if_void<_Res>::type = 0) const volatile
1454         {
1455           return _M_f(_Mu<_Bound_args>()
1456                       (__volget<_Indexes>(_M_bound_args), __args)...);
1457         }
1459       // Call as const volatile, return void
1460       template<typename _Res, typename... _Args, std::size_t... _Indexes>
1461         void
1462         __call(tuple<_Args...>&& __args,
1463                _Index_tuple<_Indexes...>,
1464             typename __enable_if_void<_Res>::type = 0) const volatile
1465         {
1466           _M_f(_Mu<_Bound_args>()
1467                (__volget<_Indexes>(_M_bound_args), __args)...);
1468         }
1470     public:
1471       typedef _Result result_type;
1473       template<typename... _Args>
1474         explicit _Bind_result(const _Functor& __f, _Args&&... __args)
1475         : _M_f(__f), _M_bound_args(std::forward<_Args>(__args)...)
1476         { }
1478       template<typename... _Args>
1479         explicit _Bind_result(_Functor&& __f, _Args&&... __args)
1480         : _M_f(std::move(__f)), _M_bound_args(std::forward<_Args>(__args)...)
1481         { }
1483       _Bind_result(const _Bind_result&) = default;
1485       _Bind_result(_Bind_result&& __b)
1486       : _M_f(std::move(__b._M_f)), _M_bound_args(std::move(__b._M_bound_args))
1487       { }
1489       // Call unqualified
1490       template<typename... _Args>
1491         result_type
1492         operator()(_Args&&... __args)
1493         {
1494           return this->__call<_Result>(
1495               std::forward_as_tuple(std::forward<_Args>(__args)...),
1496               _Bound_indexes());
1497         }
1499       // Call as const
1500       template<typename... _Args>
1501         result_type
1502         operator()(_Args&&... __args) const
1503         {
1504           return this->__call<_Result>(
1505               std::forward_as_tuple(std::forward<_Args>(__args)...),
1506               _Bound_indexes());
1507         }
1509       // Call as volatile
1510       template<typename... _Args>
1511         result_type
1512         operator()(_Args&&... __args) volatile
1513         {
1514           return this->__call<_Result>(
1515               std::forward_as_tuple(std::forward<_Args>(__args)...),
1516               _Bound_indexes());
1517         }
1519       // Call as const volatile
1520       template<typename... _Args>
1521         result_type
1522         operator()(_Args&&... __args) const volatile
1523         {
1524           return this->__call<_Result>(
1525               std::forward_as_tuple(std::forward<_Args>(__args)...),
1526               _Bound_indexes());
1527         }
1528     };
1530   /**
1531    *  @brief Class template _Bind is always a bind expression.
1532    *  @ingroup binders
1533    */
1534   template<typename _Signature>
1535     struct is_bind_expression<_Bind<_Signature> >
1536     : public true_type { };
1538   /**
1539    *  @brief Class template _Bind is always a bind expression.
1540    *  @ingroup binders
1541    */
1542   template<typename _Signature>
1543     struct is_bind_expression<const _Bind<_Signature> >
1544     : public true_type { };
1546   /**
1547    *  @brief Class template _Bind is always a bind expression.
1548    *  @ingroup binders
1549    */
1550   template<typename _Signature>
1551     struct is_bind_expression<volatile _Bind<_Signature> >
1552     : public true_type { };
1554   /**
1555    *  @brief Class template _Bind is always a bind expression.
1556    *  @ingroup binders
1557    */
1558   template<typename _Signature>
1559     struct is_bind_expression<const volatile _Bind<_Signature>>
1560     : public true_type { };
1562   /**
1563    *  @brief Class template _Bind_result is always a bind expression.
1564    *  @ingroup binders
1565    */
1566   template<typename _Result, typename _Signature>
1567     struct is_bind_expression<_Bind_result<_Result, _Signature>>
1568     : public true_type { };
1570   /**
1571    *  @brief Class template _Bind_result is always a bind expression.
1572    *  @ingroup binders
1573    */
1574   template<typename _Result, typename _Signature>
1575     struct is_bind_expression<const _Bind_result<_Result, _Signature>>
1576     : public true_type { };
1578   /**
1579    *  @brief Class template _Bind_result is always a bind expression.
1580    *  @ingroup binders
1581    */
1582   template<typename _Result, typename _Signature>
1583     struct is_bind_expression<volatile _Bind_result<_Result, _Signature>>
1584     : public true_type { };
1586   /**
1587    *  @brief Class template _Bind_result is always a bind expression.
1588    *  @ingroup binders
1589    */
1590   template<typename _Result, typename _Signature>
1591     struct is_bind_expression<const volatile _Bind_result<_Result, _Signature>>
1592     : public true_type { };
1594   // Trait type used to remove std::bind() from overload set via SFINAE
1595   // when first argument has integer type, so that std::bind() will
1596   // not be a better match than ::bind() from the BSD Sockets API.
1597   template<typename _Tp, typename _Tp2 = typename decay<_Tp>::type>
1598     using __is_socketlike = __or_<is_integral<_Tp2>, is_enum<_Tp2>>;
1600   template<bool _SocketLike, typename _Func, typename... _BoundArgs>
1601     struct _Bind_helper
1602     {
1603       typedef _Maybe_wrap_member_pointer<typename decay<_Func>::type>
1604         __maybe_type;
1605       typedef typename __maybe_type::type __func_type;
1606       typedef _Bind<__func_type(typename decay<_BoundArgs>::type...)> type;
1607     };
1609   // Partial specialization for is_socketlike == true, does not define
1610   // nested type so std::bind() will not participate in overload resolution
1611   // when the first argument might be a socket file descriptor.
1612   template<typename _Func, typename... _BoundArgs>
1613     struct _Bind_helper<true, _Func, _BoundArgs...>
1614     { };
1616   /**
1617    *  @brief Function template for std::bind.
1618    *  @ingroup binders
1619    */
1620   template<typename _Func, typename... _BoundArgs>
1621     inline typename
1622     _Bind_helper<__is_socketlike<_Func>::value, _Func, _BoundArgs...>::type
1623     bind(_Func&& __f, _BoundArgs&&... __args)
1624     {
1625       typedef _Bind_helper<false, _Func, _BoundArgs...> __helper_type;
1626       typedef typename __helper_type::__maybe_type __maybe_type;
1627       typedef typename __helper_type::type __result_type;
1628       return __result_type(__maybe_type::__do_wrap(std::forward<_Func>(__f)),
1629                            std::forward<_BoundArgs>(__args)...);
1630     }
1632   template<typename _Result, typename _Func, typename... _BoundArgs>
1633     struct _Bindres_helper
1634     {
1635       typedef _Maybe_wrap_member_pointer<typename decay<_Func>::type>
1636         __maybe_type;
1637       typedef typename __maybe_type::type __functor_type;
1638       typedef _Bind_result<_Result,
1639                            __functor_type(typename decay<_BoundArgs>::type...)>
1640         type;
1641     };
1643   /**
1644    *  @brief Function template for std::bind<R>.
1645    *  @ingroup binders
1646    */
1647   template<typename _Result, typename _Func, typename... _BoundArgs>
1648     inline
1649     typename _Bindres_helper<_Result, _Func, _BoundArgs...>::type
1650     bind(_Func&& __f, _BoundArgs&&... __args)
1651     {
1652       typedef _Bindres_helper<_Result, _Func, _BoundArgs...> __helper_type;
1653       typedef typename __helper_type::__maybe_type __maybe_type;
1654       typedef typename __helper_type::type __result_type;
1655       return __result_type(__maybe_type::__do_wrap(std::forward<_Func>(__f)),
1656                            std::forward<_BoundArgs>(__args)...);
1657     }
1659   template<typename _Signature>
1660     struct _Bind_simple;
1662   template<typename _Callable, typename... _Args>
1663     struct _Bind_simple<_Callable(_Args...)>
1664     {
1665       typedef typename result_of<_Callable(_Args...)>::type result_type;
1667       template<typename... _Args2, typename = typename
1668                enable_if< sizeof...(_Args) == sizeof...(_Args2)>::type>
1669         explicit
1670         _Bind_simple(const _Callable& __callable, _Args2&&... __args)
1671         : _M_bound(__callable, std::forward<_Args2>(__args)...)
1672         { }
1674       template<typename... _Args2, typename = typename
1675                enable_if< sizeof...(_Args) == sizeof...(_Args2)>::type>
1676         explicit
1677         _Bind_simple(_Callable&& __callable, _Args2&&... __args)
1678         : _M_bound(std::move(__callable), std::forward<_Args2>(__args)...)
1679         { }
1681       _Bind_simple(const _Bind_simple&) = default;
1682       _Bind_simple(_Bind_simple&&) = default;
1684       result_type
1685       operator()()
1686       {
1687         typedef typename _Build_index_tuple<sizeof...(_Args)>::__type _Indices;
1688         return _M_invoke(_Indices());
1689       }
1691     private:
1693       template<std::size_t... _Indices>
1694         typename result_of<_Callable(_Args...)>::type
1695         _M_invoke(_Index_tuple<_Indices...>)
1696         {
1697           // std::bind always forwards bound arguments as lvalues,
1698           // but this type can call functions which only accept rvalues.
1699           return std::forward<_Callable>(std::get<0>(_M_bound))(
1700               std::forward<_Args>(std::get<_Indices+1>(_M_bound))...);
1701         }
1703       std::tuple<_Callable, _Args...> _M_bound;
1704     };
1706   template<typename _Func, typename... _BoundArgs>
1707     struct _Bind_simple_helper
1708     {
1709       typedef _Maybe_wrap_member_pointer<typename decay<_Func>::type>
1710         __maybe_type;
1711       typedef typename __maybe_type::type __func_type;
1712       typedef _Bind_simple<__func_type(typename decay<_BoundArgs>::type...)>
1713         __type;
1714     };
1716   // Simplified version of std::bind for internal use, without support for
1717   // unbound arguments, placeholders or nested bind expressions.
1718   template<typename _Callable, typename... _Args>
1719     typename _Bind_simple_helper<_Callable, _Args...>::__type
1720     __bind_simple(_Callable&& __callable, _Args&&... __args)
1721     {
1722       typedef _Bind_simple_helper<_Callable, _Args...> __helper_type;
1723       typedef typename __helper_type::__maybe_type __maybe_type;
1724       typedef typename __helper_type::__type __result_type;
1725       return __result_type(
1726           __maybe_type::__do_wrap( std::forward<_Callable>(__callable)),
1727           std::forward<_Args>(__args)...);
1728     }
1730   /**
1731    *  @brief Exception class thrown when class template function's
1732    *  operator() is called with an empty target.
1733    *  @ingroup exceptions
1734    */
1735   class bad_function_call : public std::exception
1736   {
1737   public:
1738     virtual ~bad_function_call() noexcept;
1740     const char* what() const noexcept;
1741   };
1743   /**
1744    *  Trait identifying "location-invariant" types, meaning that the
1745    *  address of the object (or any of its members) will not escape.
1746    *  Also implies a trivial copy constructor and assignment operator.
1747    */
1748   template<typename _Tp>
1749     struct __is_location_invariant
1750     : __or_<is_pointer<_Tp>, is_member_pointer<_Tp>>::type
1751     { };
1753   class _Undefined_class;
1755   union _Nocopy_types
1756   {
1757     void*       _M_object;
1758     const void* _M_const_object;
1759     void (*_M_function_pointer)();
1760     void (_Undefined_class::*_M_member_pointer)();
1761   };
1763   union _Any_data
1764   {
1765     void*       _M_access()       { return &_M_pod_data[0]; }
1766     const void* _M_access() const { return &_M_pod_data[0]; }
1768     template<typename _Tp>
1769       _Tp&
1770       _M_access()
1771       { return *static_cast<_Tp*>(_M_access()); }
1773     template<typename _Tp>
1774       const _Tp&
1775       _M_access() const
1776       { return *static_cast<const _Tp*>(_M_access()); }
1778     _Nocopy_types _M_unused;
1779     char _M_pod_data[sizeof(_Nocopy_types)];
1780   };
1782   enum _Manager_operation
1783   {
1784     __get_type_info,
1785     __get_functor_ptr,
1786     __clone_functor,
1787     __destroy_functor
1788   };
1790   // Simple type wrapper that helps avoid annoying const problems
1791   // when casting between void pointers and pointers-to-pointers.
1792   template<typename _Tp>
1793     struct _Simple_type_wrapper
1794     {
1795       _Simple_type_wrapper(_Tp __value) : __value(__value) { }
1797       _Tp __value;
1798     };
1800   template<typename _Tp>
1801     struct __is_location_invariant<_Simple_type_wrapper<_Tp> >
1802     : __is_location_invariant<_Tp>
1803     { };
1805   // Converts a reference to a function object into a callable
1806   // function object.
1807   template<typename _Functor>
1808     inline _Functor&
1809     __callable_functor(_Functor& __f)
1810     { return __f; }
1812   template<typename _Member, typename _Class>
1813     inline _Mem_fn<_Member _Class::*>
1814     __callable_functor(_Member _Class::* &__p)
1815     { return std::mem_fn(__p); }
1817   template<typename _Member, typename _Class>
1818     inline _Mem_fn<_Member _Class::*>
1819     __callable_functor(_Member _Class::* const &__p)
1820     { return std::mem_fn(__p); }
1822   template<typename _Member, typename _Class>
1823     inline _Mem_fn<_Member _Class::*>
1824     __callable_functor(_Member _Class::* volatile &__p)
1825     { return std::mem_fn(__p); }
1827   template<typename _Member, typename _Class>
1828     inline _Mem_fn<_Member _Class::*>
1829     __callable_functor(_Member _Class::* const volatile &__p)
1830     { return std::mem_fn(__p); }
1832   template<typename _Signature>
1833     class function;
1835   /// Base class of all polymorphic function object wrappers.
1836   class _Function_base
1837   {
1838   public:
1839     static const std::size_t _M_max_size = sizeof(_Nocopy_types);
1840     static const std::size_t _M_max_align = __alignof__(_Nocopy_types);
1842     template<typename _Functor>
1843       class _Base_manager
1844       {
1845       protected:
1846         static const bool __stored_locally =
1847         (__is_location_invariant<_Functor>::value
1848          && sizeof(_Functor) <= _M_max_size
1849          && __alignof__(_Functor) <= _M_max_align
1850          && (_M_max_align % __alignof__(_Functor) == 0));
1852         typedef integral_constant<bool, __stored_locally> _Local_storage;
1854         // Retrieve a pointer to the function object
1855         static _Functor*
1856         _M_get_pointer(const _Any_data& __source)
1857         {
1858           const _Functor* __ptr =
1859             __stored_locally? std::__addressof(__source._M_access<_Functor>())
1860             /* have stored a pointer */ : __source._M_access<_Functor*>();
1861           return const_cast<_Functor*>(__ptr);
1862         }
1864         // Clone a location-invariant function object that fits within
1865         // an _Any_data structure.
1866         static void
1867         _M_clone(_Any_data& __dest, const _Any_data& __source, true_type)
1868         {
1869           new (__dest._M_access()) _Functor(__source._M_access<_Functor>());
1870         }
1872         // Clone a function object that is not location-invariant or
1873         // that cannot fit into an _Any_data structure.
1874         static void
1875         _M_clone(_Any_data& __dest, const _Any_data& __source, false_type)
1876         {
1877           __dest._M_access<_Functor*>() =
1878             new _Functor(*__source._M_access<_Functor*>());
1879         }
1881         // Destroying a location-invariant object may still require
1882         // destruction.
1883         static void
1884         _M_destroy(_Any_data& __victim, true_type)
1885         {
1886           __victim._M_access<_Functor>().~_Functor();
1887         }
1889         // Destroying an object located on the heap.
1890         static void
1891         _M_destroy(_Any_data& __victim, false_type)
1892         {
1893           delete __victim._M_access<_Functor*>();
1894         }
1896       public:
1897         static bool
1898         _M_manager(_Any_data& __dest, const _Any_data& __source,
1899                    _Manager_operation __op)
1900         {
1901           switch (__op)
1902             {
1903 #ifdef __GXX_RTTI
1904             case __get_type_info:
1905               __dest._M_access<const type_info*>() = &typeid(_Functor);
1906               break;
1907 #endif
1908             case __get_functor_ptr:
1909               __dest._M_access<_Functor*>() = _M_get_pointer(__source);
1910               break;
1912             case __clone_functor:
1913               _M_clone(__dest, __source, _Local_storage());
1914               break;
1916             case __destroy_functor:
1917               _M_destroy(__dest, _Local_storage());
1918               break;
1919             }
1920           return false;
1921         }
1923         static void
1924         _M_init_functor(_Any_data& __functor, _Functor&& __f)
1925         { _M_init_functor(__functor, std::move(__f), _Local_storage()); }
1927         template<typename _Signature>
1928           static bool
1929           _M_not_empty_function(const function<_Signature>& __f)
1930           { return static_cast<bool>(__f); }
1932         template<typename _Tp>
1933           static bool
1934           _M_not_empty_function(_Tp* const& __fp)
1935           { return __fp; }
1937         template<typename _Class, typename _Tp>
1938           static bool
1939           _M_not_empty_function(_Tp _Class::* const& __mp)
1940           { return __mp; }
1942         template<typename _Tp>
1943           static bool
1944           _M_not_empty_function(const _Tp&)
1945           { return true; }
1947       private:
1948         static void
1949         _M_init_functor(_Any_data& __functor, _Functor&& __f, true_type)
1950         { new (__functor._M_access()) _Functor(std::move(__f)); }
1952         static void
1953         _M_init_functor(_Any_data& __functor, _Functor&& __f, false_type)
1954         { __functor._M_access<_Functor*>() = new _Functor(std::move(__f)); }
1955       };
1957     template<typename _Functor>
1958       class _Ref_manager : public _Base_manager<_Functor*>
1959       {
1960         typedef _Function_base::_Base_manager<_Functor*> _Base;
1962       public:
1963         static bool
1964         _M_manager(_Any_data& __dest, const _Any_data& __source,
1965                    _Manager_operation __op)
1966         {
1967           switch (__op)
1968             {
1969 #ifdef __GXX_RTTI
1970             case __get_type_info:
1971               __dest._M_access<const type_info*>() = &typeid(_Functor);
1972               break;
1973 #endif
1974             case __get_functor_ptr:
1975               __dest._M_access<_Functor*>() = *_Base::_M_get_pointer(__source);
1976               return is_const<_Functor>::value;
1977               break;
1979             default:
1980               _Base::_M_manager(__dest, __source, __op);
1981             }
1982           return false;
1983         }
1985         static void
1986         _M_init_functor(_Any_data& __functor, reference_wrapper<_Functor> __f)
1987         {
1988           _Base::_M_init_functor(__functor, std::__addressof(__f.get()));
1989         }
1990       };
1992     _Function_base() : _M_manager(0) { }
1994     ~_Function_base()
1995     {
1996       if (_M_manager)
1997         _M_manager(_M_functor, _M_functor, __destroy_functor);
1998     }
2001     bool _M_empty() const { return !_M_manager; }
2003     typedef bool (*_Manager_type)(_Any_data&, const _Any_data&,
2004                                   _Manager_operation);
2006     _Any_data     _M_functor;
2007     _Manager_type _M_manager;
2008   };
2010   template<typename _Signature, typename _Functor>
2011     class _Function_handler;
2013   template<typename _Res, typename _Functor, typename... _ArgTypes>
2014     class _Function_handler<_Res(_ArgTypes...), _Functor>
2015     : public _Function_base::_Base_manager<_Functor>
2016     {
2017       typedef _Function_base::_Base_manager<_Functor> _Base;
2019     public:
2020       static _Res
2021       _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
2022       {
2023         return (*_Base::_M_get_pointer(__functor))(
2024             std::forward<_ArgTypes>(__args)...);
2025       }
2026     };
2028   template<typename _Functor, typename... _ArgTypes>
2029     class _Function_handler<void(_ArgTypes...), _Functor>
2030     : public _Function_base::_Base_manager<_Functor>
2031     {
2032       typedef _Function_base::_Base_manager<_Functor> _Base;
2034      public:
2035       static void
2036       _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
2037       {
2038         (*_Base::_M_get_pointer(__functor))(
2039             std::forward<_ArgTypes>(__args)...);
2040       }
2041     };
2043   template<typename _Res, typename _Functor, typename... _ArgTypes>
2044     class _Function_handler<_Res(_ArgTypes...), reference_wrapper<_Functor> >
2045     : public _Function_base::_Ref_manager<_Functor>
2046     {
2047       typedef _Function_base::_Ref_manager<_Functor> _Base;
2049      public:
2050       static _Res
2051       _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
2052       {
2053         return __callable_functor(**_Base::_M_get_pointer(__functor))(
2054               std::forward<_ArgTypes>(__args)...);
2055       }
2056     };
2058   template<typename _Functor, typename... _ArgTypes>
2059     class _Function_handler<void(_ArgTypes...), reference_wrapper<_Functor> >
2060     : public _Function_base::_Ref_manager<_Functor>
2061     {
2062       typedef _Function_base::_Ref_manager<_Functor> _Base;
2064      public:
2065       static void
2066       _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
2067       {
2068         __callable_functor(**_Base::_M_get_pointer(__functor))(
2069             std::forward<_ArgTypes>(__args)...);
2070       }
2071     };
2073   template<typename _Class, typename _Member, typename _Res,
2074            typename... _ArgTypes>
2075     class _Function_handler<_Res(_ArgTypes...), _Member _Class::*>
2076     : public _Function_handler<void(_ArgTypes...), _Member _Class::*>
2077     {
2078       typedef _Function_handler<void(_ArgTypes...), _Member _Class::*>
2079         _Base;
2081      public:
2082       static _Res
2083       _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
2084       {
2085         return std::mem_fn(_Base::_M_get_pointer(__functor)->__value)(
2086             std::forward<_ArgTypes>(__args)...);
2087       }
2088     };
2090   template<typename _Class, typename _Member, typename... _ArgTypes>
2091     class _Function_handler<void(_ArgTypes...), _Member _Class::*>
2092     : public _Function_base::_Base_manager<
2093                  _Simple_type_wrapper< _Member _Class::* > >
2094     {
2095       typedef _Member _Class::* _Functor;
2096       typedef _Simple_type_wrapper<_Functor> _Wrapper;
2097       typedef _Function_base::_Base_manager<_Wrapper> _Base;
2099     public:
2100       static bool
2101       _M_manager(_Any_data& __dest, const _Any_data& __source,
2102                  _Manager_operation __op)
2103       {
2104         switch (__op)
2105           {
2106 #ifdef __GXX_RTTI
2107           case __get_type_info:
2108             __dest._M_access<const type_info*>() = &typeid(_Functor);
2109             break;
2110 #endif
2111           case __get_functor_ptr:
2112             __dest._M_access<_Functor*>() =
2113               &_Base::_M_get_pointer(__source)->__value;
2114             break;
2116           default:
2117             _Base::_M_manager(__dest, __source, __op);
2118           }
2119         return false;
2120       }
2122       static void
2123       _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
2124       {
2125         std::mem_fn(_Base::_M_get_pointer(__functor)->__value)(
2126             std::forward<_ArgTypes>(__args)...);
2127       }
2128     };
2130   template<typename _From, typename _To>
2131     using __check_func_return_type
2132       = __or_<is_void<_To>, is_convertible<_From, _To>>;
2134   /**
2135    *  @brief Primary class template for std::function.
2136    *  @ingroup functors
2137    *
2138    *  Polymorphic function wrapper.
2139    */
2140   template<typename _Res, typename... _ArgTypes>
2141     class function<_Res(_ArgTypes...)>
2142     : public _Maybe_unary_or_binary_function<_Res, _ArgTypes...>,
2143       private _Function_base
2144     {
2145       typedef _Res _Signature_type(_ArgTypes...);
2147       template<typename _Functor>
2148         using _Invoke = decltype(__callable_functor(std::declval<_Functor&>())
2149                                  (std::declval<_ArgTypes>()...) );
2151       // Used so the return type convertibility checks aren't done when
2152       // performing overload resolution for copy construction/assignment.
2153       template<typename _Tp>
2154         using _NotSelf = __not_<is_same<_Tp, function>>;
2156       template<typename _Functor>
2157         using _Callable
2158           = __and_<_NotSelf<_Functor>,
2159                    __check_func_return_type<_Invoke<_Functor>, _Res>>;
2161       template<typename _Cond, typename _Tp>
2162         using _Requires = typename enable_if<_Cond::value, _Tp>::type;
2164     public:
2165       typedef _Res result_type;
2167       // [3.7.2.1] construct/copy/destroy
2169       /**
2170        *  @brief Default construct creates an empty function call wrapper.
2171        *  @post @c !(bool)*this
2172        */
2173       function() noexcept
2174       : _Function_base() { }
2176       /**
2177        *  @brief Creates an empty function call wrapper.
2178        *  @post @c !(bool)*this
2179        */
2180       function(nullptr_t) noexcept
2181       : _Function_base() { }
2183       /**
2184        *  @brief %Function copy constructor.
2185        *  @param __x A %function object with identical call signature.
2186        *  @post @c bool(*this) == bool(__x)
2187        *
2188        *  The newly-created %function contains a copy of the target of @a
2189        *  __x (if it has one).
2190        */
2191       function(const function& __x);
2193       /**
2194        *  @brief %Function move constructor.
2195        *  @param __x A %function object rvalue with identical call signature.
2196        *
2197        *  The newly-created %function contains the target of @a __x
2198        *  (if it has one).
2199        */
2200       function(function&& __x) : _Function_base()
2201       {
2202         __x.swap(*this);
2203       }
2205       // TODO: needs allocator_arg_t
2207       /**
2208        *  @brief Builds a %function that targets a copy of the incoming
2209        *  function object.
2210        *  @param __f A %function object that is callable with parameters of
2211        *  type @c T1, @c T2, ..., @c TN and returns a value convertible
2212        *  to @c Res.
2213        *
2214        *  The newly-created %function object will target a copy of 
2215        *  @a __f. If @a __f is @c reference_wrapper<F>, then this function
2216        *  object will contain a reference to the function object @c
2217        *  __f.get(). If @a __f is a NULL function pointer or NULL
2218        *  pointer-to-member, the newly-created object will be empty.
2219        *
2220        *  If @a __f is a non-NULL function pointer or an object of type @c
2221        *  reference_wrapper<F>, this function will not throw.
2222        */
2223       template<typename _Functor,
2224                typename = _Requires<_Callable<_Functor>, void>>
2225         function(_Functor);
2227       /**
2228        *  @brief %Function assignment operator.
2229        *  @param __x A %function with identical call signature.
2230        *  @post @c (bool)*this == (bool)x
2231        *  @returns @c *this
2232        *
2233        *  The target of @a __x is copied to @c *this. If @a __x has no
2234        *  target, then @c *this will be empty.
2235        *
2236        *  If @a __x targets a function pointer or a reference to a function
2237        *  object, then this operation will not throw an %exception.
2238        */
2239       function&
2240       operator=(const function& __x)
2241       {
2242         function(__x).swap(*this);
2243         return *this;
2244       }
2246       /**
2247        *  @brief %Function move-assignment operator.
2248        *  @param __x A %function rvalue with identical call signature.
2249        *  @returns @c *this
2250        *
2251        *  The target of @a __x is moved to @c *this. If @a __x has no
2252        *  target, then @c *this will be empty.
2253        *
2254        *  If @a __x targets a function pointer or a reference to a function
2255        *  object, then this operation will not throw an %exception.
2256        */
2257       function&
2258       operator=(function&& __x)
2259       {
2260         function(std::move(__x)).swap(*this);
2261         return *this;
2262       }
2264       /**
2265        *  @brief %Function assignment to zero.
2266        *  @post @c !(bool)*this
2267        *  @returns @c *this
2268        *
2269        *  The target of @c *this is deallocated, leaving it empty.
2270        */
2271       function&
2272       operator=(nullptr_t)
2273       {
2274         if (_M_manager)
2275           {
2276             _M_manager(_M_functor, _M_functor, __destroy_functor);
2277             _M_manager = 0;
2278             _M_invoker = 0;
2279           }
2280         return *this;
2281       }
2283       /**
2284        *  @brief %Function assignment to a new target.
2285        *  @param __f A %function object that is callable with parameters of
2286        *  type @c T1, @c T2, ..., @c TN and returns a value convertible
2287        *  to @c Res.
2288        *  @return @c *this
2289        *
2290        *  This  %function object wrapper will target a copy of @a
2291        *  __f. If @a __f is @c reference_wrapper<F>, then this function
2292        *  object will contain a reference to the function object @c
2293        *  __f.get(). If @a __f is a NULL function pointer or NULL
2294        *  pointer-to-member, @c this object will be empty.
2295        *
2296        *  If @a __f is a non-NULL function pointer or an object of type @c
2297        *  reference_wrapper<F>, this function will not throw.
2298        */
2299       template<typename _Functor>
2300         _Requires<_Callable<typename decay<_Functor>::type>, function&>
2301         operator=(_Functor&& __f)
2302         {
2303           function(std::forward<_Functor>(__f)).swap(*this);
2304           return *this;
2305         }
2307       /// @overload
2308       template<typename _Functor>
2309         function&
2310         operator=(reference_wrapper<_Functor> __f) noexcept
2311         {
2312           function(__f).swap(*this);
2313           return *this;
2314         }
2316       // [3.7.2.2] function modifiers
2318       /**
2319        *  @brief Swap the targets of two %function objects.
2320        *  @param __x A %function with identical call signature.
2321        *
2322        *  Swap the targets of @c this function object and @a __f. This
2323        *  function will not throw an %exception.
2324        */
2325       void swap(function& __x)
2326       {
2327         std::swap(_M_functor, __x._M_functor);
2328         std::swap(_M_manager, __x._M_manager);
2329         std::swap(_M_invoker, __x._M_invoker);
2330       }
2332       // TODO: needs allocator_arg_t
2333       /*
2334       template<typename _Functor, typename _Alloc>
2335         void
2336         assign(_Functor&& __f, const _Alloc& __a)
2337         {
2338           function(allocator_arg, __a,
2339                    std::forward<_Functor>(__f)).swap(*this);
2340         }
2341       */
2343       // [3.7.2.3] function capacity
2345       /**
2346        *  @brief Determine if the %function wrapper has a target.
2347        *
2348        *  @return @c true when this %function object contains a target,
2349        *  or @c false when it is empty.
2350        *
2351        *  This function will not throw an %exception.
2352        */
2353       explicit operator bool() const noexcept
2354       { return !_M_empty(); }
2356       // [3.7.2.4] function invocation
2358       /**
2359        *  @brief Invokes the function targeted by @c *this.
2360        *  @returns the result of the target.
2361        *  @throws bad_function_call when @c !(bool)*this
2362        *
2363        *  The function call operator invokes the target function object
2364        *  stored by @c this.
2365        */
2366       _Res operator()(_ArgTypes... __args) const;
2368 #ifdef __GXX_RTTI
2369       // [3.7.2.5] function target access
2370       /**
2371        *  @brief Determine the type of the target of this function object
2372        *  wrapper.
2373        *
2374        *  @returns the type identifier of the target function object, or
2375        *  @c typeid(void) if @c !(bool)*this.
2376        *
2377        *  This function will not throw an %exception.
2378        */
2379       const type_info& target_type() const noexcept;
2381       /**
2382        *  @brief Access the stored target function object.
2383        *
2384        *  @return Returns a pointer to the stored target function object,
2385        *  if @c typeid(Functor).equals(target_type()); otherwise, a NULL
2386        *  pointer.
2387        *
2388        * This function will not throw an %exception.
2389        */
2390       template<typename _Functor>       _Functor* target() noexcept;
2392       /// @overload
2393       template<typename _Functor> const _Functor* target() const noexcept;
2394 #endif
2396     private:
2397       typedef _Res (*_Invoker_type)(const _Any_data&, _ArgTypes...);
2398       _Invoker_type _M_invoker;
2399   };
2401   // Out-of-line member definitions.
2402   template<typename _Res, typename... _ArgTypes>
2403     function<_Res(_ArgTypes...)>::
2404     function(const function& __x)
2405     : _Function_base()
2406     {
2407       if (static_cast<bool>(__x))
2408         {
2409           _M_invoker = __x._M_invoker;
2410           _M_manager = __x._M_manager;
2411           __x._M_manager(_M_functor, __x._M_functor, __clone_functor);
2412         }
2413     }
2415   template<typename _Res, typename... _ArgTypes>
2416     template<typename _Functor, typename>
2417       function<_Res(_ArgTypes...)>::
2418       function(_Functor __f)
2419       : _Function_base()
2420       {
2421         typedef _Function_handler<_Signature_type, _Functor> _My_handler;
2423         if (_My_handler::_M_not_empty_function(__f))
2424           {
2425             _My_handler::_M_init_functor(_M_functor, std::move(__f));
2426             _M_invoker = &_My_handler::_M_invoke;
2427             _M_manager = &_My_handler::_M_manager;
2428           }
2429       }
2431   template<typename _Res, typename... _ArgTypes>
2432     _Res
2433     function<_Res(_ArgTypes...)>::
2434     operator()(_ArgTypes... __args) const
2435     {
2436       if (_M_empty())
2437         __throw_bad_function_call();
2438       return _M_invoker(_M_functor, std::forward<_ArgTypes>(__args)...);
2439     }
2441 #ifdef __GXX_RTTI
2442   template<typename _Res, typename... _ArgTypes>
2443     const type_info&
2444     function<_Res(_ArgTypes...)>::
2445     target_type() const noexcept
2446     {
2447       if (_M_manager)
2448         {
2449           _Any_data __typeinfo_result;
2450           _M_manager(__typeinfo_result, _M_functor, __get_type_info);
2451           return *__typeinfo_result._M_access<const type_info*>();
2452         }
2453       else
2454         return typeid(void);
2455     }
2457   template<typename _Res, typename... _ArgTypes>
2458     template<typename _Functor>
2459       _Functor*
2460       function<_Res(_ArgTypes...)>::
2461       target() noexcept
2462       {
2463         if (typeid(_Functor) == target_type() && _M_manager)
2464           {
2465             _Any_data __ptr;
2466             if (_M_manager(__ptr, _M_functor, __get_functor_ptr)
2467                 && !is_const<_Functor>::value)
2468               return 0;
2469             else
2470               return __ptr._M_access<_Functor*>();
2471           }
2472         else
2473           return 0;
2474       }
2476   template<typename _Res, typename... _ArgTypes>
2477     template<typename _Functor>
2478       const _Functor*
2479       function<_Res(_ArgTypes...)>::
2480       target() const noexcept
2481       {
2482         if (typeid(_Functor) == target_type() && _M_manager)
2483           {
2484             _Any_data __ptr;
2485             _M_manager(__ptr, _M_functor, __get_functor_ptr);
2486             return __ptr._M_access<const _Functor*>();
2487           }
2488         else
2489           return 0;
2490       }
2491 #endif
2493   // [20.7.15.2.6] null pointer comparisons
2495   /**
2496    *  @brief Compares a polymorphic function object wrapper against 0
2497    *  (the NULL pointer).
2498    *  @returns @c true if the wrapper has no target, @c false otherwise
2499    *
2500    *  This function will not throw an %exception.
2501    */
2502   template<typename _Res, typename... _Args>
2503     inline bool
2504     operator==(const function<_Res(_Args...)>& __f, nullptr_t) noexcept
2505     { return !static_cast<bool>(__f); }
2507   /// @overload
2508   template<typename _Res, typename... _Args>
2509     inline bool
2510     operator==(nullptr_t, const function<_Res(_Args...)>& __f) noexcept
2511     { return !static_cast<bool>(__f); }
2513   /**
2514    *  @brief Compares a polymorphic function object wrapper against 0
2515    *  (the NULL pointer).
2516    *  @returns @c false if the wrapper has no target, @c true otherwise
2517    *
2518    *  This function will not throw an %exception.
2519    */
2520   template<typename _Res, typename... _Args>
2521     inline bool
2522     operator!=(const function<_Res(_Args...)>& __f, nullptr_t) noexcept
2523     { return static_cast<bool>(__f); }
2525   /// @overload
2526   template<typename _Res, typename... _Args>
2527     inline bool
2528     operator!=(nullptr_t, const function<_Res(_Args...)>& __f) noexcept
2529     { return static_cast<bool>(__f); }
2531   // [20.7.15.2.7] specialized algorithms
2533   /**
2534    *  @brief Swap the targets of two polymorphic function object wrappers.
2535    *
2536    *  This function will not throw an %exception.
2537    */
2538   template<typename _Res, typename... _Args>
2539     inline void
2540     swap(function<_Res(_Args...)>& __x, function<_Res(_Args...)>& __y)
2541     { __x.swap(__y); }
2543 _GLIBCXX_END_NAMESPACE_VERSION
2544 } // namespace std
2546 #endif // C++11
2548 #endif // _GLIBCXX_FUNCTIONAL