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