OSDN Git Service

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