OSDN Git Service

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