OSDN Git Service

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