1 // TR1 functional header -*- C++ -*-
3 // Copyright (C) 2004-2022 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 // Ignore warnings about std::unary_function and std::binary_function.
268 #pragma GCC diagnostic push
269 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
271 /// Determines if the type _Tp derives from unary_function.
272 template<typename _Tp>
273 struct _Derives_from_unary_function : __sfinae_types
276 template<typename _T1, typename _Res>
277 static __one __test(const volatile unary_function<_T1, _Res>*);
279 // It's tempting to change "..." to const volatile void*, but
280 // that fails when _Tp is a function type.
281 static __two __test(...);
284 static const bool value = sizeof(__test((_Tp*)0)) == 1;
287 /// Determines if the type _Tp derives from binary_function.
288 template<typename _Tp>
289 struct _Derives_from_binary_function : __sfinae_types
292 template<typename _T1, typename _T2, typename _Res>
293 static __one __test(const volatile binary_function<_T1, _T2, _Res>*);
295 // It's tempting to change "..." to const volatile void*, but
296 // that fails when _Tp is a function type.
297 static __two __test(...);
300 static const bool value = sizeof(__test((_Tp*)0)) == 1;
303 /// Turns a function type into a function pointer type
304 template<typename _Tp, bool _IsFunctionType = is_function<_Tp>::value>
305 struct _Function_to_function_pointer
310 template<typename _Tp>
311 struct _Function_to_function_pointer<_Tp, true>
317 * Invoke a function object, which may be either a member pointer or a
318 * function object. The first parameter will tell which.
320 template<typename _Functor, typename... _Args>
322 typename __gnu_cxx::__enable_if<
323 (!is_member_pointer<_Functor>::value
324 && !is_function<_Functor>::value
325 && !is_function<typename remove_pointer<_Functor>::type>::value),
326 typename result_of<_Functor(_Args...)>::type
328 __invoke(_Functor& __f, _Args&... __args)
330 return __f(__args...);
333 template<typename _Functor, typename... _Args>
335 typename __gnu_cxx::__enable_if<
336 (is_member_pointer<_Functor>::value
337 && !is_function<_Functor>::value
338 && !is_function<typename remove_pointer<_Functor>::type>::value),
339 typename result_of<_Functor(_Args...)>::type
341 __invoke(_Functor& __f, _Args&... __args)
343 return mem_fn(__f)(__args...);
346 // To pick up function references (that will become function pointers)
347 template<typename _Functor, typename... _Args>
349 typename __gnu_cxx::__enable_if<
350 (is_pointer<_Functor>::value
351 && is_function<typename remove_pointer<_Functor>::type>::value),
352 typename result_of<_Functor(_Args...)>::type
354 __invoke(_Functor __f, _Args&... __args)
356 return __f(__args...);
360 * Knowing which of unary_function and binary_function _Tp derives
361 * from, derives from the same and ensures that reference_wrapper
362 * will have a weak result type. See cases below.
364 template<bool _Unary, bool _Binary, typename _Tp>
365 struct _Reference_wrapper_base_impl;
367 // Not a unary_function or binary_function, so try a weak result type.
368 template<typename _Tp>
369 struct _Reference_wrapper_base_impl<false, false, _Tp>
370 : _Weak_result_type<_Tp>
373 // unary_function but not binary_function
374 template<typename _Tp>
375 struct _Reference_wrapper_base_impl<true, false, _Tp>
376 : unary_function<typename _Tp::argument_type,
377 typename _Tp::result_type>
380 // binary_function but not unary_function
381 template<typename _Tp>
382 struct _Reference_wrapper_base_impl<false, true, _Tp>
383 : binary_function<typename _Tp::first_argument_type,
384 typename _Tp::second_argument_type,
385 typename _Tp::result_type>
388 // Both unary_function and binary_function. Import result_type to
390 template<typename _Tp>
391 struct _Reference_wrapper_base_impl<true, true, _Tp>
392 : unary_function<typename _Tp::argument_type,
393 typename _Tp::result_type>,
394 binary_function<typename _Tp::first_argument_type,
395 typename _Tp::second_argument_type,
396 typename _Tp::result_type>
398 typedef typename _Tp::result_type result_type;
402 * Derives from unary_function or binary_function when it
403 * can. Specializations handle all of the easy cases. The primary
404 * template determines what to do with a class type, which may
405 * derive from both unary_function and binary_function.
407 template<typename _Tp>
408 struct _Reference_wrapper_base
409 : _Reference_wrapper_base_impl<
410 _Derives_from_unary_function<_Tp>::value,
411 _Derives_from_binary_function<_Tp>::value,
415 // - a function type (unary)
416 template<typename _Res, typename _T1>
417 struct _Reference_wrapper_base<_Res(_T1)>
418 : unary_function<_T1, _Res>
421 // - a function type (binary)
422 template<typename _Res, typename _T1, typename _T2>
423 struct _Reference_wrapper_base<_Res(_T1, _T2)>
424 : binary_function<_T1, _T2, _Res>
427 // - a function pointer type (unary)
428 template<typename _Res, typename _T1>
429 struct _Reference_wrapper_base<_Res(*)(_T1)>
430 : unary_function<_T1, _Res>
433 // - a function pointer type (binary)
434 template<typename _Res, typename _T1, typename _T2>
435 struct _Reference_wrapper_base<_Res(*)(_T1, _T2)>
436 : binary_function<_T1, _T2, _Res>
439 // - a pointer to member function type (unary, no qualifiers)
440 template<typename _Res, typename _T1>
441 struct _Reference_wrapper_base<_Res (_T1::*)()>
442 : unary_function<_T1*, _Res>
445 // - a pointer to member function type (binary, no qualifiers)
446 template<typename _Res, typename _T1, typename _T2>
447 struct _Reference_wrapper_base<_Res (_T1::*)(_T2)>
448 : binary_function<_T1*, _T2, _Res>
451 // - a pointer to member function type (unary, const)
452 template<typename _Res, typename _T1>
453 struct _Reference_wrapper_base<_Res (_T1::*)() const>
454 : unary_function<const _T1*, _Res>
457 // - a pointer to member function type (binary, const)
458 template<typename _Res, typename _T1, typename _T2>
459 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const>
460 : binary_function<const _T1*, _T2, _Res>
463 // - a pointer to member function type (unary, volatile)
464 template<typename _Res, typename _T1>
465 struct _Reference_wrapper_base<_Res (_T1::*)() volatile>
466 : unary_function<volatile _T1*, _Res>
469 // - a pointer to member function type (binary, volatile)
470 template<typename _Res, typename _T1, typename _T2>
471 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) volatile>
472 : binary_function<volatile _T1*, _T2, _Res>
475 // - a pointer to member function type (unary, const volatile)
476 template<typename _Res, typename _T1>
477 struct _Reference_wrapper_base<_Res (_T1::*)() const volatile>
478 : unary_function<const volatile _T1*, _Res>
481 // - a pointer to member function type (binary, const volatile)
482 template<typename _Res, typename _T1, typename _T2>
483 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const volatile>
484 : binary_function<const volatile _T1*, _T2, _Res>
487 /// reference_wrapper
488 template<typename _Tp>
489 class reference_wrapper
490 : public _Reference_wrapper_base<typename remove_cv<_Tp>::type>
492 // If _Tp is a function type, we can't form result_of<_Tp(...)>,
493 // so turn it into a function pointer type.
494 typedef typename _Function_to_function_pointer<_Tp>::type
502 reference_wrapper(_Tp& __indata)
503 : _M_data(std::__addressof(__indata))
506 reference_wrapper(const reference_wrapper<_Tp>& __inref):
507 _M_data(__inref._M_data)
511 operator=(const reference_wrapper<_Tp>& __inref)
513 _M_data = __inref._M_data;
517 operator _Tp&() const
518 { return this->get(); }
524 template<typename... _Args>
525 typename result_of<_M_func_type(_Args...)>::type
526 operator()(_Args&... __args) const
528 return __invoke(get(), __args...);
533 // Denotes a reference should be taken to a variable.
534 template<typename _Tp>
535 inline reference_wrapper<_Tp>
537 { return reference_wrapper<_Tp>(__t); }
539 // Denotes a const reference should be taken to a variable.
540 template<typename _Tp>
541 inline reference_wrapper<const _Tp>
543 { return reference_wrapper<const _Tp>(__t); }
545 template<typename _Tp>
546 inline reference_wrapper<_Tp>
547 ref(reference_wrapper<_Tp> __t)
548 { return ref(__t.get()); }
550 template<typename _Tp>
551 inline reference_wrapper<const _Tp>
552 cref(reference_wrapper<_Tp> __t)
553 { return cref(__t.get()); }
555 template<typename _Tp, bool>
556 struct _Mem_fn_const_or_non
558 typedef const _Tp& type;
561 template<typename _Tp>
562 struct _Mem_fn_const_or_non<_Tp, false>
568 * Derives from @c unary_function or @c binary_function, or perhaps
569 * nothing, depending on the number of arguments provided. The
570 * primary template is the basis case, which derives nothing.
572 template<typename _Res, typename... _ArgTypes>
573 struct _Maybe_unary_or_binary_function { };
575 /// Derives from @c unary_function, as appropriate.
576 template<typename _Res, typename _T1>
577 struct _Maybe_unary_or_binary_function<_Res, _T1>
578 : std::unary_function<_T1, _Res> { };
580 /// Derives from @c binary_function, as appropriate.
581 template<typename _Res, typename _T1, typename _T2>
582 struct _Maybe_unary_or_binary_function<_Res, _T1, _T2>
583 : std::binary_function<_T1, _T2, _Res> { };
585 /// Implementation of @c mem_fn for member function pointers.
586 template<typename _Res, typename _Class, typename... _ArgTypes>
587 class _Mem_fn<_Res (_Class::*)(_ArgTypes...)>
588 : public _Maybe_unary_or_binary_function<_Res, _Class*, _ArgTypes...>
590 typedef _Res (_Class::*_Functor)(_ArgTypes...);
592 template<typename _Tp>
594 _M_call(_Tp& __object, const volatile _Class *,
595 _ArgTypes... __args) const
596 { return (__object.*__pmf)(__args...); }
598 template<typename _Tp>
600 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
601 { return ((*__ptr).*__pmf)(__args...); }
604 typedef _Res result_type;
606 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
610 operator()(_Class& __object, _ArgTypes... __args) const
611 { return (__object.*__pmf)(__args...); }
615 operator()(_Class* __object, _ArgTypes... __args) const
616 { return (__object->*__pmf)(__args...); }
618 // Handle smart pointers, references and pointers to derived
619 template<typename _Tp>
621 operator()(_Tp& __object, _ArgTypes... __args) const
622 { return _M_call(__object, &__object, __args...); }
628 /// Implementation of @c mem_fn for const member function pointers.
629 template<typename _Res, typename _Class, typename... _ArgTypes>
630 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) const>
631 : public _Maybe_unary_or_binary_function<_Res, const _Class*,
634 typedef _Res (_Class::*_Functor)(_ArgTypes...) const;
636 template<typename _Tp>
638 _M_call(_Tp& __object, const volatile _Class *,
639 _ArgTypes... __args) const
640 { return (__object.*__pmf)(__args...); }
642 template<typename _Tp>
644 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
645 { return ((*__ptr).*__pmf)(__args...); }
648 typedef _Res result_type;
650 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
654 operator()(const _Class& __object, _ArgTypes... __args) const
655 { return (__object.*__pmf)(__args...); }
659 operator()(const _Class* __object, _ArgTypes... __args) const
660 { return (__object->*__pmf)(__args...); }
662 // Handle smart pointers, references and pointers to derived
663 template<typename _Tp>
664 _Res operator()(_Tp& __object, _ArgTypes... __args) const
665 { return _M_call(__object, &__object, __args...); }
671 /// Implementation of @c mem_fn for volatile member function pointers.
672 template<typename _Res, typename _Class, typename... _ArgTypes>
673 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) volatile>
674 : public _Maybe_unary_or_binary_function<_Res, volatile _Class*,
677 typedef _Res (_Class::*_Functor)(_ArgTypes...) volatile;
679 template<typename _Tp>
681 _M_call(_Tp& __object, const volatile _Class *,
682 _ArgTypes... __args) const
683 { return (__object.*__pmf)(__args...); }
685 template<typename _Tp>
687 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
688 { return ((*__ptr).*__pmf)(__args...); }
691 typedef _Res result_type;
693 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
697 operator()(volatile _Class& __object, _ArgTypes... __args) const
698 { return (__object.*__pmf)(__args...); }
702 operator()(volatile _Class* __object, _ArgTypes... __args) const
703 { return (__object->*__pmf)(__args...); }
705 // Handle smart pointers, references and pointers to derived
706 template<typename _Tp>
708 operator()(_Tp& __object, _ArgTypes... __args) const
709 { return _M_call(__object, &__object, __args...); }
715 /// Implementation of @c mem_fn for const volatile member function pointers.
716 template<typename _Res, typename _Class, typename... _ArgTypes>
717 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) const volatile>
718 : public _Maybe_unary_or_binary_function<_Res, const volatile _Class*,
721 typedef _Res (_Class::*_Functor)(_ArgTypes...) const volatile;
723 template<typename _Tp>
725 _M_call(_Tp& __object, const volatile _Class *,
726 _ArgTypes... __args) const
727 { return (__object.*__pmf)(__args...); }
729 template<typename _Tp>
731 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
732 { return ((*__ptr).*__pmf)(__args...); }
735 typedef _Res result_type;
737 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
741 operator()(const volatile _Class& __object, _ArgTypes... __args) const
742 { return (__object.*__pmf)(__args...); }
746 operator()(const volatile _Class* __object, _ArgTypes... __args) const
747 { return (__object->*__pmf)(__args...); }
749 // Handle smart pointers, references and pointers to derived
750 template<typename _Tp>
751 _Res operator()(_Tp& __object, _ArgTypes... __args) const
752 { return _M_call(__object, &__object, __args...); }
759 template<typename _Res, typename _Class>
760 class _Mem_fn<_Res _Class::*>
762 // This bit of genius is due to Peter Dimov, improved slightly by
764 template<typename _Tp>
766 _M_call(_Tp& __object, _Class *) const
767 { return __object.*__pm; }
769 template<typename _Tp, typename _Up>
771 _M_call(_Tp& __object, _Up * const *) const
772 { return (*__object).*__pm; }
774 template<typename _Tp, typename _Up>
776 _M_call(_Tp& __object, const _Up * const *) const
777 { return (*__object).*__pm; }
779 template<typename _Tp>
781 _M_call(_Tp& __object, const _Class *) const
782 { return __object.*__pm; }
784 template<typename _Tp>
786 _M_call(_Tp& __ptr, const volatile void*) const
787 { return (*__ptr).*__pm; }
789 template<typename _Tp> static _Tp& __get_ref();
791 template<typename _Tp>
792 static __sfinae_types::__one __check_const(_Tp&, _Class*);
793 template<typename _Tp, typename _Up>
794 static __sfinae_types::__one __check_const(_Tp&, _Up * const *);
795 template<typename _Tp, typename _Up>
796 static __sfinae_types::__two __check_const(_Tp&, const _Up * const *);
797 template<typename _Tp>
798 static __sfinae_types::__two __check_const(_Tp&, const _Class*);
799 template<typename _Tp>
800 static __sfinae_types::__two __check_const(_Tp&, const volatile void*);
803 template<typename _Tp>
805 : _Mem_fn_const_or_non<_Res,
806 (sizeof(__sfinae_types::__two)
807 == sizeof(__check_const<_Tp>(__get_ref<_Tp>(), (_Tp*)0)))>
810 template<typename _Signature>
813 template<typename _CVMem, typename _Tp>
814 struct result<_CVMem(_Tp)>
815 : public _Result_type<_Tp> { };
817 template<typename _CVMem, typename _Tp>
818 struct result<_CVMem(_Tp&)>
819 : public _Result_type<_Tp> { };
822 _Mem_fn(_Res _Class::*__pm) : __pm(__pm) { }
826 operator()(_Class& __object) const
827 { return __object.*__pm; }
830 operator()(const _Class& __object) const
831 { return __object.*__pm; }
835 operator()(_Class* __object) const
836 { return __object->*__pm; }
839 operator()(const _Class* __object) const
840 { return __object->*__pm; }
842 // Handle smart pointers and derived
843 template<typename _Tp>
844 typename _Result_type<_Tp>::type
845 operator()(_Tp& __unknown) const
846 { return _M_call(__unknown, &__unknown); }
853 * @brief Returns a function object that forwards to the member
856 template<typename _Tp, typename _Class>
857 inline _Mem_fn<_Tp _Class::*>
858 mem_fn(_Tp _Class::* __pm)
860 return _Mem_fn<_Tp _Class::*>(__pm);
864 * @brief Determines if the given type _Tp is a function object
865 * should be treated as a subexpression when evaluating calls to
866 * function objects returned by bind(). [TR1 3.6.1]
868 template<typename _Tp>
869 struct is_bind_expression
870 { static const bool value = false; };
872 template<typename _Tp>
873 const bool is_bind_expression<_Tp>::value;
876 * @brief Determines if the given type _Tp is a placeholder in a
877 * bind() expression and, if so, which placeholder it is. [TR1 3.6.2]
879 template<typename _Tp>
880 struct is_placeholder
881 { static const int value = 0; };
883 template<typename _Tp>
884 const int is_placeholder<_Tp>::value;
886 /// The type of placeholder objects defined by libstdc++.
887 using ::std::_Placeholder;
889 /** @namespace std::tr1::placeholders
890 * @brief Sub-namespace for tr1/functional.
892 namespace placeholders
894 // The C++11 std::placeholders are already exported from the library.
895 // Reusing them here avoids needing to export additional symbols for
896 // the TR1 placeholders, and avoids ODR violations due to defining
897 // them with internal linkage (as we used to do).
898 using namespace ::std::placeholders;
902 * Partial specialization of is_placeholder that provides the placeholder
903 * number for the placeholder objects defined by libstdc++.
906 struct is_placeholder<_Placeholder<_Num> >
907 : integral_constant<int, _Num>
911 struct is_placeholder<const _Placeholder<_Num> >
912 : integral_constant<int, _Num>
916 * Stores a tuple of indices. Used by bind() to extract the elements
919 template<int... _Indexes>
920 struct _Index_tuple { };
922 /// Builds an _Index_tuple<0, 1, 2, ..., _Num-1>.
923 template<std::size_t _Num, typename _Tuple = _Index_tuple<> >
924 struct _Build_index_tuple;
926 template<std::size_t _Num, int... _Indexes>
927 struct _Build_index_tuple<_Num, _Index_tuple<_Indexes...> >
928 : _Build_index_tuple<_Num - 1,
929 _Index_tuple<_Indexes..., sizeof...(_Indexes)> >
933 template<int... _Indexes>
934 struct _Build_index_tuple<0, _Index_tuple<_Indexes...> >
936 typedef _Index_tuple<_Indexes...> __type;
940 * Used by _Safe_tuple_element to indicate that there is no tuple
941 * element at this position.
943 struct _No_tuple_element;
946 * Implementation helper for _Safe_tuple_element. This primary
947 * template handles the case where it is safe to use @c
950 template<int __i, typename _Tuple, bool _IsSafe>
951 struct _Safe_tuple_element_impl
952 : tuple_element<__i, _Tuple> { };
955 * Implementation helper for _Safe_tuple_element. This partial
956 * specialization handles the case where it is not safe to use @c
957 * tuple_element. We just return @c _No_tuple_element.
959 template<int __i, typename _Tuple>
960 struct _Safe_tuple_element_impl<__i, _Tuple, false>
962 typedef _No_tuple_element type;
966 * Like tuple_element, but returns @c _No_tuple_element when
967 * tuple_element would return an error.
969 template<int __i, typename _Tuple>
970 struct _Safe_tuple_element
971 : _Safe_tuple_element_impl<__i, _Tuple,
972 (__i >= 0 && __i < tuple_size<_Tuple>::value)>
977 * Maps an argument to bind() into an actual argument to the bound
978 * function object [TR1 3.6.3/5]. Only the first parameter should
979 * be specified: the rest are used to determine among the various
980 * implementations. Note that, although this class is a function
981 * object, it isn't entirely normal because it takes only two
982 * parameters regardless of the number of parameters passed to the
983 * bind expression. The first parameter is the bound argument and
984 * the second parameter is a tuple containing references to the
985 * rest of the arguments.
987 template<typename _Arg,
988 bool _IsBindExp = is_bind_expression<_Arg>::value,
989 bool _IsPlaceholder = (is_placeholder<_Arg>::value > 0)>
993 * If the argument is reference_wrapper<_Tp>, returns the
994 * underlying reference. [TR1 3.6.3/5 bullet 1]
996 template<typename _Tp>
997 class _Mu<reference_wrapper<_Tp>, false, false>
1000 typedef _Tp& result_type;
1002 /* Note: This won't actually work for const volatile
1003 * reference_wrappers, because reference_wrapper::get() is const
1004 * but not volatile-qualified. This might be a defect in the TR.
1006 template<typename _CVRef, typename _Tuple>
1008 operator()(_CVRef& __arg, const _Tuple&) const volatile
1009 { return __arg.get(); }
1013 * If the argument is a bind expression, we invoke the underlying
1014 * function object with the same cv-qualifiers as we are given and
1015 * pass along all of our arguments (unwrapped). [TR1 3.6.3/5 bullet 2]
1017 template<typename _Arg>
1018 class _Mu<_Arg, true, false>
1021 template<typename _Signature> class result;
1023 // Determine the result type when we pass the arguments along. This
1024 // involves passing along the cv-qualifiers placed on _Mu and
1025 // unwrapping the argument bundle.
1026 template<typename _CVMu, typename _CVArg, typename... _Args>
1027 class result<_CVMu(_CVArg, tuple<_Args...>)>
1028 : public result_of<_CVArg(_Args...)> { };
1030 template<typename _CVArg, typename... _Args>
1031 typename result_of<_CVArg(_Args...)>::type
1032 operator()(_CVArg& __arg,
1033 const tuple<_Args...>& __tuple) const volatile
1035 // Construct an index tuple and forward to __call
1036 typedef typename _Build_index_tuple<sizeof...(_Args)>::__type
1038 return this->__call(__arg, __tuple, _Indexes());
1042 // Invokes the underlying function object __arg by unpacking all
1043 // of the arguments in the tuple.
1044 template<typename _CVArg, typename... _Args, int... _Indexes>
1045 typename result_of<_CVArg(_Args...)>::type
1046 __call(_CVArg& __arg, const tuple<_Args...>& __tuple,
1047 const _Index_tuple<_Indexes...>&) const volatile
1049 return __arg(tr1::get<_Indexes>(__tuple)...);
1054 * If the argument is a placeholder for the Nth argument, returns
1055 * a reference to the Nth argument to the bind function object.
1056 * [TR1 3.6.3/5 bullet 3]
1058 template<typename _Arg>
1059 class _Mu<_Arg, false, true>
1062 template<typename _Signature> class result;
1064 template<typename _CVMu, typename _CVArg, typename _Tuple>
1065 class result<_CVMu(_CVArg, _Tuple)>
1067 // Add a reference, if it hasn't already been done for us.
1068 // This allows us to be a little bit sloppy in constructing
1069 // the tuple that we pass to result_of<...>.
1070 typedef typename _Safe_tuple_element<(is_placeholder<_Arg>::value
1075 typedef typename add_reference<__base_type>::type type;
1078 template<typename _Tuple>
1079 typename result<_Mu(_Arg, _Tuple)>::type
1080 operator()(const volatile _Arg&, const _Tuple& __tuple) const volatile
1082 return ::std::tr1::get<(is_placeholder<_Arg>::value - 1)>(__tuple);
1087 * If the argument is just a value, returns a reference to that
1088 * value. The cv-qualifiers on the reference are the same as the
1089 * cv-qualifiers on the _Mu object. [TR1 3.6.3/5 bullet 4]
1091 template<typename _Arg>
1092 class _Mu<_Arg, false, false>
1095 template<typename _Signature> struct result;
1097 template<typename _CVMu, typename _CVArg, typename _Tuple>
1098 struct result<_CVMu(_CVArg, _Tuple)>
1100 typedef typename add_reference<_CVArg>::type type;
1103 // Pick up the cv-qualifiers of the argument
1104 template<typename _CVArg, typename _Tuple>
1106 operator()(_CVArg& __arg, const _Tuple&) const volatile
1111 * Maps member pointers into instances of _Mem_fn but leaves all
1112 * other function objects untouched. Used by tr1::bind(). The
1113 * primary template handles the non--member-pointer case.
1115 template<typename _Tp>
1116 struct _Maybe_wrap_member_pointer
1121 __do_wrap(const _Tp& __x)
1126 * Maps member pointers into instances of _Mem_fn but leaves all
1127 * other function objects untouched. Used by tr1::bind(). This
1128 * partial specialization handles the member pointer case.
1130 template<typename _Tp, typename _Class>
1131 struct _Maybe_wrap_member_pointer<_Tp _Class::*>
1133 typedef _Mem_fn<_Tp _Class::*> type;
1136 __do_wrap(_Tp _Class::* __pm)
1137 { return type(__pm); }
1140 /// Type of the function object returned from bind().
1141 template<typename _Signature>
1144 template<typename _Functor, typename... _Bound_args>
1145 class _Bind<_Functor(_Bound_args...)>
1146 : public _Weak_result_type<_Functor>
1148 typedef _Bind __self_type;
1149 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
1153 tuple<_Bound_args...> _M_bound_args;
1156 template<typename... _Args, int... _Indexes>
1158 _Functor(typename result_of<_Mu<_Bound_args>
1159 (_Bound_args, tuple<_Args...>)>::type...)
1161 __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>)
1163 return _M_f(_Mu<_Bound_args>()
1164 (tr1::get<_Indexes>(_M_bound_args), __args)...);
1168 template<typename... _Args, int... _Indexes>
1170 const _Functor(typename result_of<_Mu<_Bound_args>
1171 (const _Bound_args, tuple<_Args...>)
1173 __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>) const
1175 return _M_f(_Mu<_Bound_args>()
1176 (tr1::get<_Indexes>(_M_bound_args), __args)...);
1180 template<typename... _Args, int... _Indexes>
1182 volatile _Functor(typename result_of<_Mu<_Bound_args>
1183 (volatile _Bound_args, tuple<_Args...>)
1185 __call(const tuple<_Args...>& __args,
1186 _Index_tuple<_Indexes...>) volatile
1188 return _M_f(_Mu<_Bound_args>()
1189 (tr1::get<_Indexes>(_M_bound_args), __args)...);
1192 // Call as const volatile
1193 template<typename... _Args, int... _Indexes>
1195 const volatile _Functor(typename result_of<_Mu<_Bound_args>
1196 (const volatile _Bound_args,
1199 __call(const tuple<_Args...>& __args,
1200 _Index_tuple<_Indexes...>) const volatile
1202 return _M_f(_Mu<_Bound_args>()
1203 (tr1::get<_Indexes>(_M_bound_args), __args)...);
1207 explicit _Bind(_Functor __f, _Bound_args... __bound_args)
1208 : _M_f(__f), _M_bound_args(__bound_args...) { }
1211 template<typename... _Args>
1213 _Functor(typename result_of<_Mu<_Bound_args>
1214 (_Bound_args, tuple<_Args...>)>::type...)
1216 operator()(_Args&... __args)
1218 return this->__call(tr1::tie(__args...), _Bound_indexes());
1222 template<typename... _Args>
1224 const _Functor(typename result_of<_Mu<_Bound_args>
1225 (const _Bound_args, tuple<_Args...>)>::type...)
1227 operator()(_Args&... __args) const
1229 return this->__call(tr1::tie(__args...), _Bound_indexes());
1234 template<typename... _Args>
1236 volatile _Functor(typename result_of<_Mu<_Bound_args>
1237 (volatile _Bound_args, tuple<_Args...>)>::type...)
1239 operator()(_Args&... __args) volatile
1241 return this->__call(tr1::tie(__args...), _Bound_indexes());
1245 // Call as const volatile
1246 template<typename... _Args>
1248 const volatile _Functor(typename result_of<_Mu<_Bound_args>
1249 (const volatile _Bound_args,
1250 tuple<_Args...>)>::type...)
1252 operator()(_Args&... __args) const volatile
1254 return this->__call(tr1::tie(__args...), _Bound_indexes());
1258 /// Type of the function object returned from bind<R>().
1259 template<typename _Result, typename _Signature>
1260 struct _Bind_result;
1262 template<typename _Result, typename _Functor, typename... _Bound_args>
1263 class _Bind_result<_Result, _Functor(_Bound_args...)>
1265 typedef _Bind_result __self_type;
1266 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
1270 tuple<_Bound_args...> _M_bound_args;
1273 template<typename... _Args, int... _Indexes>
1275 __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>)
1277 return _M_f(_Mu<_Bound_args>()
1278 (tr1::get<_Indexes>(_M_bound_args), __args)...);
1282 template<typename... _Args, int... _Indexes>
1284 __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>) const
1286 return _M_f(_Mu<_Bound_args>()
1287 (tr1::get<_Indexes>(_M_bound_args), __args)...);
1291 template<typename... _Args, int... _Indexes>
1293 __call(const tuple<_Args...>& __args,
1294 _Index_tuple<_Indexes...>) volatile
1296 return _M_f(_Mu<_Bound_args>()
1297 (tr1::get<_Indexes>(_M_bound_args), __args)...);
1300 // Call as const volatile
1301 template<typename... _Args, int... _Indexes>
1303 __call(const tuple<_Args...>& __args,
1304 _Index_tuple<_Indexes...>) const volatile
1306 return _M_f(_Mu<_Bound_args>()
1307 (tr1::get<_Indexes>(_M_bound_args), __args)...);
1311 typedef _Result result_type;
1314 _Bind_result(_Functor __f, _Bound_args... __bound_args)
1315 : _M_f(__f), _M_bound_args(__bound_args...) { }
1318 template<typename... _Args>
1320 operator()(_Args&... __args)
1322 return this->__call(tr1::tie(__args...), _Bound_indexes());
1326 template<typename... _Args>
1328 operator()(_Args&... __args) const
1330 return this->__call(tr1::tie(__args...), _Bound_indexes());
1334 template<typename... _Args>
1336 operator()(_Args&... __args) volatile
1338 return this->__call(tr1::tie(__args...), _Bound_indexes());
1341 // Call as const volatile
1342 template<typename... _Args>
1344 operator()(_Args&... __args) const volatile
1346 return this->__call(tr1::tie(__args...), _Bound_indexes());
1350 /// Class template _Bind is always a bind expression.
1351 template<typename _Signature>
1352 struct is_bind_expression<_Bind<_Signature> >
1353 { static const bool value = true; };
1355 template<typename _Signature>
1356 const bool is_bind_expression<_Bind<_Signature> >::value;
1358 /// Class template _Bind is always a bind expression.
1359 template<typename _Signature>
1360 struct is_bind_expression<const _Bind<_Signature> >
1361 { static const bool value = true; };
1363 template<typename _Signature>
1364 const bool is_bind_expression<const _Bind<_Signature> >::value;
1366 /// Class template _Bind is always a bind expression.
1367 template<typename _Signature>
1368 struct is_bind_expression<volatile _Bind<_Signature> >
1369 { static const bool value = true; };
1371 template<typename _Signature>
1372 const bool is_bind_expression<volatile _Bind<_Signature> >::value;
1374 /// Class template _Bind is always a bind expression.
1375 template<typename _Signature>
1376 struct is_bind_expression<const volatile _Bind<_Signature> >
1377 { static const bool value = true; };
1379 template<typename _Signature>
1380 const bool is_bind_expression<const volatile _Bind<_Signature> >::value;
1382 /// Class template _Bind_result is always a bind expression.
1383 template<typename _Result, typename _Signature>
1384 struct is_bind_expression<_Bind_result<_Result, _Signature> >
1385 { static const bool value = true; };
1387 template<typename _Result, typename _Signature>
1388 const bool is_bind_expression<_Bind_result<_Result, _Signature> >::value;
1390 /// Class template _Bind_result is always a bind expression.
1391 template<typename _Result, typename _Signature>
1392 struct is_bind_expression<const _Bind_result<_Result, _Signature> >
1393 { static const bool value = true; };
1395 template<typename _Result, typename _Signature>
1397 is_bind_expression<const _Bind_result<_Result, _Signature> >::value;
1399 /// Class template _Bind_result is always a bind expression.
1400 template<typename _Result, typename _Signature>
1401 struct is_bind_expression<volatile _Bind_result<_Result, _Signature> >
1402 { static const bool value = true; };
1404 template<typename _Result, typename _Signature>
1406 is_bind_expression<volatile _Bind_result<_Result, _Signature> >::value;
1408 /// Class template _Bind_result is always a bind expression.
1409 template<typename _Result, typename _Signature>
1411 is_bind_expression<const volatile _Bind_result<_Result, _Signature> >
1412 { static const bool value = true; };
1414 template<typename _Result, typename _Signature>
1416 is_bind_expression<const volatile _Bind_result<_Result,
1417 _Signature> >::value;
1419 #if __cplusplus >= 201103L
1420 // Specialize tr1::is_bind_expression for std::bind closure types,
1421 // so that they can also work with tr1::bind.
1423 template<typename _Signature>
1424 struct is_bind_expression<std::_Bind<_Signature>>
1427 template<typename _Signature>
1428 struct is_bind_expression<const std::_Bind<_Signature>>
1431 template<typename _Signature>
1432 struct is_bind_expression<volatile std::_Bind<_Signature>>
1435 template<typename _Signature>
1436 struct is_bind_expression<const volatile std::_Bind<_Signature>>
1439 template<typename _Result, typename _Signature>
1440 struct is_bind_expression<std::_Bind_result<_Result, _Signature>>
1443 template<typename _Result, typename _Signature>
1444 struct is_bind_expression<const std::_Bind_result<_Result, _Signature>>
1447 template<typename _Result, typename _Signature>
1448 struct is_bind_expression<volatile std::_Bind_result<_Result, _Signature>>
1451 template<typename _Result, typename _Signature>
1452 struct is_bind_expression<const volatile std::_Bind_result<_Result,
1458 template<typename _Functor, typename... _ArgTypes>
1460 _Bind<typename _Maybe_wrap_member_pointer<_Functor>::type(_ArgTypes...)>
1461 bind(_Functor __f, _ArgTypes... __args)
1463 typedef _Maybe_wrap_member_pointer<_Functor> __maybe_type;
1464 typedef typename __maybe_type::type __functor_type;
1465 typedef _Bind<__functor_type(_ArgTypes...)> __result_type;
1466 return __result_type(__maybe_type::__do_wrap(__f), __args...);
1469 template<typename _Result, typename _Functor, typename... _ArgTypes>
1471 _Bind_result<_Result,
1472 typename _Maybe_wrap_member_pointer<_Functor>::type
1474 bind(_Functor __f, _ArgTypes... __args)
1476 typedef _Maybe_wrap_member_pointer<_Functor> __maybe_type;
1477 typedef typename __maybe_type::type __functor_type;
1478 typedef _Bind_result<_Result, __functor_type(_ArgTypes...)>
1480 return __result_type(__maybe_type::__do_wrap(__f), __args...);
1484 * @brief Exception class thrown when class template function's
1485 * operator() is called with an empty target.
1486 * @ingroup exceptions
1488 class bad_function_call : public std::exception { };
1491 * The integral constant expression 0 can be converted into a
1492 * pointer to this type. It is used by the function template to
1493 * accept NULL pointers.
1495 struct _M_clear_type;
1498 * Trait identifying @a location-invariant types, meaning that the
1499 * address of the object (or any of its members) will not escape.
1500 * Also implies a trivial copy constructor and assignment operator.
1502 template<typename _Tp>
1503 struct __is_location_invariant
1504 : integral_constant<bool,
1505 (is_pointer<_Tp>::value
1506 || is_member_pointer<_Tp>::value)>
1510 class _Undefined_class;
1515 const void* _M_const_object;
1516 void (*_M_function_pointer)();
1517 void (_Undefined_class::*_M_member_pointer)();
1522 void* _M_access() { return &_M_pod_data[0]; }
1523 const void* _M_access() const { return &_M_pod_data[0]; }
1525 template<typename _Tp>
1528 { return *static_cast<_Tp*>(_M_access()); }
1530 template<typename _Tp>
1533 { return *static_cast<const _Tp*>(_M_access()); }
1535 _Nocopy_types _M_unused;
1536 char _M_pod_data[sizeof(_Nocopy_types)];
1539 enum _Manager_operation
1547 // Simple type wrapper that helps avoid annoying const problems
1548 // when casting between void pointers and pointers-to-pointers.
1549 template<typename _Tp>
1550 struct _Simple_type_wrapper
1552 _Simple_type_wrapper(_Tp __value) : __value(__value) { }
1557 template<typename _Tp>
1558 struct __is_location_invariant<_Simple_type_wrapper<_Tp> >
1559 : __is_location_invariant<_Tp>
1563 // Converts a reference to a function object into a callable
1565 template<typename _Functor>
1567 __callable_functor(_Functor& __f)
1570 template<typename _Member, typename _Class>
1571 inline _Mem_fn<_Member _Class::*>
1572 __callable_functor(_Member _Class::* &__p)
1573 { return mem_fn(__p); }
1575 template<typename _Member, typename _Class>
1576 inline _Mem_fn<_Member _Class::*>
1577 __callable_functor(_Member _Class::* const &__p)
1578 { return mem_fn(__p); }
1580 template<typename _Signature>
1583 /// Base class of all polymorphic function object wrappers.
1584 class _Function_base
1587 static const std::size_t _M_max_size = sizeof(_Nocopy_types);
1588 static const std::size_t _M_max_align = __alignof__(_Nocopy_types);
1590 template<typename _Functor>
1594 static const bool __stored_locally =
1595 (__is_location_invariant<_Functor>::value
1596 && sizeof(_Functor) <= _M_max_size
1597 && __alignof__(_Functor) <= _M_max_align
1598 && (_M_max_align % __alignof__(_Functor) == 0));
1600 typedef integral_constant<bool, __stored_locally> _Local_storage;
1602 // Retrieve a pointer to the function object
1604 _M_get_pointer(const _Any_data& __source)
1606 const _Functor* __ptr =
1607 __stored_locally? std::__addressof(__source._M_access<_Functor>())
1608 /* have stored a pointer */ : __source._M_access<_Functor*>();
1609 return const_cast<_Functor*>(__ptr);
1612 // Clone a location-invariant function object that fits within
1613 // an _Any_data structure.
1615 _M_clone(_Any_data& __dest, const _Any_data& __source, true_type)
1617 new (__dest._M_access()) _Functor(__source._M_access<_Functor>());
1620 // Clone a function object that is not location-invariant or
1621 // that cannot fit into an _Any_data structure.
1623 _M_clone(_Any_data& __dest, const _Any_data& __source, false_type)
1625 __dest._M_access<_Functor*>() =
1626 new _Functor(*__source._M_access<_Functor*>());
1629 // Destroying a location-invariant object may still require
1632 _M_destroy(_Any_data& __victim, true_type)
1634 __victim._M_access<_Functor>().~_Functor();
1637 // Destroying an object located on the heap.
1639 _M_destroy(_Any_data& __victim, false_type)
1641 delete __victim._M_access<_Functor*>();
1646 _M_manager(_Any_data& __dest, const _Any_data& __source,
1647 _Manager_operation __op)
1652 case __get_type_info:
1653 __dest._M_access<const type_info*>() = &typeid(_Functor);
1656 case __get_functor_ptr:
1657 __dest._M_access<_Functor*>() = _M_get_pointer(__source);
1660 case __clone_functor:
1661 _M_clone(__dest, __source, _Local_storage());
1664 case __destroy_functor:
1665 _M_destroy(__dest, _Local_storage());
1672 _M_init_functor(_Any_data& __functor, const _Functor& __f)
1673 { _M_init_functor(__functor, __f, _Local_storage()); }
1675 template<typename _Signature>
1677 _M_not_empty_function(const function<_Signature>& __f)
1678 { return static_cast<bool>(__f); }
1680 template<typename _Tp>
1682 _M_not_empty_function(const _Tp*& __fp)
1685 template<typename _Class, typename _Tp>
1687 _M_not_empty_function(_Tp _Class::* const& __mp)
1690 template<typename _Tp>
1692 _M_not_empty_function(const _Tp&)
1697 _M_init_functor(_Any_data& __functor, const _Functor& __f, true_type)
1698 { new (__functor._M_access()) _Functor(__f); }
1701 _M_init_functor(_Any_data& __functor, const _Functor& __f, false_type)
1702 { __functor._M_access<_Functor*>() = new _Functor(__f); }
1705 template<typename _Functor>
1706 class _Ref_manager : public _Base_manager<_Functor*>
1708 typedef _Function_base::_Base_manager<_Functor*> _Base;
1712 _M_manager(_Any_data& __dest, const _Any_data& __source,
1713 _Manager_operation __op)
1718 case __get_type_info:
1719 __dest._M_access<const type_info*>() = &typeid(_Functor);
1722 case __get_functor_ptr:
1723 __dest._M_access<_Functor*>() = *_Base::_M_get_pointer(__source);
1724 return is_const<_Functor>::value;
1728 _Base::_M_manager(__dest, __source, __op);
1734 _M_init_functor(_Any_data& __functor, reference_wrapper<_Functor> __f)
1736 _Base::_M_init_functor(__functor, std::__addressof(__f.get()));
1740 _Function_base() : _M_manager(0) { }
1745 _M_manager(_M_functor, _M_functor, __destroy_functor);
1749 bool _M_empty() const { return !_M_manager; }
1751 typedef bool (*_Manager_type)(_Any_data&, const _Any_data&,
1752 _Manager_operation);
1754 _Any_data _M_functor;
1755 _Manager_type _M_manager;
1758 template<typename _Signature, typename _Functor>
1759 class _Function_handler;
1761 template<typename _Res, typename _Functor, typename... _ArgTypes>
1762 class _Function_handler<_Res(_ArgTypes...), _Functor>
1763 : public _Function_base::_Base_manager<_Functor>
1765 typedef _Function_base::_Base_manager<_Functor> _Base;
1769 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1771 return (*_Base::_M_get_pointer(__functor))(__args...);
1775 template<typename _Functor, typename... _ArgTypes>
1776 class _Function_handler<void(_ArgTypes...), _Functor>
1777 : public _Function_base::_Base_manager<_Functor>
1779 typedef _Function_base::_Base_manager<_Functor> _Base;
1783 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1785 (*_Base::_M_get_pointer(__functor))(__args...);
1789 template<typename _Res, typename _Functor, typename... _ArgTypes>
1790 class _Function_handler<_Res(_ArgTypes...), reference_wrapper<_Functor> >
1791 : public _Function_base::_Ref_manager<_Functor>
1793 typedef _Function_base::_Ref_manager<_Functor> _Base;
1797 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1800 __callable_functor(**_Base::_M_get_pointer(__functor))(__args...);
1804 template<typename _Functor, typename... _ArgTypes>
1805 class _Function_handler<void(_ArgTypes...), reference_wrapper<_Functor> >
1806 : public _Function_base::_Ref_manager<_Functor>
1808 typedef _Function_base::_Ref_manager<_Functor> _Base;
1812 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1814 __callable_functor(**_Base::_M_get_pointer(__functor))(__args...);
1818 template<typename _Class, typename _Member, typename _Res,
1819 typename... _ArgTypes>
1820 class _Function_handler<_Res(_ArgTypes...), _Member _Class::*>
1821 : public _Function_handler<void(_ArgTypes...), _Member _Class::*>
1823 typedef _Function_handler<void(_ArgTypes...), _Member _Class::*>
1828 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1831 mem_fn(_Base::_M_get_pointer(__functor)->__value)(__args...);
1835 template<typename _Class, typename _Member, typename... _ArgTypes>
1836 class _Function_handler<void(_ArgTypes...), _Member _Class::*>
1837 : public _Function_base::_Base_manager<
1838 _Simple_type_wrapper< _Member _Class::* > >
1840 typedef _Member _Class::* _Functor;
1841 typedef _Simple_type_wrapper<_Functor> _Wrapper;
1842 typedef _Function_base::_Base_manager<_Wrapper> _Base;
1846 _M_manager(_Any_data& __dest, const _Any_data& __source,
1847 _Manager_operation __op)
1852 case __get_type_info:
1853 __dest._M_access<const type_info*>() = &typeid(_Functor);
1856 case __get_functor_ptr:
1857 __dest._M_access<_Functor*>() =
1858 &_Base::_M_get_pointer(__source)->__value;
1862 _Base::_M_manager(__dest, __source, __op);
1868 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1870 tr1::mem_fn(_Base::_M_get_pointer(__functor)->__value)(__args...);
1875 template<typename _Res, typename... _ArgTypes>
1876 class function<_Res(_ArgTypes...)>
1877 : public _Maybe_unary_or_binary_function<_Res, _ArgTypes...>,
1878 private _Function_base
1880 #if __cplusplus < 201103L
1881 /// This class is used to implement the safe_bool idiom.
1884 _Hidden_type* _M_bool;
1887 /// This typedef is used to implement the safe_bool idiom.
1888 typedef _Hidden_type* _Hidden_type::* _Safe_bool;
1891 typedef _Res _Signature_type(_ArgTypes...);
1893 struct _Useless { };
1896 typedef _Res result_type;
1898 // [3.7.2.1] construct/copy/destroy
1901 * @brief Default construct creates an empty function call wrapper.
1902 * @post @c !(bool)*this
1904 function() : _Function_base() { }
1907 * @brief Default construct creates an empty function call wrapper.
1908 * @post @c !(bool)*this
1910 function(_M_clear_type*) : _Function_base() { }
1913 * @brief %Function copy constructor.
1914 * @param x A %function object with identical call signature.
1915 * @post @c (bool)*this == (bool)x
1917 * The newly-created %function contains a copy of the target of @a
1918 * x (if it has one).
1920 function(const function& __x);
1923 * @brief Builds a %function that targets a copy of the incoming
1925 * @param f A %function object that is callable with parameters of
1926 * type @c T1, @c T2, ..., @c TN and returns a value convertible
1929 * The newly-created %function object will target a copy of @a
1930 * f. If @a f is @c reference_wrapper<F>, then this function
1931 * object will contain a reference to the function object @c
1932 * f.get(). If @a f is a NULL function pointer or NULL
1933 * pointer-to-member, the newly-created object will be empty.
1935 * If @a f is a non-NULL function pointer or an object of type @c
1936 * reference_wrapper<F>, this function will not throw.
1938 template<typename _Functor>
1939 function(_Functor __f,
1940 typename __gnu_cxx::__enable_if<
1941 !is_integral<_Functor>::value, _Useless>::__type
1945 * @brief %Function assignment operator.
1946 * @param x A %function with identical call signature.
1947 * @post @c (bool)*this == (bool)x
1950 * The target of @a x is copied to @c *this. If @a x has no
1951 * target, then @c *this will be empty.
1953 * If @a x targets a function pointer or a reference to a function
1954 * object, then this operation will not throw an %exception.
1957 operator=(const function& __x)
1959 function(__x).swap(*this);
1964 * @brief %Function assignment to zero.
1965 * @post @c !(bool)*this
1968 * The target of @c *this is deallocated, leaving it empty.
1971 operator=(_M_clear_type*)
1975 _M_manager(_M_functor, _M_functor, __destroy_functor);
1983 * @brief %Function assignment to a new target.
1984 * @param f A %function object that is callable with parameters of
1985 * type @c T1, @c T2, ..., @c TN and returns a value convertible
1989 * This %function object wrapper will target a copy of @a
1990 * f. If @a f is @c reference_wrapper<F>, then this function
1991 * object will contain a reference to the function object @c
1992 * f.get(). If @a f is a NULL function pointer or NULL
1993 * pointer-to-member, @c this object will be empty.
1995 * If @a f is a non-NULL function pointer or an object of type @c
1996 * reference_wrapper<F>, this function will not throw.
1998 template<typename _Functor>
1999 typename __gnu_cxx::__enable_if<!is_integral<_Functor>::value,
2001 operator=(_Functor __f)
2003 function(__f).swap(*this);
2007 // [3.7.2.2] function modifiers
2010 * @brief Swap the targets of two %function objects.
2011 * @param f A %function with identical call signature.
2013 * Swap the targets of @c this function object and @a f. This
2014 * function will not throw an %exception.
2016 void swap(function& __x)
2018 std::swap(_M_functor, __x._M_functor);
2019 std::swap(_M_manager, __x._M_manager);
2020 std::swap(_M_invoker, __x._M_invoker);
2023 // [3.7.2.3] function capacity
2026 * @brief Determine if the %function wrapper has a target.
2028 * @return @c true when this %function object contains a target,
2029 * or @c false when it is empty.
2031 * This function will not throw an %exception.
2033 #if __cplusplus >= 201103L
2034 explicit operator bool() const
2035 { return !_M_empty(); }
2037 operator _Safe_bool() const
2042 return &_Hidden_type::_M_bool;
2046 // [3.7.2.4] function invocation
2049 * @brief Invokes the function targeted by @c *this.
2050 * @returns the result of the target.
2051 * @throws bad_function_call when @c !(bool)*this
2053 * The function call operator invokes the target function object
2054 * stored by @c this.
2056 _Res operator()(_ArgTypes... __args) const;
2059 // [3.7.2.5] function target access
2061 * @brief Determine the type of the target of this function object
2064 * @returns the type identifier of the target function object, or
2065 * @c typeid(void) if @c !(bool)*this.
2067 * This function will not throw an %exception.
2069 const type_info& target_type() const;
2072 * @brief Access the stored target function object.
2074 * @return Returns a pointer to the stored target function object,
2075 * if @c typeid(Functor).equals(target_type()); otherwise, a NULL
2078 * This function will not throw an %exception.
2080 template<typename _Functor> _Functor* target();
2083 template<typename _Functor> const _Functor* target() const;
2087 // [3.7.2.6] undefined operators
2088 template<typename _Function>
2089 void operator==(const function<_Function>&) const;
2090 template<typename _Function>
2091 void operator!=(const function<_Function>&) const;
2093 typedef _Res (*_Invoker_type)(const _Any_data&, _ArgTypes...);
2094 _Invoker_type _M_invoker;
2096 #pragma GCC diagnostic pop
2098 template<typename _Res, typename... _ArgTypes>
2099 function<_Res(_ArgTypes...)>::
2100 function(const function& __x)
2103 if (static_cast<bool>(__x))
2105 __x._M_manager(_M_functor, __x._M_functor, __clone_functor);
2106 _M_invoker = __x._M_invoker;
2107 _M_manager = __x._M_manager;
2111 template<typename _Res, typename... _ArgTypes>
2112 template<typename _Functor>
2113 function<_Res(_ArgTypes...)>::
2114 function(_Functor __f,
2115 typename __gnu_cxx::__enable_if<
2116 !is_integral<_Functor>::value, _Useless>::__type)
2119 typedef _Function_handler<_Signature_type, _Functor> _My_handler;
2121 if (_My_handler::_M_not_empty_function(__f))
2123 _My_handler::_M_init_functor(_M_functor, __f);
2124 _M_invoker = &_My_handler::_M_invoke;
2125 _M_manager = &_My_handler::_M_manager;
2129 template<typename _Res, typename... _ArgTypes>
2131 function<_Res(_ArgTypes...)>::
2132 operator()(_ArgTypes... __args) const
2135 _GLIBCXX_THROW_OR_ABORT(bad_function_call());
2136 return _M_invoker(_M_functor, __args...);
2140 template<typename _Res, typename... _ArgTypes>
2142 function<_Res(_ArgTypes...)>::
2147 _Any_data __typeinfo_result;
2148 _M_manager(__typeinfo_result, _M_functor, __get_type_info);
2149 return *__typeinfo_result._M_access<const type_info*>();
2152 return typeid(void);
2155 template<typename _Res, typename... _ArgTypes>
2156 template<typename _Functor>
2158 function<_Res(_ArgTypes...)>::
2161 if (typeid(_Functor) == target_type() && _M_manager)
2164 if (_M_manager(__ptr, _M_functor, __get_functor_ptr)
2165 && !is_const<_Functor>::value)
2168 return __ptr._M_access<_Functor*>();
2174 template<typename _Res, typename... _ArgTypes>
2175 template<typename _Functor>
2177 function<_Res(_ArgTypes...)>::
2180 if (typeid(_Functor) == target_type() && _M_manager)
2183 _M_manager(__ptr, _M_functor, __get_functor_ptr);
2184 return __ptr._M_access<const _Functor*>();
2191 // [3.7.2.7] null pointer comparisons
2194 * @brief Compares a polymorphic function object wrapper against 0
2195 * (the NULL pointer).
2196 * @returns @c true if the wrapper has no target, @c false otherwise
2198 * This function will not throw an %exception.
2200 template<typename _Signature>
2202 operator==(const function<_Signature>& __f, _M_clear_type*)
2203 { return !static_cast<bool>(__f); }
2206 template<typename _Signature>
2208 operator==(_M_clear_type*, const function<_Signature>& __f)
2209 { return !static_cast<bool>(__f); }
2212 * @brief Compares a polymorphic function object wrapper against 0
2213 * (the NULL pointer).
2214 * @returns @c false if the wrapper has no target, @c true otherwise
2216 * This function will not throw an %exception.
2218 template<typename _Signature>
2220 operator!=(const function<_Signature>& __f, _M_clear_type*)
2221 { return static_cast<bool>(__f); }
2224 template<typename _Signature>
2226 operator!=(_M_clear_type*, const function<_Signature>& __f)
2227 { return static_cast<bool>(__f); }
2229 // [3.7.2.8] specialized algorithms
2232 * @brief Swap the targets of two polymorphic function object wrappers.
2234 * This function will not throw an %exception.
2236 template<typename _Signature>
2238 swap(function<_Signature>& __x, function<_Signature>& __y)
2242 #if __cplusplus >= 201103L
2243 // Specialize std::is_bind_expression for tr1::bind closure types,
2244 // so that they can also work with std::bind.
2246 template<typename _Signature>
2247 struct is_bind_expression<tr1::_Bind<_Signature>>
2250 template<typename _Signature>
2251 struct is_bind_expression<const tr1::_Bind<_Signature>>
2254 template<typename _Signature>
2255 struct is_bind_expression<volatile tr1::_Bind<_Signature>>
2258 template<typename _Signature>
2259 struct is_bind_expression<const volatile tr1::_Bind<_Signature>>
2262 template<typename _Result, typename _Signature>
2263 struct is_bind_expression<tr1::_Bind_result<_Result, _Signature>>
2266 template<typename _Result, typename _Signature>
2267 struct is_bind_expression<const tr1::_Bind_result<_Result, _Signature>>
2270 template<typename _Result, typename _Signature>
2271 struct is_bind_expression<volatile tr1::_Bind_result<_Result, _Signature>>
2274 template<typename _Result, typename _Signature>
2275 struct is_bind_expression<const volatile tr1::_Bind_result<_Result,
2280 _GLIBCXX_END_NAMESPACE_VERSION
2283 #endif // _GLIBCXX_TR1_FUNCTIONAL