OSDN Git Service

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