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