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