OSDN Git Service

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