1 // TR1 functional header -*- C++ -*-
3 // Copyright (C) 2004-2021 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 3, 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 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 /** @file tr1/functional
26 * This is a TR1 C++ Library header.
29 #ifndef _GLIBCXX_TR1_FUNCTIONAL
30 #define _GLIBCXX_TR1_FUNCTIONAL 1
32 #pragma GCC system_header
34 #include <functional> // for std::_Placeholder, std::_Bind, std::_Bind_result
39 #include <tr1/type_traits>
40 #include <bits/stringfwd.h>
41 #include <tr1/functional_hash.h>
42 #include <ext/type_traits.h>
43 #include <bits/move.h> // for std::__addressof
45 namespace std _GLIBCXX_VISIBILITY(default)
47 _GLIBCXX_BEGIN_NAMESPACE_VERSION
49 #if __cplusplus < 201103L
50 // In C++98 mode, <functional> doesn't declare std::placeholders::_1 etc.
51 // because they are not reserved names in C++98. However, they are reserved
52 // by <tr1/functional> so we can declare them here, in order to redeclare
53 // them in the std::tr1::placeholders namespace below.
54 namespace placeholders
56 extern const _Placeholder<1> _1;
57 extern const _Placeholder<2> _2;
58 extern const _Placeholder<3> _3;
59 extern const _Placeholder<4> _4;
60 extern const _Placeholder<5> _5;
61 extern const _Placeholder<6> _6;
62 extern const _Placeholder<7> _7;
63 extern const _Placeholder<8> _8;
64 extern const _Placeholder<9> _9;
65 extern const _Placeholder<10> _10;
66 extern const _Placeholder<11> _11;
67 extern const _Placeholder<12> _12;
68 extern const _Placeholder<13> _13;
69 extern const _Placeholder<14> _14;
70 extern const _Placeholder<15> _15;
71 extern const _Placeholder<16> _16;
72 extern const _Placeholder<17> _17;
73 extern const _Placeholder<18> _18;
74 extern const _Placeholder<19> _19;
75 extern const _Placeholder<20> _20;
76 extern const _Placeholder<21> _21;
77 extern const _Placeholder<22> _22;
78 extern const _Placeholder<23> _23;
79 extern const _Placeholder<24> _24;
80 extern const _Placeholder<25> _25;
81 extern const _Placeholder<26> _26;
82 extern const _Placeholder<27> _27;
83 extern const _Placeholder<28> _28;
84 extern const _Placeholder<29> _29;
90 template<typename _MemberPointer>
92 template<typename _Tp, typename _Class>
93 _Mem_fn<_Tp _Class::*>
94 mem_fn(_Tp _Class::*);
97 * Actual implementation of _Has_result_type, which uses SFINAE to
98 * determine if the type _Tp has a publicly-accessible member type
101 template<typename _Tp>
102 class _Has_result_type_helper : __sfinae_types
104 template<typename _Up>
108 template<typename _Up>
109 static __one __test(_Wrap_type<typename _Up::result_type>*);
111 template<typename _Up>
112 static __two __test(...);
115 static const bool value = sizeof(__test<_Tp>(0)) == 1;
118 template<typename _Tp>
119 struct _Has_result_type
120 : integral_constant<bool,
121 _Has_result_type_helper<typename remove_cv<_Tp>::type>::value>
127 /// If we have found a result_type, extract it.
128 template<bool _Has_result_type, typename _Functor>
129 struct _Maybe_get_result_type
132 template<typename _Functor>
133 struct _Maybe_get_result_type<true, _Functor>
135 typedef typename _Functor::result_type result_type;
139 * Base class for any function object that has a weak result type, as
140 * defined in 3.3/3 of TR1.
142 template<typename _Functor>
143 struct _Weak_result_type_impl
144 : _Maybe_get_result_type<_Has_result_type<_Functor>::value, _Functor>
148 /// Retrieve the result type for a function type.
149 template<typename _Res, typename... _ArgTypes>
150 struct _Weak_result_type_impl<_Res(_ArgTypes...)>
152 typedef _Res result_type;
155 /// Retrieve the result type for a function reference.
156 template<typename _Res, typename... _ArgTypes>
157 struct _Weak_result_type_impl<_Res(&)(_ArgTypes...)>
159 typedef _Res result_type;
162 /// Retrieve the result type for a function pointer.
163 template<typename _Res, typename... _ArgTypes>
164 struct _Weak_result_type_impl<_Res(*)(_ArgTypes...)>
166 typedef _Res result_type;
169 /// Retrieve result type for a member function pointer.
170 template<typename _Res, typename _Class, typename... _ArgTypes>
171 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...)>
173 typedef _Res result_type;
176 /// Retrieve result type for a const member function pointer.
177 template<typename _Res, typename _Class, typename... _ArgTypes>
178 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) const>
180 typedef _Res result_type;
183 /// Retrieve result type for a volatile member function pointer.
184 template<typename _Res, typename _Class, typename... _ArgTypes>
185 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) volatile>
187 typedef _Res result_type;
190 /// Retrieve result type for a const volatile member function pointer.
191 template<typename _Res, typename _Class, typename... _ArgTypes>
192 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...)const volatile>
194 typedef _Res result_type;
198 * Strip top-level cv-qualifiers from the function object and let
199 * _Weak_result_type_impl perform the real work.
201 template<typename _Functor>
202 struct _Weak_result_type
203 : _Weak_result_type_impl<typename remove_cv<_Functor>::type>
207 template<typename _Signature>
211 * Actual implementation of result_of. When _Has_result_type is
212 * true, gets its result from _Weak_result_type. Otherwise, uses
213 * the function object's member template result to extract the
216 template<bool _Has_result_type, typename _Signature>
217 struct _Result_of_impl;
219 // Handle member data pointers using _Mem_fn's logic
220 template<typename _Res, typename _Class, typename _T1>
221 struct _Result_of_impl<false, _Res _Class::*(_T1)>
223 typedef typename _Mem_fn<_Res _Class::*>
224 ::template _Result_type<_T1>::type type;
228 * Determine whether we can determine a result type from @c Functor
231 template<typename _Functor, typename... _ArgTypes>
232 class result_of<_Functor(_ArgTypes...)>
233 : public _Result_of_impl<
234 _Has_result_type<_Weak_result_type<_Functor> >::value,
235 _Functor(_ArgTypes...)>
239 /// We already know the result type for @c Functor; use it.
240 template<typename _Functor, typename... _ArgTypes>
241 struct _Result_of_impl<true, _Functor(_ArgTypes...)>
243 typedef typename _Weak_result_type<_Functor>::result_type type;
247 * We need to compute the result type for this invocation the hard
250 template<typename _Functor, typename... _ArgTypes>
251 struct _Result_of_impl<false, _Functor(_ArgTypes...)>
253 typedef typename _Functor
254 ::template result<_Functor(_ArgTypes...)>::type type;
258 * It is unsafe to access ::result when there are zero arguments, so we
259 * return @c void instead.
261 template<typename _Functor>
262 struct _Result_of_impl<false, _Functor()>
267 /// Determines if the type _Tp derives from unary_function.
268 template<typename _Tp>
269 struct _Derives_from_unary_function : __sfinae_types
272 template<typename _T1, typename _Res>
273 static __one __test(const volatile unary_function<_T1, _Res>*);
275 // It's tempting to change "..." to const volatile void*, but
276 // that fails when _Tp is a function type.
277 static __two __test(...);
280 static const bool value = sizeof(__test((_Tp*)0)) == 1;
283 /// Determines if the type _Tp derives from binary_function.
284 template<typename _Tp>
285 struct _Derives_from_binary_function : __sfinae_types
288 template<typename _T1, typename _T2, typename _Res>
289 static __one __test(const volatile binary_function<_T1, _T2, _Res>*);
291 // It's tempting to change "..." to const volatile void*, but
292 // that fails when _Tp is a function type.
293 static __two __test(...);
296 static const bool value = sizeof(__test((_Tp*)0)) == 1;
299 /// Turns a function type into a function pointer type
300 template<typename _Tp, bool _IsFunctionType = is_function<_Tp>::value>
301 struct _Function_to_function_pointer
306 template<typename _Tp>
307 struct _Function_to_function_pointer<_Tp, true>
313 * Invoke a function object, which may be either a member pointer or a
314 * function object. The first parameter will tell which.
316 template<typename _Functor, typename... _Args>
318 typename __gnu_cxx::__enable_if<
319 (!is_member_pointer<_Functor>::value
320 && !is_function<_Functor>::value
321 && !is_function<typename remove_pointer<_Functor>::type>::value),
322 typename result_of<_Functor(_Args...)>::type
324 __invoke(_Functor& __f, _Args&... __args)
326 return __f(__args...);
329 template<typename _Functor, typename... _Args>
331 typename __gnu_cxx::__enable_if<
332 (is_member_pointer<_Functor>::value
333 && !is_function<_Functor>::value
334 && !is_function<typename remove_pointer<_Functor>::type>::value),
335 typename result_of<_Functor(_Args...)>::type
337 __invoke(_Functor& __f, _Args&... __args)
339 return mem_fn(__f)(__args...);
342 // To pick up function references (that will become function pointers)
343 template<typename _Functor, typename... _Args>
345 typename __gnu_cxx::__enable_if<
346 (is_pointer<_Functor>::value
347 && is_function<typename remove_pointer<_Functor>::type>::value),
348 typename result_of<_Functor(_Args...)>::type
350 __invoke(_Functor __f, _Args&... __args)
352 return __f(__args...);
356 * Knowing which of unary_function and binary_function _Tp derives
357 * from, derives from the same and ensures that reference_wrapper
358 * will have a weak result type. See cases below.
360 template<bool _Unary, bool _Binary, typename _Tp>
361 struct _Reference_wrapper_base_impl;
363 // Not a unary_function or binary_function, so try a weak result type.
364 template<typename _Tp>
365 struct _Reference_wrapper_base_impl<false, false, _Tp>
366 : _Weak_result_type<_Tp>
369 // unary_function but not binary_function
370 template<typename _Tp>
371 struct _Reference_wrapper_base_impl<true, false, _Tp>
372 : unary_function<typename _Tp::argument_type,
373 typename _Tp::result_type>
376 // binary_function but not unary_function
377 template<typename _Tp>
378 struct _Reference_wrapper_base_impl<false, true, _Tp>
379 : binary_function<typename _Tp::first_argument_type,
380 typename _Tp::second_argument_type,
381 typename _Tp::result_type>
384 // Both unary_function and binary_function. Import result_type to
386 template<typename _Tp>
387 struct _Reference_wrapper_base_impl<true, true, _Tp>
388 : unary_function<typename _Tp::argument_type,
389 typename _Tp::result_type>,
390 binary_function<typename _Tp::first_argument_type,
391 typename _Tp::second_argument_type,
392 typename _Tp::result_type>
394 typedef typename _Tp::result_type result_type;
398 * Derives from unary_function or binary_function when it
399 * can. Specializations handle all of the easy cases. The primary
400 * template determines what to do with a class type, which may
401 * derive from both unary_function and binary_function.
403 template<typename _Tp>
404 struct _Reference_wrapper_base
405 : _Reference_wrapper_base_impl<
406 _Derives_from_unary_function<_Tp>::value,
407 _Derives_from_binary_function<_Tp>::value,
411 // - a function type (unary)
412 template<typename _Res, typename _T1>
413 struct _Reference_wrapper_base<_Res(_T1)>
414 : unary_function<_T1, _Res>
417 // - a function type (binary)
418 template<typename _Res, typename _T1, typename _T2>
419 struct _Reference_wrapper_base<_Res(_T1, _T2)>
420 : binary_function<_T1, _T2, _Res>
423 // - a function pointer type (unary)
424 template<typename _Res, typename _T1>
425 struct _Reference_wrapper_base<_Res(*)(_T1)>
426 : unary_function<_T1, _Res>
429 // - a function pointer type (binary)
430 template<typename _Res, typename _T1, typename _T2>
431 struct _Reference_wrapper_base<_Res(*)(_T1, _T2)>
432 : binary_function<_T1, _T2, _Res>
435 // - a pointer to member function type (unary, no qualifiers)
436 template<typename _Res, typename _T1>
437 struct _Reference_wrapper_base<_Res (_T1::*)()>
438 : unary_function<_T1*, _Res>
441 // - a pointer to member function type (binary, no qualifiers)
442 template<typename _Res, typename _T1, typename _T2>
443 struct _Reference_wrapper_base<_Res (_T1::*)(_T2)>
444 : binary_function<_T1*, _T2, _Res>
447 // - a pointer to member function type (unary, const)
448 template<typename _Res, typename _T1>
449 struct _Reference_wrapper_base<_Res (_T1::*)() const>
450 : unary_function<const _T1*, _Res>
453 // - a pointer to member function type (binary, const)
454 template<typename _Res, typename _T1, typename _T2>
455 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const>
456 : binary_function<const _T1*, _T2, _Res>
459 // - a pointer to member function type (unary, volatile)
460 template<typename _Res, typename _T1>
461 struct _Reference_wrapper_base<_Res (_T1::*)() volatile>
462 : unary_function<volatile _T1*, _Res>
465 // - a pointer to member function type (binary, volatile)
466 template<typename _Res, typename _T1, typename _T2>
467 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) volatile>
468 : binary_function<volatile _T1*, _T2, _Res>
471 // - a pointer to member function type (unary, const volatile)
472 template<typename _Res, typename _T1>
473 struct _Reference_wrapper_base<_Res (_T1::*)() const volatile>
474 : unary_function<const volatile _T1*, _Res>
477 // - a pointer to member function type (binary, const volatile)
478 template<typename _Res, typename _T1, typename _T2>
479 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const volatile>
480 : binary_function<const volatile _T1*, _T2, _Res>
483 /// reference_wrapper
484 template<typename _Tp>
485 class reference_wrapper
486 : public _Reference_wrapper_base<typename remove_cv<_Tp>::type>
488 // If _Tp is a function type, we can't form result_of<_Tp(...)>,
489 // so turn it into a function pointer type.
490 typedef typename _Function_to_function_pointer<_Tp>::type
498 reference_wrapper(_Tp& __indata)
499 : _M_data(std::__addressof(__indata))
502 reference_wrapper(const reference_wrapper<_Tp>& __inref):
503 _M_data(__inref._M_data)
507 operator=(const reference_wrapper<_Tp>& __inref)
509 _M_data = __inref._M_data;
513 operator _Tp&() const
514 { return this->get(); }
520 template<typename... _Args>
521 typename result_of<_M_func_type(_Args...)>::type
522 operator()(_Args&... __args) const
524 return __invoke(get(), __args...);
529 // Denotes a reference should be taken to a variable.
530 template<typename _Tp>
531 inline reference_wrapper<_Tp>
533 { return reference_wrapper<_Tp>(__t); }
535 // Denotes a const reference should be taken to a variable.
536 template<typename _Tp>
537 inline reference_wrapper<const _Tp>
539 { return reference_wrapper<const _Tp>(__t); }
541 template<typename _Tp>
542 inline reference_wrapper<_Tp>
543 ref(reference_wrapper<_Tp> __t)
544 { return ref(__t.get()); }
546 template<typename _Tp>
547 inline reference_wrapper<const _Tp>
548 cref(reference_wrapper<_Tp> __t)
549 { return cref(__t.get()); }
551 template<typename _Tp, bool>
552 struct _Mem_fn_const_or_non
554 typedef const _Tp& type;
557 template<typename _Tp>
558 struct _Mem_fn_const_or_non<_Tp, false>
564 * Derives from @c unary_function or @c binary_function, or perhaps
565 * nothing, depending on the number of arguments provided. The
566 * primary template is the basis case, which derives nothing.
568 template<typename _Res, typename... _ArgTypes>
569 struct _Maybe_unary_or_binary_function { };
571 /// Derives from @c unary_function, as appropriate.
572 template<typename _Res, typename _T1>
573 struct _Maybe_unary_or_binary_function<_Res, _T1>
574 : std::unary_function<_T1, _Res> { };
576 /// Derives from @c binary_function, as appropriate.
577 template<typename _Res, typename _T1, typename _T2>
578 struct _Maybe_unary_or_binary_function<_Res, _T1, _T2>
579 : std::binary_function<_T1, _T2, _Res> { };
581 /// Implementation of @c mem_fn for member function pointers.
582 template<typename _Res, typename _Class, typename... _ArgTypes>
583 class _Mem_fn<_Res (_Class::*)(_ArgTypes...)>
584 : public _Maybe_unary_or_binary_function<_Res, _Class*, _ArgTypes...>
586 typedef _Res (_Class::*_Functor)(_ArgTypes...);
588 template<typename _Tp>
590 _M_call(_Tp& __object, const volatile _Class *,
591 _ArgTypes... __args) const
592 { return (__object.*__pmf)(__args...); }
594 template<typename _Tp>
596 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
597 { return ((*__ptr).*__pmf)(__args...); }
600 typedef _Res result_type;
602 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
606 operator()(_Class& __object, _ArgTypes... __args) const
607 { return (__object.*__pmf)(__args...); }
611 operator()(_Class* __object, _ArgTypes... __args) const
612 { return (__object->*__pmf)(__args...); }
614 // Handle smart pointers, references and pointers to derived
615 template<typename _Tp>
617 operator()(_Tp& __object, _ArgTypes... __args) const
618 { return _M_call(__object, &__object, __args...); }
624 /// Implementation of @c mem_fn for const member function pointers.
625 template<typename _Res, typename _Class, typename... _ArgTypes>
626 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) const>
627 : public _Maybe_unary_or_binary_function<_Res, const _Class*,
630 typedef _Res (_Class::*_Functor)(_ArgTypes...) const;
632 template<typename _Tp>
634 _M_call(_Tp& __object, const volatile _Class *,
635 _ArgTypes... __args) const
636 { return (__object.*__pmf)(__args...); }
638 template<typename _Tp>
640 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
641 { return ((*__ptr).*__pmf)(__args...); }
644 typedef _Res result_type;
646 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
650 operator()(const _Class& __object, _ArgTypes... __args) const
651 { return (__object.*__pmf)(__args...); }
655 operator()(const _Class* __object, _ArgTypes... __args) const
656 { return (__object->*__pmf)(__args...); }
658 // Handle smart pointers, references and pointers to derived
659 template<typename _Tp>
660 _Res operator()(_Tp& __object, _ArgTypes... __args) const
661 { return _M_call(__object, &__object, __args...); }
667 /// Implementation of @c mem_fn for volatile member function pointers.
668 template<typename _Res, typename _Class, typename... _ArgTypes>
669 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) volatile>
670 : public _Maybe_unary_or_binary_function<_Res, volatile _Class*,
673 typedef _Res (_Class::*_Functor)(_ArgTypes...) volatile;
675 template<typename _Tp>
677 _M_call(_Tp& __object, const volatile _Class *,
678 _ArgTypes... __args) const
679 { return (__object.*__pmf)(__args...); }
681 template<typename _Tp>
683 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
684 { return ((*__ptr).*__pmf)(__args...); }
687 typedef _Res result_type;
689 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
693 operator()(volatile _Class& __object, _ArgTypes... __args) const
694 { return (__object.*__pmf)(__args...); }
698 operator()(volatile _Class* __object, _ArgTypes... __args) const
699 { return (__object->*__pmf)(__args...); }
701 // Handle smart pointers, references and pointers to derived
702 template<typename _Tp>
704 operator()(_Tp& __object, _ArgTypes... __args) const
705 { return _M_call(__object, &__object, __args...); }
711 /// Implementation of @c mem_fn for const volatile member function pointers.
712 template<typename _Res, typename _Class, typename... _ArgTypes>
713 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) const volatile>
714 : public _Maybe_unary_or_binary_function<_Res, const volatile _Class*,
717 typedef _Res (_Class::*_Functor)(_ArgTypes...) const volatile;
719 template<typename _Tp>
721 _M_call(_Tp& __object, const volatile _Class *,
722 _ArgTypes... __args) const
723 { return (__object.*__pmf)(__args...); }
725 template<typename _Tp>
727 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
728 { return ((*__ptr).*__pmf)(__args...); }
731 typedef _Res result_type;
733 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
737 operator()(const volatile _Class& __object, _ArgTypes... __args) const
738 { return (__object.*__pmf)(__args...); }
742 operator()(const volatile _Class* __object, _ArgTypes... __args) const
743 { return (__object->*__pmf)(__args...); }
745 // Handle smart pointers, references and pointers to derived
746 template<typename _Tp>
747 _Res operator()(_Tp& __object, _ArgTypes... __args) const
748 { return _M_call(__object, &__object, __args...); }
755 template<typename _Res, typename _Class>
756 class _Mem_fn<_Res _Class::*>
758 // This bit of genius is due to Peter Dimov, improved slightly by
760 template<typename _Tp>
762 _M_call(_Tp& __object, _Class *) const
763 { return __object.*__pm; }
765 template<typename _Tp, typename _Up>
767 _M_call(_Tp& __object, _Up * const *) const
768 { return (*__object).*__pm; }
770 template<typename _Tp, typename _Up>
772 _M_call(_Tp& __object, const _Up * const *) const
773 { return (*__object).*__pm; }
775 template<typename _Tp>
777 _M_call(_Tp& __object, const _Class *) const
778 { return __object.*__pm; }
780 template<typename _Tp>
782 _M_call(_Tp& __ptr, const volatile void*) const
783 { return (*__ptr).*__pm; }
785 template<typename _Tp> static _Tp& __get_ref();
787 template<typename _Tp>
788 static __sfinae_types::__one __check_const(_Tp&, _Class*);
789 template<typename _Tp, typename _Up>
790 static __sfinae_types::__one __check_const(_Tp&, _Up * const *);
791 template<typename _Tp, typename _Up>
792 static __sfinae_types::__two __check_const(_Tp&, const _Up * const *);
793 template<typename _Tp>
794 static __sfinae_types::__two __check_const(_Tp&, const _Class*);
795 template<typename _Tp>
796 static __sfinae_types::__two __check_const(_Tp&, const volatile void*);
799 template<typename _Tp>
801 : _Mem_fn_const_or_non<_Res,
802 (sizeof(__sfinae_types::__two)
803 == sizeof(__check_const<_Tp>(__get_ref<_Tp>(), (_Tp*)0)))>
806 template<typename _Signature>
809 template<typename _CVMem, typename _Tp>
810 struct result<_CVMem(_Tp)>
811 : public _Result_type<_Tp> { };
813 template<typename _CVMem, typename _Tp>
814 struct result<_CVMem(_Tp&)>
815 : public _Result_type<_Tp> { };
818 _Mem_fn(_Res _Class::*__pm) : __pm(__pm) { }
822 operator()(_Class& __object) const
823 { return __object.*__pm; }
826 operator()(const _Class& __object) const
827 { return __object.*__pm; }
831 operator()(_Class* __object) const
832 { return __object->*__pm; }
835 operator()(const _Class* __object) const
836 { return __object->*__pm; }
838 // Handle smart pointers and derived
839 template<typename _Tp>
840 typename _Result_type<_Tp>::type
841 operator()(_Tp& __unknown) const
842 { return _M_call(__unknown, &__unknown); }
849 * @brief Returns a function object that forwards to the member
852 template<typename _Tp, typename _Class>
853 inline _Mem_fn<_Tp _Class::*>
854 mem_fn(_Tp _Class::* __pm)
856 return _Mem_fn<_Tp _Class::*>(__pm);
860 * @brief Determines if the given type _Tp is a function object
861 * should be treated as a subexpression when evaluating calls to
862 * function objects returned by bind(). [TR1 3.6.1]
864 template<typename _Tp>
865 struct is_bind_expression
866 { static const bool value = false; };
868 template<typename _Tp>
869 const bool is_bind_expression<_Tp>::value;
872 * @brief Determines if the given type _Tp is a placeholder in a
873 * bind() expression and, if so, which placeholder it is. [TR1 3.6.2]
875 template<typename _Tp>
876 struct is_placeholder
877 { static const int value = 0; };
879 template<typename _Tp>
880 const int is_placeholder<_Tp>::value;
882 /// The type of placeholder objects defined by libstdc++.
883 using ::std::_Placeholder;
885 /** @namespace std::tr1::placeholders
886 * @brief Sub-namespace for tr1/functional.
888 namespace placeholders
890 // The C++11 std::placeholders are already exported from the library.
891 // Reusing them here avoids needing to export additional symbols for
892 // the TR1 placeholders, and avoids ODR violations due to defining
893 // them with internal linkage (as we used to do).
894 using namespace ::std::placeholders;
898 * Partial specialization of is_placeholder that provides the placeholder
899 * number for the placeholder objects defined by libstdc++.
902 struct is_placeholder<_Placeholder<_Num> >
903 : integral_constant<int, _Num>
907 struct is_placeholder<const _Placeholder<_Num> >
908 : integral_constant<int, _Num>
912 * Stores a tuple of indices. Used by bind() to extract the elements
915 template<int... _Indexes>
916 struct _Index_tuple { };
918 /// Builds an _Index_tuple<0, 1, 2, ..., _Num-1>.
919 template<std::size_t _Num, typename _Tuple = _Index_tuple<> >
920 struct _Build_index_tuple;
922 template<std::size_t _Num, int... _Indexes>
923 struct _Build_index_tuple<_Num, _Index_tuple<_Indexes...> >
924 : _Build_index_tuple<_Num - 1,
925 _Index_tuple<_Indexes..., sizeof...(_Indexes)> >
929 template<int... _Indexes>
930 struct _Build_index_tuple<0, _Index_tuple<_Indexes...> >
932 typedef _Index_tuple<_Indexes...> __type;
936 * Used by _Safe_tuple_element to indicate that there is no tuple
937 * element at this position.
939 struct _No_tuple_element;
942 * Implementation helper for _Safe_tuple_element. This primary
943 * template handles the case where it is safe to use @c
946 template<int __i, typename _Tuple, bool _IsSafe>
947 struct _Safe_tuple_element_impl
948 : tuple_element<__i, _Tuple> { };
951 * Implementation helper for _Safe_tuple_element. This partial
952 * specialization handles the case where it is not safe to use @c
953 * tuple_element. We just return @c _No_tuple_element.
955 template<int __i, typename _Tuple>
956 struct _Safe_tuple_element_impl<__i, _Tuple, false>
958 typedef _No_tuple_element type;
962 * Like tuple_element, but returns @c _No_tuple_element when
963 * tuple_element would return an error.
965 template<int __i, typename _Tuple>
966 struct _Safe_tuple_element
967 : _Safe_tuple_element_impl<__i, _Tuple,
968 (__i >= 0 && __i < tuple_size<_Tuple>::value)>
973 * Maps an argument to bind() into an actual argument to the bound
974 * function object [TR1 3.6.3/5]. Only the first parameter should
975 * be specified: the rest are used to determine among the various
976 * implementations. Note that, although this class is a function
977 * object, it isn't entirely normal because it takes only two
978 * parameters regardless of the number of parameters passed to the
979 * bind expression. The first parameter is the bound argument and
980 * the second parameter is a tuple containing references to the
981 * rest of the arguments.
983 template<typename _Arg,
984 bool _IsBindExp = is_bind_expression<_Arg>::value,
985 bool _IsPlaceholder = (is_placeholder<_Arg>::value > 0)>
989 * If the argument is reference_wrapper<_Tp>, returns the
990 * underlying reference. [TR1 3.6.3/5 bullet 1]
992 template<typename _Tp>
993 class _Mu<reference_wrapper<_Tp>, false, false>
996 typedef _Tp& result_type;
998 /* Note: This won't actually work for const volatile
999 * reference_wrappers, because reference_wrapper::get() is const
1000 * but not volatile-qualified. This might be a defect in the TR.
1002 template<typename _CVRef, typename _Tuple>
1004 operator()(_CVRef& __arg, const _Tuple&) const volatile
1005 { return __arg.get(); }
1009 * If the argument is a bind expression, we invoke the underlying
1010 * function object with the same cv-qualifiers as we are given and
1011 * pass along all of our arguments (unwrapped). [TR1 3.6.3/5 bullet 2]
1013 template<typename _Arg>
1014 class _Mu<_Arg, true, false>
1017 template<typename _Signature> class result;
1019 // Determine the result type when we pass the arguments along. This
1020 // involves passing along the cv-qualifiers placed on _Mu and
1021 // unwrapping the argument bundle.
1022 template<typename _CVMu, typename _CVArg, typename... _Args>
1023 class result<_CVMu(_CVArg, tuple<_Args...>)>
1024 : public result_of<_CVArg(_Args...)> { };
1026 template<typename _CVArg, typename... _Args>
1027 typename result_of<_CVArg(_Args...)>::type
1028 operator()(_CVArg& __arg,
1029 const tuple<_Args...>& __tuple) const volatile
1031 // Construct an index tuple and forward to __call
1032 typedef typename _Build_index_tuple<sizeof...(_Args)>::__type
1034 return this->__call(__arg, __tuple, _Indexes());
1038 // Invokes the underlying function object __arg by unpacking all
1039 // of the arguments in the tuple.
1040 template<typename _CVArg, typename... _Args, int... _Indexes>
1041 typename result_of<_CVArg(_Args...)>::type
1042 __call(_CVArg& __arg, const tuple<_Args...>& __tuple,
1043 const _Index_tuple<_Indexes...>&) const volatile
1045 return __arg(tr1::get<_Indexes>(__tuple)...);
1050 * If the argument is a placeholder for the Nth argument, returns
1051 * a reference to the Nth argument to the bind function object.
1052 * [TR1 3.6.3/5 bullet 3]
1054 template<typename _Arg>
1055 class _Mu<_Arg, false, true>
1058 template<typename _Signature> class result;
1060 template<typename _CVMu, typename _CVArg, typename _Tuple>
1061 class result<_CVMu(_CVArg, _Tuple)>
1063 // Add a reference, if it hasn't already been done for us.
1064 // This allows us to be a little bit sloppy in constructing
1065 // the tuple that we pass to result_of<...>.
1066 typedef typename _Safe_tuple_element<(is_placeholder<_Arg>::value
1071 typedef typename add_reference<__base_type>::type type;
1074 template<typename _Tuple>
1075 typename result<_Mu(_Arg, _Tuple)>::type
1076 operator()(const volatile _Arg&, const _Tuple& __tuple) const volatile
1078 return ::std::tr1::get<(is_placeholder<_Arg>::value - 1)>(__tuple);
1083 * If the argument is just a value, returns a reference to that
1084 * value. The cv-qualifiers on the reference are the same as the
1085 * cv-qualifiers on the _Mu object. [TR1 3.6.3/5 bullet 4]
1087 template<typename _Arg>
1088 class _Mu<_Arg, false, false>
1091 template<typename _Signature> struct result;
1093 template<typename _CVMu, typename _CVArg, typename _Tuple>
1094 struct result<_CVMu(_CVArg, _Tuple)>
1096 typedef typename add_reference<_CVArg>::type type;
1099 // Pick up the cv-qualifiers of the argument
1100 template<typename _CVArg, typename _Tuple>
1102 operator()(_CVArg& __arg, const _Tuple&) const volatile
1107 * Maps member pointers into instances of _Mem_fn but leaves all
1108 * other function objects untouched. Used by tr1::bind(). The
1109 * primary template handles the non--member-pointer case.
1111 template<typename _Tp>
1112 struct _Maybe_wrap_member_pointer
1117 __do_wrap(const _Tp& __x)
1122 * Maps member pointers into instances of _Mem_fn but leaves all
1123 * other function objects untouched. Used by tr1::bind(). This
1124 * partial specialization handles the member pointer case.
1126 template<typename _Tp, typename _Class>
1127 struct _Maybe_wrap_member_pointer<_Tp _Class::*>
1129 typedef _Mem_fn<_Tp _Class::*> type;
1132 __do_wrap(_Tp _Class::* __pm)
1133 { return type(__pm); }
1136 /// Type of the function object returned from bind().
1137 template<typename _Signature>
1140 template<typename _Functor, typename... _Bound_args>
1141 class _Bind<_Functor(_Bound_args...)>
1142 : public _Weak_result_type<_Functor>
1144 typedef _Bind __self_type;
1145 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
1149 tuple<_Bound_args...> _M_bound_args;
1152 template<typename... _Args, int... _Indexes>
1154 _Functor(typename result_of<_Mu<_Bound_args>
1155 (_Bound_args, tuple<_Args...>)>::type...)
1157 __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>)
1159 return _M_f(_Mu<_Bound_args>()
1160 (tr1::get<_Indexes>(_M_bound_args), __args)...);
1164 template<typename... _Args, int... _Indexes>
1166 const _Functor(typename result_of<_Mu<_Bound_args>
1167 (const _Bound_args, tuple<_Args...>)
1169 __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>) const
1171 return _M_f(_Mu<_Bound_args>()
1172 (tr1::get<_Indexes>(_M_bound_args), __args)...);
1176 template<typename... _Args, int... _Indexes>
1178 volatile _Functor(typename result_of<_Mu<_Bound_args>
1179 (volatile _Bound_args, tuple<_Args...>)
1181 __call(const tuple<_Args...>& __args,
1182 _Index_tuple<_Indexes...>) volatile
1184 return _M_f(_Mu<_Bound_args>()
1185 (tr1::get<_Indexes>(_M_bound_args), __args)...);
1188 // Call as const volatile
1189 template<typename... _Args, int... _Indexes>
1191 const volatile _Functor(typename result_of<_Mu<_Bound_args>
1192 (const volatile _Bound_args,
1195 __call(const tuple<_Args...>& __args,
1196 _Index_tuple<_Indexes...>) const volatile
1198 return _M_f(_Mu<_Bound_args>()
1199 (tr1::get<_Indexes>(_M_bound_args), __args)...);
1203 explicit _Bind(_Functor __f, _Bound_args... __bound_args)
1204 : _M_f(__f), _M_bound_args(__bound_args...) { }
1207 template<typename... _Args>
1209 _Functor(typename result_of<_Mu<_Bound_args>
1210 (_Bound_args, tuple<_Args...>)>::type...)
1212 operator()(_Args&... __args)
1214 return this->__call(tr1::tie(__args...), _Bound_indexes());
1218 template<typename... _Args>
1220 const _Functor(typename result_of<_Mu<_Bound_args>
1221 (const _Bound_args, tuple<_Args...>)>::type...)
1223 operator()(_Args&... __args) const
1225 return this->__call(tr1::tie(__args...), _Bound_indexes());
1230 template<typename... _Args>
1232 volatile _Functor(typename result_of<_Mu<_Bound_args>
1233 (volatile _Bound_args, tuple<_Args...>)>::type...)
1235 operator()(_Args&... __args) volatile
1237 return this->__call(tr1::tie(__args...), _Bound_indexes());
1241 // Call as const volatile
1242 template<typename... _Args>
1244 const volatile _Functor(typename result_of<_Mu<_Bound_args>
1245 (const volatile _Bound_args,
1246 tuple<_Args...>)>::type...)
1248 operator()(_Args&... __args) const volatile
1250 return this->__call(tr1::tie(__args...), _Bound_indexes());
1254 /// Type of the function object returned from bind<R>().
1255 template<typename _Result, typename _Signature>
1256 struct _Bind_result;
1258 template<typename _Result, typename _Functor, typename... _Bound_args>
1259 class _Bind_result<_Result, _Functor(_Bound_args...)>
1261 typedef _Bind_result __self_type;
1262 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
1266 tuple<_Bound_args...> _M_bound_args;
1269 template<typename... _Args, int... _Indexes>
1271 __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>)
1273 return _M_f(_Mu<_Bound_args>()
1274 (tr1::get<_Indexes>(_M_bound_args), __args)...);
1278 template<typename... _Args, int... _Indexes>
1280 __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>) const
1282 return _M_f(_Mu<_Bound_args>()
1283 (tr1::get<_Indexes>(_M_bound_args), __args)...);
1287 template<typename... _Args, int... _Indexes>
1289 __call(const tuple<_Args...>& __args,
1290 _Index_tuple<_Indexes...>) volatile
1292 return _M_f(_Mu<_Bound_args>()
1293 (tr1::get<_Indexes>(_M_bound_args), __args)...);
1296 // Call as const volatile
1297 template<typename... _Args, int... _Indexes>
1299 __call(const tuple<_Args...>& __args,
1300 _Index_tuple<_Indexes...>) const volatile
1302 return _M_f(_Mu<_Bound_args>()
1303 (tr1::get<_Indexes>(_M_bound_args), __args)...);
1307 typedef _Result result_type;
1310 _Bind_result(_Functor __f, _Bound_args... __bound_args)
1311 : _M_f(__f), _M_bound_args(__bound_args...) { }
1314 template<typename... _Args>
1316 operator()(_Args&... __args)
1318 return this->__call(tr1::tie(__args...), _Bound_indexes());
1322 template<typename... _Args>
1324 operator()(_Args&... __args) const
1326 return this->__call(tr1::tie(__args...), _Bound_indexes());
1330 template<typename... _Args>
1332 operator()(_Args&... __args) volatile
1334 return this->__call(tr1::tie(__args...), _Bound_indexes());
1337 // Call as const volatile
1338 template<typename... _Args>
1340 operator()(_Args&... __args) const volatile
1342 return this->__call(tr1::tie(__args...), _Bound_indexes());
1346 /// Class template _Bind is always a bind expression.
1347 template<typename _Signature>
1348 struct is_bind_expression<_Bind<_Signature> >
1349 { static const bool value = true; };
1351 template<typename _Signature>
1352 const bool is_bind_expression<_Bind<_Signature> >::value;
1354 /// Class template _Bind is always a bind expression.
1355 template<typename _Signature>
1356 struct is_bind_expression<const _Bind<_Signature> >
1357 { static const bool value = true; };
1359 template<typename _Signature>
1360 const bool is_bind_expression<const _Bind<_Signature> >::value;
1362 /// Class template _Bind is always a bind expression.
1363 template<typename _Signature>
1364 struct is_bind_expression<volatile _Bind<_Signature> >
1365 { static const bool value = true; };
1367 template<typename _Signature>
1368 const bool is_bind_expression<volatile _Bind<_Signature> >::value;
1370 /// Class template _Bind is always a bind expression.
1371 template<typename _Signature>
1372 struct is_bind_expression<const volatile _Bind<_Signature> >
1373 { static const bool value = true; };
1375 template<typename _Signature>
1376 const bool is_bind_expression<const volatile _Bind<_Signature> >::value;
1378 /// Class template _Bind_result is always a bind expression.
1379 template<typename _Result, typename _Signature>
1380 struct is_bind_expression<_Bind_result<_Result, _Signature> >
1381 { static const bool value = true; };
1383 template<typename _Result, typename _Signature>
1384 const bool is_bind_expression<_Bind_result<_Result, _Signature> >::value;
1386 /// Class template _Bind_result is always a bind expression.
1387 template<typename _Result, typename _Signature>
1388 struct is_bind_expression<const _Bind_result<_Result, _Signature> >
1389 { static const bool value = true; };
1391 template<typename _Result, typename _Signature>
1393 is_bind_expression<const _Bind_result<_Result, _Signature> >::value;
1395 /// Class template _Bind_result is always a bind expression.
1396 template<typename _Result, typename _Signature>
1397 struct is_bind_expression<volatile _Bind_result<_Result, _Signature> >
1398 { static const bool value = true; };
1400 template<typename _Result, typename _Signature>
1402 is_bind_expression<volatile _Bind_result<_Result, _Signature> >::value;
1404 /// Class template _Bind_result is always a bind expression.
1405 template<typename _Result, typename _Signature>
1407 is_bind_expression<const volatile _Bind_result<_Result, _Signature> >
1408 { static const bool value = true; };
1410 template<typename _Result, typename _Signature>
1412 is_bind_expression<const volatile _Bind_result<_Result,
1413 _Signature> >::value;
1415 #if __cplusplus >= 201103L
1416 // Specialize tr1::is_bind_expression for std::bind closure types,
1417 // so that they can also work with tr1::bind.
1419 template<typename _Signature>
1420 struct is_bind_expression<std::_Bind<_Signature>>
1423 template<typename _Signature>
1424 struct is_bind_expression<const std::_Bind<_Signature>>
1427 template<typename _Signature>
1428 struct is_bind_expression<volatile std::_Bind<_Signature>>
1431 template<typename _Signature>
1432 struct is_bind_expression<const volatile std::_Bind<_Signature>>
1435 template<typename _Result, typename _Signature>
1436 struct is_bind_expression<std::_Bind_result<_Result, _Signature>>
1439 template<typename _Result, typename _Signature>
1440 struct is_bind_expression<const std::_Bind_result<_Result, _Signature>>
1443 template<typename _Result, typename _Signature>
1444 struct is_bind_expression<volatile std::_Bind_result<_Result, _Signature>>
1447 template<typename _Result, typename _Signature>
1448 struct is_bind_expression<const volatile std::_Bind_result<_Result,
1454 template<typename _Functor, typename... _ArgTypes>
1456 _Bind<typename _Maybe_wrap_member_pointer<_Functor>::type(_ArgTypes...)>
1457 bind(_Functor __f, _ArgTypes... __args)
1459 typedef _Maybe_wrap_member_pointer<_Functor> __maybe_type;
1460 typedef typename __maybe_type::type __functor_type;
1461 typedef _Bind<__functor_type(_ArgTypes...)> __result_type;
1462 return __result_type(__maybe_type::__do_wrap(__f), __args...);
1465 template<typename _Result, typename _Functor, typename... _ArgTypes>
1467 _Bind_result<_Result,
1468 typename _Maybe_wrap_member_pointer<_Functor>::type
1470 bind(_Functor __f, _ArgTypes... __args)
1472 typedef _Maybe_wrap_member_pointer<_Functor> __maybe_type;
1473 typedef typename __maybe_type::type __functor_type;
1474 typedef _Bind_result<_Result, __functor_type(_ArgTypes...)>
1476 return __result_type(__maybe_type::__do_wrap(__f), __args...);
1480 * @brief Exception class thrown when class template function's
1481 * operator() is called with an empty target.
1482 * @ingroup exceptions
1484 class bad_function_call : public std::exception { };
1487 * The integral constant expression 0 can be converted into a
1488 * pointer to this type. It is used by the function template to
1489 * accept NULL pointers.
1491 struct _M_clear_type;
1494 * Trait identifying @a location-invariant types, meaning that the
1495 * address of the object (or any of its members) will not escape.
1496 * Also implies a trivial copy constructor and assignment operator.
1498 template<typename _Tp>
1499 struct __is_location_invariant
1500 : integral_constant<bool,
1501 (is_pointer<_Tp>::value
1502 || is_member_pointer<_Tp>::value)>
1506 class _Undefined_class;
1511 const void* _M_const_object;
1512 void (*_M_function_pointer)();
1513 void (_Undefined_class::*_M_member_pointer)();
1518 void* _M_access() { return &_M_pod_data[0]; }
1519 const void* _M_access() const { return &_M_pod_data[0]; }
1521 template<typename _Tp>
1524 { return *static_cast<_Tp*>(_M_access()); }
1526 template<typename _Tp>
1529 { return *static_cast<const _Tp*>(_M_access()); }
1531 _Nocopy_types _M_unused;
1532 char _M_pod_data[sizeof(_Nocopy_types)];
1535 enum _Manager_operation
1543 // Simple type wrapper that helps avoid annoying const problems
1544 // when casting between void pointers and pointers-to-pointers.
1545 template<typename _Tp>
1546 struct _Simple_type_wrapper
1548 _Simple_type_wrapper(_Tp __value) : __value(__value) { }
1553 template<typename _Tp>
1554 struct __is_location_invariant<_Simple_type_wrapper<_Tp> >
1555 : __is_location_invariant<_Tp>
1559 // Converts a reference to a function object into a callable
1561 template<typename _Functor>
1563 __callable_functor(_Functor& __f)
1566 template<typename _Member, typename _Class>
1567 inline _Mem_fn<_Member _Class::*>
1568 __callable_functor(_Member _Class::* &__p)
1569 { return mem_fn(__p); }
1571 template<typename _Member, typename _Class>
1572 inline _Mem_fn<_Member _Class::*>
1573 __callable_functor(_Member _Class::* const &__p)
1574 { return mem_fn(__p); }
1576 template<typename _Signature>
1579 /// Base class of all polymorphic function object wrappers.
1580 class _Function_base
1583 static const std::size_t _M_max_size = sizeof(_Nocopy_types);
1584 static const std::size_t _M_max_align = __alignof__(_Nocopy_types);
1586 template<typename _Functor>
1590 static const bool __stored_locally =
1591 (__is_location_invariant<_Functor>::value
1592 && sizeof(_Functor) <= _M_max_size
1593 && __alignof__(_Functor) <= _M_max_align
1594 && (_M_max_align % __alignof__(_Functor) == 0));
1596 typedef integral_constant<bool, __stored_locally> _Local_storage;
1598 // Retrieve a pointer to the function object
1600 _M_get_pointer(const _Any_data& __source)
1602 const _Functor* __ptr =
1603 __stored_locally? std::__addressof(__source._M_access<_Functor>())
1604 /* have stored a pointer */ : __source._M_access<_Functor*>();
1605 return const_cast<_Functor*>(__ptr);
1608 // Clone a location-invariant function object that fits within
1609 // an _Any_data structure.
1611 _M_clone(_Any_data& __dest, const _Any_data& __source, true_type)
1613 new (__dest._M_access()) _Functor(__source._M_access<_Functor>());
1616 // Clone a function object that is not location-invariant or
1617 // that cannot fit into an _Any_data structure.
1619 _M_clone(_Any_data& __dest, const _Any_data& __source, false_type)
1621 __dest._M_access<_Functor*>() =
1622 new _Functor(*__source._M_access<_Functor*>());
1625 // Destroying a location-invariant object may still require
1628 _M_destroy(_Any_data& __victim, true_type)
1630 __victim._M_access<_Functor>().~_Functor();
1633 // Destroying an object located on the heap.
1635 _M_destroy(_Any_data& __victim, false_type)
1637 delete __victim._M_access<_Functor*>();
1642 _M_manager(_Any_data& __dest, const _Any_data& __source,
1643 _Manager_operation __op)
1648 case __get_type_info:
1649 __dest._M_access<const type_info*>() = &typeid(_Functor);
1652 case __get_functor_ptr:
1653 __dest._M_access<_Functor*>() = _M_get_pointer(__source);
1656 case __clone_functor:
1657 _M_clone(__dest, __source, _Local_storage());
1660 case __destroy_functor:
1661 _M_destroy(__dest, _Local_storage());
1668 _M_init_functor(_Any_data& __functor, const _Functor& __f)
1669 { _M_init_functor(__functor, __f, _Local_storage()); }
1671 template<typename _Signature>
1673 _M_not_empty_function(const function<_Signature>& __f)
1674 { return static_cast<bool>(__f); }
1676 template<typename _Tp>
1678 _M_not_empty_function(const _Tp*& __fp)
1681 template<typename _Class, typename _Tp>
1683 _M_not_empty_function(_Tp _Class::* const& __mp)
1686 template<typename _Tp>
1688 _M_not_empty_function(const _Tp&)
1693 _M_init_functor(_Any_data& __functor, const _Functor& __f, true_type)
1694 { new (__functor._M_access()) _Functor(__f); }
1697 _M_init_functor(_Any_data& __functor, const _Functor& __f, false_type)
1698 { __functor._M_access<_Functor*>() = new _Functor(__f); }
1701 template<typename _Functor>
1702 class _Ref_manager : public _Base_manager<_Functor*>
1704 typedef _Function_base::_Base_manager<_Functor*> _Base;
1708 _M_manager(_Any_data& __dest, const _Any_data& __source,
1709 _Manager_operation __op)
1714 case __get_type_info:
1715 __dest._M_access<const type_info*>() = &typeid(_Functor);
1718 case __get_functor_ptr:
1719 __dest._M_access<_Functor*>() = *_Base::_M_get_pointer(__source);
1720 return is_const<_Functor>::value;
1724 _Base::_M_manager(__dest, __source, __op);
1730 _M_init_functor(_Any_data& __functor, reference_wrapper<_Functor> __f)
1732 _Base::_M_init_functor(__functor, std::__addressof(__f.get()));
1736 _Function_base() : _M_manager(0) { }
1741 _M_manager(_M_functor, _M_functor, __destroy_functor);
1745 bool _M_empty() const { return !_M_manager; }
1747 typedef bool (*_Manager_type)(_Any_data&, const _Any_data&,
1748 _Manager_operation);
1750 _Any_data _M_functor;
1751 _Manager_type _M_manager;
1754 template<typename _Signature, typename _Functor>
1755 class _Function_handler;
1757 template<typename _Res, typename _Functor, typename... _ArgTypes>
1758 class _Function_handler<_Res(_ArgTypes...), _Functor>
1759 : public _Function_base::_Base_manager<_Functor>
1761 typedef _Function_base::_Base_manager<_Functor> _Base;
1765 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1767 return (*_Base::_M_get_pointer(__functor))(__args...);
1771 template<typename _Functor, typename... _ArgTypes>
1772 class _Function_handler<void(_ArgTypes...), _Functor>
1773 : public _Function_base::_Base_manager<_Functor>
1775 typedef _Function_base::_Base_manager<_Functor> _Base;
1779 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1781 (*_Base::_M_get_pointer(__functor))(__args...);
1785 template<typename _Res, typename _Functor, typename... _ArgTypes>
1786 class _Function_handler<_Res(_ArgTypes...), reference_wrapper<_Functor> >
1787 : public _Function_base::_Ref_manager<_Functor>
1789 typedef _Function_base::_Ref_manager<_Functor> _Base;
1793 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1796 __callable_functor(**_Base::_M_get_pointer(__functor))(__args...);
1800 template<typename _Functor, typename... _ArgTypes>
1801 class _Function_handler<void(_ArgTypes...), reference_wrapper<_Functor> >
1802 : public _Function_base::_Ref_manager<_Functor>
1804 typedef _Function_base::_Ref_manager<_Functor> _Base;
1808 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1810 __callable_functor(**_Base::_M_get_pointer(__functor))(__args...);
1814 template<typename _Class, typename _Member, typename _Res,
1815 typename... _ArgTypes>
1816 class _Function_handler<_Res(_ArgTypes...), _Member _Class::*>
1817 : public _Function_handler<void(_ArgTypes...), _Member _Class::*>
1819 typedef _Function_handler<void(_ArgTypes...), _Member _Class::*>
1824 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1827 mem_fn(_Base::_M_get_pointer(__functor)->__value)(__args...);
1831 template<typename _Class, typename _Member, typename... _ArgTypes>
1832 class _Function_handler<void(_ArgTypes...), _Member _Class::*>
1833 : public _Function_base::_Base_manager<
1834 _Simple_type_wrapper< _Member _Class::* > >
1836 typedef _Member _Class::* _Functor;
1837 typedef _Simple_type_wrapper<_Functor> _Wrapper;
1838 typedef _Function_base::_Base_manager<_Wrapper> _Base;
1842 _M_manager(_Any_data& __dest, const _Any_data& __source,
1843 _Manager_operation __op)
1848 case __get_type_info:
1849 __dest._M_access<const type_info*>() = &typeid(_Functor);
1852 case __get_functor_ptr:
1853 __dest._M_access<_Functor*>() =
1854 &_Base::_M_get_pointer(__source)->__value;
1858 _Base::_M_manager(__dest, __source, __op);
1864 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1866 tr1::mem_fn(_Base::_M_get_pointer(__functor)->__value)(__args...);
1871 template<typename _Res, typename... _ArgTypes>
1872 class function<_Res(_ArgTypes...)>
1873 : public _Maybe_unary_or_binary_function<_Res, _ArgTypes...>,
1874 private _Function_base
1876 #if __cplusplus < 201103L
1877 /// This class is used to implement the safe_bool idiom.
1880 _Hidden_type* _M_bool;
1883 /// This typedef is used to implement the safe_bool idiom.
1884 typedef _Hidden_type* _Hidden_type::* _Safe_bool;
1887 typedef _Res _Signature_type(_ArgTypes...);
1889 struct _Useless { };
1892 typedef _Res result_type;
1894 // [3.7.2.1] construct/copy/destroy
1897 * @brief Default construct creates an empty function call wrapper.
1898 * @post @c !(bool)*this
1900 function() : _Function_base() { }
1903 * @brief Default construct creates an empty function call wrapper.
1904 * @post @c !(bool)*this
1906 function(_M_clear_type*) : _Function_base() { }
1909 * @brief %Function copy constructor.
1910 * @param x A %function object with identical call signature.
1911 * @post @c (bool)*this == (bool)x
1913 * The newly-created %function contains a copy of the target of @a
1914 * x (if it has one).
1916 function(const function& __x);
1919 * @brief Builds a %function that targets a copy of the incoming
1921 * @param f A %function object that is callable with parameters of
1922 * type @c T1, @c T2, ..., @c TN and returns a value convertible
1925 * The newly-created %function object will target a copy of @a
1926 * f. If @a f is @c reference_wrapper<F>, then this function
1927 * object will contain a reference to the function object @c
1928 * f.get(). If @a f is a NULL function pointer or NULL
1929 * pointer-to-member, the newly-created object will be empty.
1931 * If @a f is a non-NULL function pointer or an object of type @c
1932 * reference_wrapper<F>, this function will not throw.
1934 template<typename _Functor>
1935 function(_Functor __f,
1936 typename __gnu_cxx::__enable_if<
1937 !is_integral<_Functor>::value, _Useless>::__type
1941 * @brief %Function assignment operator.
1942 * @param x A %function with identical call signature.
1943 * @post @c (bool)*this == (bool)x
1946 * The target of @a x is copied to @c *this. If @a x has no
1947 * target, then @c *this will be empty.
1949 * If @a x targets a function pointer or a reference to a function
1950 * object, then this operation will not throw an %exception.
1953 operator=(const function& __x)
1955 function(__x).swap(*this);
1960 * @brief %Function assignment to zero.
1961 * @post @c !(bool)*this
1964 * The target of @c *this is deallocated, leaving it empty.
1967 operator=(_M_clear_type*)
1971 _M_manager(_M_functor, _M_functor, __destroy_functor);
1979 * @brief %Function assignment to a new target.
1980 * @param f A %function object that is callable with parameters of
1981 * type @c T1, @c T2, ..., @c TN and returns a value convertible
1985 * This %function object wrapper will target a copy of @a
1986 * f. If @a f is @c reference_wrapper<F>, then this function
1987 * object will contain a reference to the function object @c
1988 * f.get(). If @a f is a NULL function pointer or NULL
1989 * pointer-to-member, @c this object will be empty.
1991 * If @a f is a non-NULL function pointer or an object of type @c
1992 * reference_wrapper<F>, this function will not throw.
1994 template<typename _Functor>
1995 typename __gnu_cxx::__enable_if<!is_integral<_Functor>::value,
1997 operator=(_Functor __f)
1999 function(__f).swap(*this);
2003 // [3.7.2.2] function modifiers
2006 * @brief Swap the targets of two %function objects.
2007 * @param f A %function with identical call signature.
2009 * Swap the targets of @c this function object and @a f. This
2010 * function will not throw an %exception.
2012 void swap(function& __x)
2014 std::swap(_M_functor, __x._M_functor);
2015 std::swap(_M_manager, __x._M_manager);
2016 std::swap(_M_invoker, __x._M_invoker);
2019 // [3.7.2.3] function capacity
2022 * @brief Determine if the %function wrapper has a target.
2024 * @return @c true when this %function object contains a target,
2025 * or @c false when it is empty.
2027 * This function will not throw an %exception.
2029 #if __cplusplus >= 201103L
2030 explicit operator bool() const
2031 { return !_M_empty(); }
2033 operator _Safe_bool() const
2038 return &_Hidden_type::_M_bool;
2042 // [3.7.2.4] function invocation
2045 * @brief Invokes the function targeted by @c *this.
2046 * @returns the result of the target.
2047 * @throws bad_function_call when @c !(bool)*this
2049 * The function call operator invokes the target function object
2050 * stored by @c this.
2052 _Res operator()(_ArgTypes... __args) const;
2055 // [3.7.2.5] function target access
2057 * @brief Determine the type of the target of this function object
2060 * @returns the type identifier of the target function object, or
2061 * @c typeid(void) if @c !(bool)*this.
2063 * This function will not throw an %exception.
2065 const type_info& target_type() const;
2068 * @brief Access the stored target function object.
2070 * @return Returns a pointer to the stored target function object,
2071 * if @c typeid(Functor).equals(target_type()); otherwise, a NULL
2074 * This function will not throw an %exception.
2076 template<typename _Functor> _Functor* target();
2079 template<typename _Functor> const _Functor* target() const;
2083 // [3.7.2.6] undefined operators
2084 template<typename _Function>
2085 void operator==(const function<_Function>&) const;
2086 template<typename _Function>
2087 void operator!=(const function<_Function>&) const;
2089 typedef _Res (*_Invoker_type)(const _Any_data&, _ArgTypes...);
2090 _Invoker_type _M_invoker;
2093 template<typename _Res, typename... _ArgTypes>
2094 function<_Res(_ArgTypes...)>::
2095 function(const function& __x)
2098 if (static_cast<bool>(__x))
2100 __x._M_manager(_M_functor, __x._M_functor, __clone_functor);
2101 _M_invoker = __x._M_invoker;
2102 _M_manager = __x._M_manager;
2106 template<typename _Res, typename... _ArgTypes>
2107 template<typename _Functor>
2108 function<_Res(_ArgTypes...)>::
2109 function(_Functor __f,
2110 typename __gnu_cxx::__enable_if<
2111 !is_integral<_Functor>::value, _Useless>::__type)
2114 typedef _Function_handler<_Signature_type, _Functor> _My_handler;
2116 if (_My_handler::_M_not_empty_function(__f))
2118 _My_handler::_M_init_functor(_M_functor, __f);
2119 _M_invoker = &_My_handler::_M_invoke;
2120 _M_manager = &_My_handler::_M_manager;
2124 template<typename _Res, typename... _ArgTypes>
2126 function<_Res(_ArgTypes...)>::
2127 operator()(_ArgTypes... __args) const
2130 _GLIBCXX_THROW_OR_ABORT(bad_function_call());
2131 return _M_invoker(_M_functor, __args...);
2135 template<typename _Res, typename... _ArgTypes>
2137 function<_Res(_ArgTypes...)>::
2142 _Any_data __typeinfo_result;
2143 _M_manager(__typeinfo_result, _M_functor, __get_type_info);
2144 return *__typeinfo_result._M_access<const type_info*>();
2147 return typeid(void);
2150 template<typename _Res, typename... _ArgTypes>
2151 template<typename _Functor>
2153 function<_Res(_ArgTypes...)>::
2156 if (typeid(_Functor) == target_type() && _M_manager)
2159 if (_M_manager(__ptr, _M_functor, __get_functor_ptr)
2160 && !is_const<_Functor>::value)
2163 return __ptr._M_access<_Functor*>();
2169 template<typename _Res, typename... _ArgTypes>
2170 template<typename _Functor>
2172 function<_Res(_ArgTypes...)>::
2175 if (typeid(_Functor) == target_type() && _M_manager)
2178 _M_manager(__ptr, _M_functor, __get_functor_ptr);
2179 return __ptr._M_access<const _Functor*>();
2186 // [3.7.2.7] null pointer comparisons
2189 * @brief Compares a polymorphic function object wrapper against 0
2190 * (the NULL pointer).
2191 * @returns @c true if the wrapper has no target, @c false otherwise
2193 * This function will not throw an %exception.
2195 template<typename _Signature>
2197 operator==(const function<_Signature>& __f, _M_clear_type*)
2198 { return !static_cast<bool>(__f); }
2201 template<typename _Signature>
2203 operator==(_M_clear_type*, const function<_Signature>& __f)
2204 { return !static_cast<bool>(__f); }
2207 * @brief Compares a polymorphic function object wrapper against 0
2208 * (the NULL pointer).
2209 * @returns @c false if the wrapper has no target, @c true otherwise
2211 * This function will not throw an %exception.
2213 template<typename _Signature>
2215 operator!=(const function<_Signature>& __f, _M_clear_type*)
2216 { return static_cast<bool>(__f); }
2219 template<typename _Signature>
2221 operator!=(_M_clear_type*, const function<_Signature>& __f)
2222 { return static_cast<bool>(__f); }
2224 // [3.7.2.8] specialized algorithms
2227 * @brief Swap the targets of two polymorphic function object wrappers.
2229 * This function will not throw an %exception.
2231 template<typename _Signature>
2233 swap(function<_Signature>& __x, function<_Signature>& __y)
2237 #if __cplusplus >= 201103L
2238 // Specialize std::is_bind_expression for tr1::bind closure types,
2239 // so that they can also work with std::bind.
2241 template<typename _Signature>
2242 struct is_bind_expression<tr1::_Bind<_Signature>>
2245 template<typename _Signature>
2246 struct is_bind_expression<const tr1::_Bind<_Signature>>
2249 template<typename _Signature>
2250 struct is_bind_expression<volatile tr1::_Bind<_Signature>>
2253 template<typename _Signature>
2254 struct is_bind_expression<const volatile tr1::_Bind<_Signature>>
2257 template<typename _Result, typename _Signature>
2258 struct is_bind_expression<tr1::_Bind_result<_Result, _Signature>>
2261 template<typename _Result, typename _Signature>
2262 struct is_bind_expression<const tr1::_Bind_result<_Result, _Signature>>
2265 template<typename _Result, typename _Signature>
2266 struct is_bind_expression<volatile tr1::_Bind_result<_Result, _Signature>>
2269 template<typename _Result, typename _Signature>
2270 struct is_bind_expression<const volatile tr1::_Bind_result<_Result,
2275 _GLIBCXX_END_NAMESPACE_VERSION
2278 #endif // _GLIBCXX_TR1_FUNCTIONAL