1 // TR1 functional header -*- C++ -*-
3 // Copyright (C) 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
30 /** @file tr1/functional
31 * This is a TR1 C++ Library header.
34 #ifndef _TR1_FUNCTIONAL
35 #define _TR1_FUNCTIONAL 1
37 #pragma GCC system_header
39 #include <cmath> // for std::frexp
40 #include <string> // for std::tr1::hash
44 #include <tr1/type_traits>
45 #include <ext/type_traits.h>
49 _GLIBCXX_BEGIN_NAMESPACE(_GLIBCXX_TR1)
51 template<typename _MemberPointer>
56 * Actual implementation of _Has_result_type, which uses SFINAE to
57 * determine if the type _Tp has a publicly-accessible member type
61 template<typename _Tp>
62 class _Has_result_type_helper : __sfinae_types
64 template<typename _Up>
68 template<typename _Up>
69 static __one __test(_Wrap_type<typename _Up::result_type>*);
71 template<typename _Up>
72 static __two __test(...);
75 static const bool value = sizeof(__test<_Tp>(0)) == 1;
78 template<typename _Tp>
79 struct _Has_result_type
80 : integral_constant<bool,
81 _Has_result_type_helper<typename remove_cv<_Tp>::type>::value>
86 * If we have found a result_type, extract it.
89 template<bool _Has_result_type, typename _Functor>
90 struct _Maybe_get_result_type
93 template<typename _Functor>
94 struct _Maybe_get_result_type<true, _Functor>
96 typedef typename _Functor::result_type result_type;
101 * Base class for any function object that has a weak result type, as
102 * defined in 3.3/3 of TR1.
105 template<typename _Functor>
106 struct _Weak_result_type_impl
107 : _Maybe_get_result_type<_Has_result_type<_Functor>::value, _Functor>
113 * Retrieve the result type for a function type.
116 template<typename _Res, typename... _ArgTypes>
117 struct _Weak_result_type_impl<_Res(_ArgTypes...)>
119 typedef _Res result_type;
124 * Retrieve the result type for a function reference.
127 template<typename _Res, typename... _ArgTypes>
128 struct _Weak_result_type_impl<_Res(&)(_ArgTypes...)>
130 typedef _Res result_type;
135 * Retrieve the result type for a function pointer.
138 template<typename _Res, typename... _ArgTypes>
139 struct _Weak_result_type_impl<_Res(*)(_ArgTypes...)>
141 typedef _Res result_type;
146 * Retrieve result type for a member function pointer.
149 template<typename _Res, typename _Class, typename... _ArgTypes>
150 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...)>
152 typedef _Res result_type;
157 * Retrieve result type for a const member function pointer.
160 template<typename _Res, typename _Class, typename... _ArgTypes>
161 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) const>
163 typedef _Res result_type;
168 * Retrieve result type for a volatile member function pointer.
171 template<typename _Res, typename _Class, typename... _ArgTypes>
172 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) volatile>
174 typedef _Res result_type;
179 * Retrieve result type for a const volatile member function pointer.
182 template<typename _Res, typename _Class, typename... _ArgTypes>
183 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...)const volatile>
185 typedef _Res result_type;
190 * Strip top-level cv-qualifiers from the function object and let
191 * _Weak_result_type_impl perform the real work.
194 template<typename _Functor>
195 struct _Weak_result_type
196 : _Weak_result_type_impl<typename remove_cv<_Functor>::type>
200 template<typename _Signature>
205 * Actual implementation of result_of. When _Has_result_type is
206 * true, gets its result from _Weak_result_type. Otherwise, uses
207 * the function object's member template result to extract the
211 template<bool _Has_result_type, typename _Signature>
212 struct _Result_of_impl;
214 // Handle member data pointers using _Mem_fn's logic
215 template<typename _Res, typename _Class, typename _T1>
216 struct _Result_of_impl<false, _Res _Class::*(_T1)>
218 typedef typename _Mem_fn<_Res _Class::*>
219 ::template _Result_type<_T1>::type type;
224 * Determine whether we can determine a result type from @c Functor
228 template<typename _Functor, typename... _ArgTypes>
229 class result_of<_Functor(_ArgTypes...)>
230 : public _Result_of_impl<
231 _Has_result_type<_Weak_result_type<_Functor> >::value,
232 _Functor(_ArgTypes...)>
238 * We already know the result type for @c Functor; use it.
241 template<typename _Functor, typename... _ArgTypes>
242 struct _Result_of_impl<true, _Functor(_ArgTypes...)>
244 typedef typename _Weak_result_type<_Functor>::result_type type;
249 * We need to compute the result type for this invocation the hard
253 template<typename _Functor, typename... _ArgTypes>
254 struct _Result_of_impl<false, _Functor(_ArgTypes...)>
256 typedef typename _Functor
257 ::template result<_Functor(_ArgTypes...)>::type type;
262 * It is unsafe to access ::result when there are zero arguments, so we
263 * return @c void instead.
266 template<typename _Functor>
267 struct _Result_of_impl<false, _Functor()>
274 * Determines if the type _Tp derives from unary_function.
277 template<typename _Tp>
278 struct _Derives_from_unary_function : __sfinae_types
281 template<typename _T1, typename _Res>
282 static __one __test(const volatile unary_function<_T1, _Res>*);
284 // It's tempting to change "..." to const volatile void*, but
285 // that fails when _Tp is a function type.
286 static __two __test(...);
289 static const bool value = sizeof(__test((_Tp*)0)) == 1;
294 * Determines if the type _Tp derives from binary_function.
297 template<typename _Tp>
298 struct _Derives_from_binary_function : __sfinae_types
301 template<typename _T1, typename _T2, typename _Res>
302 static __one __test(const volatile binary_function<_T1, _T2, _Res>*);
304 // It's tempting to change "..." to const volatile void*, but
305 // that fails when _Tp is a function type.
306 static __two __test(...);
309 static const bool value = sizeof(__test((_Tp*)0)) == 1;
314 * Turns a function type into a function pointer type
317 template<typename _Tp, bool _IsFunctionType = is_function<_Tp>::value>
318 struct _Function_to_function_pointer
323 template<typename _Tp>
324 struct _Function_to_function_pointer<_Tp, true>
331 * Invoke a function object, which may be either a member pointer or a
332 * function object. The first parameter will tell which.
335 template<typename _Functor, typename... _Args>
337 typename __gnu_cxx::__enable_if<
338 (!is_member_pointer<_Functor>::value
339 && !is_function<_Functor>::value
340 && !is_function<typename remove_pointer<_Functor>::type>::value),
341 typename result_of<_Functor(_Args...)>::type
343 __invoke(_Functor& __f, _Args&... __args)
345 return __f(__args...);
348 template<typename _Functor, typename... _Args>
350 typename __gnu_cxx::__enable_if<
351 (is_member_pointer<_Functor>::value
352 && !is_function<_Functor>::value
353 && !is_function<typename remove_pointer<_Functor>::type>::value),
354 typename result_of<_Functor(_Args...)>::type
356 __invoke(_Functor& __f, _Args&... __args)
358 return mem_fn(__f)(__args...);
361 // To pick up function references (that will become function pointers)
362 template<typename _Functor, typename... _Args>
364 typename __gnu_cxx::__enable_if<
365 (is_pointer<_Functor>::value
366 && is_function<typename remove_pointer<_Functor>::type>::value),
367 typename result_of<_Functor(_Args...)>::type
369 __invoke(_Functor __f, _Args&... __args)
371 return __f(__args...);
376 * Knowing which of unary_function and binary_function _Tp derives
377 * from, derives from the same and ensures that reference_wrapper
378 * will have a weak result type. See cases below.
381 template<bool _Unary, bool _Binary, typename _Tp>
382 struct _Reference_wrapper_base_impl;
384 // Not a unary_function or binary_function, so try a weak result type
385 template<typename _Tp>
386 struct _Reference_wrapper_base_impl<false, false, _Tp>
387 : _Weak_result_type<_Tp>
390 // unary_function but not binary_function
391 template<typename _Tp>
392 struct _Reference_wrapper_base_impl<true, false, _Tp>
393 : unary_function<typename _Tp::argument_type,
394 typename _Tp::result_type>
397 // binary_function but not unary_function
398 template<typename _Tp>
399 struct _Reference_wrapper_base_impl<false, true, _Tp>
400 : binary_function<typename _Tp::first_argument_type,
401 typename _Tp::second_argument_type,
402 typename _Tp::result_type>
405 // both unary_function and binary_function. import result_type to
407 template<typename _Tp>
408 struct _Reference_wrapper_base_impl<true, true, _Tp>
409 : unary_function<typename _Tp::argument_type,
410 typename _Tp::result_type>,
411 binary_function<typename _Tp::first_argument_type,
412 typename _Tp::second_argument_type,
413 typename _Tp::result_type>
415 typedef typename _Tp::result_type result_type;
420 * Derives from unary_function or binary_function when it
421 * can. Specializations handle all of the easy cases. The primary
422 * template determines what to do with a class type, which may
423 * derive from both unary_function and binary_function.
426 template<typename _Tp>
427 struct _Reference_wrapper_base
428 : _Reference_wrapper_base_impl<
429 _Derives_from_unary_function<_Tp>::value,
430 _Derives_from_binary_function<_Tp>::value,
434 // - a function type (unary)
435 template<typename _Res, typename _T1>
436 struct _Reference_wrapper_base<_Res(_T1)>
437 : unary_function<_T1, _Res>
440 // - a function type (binary)
441 template<typename _Res, typename _T1, typename _T2>
442 struct _Reference_wrapper_base<_Res(_T1, _T2)>
443 : binary_function<_T1, _T2, _Res>
446 // - a function pointer type (unary)
447 template<typename _Res, typename _T1>
448 struct _Reference_wrapper_base<_Res(*)(_T1)>
449 : unary_function<_T1, _Res>
452 // - a function pointer type (binary)
453 template<typename _Res, typename _T1, typename _T2>
454 struct _Reference_wrapper_base<_Res(*)(_T1, _T2)>
455 : binary_function<_T1, _T2, _Res>
458 // - a pointer to member function type (unary, no qualifiers)
459 template<typename _Res, typename _T1>
460 struct _Reference_wrapper_base<_Res (_T1::*)()>
461 : unary_function<_T1*, _Res>
464 // - a pointer to member function type (binary, no qualifiers)
465 template<typename _Res, typename _T1, typename _T2>
466 struct _Reference_wrapper_base<_Res (_T1::*)(_T2)>
467 : binary_function<_T1*, _T2, _Res>
470 // - a pointer to member function type (unary, const)
471 template<typename _Res, typename _T1>
472 struct _Reference_wrapper_base<_Res (_T1::*)() const>
473 : unary_function<const _T1*, _Res>
476 // - a pointer to member function type (binary, const)
477 template<typename _Res, typename _T1, typename _T2>
478 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const>
479 : binary_function<const _T1*, _T2, _Res>
482 // - a pointer to member function type (unary, volatile)
483 template<typename _Res, typename _T1>
484 struct _Reference_wrapper_base<_Res (_T1::*)() volatile>
485 : unary_function<volatile _T1*, _Res>
488 // - a pointer to member function type (binary, volatile)
489 template<typename _Res, typename _T1, typename _T2>
490 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) volatile>
491 : binary_function<volatile _T1*, _T2, _Res>
494 // - a pointer to member function type (unary, const volatile)
495 template<typename _Res, typename _T1>
496 struct _Reference_wrapper_base<_Res (_T1::*)() const volatile>
497 : unary_function<const volatile _T1*, _Res>
500 // - a pointer to member function type (binary, const volatile)
501 template<typename _Res, typename _T1, typename _T2>
502 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const volatile>
503 : binary_function<const volatile _T1*, _T2, _Res>
506 template<typename _Tp>
507 class reference_wrapper
508 : public _Reference_wrapper_base<typename remove_cv<_Tp>::type>
510 // If _Tp is a function type, we can't form result_of<_Tp(...)>,
511 // so turn it into a function pointer type.
512 typedef typename _Function_to_function_pointer<_Tp>::type
520 reference_wrapper(_Tp& __indata): _M_data(&__indata)
523 reference_wrapper(const reference_wrapper<_Tp>& __inref):
524 _M_data(__inref._M_data)
528 operator=(const reference_wrapper<_Tp>& __inref)
530 _M_data = __inref._M_data;
534 operator _Tp&() const
535 { return this->get(); }
541 template<typename... _Args>
542 typename result_of<_M_func_type(_Args...)>::type
543 operator()(_Args&... __args) const
545 return __invoke(get(), __args...);
550 // Denotes a reference should be taken to a variable.
551 template<typename _Tp>
552 inline reference_wrapper<_Tp>
554 { return reference_wrapper<_Tp>(__t); }
556 // Denotes a const reference should be taken to a variable.
557 template<typename _Tp>
558 inline reference_wrapper<const _Tp>
560 { return reference_wrapper<const _Tp>(__t); }
562 template<typename _Tp>
563 inline reference_wrapper<_Tp>
564 ref(reference_wrapper<_Tp> __t)
565 { return ref(__t.get()); }
567 template<typename _Tp>
568 inline reference_wrapper<const _Tp>
569 cref(reference_wrapper<_Tp> __t)
570 { return cref(__t.get()); }
572 template<typename _Tp, bool>
573 struct _Mem_fn_const_or_non
575 typedef const _Tp& type;
578 template<typename _Tp>
579 struct _Mem_fn_const_or_non<_Tp, false>
586 * Derives from @c unary_function or @c binary_function, or perhaps
587 * nothing, depending on the number of arguments provided. The
588 * primary template is the basis case, which derives nothing.
591 template<typename _Res, typename... _ArgTypes>
592 struct _Maybe_unary_or_binary_function { };
596 * Derives from @c unary_function, as appropriate.
599 template<typename _Res, typename _T1>
600 struct _Maybe_unary_or_binary_function<_Res, _T1>
601 : std::unary_function<_T1, _Res> { };
605 * Derives from @c binary_function, as appropriate.
608 template<typename _Res, typename _T1, typename _T2>
609 struct _Maybe_unary_or_binary_function<_Res, _T1, _T2>
610 : std::binary_function<_T1, _T2, _Res> { };
614 * Implementation of @c mem_fn for member function pointers.
617 template<typename _Res, typename _Class, typename... _ArgTypes>
618 class _Mem_fn<_Res (_Class::*)(_ArgTypes...)>
619 : public _Maybe_unary_or_binary_function<_Res, _Class*, _ArgTypes...>
621 typedef _Res (_Class::*_Functor)(_ArgTypes...);
623 template<typename _Tp>
625 _M_call(_Tp& __object, const volatile _Class *,
626 _ArgTypes... __args) const
627 { return (__object.*__pmf)(__args...); }
629 template<typename _Tp>
631 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
632 { return ((*__ptr).*__pmf)(__args...); }
635 typedef _Res result_type;
637 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
641 operator()(_Class& __object, _ArgTypes... __args) const
642 { return (__object.*__pmf)(__args...); }
646 operator()(_Class* __object, _ArgTypes... __args) const
647 { return (__object->*__pmf)(__args...); }
649 // Handle smart pointers, references and pointers to derived
650 template<typename _Tp>
652 operator()(_Tp& __object, _ArgTypes... __args) const
653 { return _M_call(__object, &__object, __args...); }
661 * Implementation of @c mem_fn for const member function pointers.
664 template<typename _Res, typename _Class, typename... _ArgTypes>
665 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) const>
666 : public _Maybe_unary_or_binary_function<_Res, const _Class*,
669 typedef _Res (_Class::*_Functor)(_ArgTypes...) const;
671 template<typename _Tp>
673 _M_call(_Tp& __object, const volatile _Class *,
674 _ArgTypes... __args) const
675 { return (__object.*__pmf)(__args...); }
677 template<typename _Tp>
679 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
680 { return ((*__ptr).*__pmf)(__args...); }
683 typedef _Res result_type;
685 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
689 operator()(const _Class& __object, _ArgTypes... __args) const
690 { return (__object.*__pmf)(__args...); }
694 operator()(const _Class* __object, _ArgTypes... __args) const
695 { return (__object->*__pmf)(__args...); }
697 // Handle smart pointers, references and pointers to derived
698 template<typename _Tp>
699 _Res operator()(_Tp& __object, _ArgTypes... __args) const
700 { return _M_call(__object, &__object, __args...); }
708 * Implementation of @c mem_fn for volatile member function pointers.
711 template<typename _Res, typename _Class, typename... _ArgTypes>
712 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) volatile>
713 : public _Maybe_unary_or_binary_function<_Res, volatile _Class*,
716 typedef _Res (_Class::*_Functor)(_ArgTypes...) volatile;
718 template<typename _Tp>
720 _M_call(_Tp& __object, const volatile _Class *,
721 _ArgTypes... __args) const
722 { return (__object.*__pmf)(__args...); }
724 template<typename _Tp>
726 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
727 { return ((*__ptr).*__pmf)(__args...); }
730 typedef _Res result_type;
732 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
736 operator()(volatile _Class& __object, _ArgTypes... __args) const
737 { return (__object.*__pmf)(__args...); }
741 operator()(volatile _Class* __object, _ArgTypes... __args) const
742 { return (__object->*__pmf)(__args...); }
744 // Handle smart pointers, references and pointers to derived
745 template<typename _Tp>
747 operator()(_Tp& __object, _ArgTypes... __args) const
748 { return _M_call(__object, &__object, __args...); }
756 * Implementation of @c mem_fn for const volatile member function pointers.
759 template<typename _Res, typename _Class, typename... _ArgTypes>
760 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) const volatile>
761 : public _Maybe_unary_or_binary_function<_Res, const volatile _Class*,
764 typedef _Res (_Class::*_Functor)(_ArgTypes...) const volatile;
766 template<typename _Tp>
768 _M_call(_Tp& __object, const volatile _Class *,
769 _ArgTypes... __args) const
770 { return (__object.*__pmf)(__args...); }
772 template<typename _Tp>
774 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
775 { return ((*__ptr).*__pmf)(__args...); }
778 typedef _Res result_type;
780 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
784 operator()(const volatile _Class& __object, _ArgTypes... __args) const
785 { return (__object.*__pmf)(__args...); }
789 operator()(const volatile _Class* __object, _ArgTypes... __args) const
790 { return (__object->*__pmf)(__args...); }
792 // Handle smart pointers, references and pointers to derived
793 template<typename _Tp>
794 _Res operator()(_Tp& __object, _ArgTypes... __args) const
795 { return _M_call(__object, &__object, __args...); }
802 template<typename _Res, typename _Class>
803 class _Mem_fn<_Res _Class::*>
805 // This bit of genius is due to Peter Dimov, improved slightly by
807 template<typename _Tp>
809 _M_call(_Tp& __object, _Class *) const
810 { return __object.*__pm; }
812 template<typename _Tp, typename _Up>
814 _M_call(_Tp& __object, _Up * const *) const
815 { return (*__object).*__pm; }
817 template<typename _Tp, typename _Up>
819 _M_call(_Tp& __object, const _Up * const *) const
820 { return (*__object).*__pm; }
822 template<typename _Tp>
824 _M_call(_Tp& __object, const _Class *) const
825 { return __object.*__pm; }
827 template<typename _Tp>
829 _M_call(_Tp& __ptr, const volatile void*) const
830 { return (*__ptr).*__pm; }
832 template<typename _Tp> static _Tp& __get_ref();
834 template<typename _Tp>
835 static __sfinae_types::__one __check_const(_Tp&, _Class*);
836 template<typename _Tp, typename _Up>
837 static __sfinae_types::__one __check_const(_Tp&, _Up * const *);
838 template<typename _Tp, typename _Up>
839 static __sfinae_types::__two __check_const(_Tp&, const _Up * const *);
840 template<typename _Tp>
841 static __sfinae_types::__two __check_const(_Tp&, const _Class*);
842 template<typename _Tp>
843 static __sfinae_types::__two __check_const(_Tp&, const volatile void*);
846 template<typename _Tp>
848 : _Mem_fn_const_or_non<_Res,
849 (sizeof(__sfinae_types::__two)
850 == sizeof(__check_const<_Tp>(__get_ref<_Tp>(), (_Tp*)0)))>
853 template<typename _Signature>
856 template<typename _CVMem, typename _Tp>
857 struct result<_CVMem(_Tp)>
858 : public _Result_type<_Tp> { };
860 template<typename _CVMem, typename _Tp>
861 struct result<_CVMem(_Tp&)>
862 : public _Result_type<_Tp> { };
865 _Mem_fn(_Res _Class::*__pm) : __pm(__pm) { }
869 operator()(_Class& __object) const
870 { return __object.*__pm; }
873 operator()(const _Class& __object) const
874 { return __object.*__pm; }
878 operator()(_Class* __object) const
879 { return __object->*__pm; }
882 operator()(const _Class* __object) const
883 { return __object->*__pm; }
885 // Handle smart pointers and derived
886 template<typename _Tp>
887 typename _Result_type<_Tp>::type
888 operator()(_Tp& __unknown) const
889 { return _M_call(__unknown, &__unknown); }
896 * @brief Returns a function object that forwards to the member
899 template<typename _Tp, typename _Class>
900 inline _Mem_fn<_Tp _Class::*>
901 mem_fn(_Tp _Class::* __pm)
903 return _Mem_fn<_Tp _Class::*>(__pm);
907 * @brief Determines if the given type _Tp is a function object
908 * should be treated as a subexpression when evaluating calls to
909 * function objects returned by bind(). [TR1 3.6.1]
911 template<typename _Tp>
912 struct is_bind_expression
913 { static const bool value = false; };
915 template<typename _Tp>
916 const bool is_bind_expression<_Tp>::value;
919 * @brief Determines if the given type _Tp is a placeholder in a
920 * bind() expression and, if so, which placeholder it is. [TR1 3.6.2]
922 template<typename _Tp>
923 struct is_placeholder
924 { static const int value = 0; };
926 template<typename _Tp>
927 const int is_placeholder<_Tp>::value;
931 * The type of placeholder objects defined by libstdc++.
934 template<int _Num> struct _Placeholder { };
936 // Define a large number of placeholders. There is no way to
937 // simplify this with variadic templates, because we're introducing
938 // unique names for each.
939 namespace placeholders { namespace {
949 _Placeholder<10> _10;
950 _Placeholder<11> _11;
951 _Placeholder<12> _12;
952 _Placeholder<13> _13;
953 _Placeholder<14> _14;
954 _Placeholder<15> _15;
955 _Placeholder<16> _16;
956 _Placeholder<17> _17;
957 _Placeholder<18> _18;
958 _Placeholder<19> _19;
959 _Placeholder<20> _20;
960 _Placeholder<21> _21;
961 _Placeholder<22> _22;
962 _Placeholder<23> _23;
963 _Placeholder<24> _24;
964 _Placeholder<25> _25;
965 _Placeholder<26> _26;
966 _Placeholder<27> _27;
967 _Placeholder<28> _28;
968 _Placeholder<29> _29;
973 * Partial specialization of is_placeholder that provides the placeholder
974 * number for the placeholder objects defined by libstdc++.
978 struct is_placeholder<_Placeholder<_Num> >
979 { static const int value = _Num; };
982 const int is_placeholder<_Placeholder<_Num> >::value;
986 * Stores a tuple of indices. Used by bind() to extract the elements
990 template<int... _Indexes>
991 struct _Index_tuple { };
995 * Builds an _Index_tuple<0, 1, 2, ..., _Num-1>.
998 template<std::size_t _Num, typename _Tuple = _Index_tuple<> >
999 struct _Build_index_tuple;
1001 template<std::size_t _Num, int... _Indexes>
1002 struct _Build_index_tuple<_Num, _Index_tuple<_Indexes...> >
1003 : _Build_index_tuple<_Num - 1,
1004 _Index_tuple<_Indexes..., sizeof...(_Indexes)> >
1008 template<int... _Indexes>
1009 struct _Build_index_tuple<0, _Index_tuple<_Indexes...> >
1011 typedef _Index_tuple<_Indexes...> __type;
1016 * Used by _Safe_tuple_element to indicate that there is no tuple
1017 * element at this position.
1020 struct _No_tuple_element;
1024 * Implementation helper for _Safe_tuple_element. This primary
1025 * template handles the case where it is safe to use @c
1029 template<int __i, typename _Tuple, bool _IsSafe>
1030 struct _Safe_tuple_element_impl
1031 : tuple_element<__i, _Tuple> { };
1035 * Implementation helper for _Safe_tuple_element. This partial
1036 * specialization handles the case where it is not safe to use @c
1037 * tuple_element. We just return @c _No_tuple_element.
1040 template<int __i, typename _Tuple>
1041 struct _Safe_tuple_element_impl<__i, _Tuple, false>
1043 typedef _No_tuple_element type;
1047 * Like tuple_element, but returns @c _No_tuple_element when
1048 * tuple_element would return an error.
1050 template<int __i, typename _Tuple>
1051 struct _Safe_tuple_element
1052 : _Safe_tuple_element_impl<__i, _Tuple,
1053 (__i >= 0 && __i < tuple_size<_Tuple>::value)>
1059 * Maps an argument to bind() into an actual argument to the bound
1060 * function object [TR1 3.6.3/5]. Only the first parameter should
1061 * be specified: the rest are used to determine among the various
1062 * implementations. Note that, although this class is a function
1063 * object, isn't not entirely normal because it takes only two
1064 * parameters regardless of the number of parameters passed to the
1065 * bind expression. The first parameter is the bound argument and
1066 * the second parameter is a tuple containing references to the
1067 * rest of the arguments.
1070 template<typename _Arg,
1071 bool _IsBindExp = is_bind_expression<_Arg>::value,
1072 bool _IsPlaceholder = (is_placeholder<_Arg>::value > 0)>
1077 * If the argument is reference_wrapper<_Tp>, returns the
1078 * underlying reference. [TR1 3.6.3/5 bullet 1]
1081 template<typename _Tp>
1082 class _Mu<reference_wrapper<_Tp>, false, false>
1085 typedef _Tp& result_type;
1087 /* Note: This won't actually work for const volatile
1088 * reference_wrappers, because reference_wrapper::get() is const
1089 * but not volatile-qualified. This might be a defect in the TR.
1091 template<typename _CVRef, typename _Tuple>
1093 operator()(_CVRef& __arg, const _Tuple&) const volatile
1094 { return __arg.get(); }
1099 * If the argument is a bind expression, we invoke the underlying
1100 * function object with the same cv-qualifiers as we are given and
1101 * pass along all of our arguments (unwrapped). [TR1 3.6.3/5 bullet 2]
1104 template<typename _Arg>
1105 class _Mu<_Arg, true, false>
1108 template<typename _Signature> class result;
1110 // Determine the result type when we pass the arguments along. This
1111 // involves passing along the cv-qualifiers placed on _Mu and
1112 // unwrapping the argument bundle.
1113 template<typename _CVMu, typename _CVArg, typename... _Args>
1114 class result<_CVMu(_CVArg, tuple<_Args...>)>
1115 : public result_of<_CVArg(_Args...)> { };
1117 template<typename _CVArg, typename... _Args>
1118 typename result_of<_CVArg(_Args...)>::type
1119 operator()(_CVArg& __arg,
1120 const tuple<_Args...>& __tuple) const volatile
1122 // Construct an index tuple and forward to __call
1123 typedef typename _Build_index_tuple<sizeof...(_Args)>::__type
1125 return this->__call(__arg, __tuple, _Indexes());
1129 // Invokes the underlying function object __arg by unpacking all
1130 // of the arguments in the tuple.
1131 template<typename _CVArg, typename... _Args, int... _Indexes>
1132 typename result_of<_CVArg(_Args...)>::type
1133 __call(_CVArg& __arg, const tuple<_Args...>& __tuple,
1134 const _Index_tuple<_Indexes...>&) const volatile
1136 return __arg(_GLIBCXX_TR1::get<_Indexes>(__tuple)...);
1142 * If the argument is a placeholder for the Nth argument, returns
1143 * a reference to the Nth argument to the bind function object.
1144 * [TR1 3.6.3/5 bullet 3]
1147 template<typename _Arg>
1148 class _Mu<_Arg, false, true>
1151 template<typename _Signature> class result;
1153 template<typename _CVMu, typename _CVArg, typename _Tuple>
1154 class result<_CVMu(_CVArg, _Tuple)>
1156 // Add a reference, if it hasn't already been done for us.
1157 // This allows us to be a little bit sloppy in constructing
1158 // the tuple that we pass to result_of<...>.
1159 typedef typename _Safe_tuple_element<(is_placeholder<_Arg>::value
1164 typedef typename add_reference<__base_type>::type type;
1167 template<typename _Tuple>
1168 typename result<_Mu(_Arg, _Tuple)>::type
1169 operator()(const volatile _Arg&, const _Tuple& __tuple) const volatile
1171 return ::std::_GLIBCXX_TR1::get<(is_placeholder<_Arg>::value
1178 * If the argument is just a value, returns a reference to that
1179 * value. The cv-qualifiers on the reference are the same as the
1180 * cv-qualifiers on the _Mu object. [TR1 3.6.3/5 bullet 4]
1183 template<typename _Arg>
1184 class _Mu<_Arg, false, false>
1187 template<typename _Signature> struct result;
1189 template<typename _CVMu, typename _CVArg, typename _Tuple>
1190 struct result<_CVMu(_CVArg, _Tuple)>
1192 typedef typename add_reference<_CVArg>::type type;
1195 // Pick up the cv-qualifiers of the argument
1196 template<typename _CVArg, typename _Tuple>
1198 operator()(_CVArg& __arg, const _Tuple&) const volatile
1204 * Maps member pointers into instances of _Mem_fn but leaves all
1205 * other function objects untouched. Used by tr1::bind(). The
1206 * primary template handles the non--member-pointer case.
1209 template<typename _Tp>
1210 struct _Maybe_wrap_member_pointer
1215 __do_wrap(const _Tp& __x)
1221 * Maps member pointers into instances of _Mem_fn but leaves all
1222 * other function objects untouched. Used by tr1::bind(). This
1223 * partial specialization handles the member pointer case.
1226 template<typename _Tp, typename _Class>
1227 struct _Maybe_wrap_member_pointer<_Tp _Class::*>
1229 typedef _Mem_fn<_Tp _Class::*> type;
1232 __do_wrap(_Tp _Class::* __pm)
1233 { return type(__pm); }
1238 * Type of the function object returned from bind().
1241 template<typename _Signature>
1244 template<typename _Functor, typename... _Bound_args>
1245 class _Bind<_Functor(_Bound_args...)>
1246 : public _Weak_result_type<_Functor>
1248 typedef _Bind __self_type;
1249 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
1253 tuple<_Bound_args...> _M_bound_args;
1256 template<typename... _Args, int... _Indexes>
1258 _Functor(typename result_of<_Mu<_Bound_args>
1259 (_Bound_args, tuple<_Args...>)>::type...)
1261 __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>)
1263 return _M_f(_Mu<_Bound_args>()
1264 (_GLIBCXX_TR1::get<_Indexes>(_M_bound_args), __args)...);
1268 template<typename... _Args, int... _Indexes>
1270 const _Functor(typename result_of<_Mu<_Bound_args>
1271 (const _Bound_args, tuple<_Args...>)
1273 __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>) const
1275 return _M_f(_Mu<_Bound_args>()
1276 (_GLIBCXX_TR1::get<_Indexes>(_M_bound_args), __args)...);
1280 template<typename... _Args, int... _Indexes>
1282 volatile _Functor(typename result_of<_Mu<_Bound_args>
1283 (volatile _Bound_args, tuple<_Args...>)
1285 __call(const tuple<_Args...>& __args,
1286 _Index_tuple<_Indexes...>) volatile
1288 return _M_f(_Mu<_Bound_args>()
1289 (_GLIBCXX_TR1::get<_Indexes>(_M_bound_args), __args)...);
1292 // Call as const volatile
1293 template<typename... _Args, int... _Indexes>
1295 const volatile _Functor(typename result_of<_Mu<_Bound_args>
1296 (const volatile _Bound_args,
1299 __call(const tuple<_Args...>& __args,
1300 _Index_tuple<_Indexes...>) const volatile
1302 return _M_f(_Mu<_Bound_args>()
1303 (_GLIBCXX_TR1::get<_Indexes>(_M_bound_args), __args)...);
1307 explicit _Bind(_Functor __f, _Bound_args... __bound_args)
1308 : _M_f(__f), _M_bound_args(__bound_args...) { }
1311 template<typename... _Args>
1313 _Functor(typename result_of<_Mu<_Bound_args>
1314 (_Bound_args, tuple<_Args...>)>::type...)
1316 operator()(_Args&... __args)
1318 return this->__call(_GLIBCXX_TR1::tie(__args...), _Bound_indexes());
1322 template<typename... _Args>
1324 const _Functor(typename result_of<_Mu<_Bound_args>
1325 (const _Bound_args, tuple<_Args...>)>::type...)
1327 operator()(_Args&... __args) const
1329 return this->__call(_GLIBCXX_TR1::tie(__args...), _Bound_indexes());
1334 template<typename... _Args>
1336 volatile _Functor(typename result_of<_Mu<_Bound_args>
1337 (volatile _Bound_args, tuple<_Args...>)>::type...)
1339 operator()(_Args&... __args) volatile
1341 return this->__call(_GLIBCXX_TR1::tie(__args...), _Bound_indexes());
1345 // Call as const volatile
1346 template<typename... _Args>
1348 const volatile _Functor(typename result_of<_Mu<_Bound_args>
1349 (const volatile _Bound_args,
1350 tuple<_Args...>)>::type...)
1352 operator()(_Args&... __args) const volatile
1354 return this->__call(_GLIBCXX_TR1::tie(__args...), _Bound_indexes());
1360 * Type of the function object returned from bind<R>().
1363 template<typename _Result, typename _Signature>
1364 struct _Bind_result;
1366 template<typename _Result, typename _Functor, typename... _Bound_args>
1367 class _Bind_result<_Result, _Functor(_Bound_args...)>
1369 typedef _Bind_result __self_type;
1370 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
1374 tuple<_Bound_args...> _M_bound_args;
1377 template<typename... _Args, int... _Indexes>
1379 __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>)
1381 return _M_f(_Mu<_Bound_args>()
1382 (_GLIBCXX_TR1::get<_Indexes>(_M_bound_args), __args)...);
1386 template<typename... _Args, int... _Indexes>
1388 __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>) const
1390 return _M_f(_Mu<_Bound_args>()
1391 (_GLIBCXX_TR1::get<_Indexes>(_M_bound_args), __args)...);
1395 template<typename... _Args, int... _Indexes>
1397 __call(const tuple<_Args...>& __args,
1398 _Index_tuple<_Indexes...>) volatile
1400 return _M_f(_Mu<_Bound_args>()
1401 (_GLIBCXX_TR1::get<_Indexes>(_M_bound_args), __args)...);
1404 // Call as const volatile
1405 template<typename... _Args, int... _Indexes>
1407 __call(const tuple<_Args...>& __args,
1408 _Index_tuple<_Indexes...>) const volatile
1410 return _M_f(_Mu<_Bound_args>()
1411 (_GLIBCXX_TR1::get<_Indexes>(_M_bound_args), __args)...);
1415 typedef _Result result_type;
1418 _Bind_result(_Functor __f, _Bound_args... __bound_args)
1419 : _M_f(__f), _M_bound_args(__bound_args...) { }
1422 template<typename... _Args>
1424 operator()(_Args&... __args)
1426 return this->__call(_GLIBCXX_TR1::tie(__args...), _Bound_indexes());
1430 template<typename... _Args>
1432 operator()(_Args&... __args) const
1434 return this->__call(_GLIBCXX_TR1::tie(__args...), _Bound_indexes());
1438 template<typename... _Args>
1440 operator()(_Args&... __args) volatile
1442 return this->__call(_GLIBCXX_TR1::tie(__args...), _Bound_indexes());
1445 // Call as const volatile
1446 template<typename... _Args>
1448 operator()(_Args&... __args) const volatile
1450 return this->__call(_GLIBCXX_TR1::tie(__args...), _Bound_indexes());
1456 * Class template _Bind is always a bind expression.
1459 template<typename _Signature>
1460 struct is_bind_expression<_Bind<_Signature> >
1461 { static const bool value = true; };
1463 template<typename _Signature>
1464 const bool is_bind_expression<_Bind<_Signature> >::value;
1468 * Class template _Bind_result is always a bind expression.
1471 template<typename _Result, typename _Signature>
1472 struct is_bind_expression<_Bind_result<_Result, _Signature> >
1473 { static const bool value = true; };
1475 template<typename _Result, typename _Signature>
1476 const bool is_bind_expression<_Bind_result<_Result, _Signature> >::value;
1478 template<typename _Functor, typename... _ArgTypes>
1480 _Bind<typename _Maybe_wrap_member_pointer<_Functor>::type(_ArgTypes...)>
1481 bind(_Functor __f, _ArgTypes... __args)
1483 typedef _Maybe_wrap_member_pointer<_Functor> __maybe_type;
1484 typedef typename __maybe_type::type __functor_type;
1485 typedef _Bind<__functor_type(_ArgTypes...)> __result_type;
1486 return __result_type(__maybe_type::__do_wrap(__f), __args...);
1489 template<typename _Result, typename _Functor, typename... _ArgTypes>
1491 _Bind_result<_Result,
1492 typename _Maybe_wrap_member_pointer<_Functor>::type
1494 bind(_Functor __f, _ArgTypes... __args)
1496 typedef _Maybe_wrap_member_pointer<_Functor> __maybe_type;
1497 typedef typename __maybe_type::type __functor_type;
1498 typedef _Bind_result<_Result, __functor_type(_ArgTypes...)>
1500 return __result_type(__maybe_type::__do_wrap(__f), __args...);
1504 * @brief Exception class thrown when class template function's
1505 * operator() is called with an empty target.
1508 class bad_function_call : public std::exception { };
1512 * The integral constant expression 0 can be converted into a
1513 * pointer to this type. It is used by the function template to
1514 * accept NULL pointers.
1517 struct _M_clear_type;
1521 * Trait identifying "location-invariant" types, meaning that the
1522 * address of the object (or any of its members) will not escape.
1523 * Also implies a trivial copy constructor and assignment operator.
1526 template<typename _Tp>
1527 struct __is_location_invariant
1528 : integral_constant<bool,
1529 (is_pointer<_Tp>::value
1530 || is_member_pointer<_Tp>::value)>
1534 class _Undefined_class;
1539 const void* _M_const_object;
1540 void (*_M_function_pointer)();
1541 void (_Undefined_class::*_M_member_pointer)();
1546 void* _M_access() { return &_M_pod_data[0]; }
1547 const void* _M_access() const { return &_M_pod_data[0]; }
1549 template<typename _Tp>
1552 { return *static_cast<_Tp*>(_M_access()); }
1554 template<typename _Tp>
1557 { return *static_cast<const _Tp*>(_M_access()); }
1559 _Nocopy_types _M_unused;
1560 char _M_pod_data[sizeof(_Nocopy_types)];
1563 enum _Manager_operation
1571 /* Simple type wrapper that helps avoid annoying const problems
1572 when casting between void pointers and pointers-to-pointers. */
1573 template<typename _Tp>
1574 struct _Simple_type_wrapper
1576 _Simple_type_wrapper(_Tp __value) : __value(__value) { }
1581 template<typename _Tp>
1582 struct __is_location_invariant<_Simple_type_wrapper<_Tp> >
1583 : __is_location_invariant<_Tp>
1587 // Converts a reference to a function object into a callable
1589 template<typename _Functor>
1591 __callable_functor(_Functor& __f)
1594 template<typename _Member, typename _Class>
1595 inline _Mem_fn<_Member _Class::*>
1596 __callable_functor(_Member _Class::* &__p)
1597 { return mem_fn(__p); }
1599 template<typename _Member, typename _Class>
1600 inline _Mem_fn<_Member _Class::*>
1601 __callable_functor(_Member _Class::* const &__p)
1602 { return mem_fn(__p); }
1604 template<typename _Signature>
1609 * Base class of all polymorphic function object wrappers.
1612 class _Function_base
1615 static const std::size_t _M_max_size = sizeof(_Nocopy_types);
1616 static const std::size_t _M_max_align = __alignof__(_Nocopy_types);
1618 template<typename _Functor>
1622 static const bool __stored_locally =
1623 (__is_location_invariant<_Functor>::value
1624 && sizeof(_Functor) <= _M_max_size
1625 && __alignof__(_Functor) <= _M_max_align
1626 && (_M_max_align % __alignof__(_Functor) == 0));
1628 typedef integral_constant<bool, __stored_locally> _Local_storage;
1630 // Retrieve a pointer to the function object
1632 _M_get_pointer(const _Any_data& __source)
1634 const _Functor* __ptr =
1635 __stored_locally? &__source._M_access<_Functor>()
1636 /* have stored a pointer */ : __source._M_access<_Functor*>();
1637 return const_cast<_Functor*>(__ptr);
1640 // Clone a location-invariant function object that fits within
1641 // an _Any_data structure.
1643 _M_clone(_Any_data& __dest, const _Any_data& __source, true_type)
1645 new (__dest._M_access()) _Functor(__source._M_access<_Functor>());
1648 // Clone a function object that is not location-invariant or
1649 // that cannot fit into an _Any_data structure.
1651 _M_clone(_Any_data& __dest, const _Any_data& __source, false_type)
1653 __dest._M_access<_Functor*>() =
1654 new _Functor(*__source._M_access<_Functor*>());
1657 // Destroying a location-invariant object may still require
1660 _M_destroy(_Any_data& __victim, true_type)
1662 __victim._M_access<_Functor>().~_Functor();
1665 // Destroying an object located on the heap.
1667 _M_destroy(_Any_data& __victim, false_type)
1669 delete __victim._M_access<_Functor*>();
1674 _M_manager(_Any_data& __dest, const _Any_data& __source,
1675 _Manager_operation __op)
1679 case __get_type_info:
1680 __dest._M_access<const type_info*>() = &typeid(_Functor);
1683 case __get_functor_ptr:
1684 __dest._M_access<_Functor*>() = _M_get_pointer(__source);
1687 case __clone_functor:
1688 _M_clone(__dest, __source, _Local_storage());
1691 case __destroy_functor:
1692 _M_destroy(__dest, _Local_storage());
1699 _M_init_functor(_Any_data& __functor, const _Functor& __f)
1701 _M_init_functor(__functor, __f, _Local_storage());
1704 template<typename _Signature>
1706 _M_not_empty_function(const function<_Signature>& __f)
1711 template<typename _Tp>
1713 _M_not_empty_function(const _Tp*& __fp)
1718 template<typename _Class, typename _Tp>
1720 _M_not_empty_function(_Tp _Class::* const& __mp)
1725 template<typename _Tp>
1727 _M_not_empty_function(const _Tp&)
1734 _M_init_functor(_Any_data& __functor, const _Functor& __f, true_type)
1736 new (__functor._M_access()) _Functor(__f);
1740 _M_init_functor(_Any_data& __functor, const _Functor& __f, false_type)
1742 __functor._M_access<_Functor*>() = new _Functor(__f);
1746 template<typename _Functor>
1747 class _Ref_manager : public _Base_manager<_Functor*>
1749 typedef _Function_base::_Base_manager<_Functor*> _Base;
1753 _M_manager(_Any_data& __dest, const _Any_data& __source,
1754 _Manager_operation __op)
1758 case __get_type_info:
1759 __dest._M_access<const type_info*>() = &typeid(_Functor);
1762 case __get_functor_ptr:
1763 __dest._M_access<_Functor*>() = *_Base::_M_get_pointer(__source);
1764 return is_const<_Functor>::value;
1768 _Base::_M_manager(__dest, __source, __op);
1774 _M_init_functor(_Any_data& __functor, reference_wrapper<_Functor> __f)
1776 // TBD: Use address_of function instead
1777 _Base::_M_init_functor(__functor, &__f.get());
1781 _Function_base() : _M_manager(0) { }
1786 _M_manager(_M_functor, _M_functor, __destroy_functor);
1790 bool _M_empty() const { return !_M_manager; }
1792 typedef bool (*_Manager_type)(_Any_data&, const _Any_data&,
1793 _Manager_operation);
1795 _Any_data _M_functor;
1796 _Manager_type _M_manager;
1799 template<typename _Signature, typename _Functor>
1800 class _Function_handler;
1802 template<typename _Res, typename _Functor, typename... _ArgTypes>
1803 class _Function_handler<_Res(_ArgTypes...), _Functor>
1804 : public _Function_base::_Base_manager<_Functor>
1806 typedef _Function_base::_Base_manager<_Functor> _Base;
1810 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1812 return (*_Base::_M_get_pointer(__functor))(__args...);
1816 template<typename _Functor, typename... _ArgTypes>
1817 class _Function_handler<void(_ArgTypes...), _Functor>
1818 : public _Function_base::_Base_manager<_Functor>
1820 typedef _Function_base::_Base_manager<_Functor> _Base;
1824 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1826 (*_Base::_M_get_pointer(__functor))(__args...);
1830 template<typename _Res, typename _Functor, typename... _ArgTypes>
1831 class _Function_handler<_Res(_ArgTypes...), reference_wrapper<_Functor> >
1832 : public _Function_base::_Ref_manager<_Functor>
1834 typedef _Function_base::_Ref_manager<_Functor> _Base;
1838 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1841 __callable_functor(**_Base::_M_get_pointer(__functor))(__args...);
1845 template<typename _Functor, typename... _ArgTypes>
1846 class _Function_handler<void(_ArgTypes...), reference_wrapper<_Functor> >
1847 : public _Function_base::_Ref_manager<_Functor>
1849 typedef _Function_base::_Ref_manager<_Functor> _Base;
1853 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1855 __callable_functor(**_Base::_M_get_pointer(__functor))(__args...);
1859 template<typename _Class, typename _Member, typename _Res,
1860 typename... _ArgTypes>
1861 class _Function_handler<_Res(_ArgTypes...), _Member _Class::*>
1862 : public _Function_handler<void(_ArgTypes...), _Member _Class::*>
1864 typedef _Function_handler<void(_ArgTypes...), _Member _Class::*>
1869 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1871 return _GLIBCXX_TR1::
1872 mem_fn(_Base::_M_get_pointer(__functor)->__value)(__args...);
1876 template<typename _Class, typename _Member, typename... _ArgTypes>
1877 class _Function_handler<void(_ArgTypes...), _Member _Class::*>
1878 : public _Function_base::_Base_manager<
1879 _Simple_type_wrapper< _Member _Class::* > >
1881 typedef _Member _Class::* _Functor;
1882 typedef _Simple_type_wrapper<_Functor> _Wrapper;
1883 typedef _Function_base::_Base_manager<_Wrapper> _Base;
1887 _M_manager(_Any_data& __dest, const _Any_data& __source,
1888 _Manager_operation __op)
1892 case __get_type_info:
1893 __dest._M_access<const type_info*>() = &typeid(_Functor);
1896 case __get_functor_ptr:
1897 __dest._M_access<_Functor*>() =
1898 &_Base::_M_get_pointer(__source)->__value;
1902 _Base::_M_manager(__dest, __source, __op);
1908 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1911 mem_fn(_Base::_M_get_pointer(__functor)->__value)(__args...);
1915 template<typename _Res, typename... _ArgTypes>
1916 class function<_Res(_ArgTypes...)>
1917 : public _Maybe_unary_or_binary_function<_Res, _ArgTypes...>,
1918 private _Function_base
1922 * This class is used to implement the safe_bool idiom.
1927 _Hidden_type* _M_bool;
1932 * This typedef is used to implement the safe_bool idiom.
1935 typedef _Hidden_type* _Hidden_type::* _Safe_bool;
1937 typedef _Res _Signature_type(_ArgTypes...);
1942 typedef _Res result_type;
1944 // [3.7.2.1] construct/copy/destroy
1947 * @brief Default construct creates an empty function call wrapper.
1948 * @post @c !(bool)*this
1950 function() : _Function_base() { }
1953 * @brief Default construct creates an empty function call wrapper.
1954 * @post @c !(bool)*this
1956 function(_M_clear_type*) : _Function_base() { }
1959 * @brief %Function copy constructor.
1960 * @param x A %function object with identical call signature.
1961 * @pre @c (bool)*this == (bool)x
1963 * The newly-created %function contains a copy of the target of @a
1964 * x (if it has one).
1966 function(const function& __x);
1969 * @brief Builds a %function that targets a copy of the incoming
1971 * @param f A %function object that is callable with parameters of
1972 * type @c T1, @c T2, ..., @c TN and returns a value convertible
1975 * The newly-created %function object will target a copy of @a
1976 * f. If @a f is @c reference_wrapper<F>, then this function
1977 * object will contain a reference to the function object @c
1978 * f.get(). If @a f is a NULL function pointer or NULL
1979 * pointer-to-member, the newly-created object will be empty.
1981 * If @a f is a non-NULL function pointer or an object of type @c
1982 * reference_wrapper<F>, this function will not throw.
1984 template<typename _Functor>
1985 function(_Functor __f,
1986 typename __gnu_cxx::__enable_if<
1987 !is_integral<_Functor>::value, _Useless>::__type
1991 * @brief %Function assignment operator.
1992 * @param x A %function with identical call signature.
1993 * @post @c (bool)*this == (bool)x
1996 * The target of @a x is copied to @c *this. If @a x has no
1997 * target, then @c *this will be empty.
1999 * If @a x targets a function pointer or a reference to a function
2000 * object, then this operation will not throw an exception.
2003 operator=(const function& __x)
2005 function(__x).swap(*this);
2010 * @brief %Function assignment to zero.
2011 * @post @c !(bool)*this
2014 * The target of @a *this is deallocated, leaving it empty.
2017 operator=(_M_clear_type*)
2021 _M_manager(_M_functor, _M_functor, __destroy_functor);
2029 * @brief %Function assignment to a new target.
2030 * @param f A %function object that is callable with parameters of
2031 * type @c T1, @c T2, ..., @c TN and returns a value convertible
2035 * This %function object wrapper will target a copy of @a
2036 * f. If @a f is @c reference_wrapper<F>, then this function
2037 * object will contain a reference to the function object @c
2038 * f.get(). If @a f is a NULL function pointer or NULL
2039 * pointer-to-member, @c this object will be empty.
2041 * If @a f is a non-NULL function pointer or an object of type @c
2042 * reference_wrapper<F>, this function will not throw.
2044 template<typename _Functor>
2045 typename __gnu_cxx::__enable_if<!is_integral<_Functor>::value,
2047 operator=(_Functor __f)
2049 function(__f).swap(*this);
2053 // [3.7.2.2] function modifiers
2056 * @brief Swap the targets of two %function objects.
2057 * @param f A %function with identical call signature.
2059 * Swap the targets of @c this function object and @a f. This
2060 * function will not throw an exception.
2062 void swap(function& __x)
2064 _Any_data __old_functor = _M_functor;
2065 _M_functor = __x._M_functor;
2066 __x._M_functor = __old_functor;
2067 _Manager_type __old_manager = _M_manager;
2068 _M_manager = __x._M_manager;
2069 __x._M_manager = __old_manager;
2070 _Invoker_type __old_invoker = _M_invoker;
2071 _M_invoker = __x._M_invoker;
2072 __x._M_invoker = __old_invoker;
2075 // [3.7.2.3] function capacity
2078 * @brief Determine if the %function wrapper has a target.
2080 * @return @c true when this %function object contains a target,
2081 * or @c false when it is empty.
2083 * This function will not throw an exception.
2085 operator _Safe_bool() const
2090 return &_Hidden_type::_M_bool;
2093 // [3.7.2.4] function invocation
2096 * @brief Invokes the function targeted by @c *this.
2097 * @returns the result of the target.
2098 * @throws bad_function_call when @c !(bool)*this
2100 * The function call operator invokes the target function object
2101 * stored by @c this.
2103 _Res operator()(_ArgTypes... __args) const;
2105 // [3.7.2.5] function target access
2107 * @brief Determine the type of the target of this function object
2110 * @returns the type identifier of the target function object, or
2111 * @c typeid(void) if @c !(bool)*this.
2113 * This function will not throw an exception.
2115 const type_info& target_type() const;
2118 * @brief Access the stored target function object.
2120 * @return Returns a pointer to the stored target function object,
2121 * if @c typeid(Functor).equals(target_type()); otherwise, a NULL
2124 * This function will not throw an exception.
2126 template<typename _Functor> _Functor* target();
2131 template<typename _Functor> const _Functor* target() const;
2134 // [3.7.2.6] undefined operators
2135 template<typename _Function>
2136 void operator==(const function<_Function>&) const;
2137 template<typename _Function>
2138 void operator!=(const function<_Function>&) const;
2140 typedef _Res (*_Invoker_type)(const _Any_data&, _ArgTypes...);
2141 _Invoker_type _M_invoker;
2144 template<typename _Res, typename... _ArgTypes>
2145 function<_Res(_ArgTypes...)>::
2146 function(const function& __x)
2151 _M_invoker = __x._M_invoker;
2152 _M_manager = __x._M_manager;
2153 __x._M_manager(_M_functor, __x._M_functor, __clone_functor);
2157 template<typename _Res, typename... _ArgTypes>
2158 template<typename _Functor>
2159 function<_Res(_ArgTypes...)>::
2160 function(_Functor __f,
2161 typename __gnu_cxx::__enable_if<
2162 !is_integral<_Functor>::value, _Useless>::__type)
2165 typedef _Function_handler<_Signature_type, _Functor> _My_handler;
2167 if (_My_handler::_M_not_empty_function(__f))
2169 _M_invoker = &_My_handler::_M_invoke;
2170 _M_manager = &_My_handler::_M_manager;
2171 _My_handler::_M_init_functor(_M_functor, __f);
2175 template<typename _Res, typename... _ArgTypes>
2177 function<_Res(_ArgTypes...)>::
2178 operator()(_ArgTypes... __args) const
2183 throw bad_function_call();
2188 return _M_invoker(_M_functor, __args...);
2191 template<typename _Res, typename... _ArgTypes>
2193 function<_Res(_ArgTypes...)>::
2198 _Any_data __typeinfo_result;
2199 _M_manager(__typeinfo_result, _M_functor, __get_type_info);
2200 return *__typeinfo_result._M_access<const type_info*>();
2203 return typeid(void);
2206 template<typename _Res, typename... _ArgTypes>
2207 template<typename _Functor>
2209 function<_Res(_ArgTypes...)>::
2212 if (typeid(_Functor) == target_type() && _M_manager)
2215 if (_M_manager(__ptr, _M_functor, __get_functor_ptr)
2216 && !is_const<_Functor>::value)
2219 return __ptr._M_access<_Functor*>();
2225 template<typename _Res, typename... _ArgTypes>
2226 template<typename _Functor>
2228 function<_Res(_ArgTypes...)>::
2231 if (typeid(_Functor) == target_type() && _M_manager)
2234 _M_manager(__ptr, _M_functor, __get_functor_ptr);
2235 return __ptr._M_access<const _Functor*>();
2241 // [3.7.2.7] null pointer comparisons
2244 * @brief Compares a polymorphic function object wrapper against 0
2245 * (the NULL pointer).
2246 * @returns @c true if the wrapper has no target, @c false otherwise
2248 * This function will not throw an exception.
2250 template<typename _Signature>
2252 operator==(const function<_Signature>& __f, _M_clear_type*)
2260 template<typename _Signature>
2262 operator==(_M_clear_type*, const function<_Signature>& __f)
2268 * @brief Compares a polymorphic function object wrapper against 0
2269 * (the NULL pointer).
2270 * @returns @c false if the wrapper has no target, @c true otherwise
2272 * This function will not throw an exception.
2274 template<typename _Signature>
2276 operator!=(const function<_Signature>& __f, _M_clear_type*)
2284 template<typename _Signature>
2286 operator!=(_M_clear_type*, const function<_Signature>& __f)
2291 // [3.7.2.8] specialized algorithms
2294 * @brief Swap the targets of two polymorphic function object wrappers.
2296 * This function will not throw an exception.
2298 template<typename _Signature>
2300 swap(function<_Signature>& __x, function<_Signature>& __y)
2305 _GLIBCXX_END_NAMESPACE
2308 #include <tr1/functional_hash.h>