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