OSDN Git Service

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