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