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