OSDN Git Service

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