1 // TR1 functional header -*- C++ -*-
3 // Copyright (C) 2004-2023 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 <bits/requires_hosted.h> // TR1
36 #include <functional> // for std::_Placeholder, std::_Bind, std::_Bind_result
41 #include <tr1/type_traits>
42 #include <bits/stringfwd.h>
43 #include <tr1/functional_hash.h>
44 #include <ext/type_traits.h>
45 #include <bits/move.h> // for std::__addressof
47 namespace std _GLIBCXX_VISIBILITY(default)
49 _GLIBCXX_BEGIN_NAMESPACE_VERSION
51 #if __cplusplus < 201103L
52 // In C++98 mode, <functional> doesn't declare std::placeholders::_1 etc.
53 // because they are not reserved names in C++98. However, they are reserved
54 // by <tr1/functional> so we can declare them here, in order to redeclare
55 // them in the std::tr1::placeholders namespace below.
56 namespace placeholders
58 extern const _Placeholder<1> _1;
59 extern const _Placeholder<2> _2;
60 extern const _Placeholder<3> _3;
61 extern const _Placeholder<4> _4;
62 extern const _Placeholder<5> _5;
63 extern const _Placeholder<6> _6;
64 extern const _Placeholder<7> _7;
65 extern const _Placeholder<8> _8;
66 extern const _Placeholder<9> _9;
67 extern const _Placeholder<10> _10;
68 extern const _Placeholder<11> _11;
69 extern const _Placeholder<12> _12;
70 extern const _Placeholder<13> _13;
71 extern const _Placeholder<14> _14;
72 extern const _Placeholder<15> _15;
73 extern const _Placeholder<16> _16;
74 extern const _Placeholder<17> _17;
75 extern const _Placeholder<18> _18;
76 extern const _Placeholder<19> _19;
77 extern const _Placeholder<20> _20;
78 extern const _Placeholder<21> _21;
79 extern const _Placeholder<22> _22;
80 extern const _Placeholder<23> _23;
81 extern const _Placeholder<24> _24;
82 extern const _Placeholder<25> _25;
83 extern const _Placeholder<26> _26;
84 extern const _Placeholder<27> _27;
85 extern const _Placeholder<28> _28;
86 extern const _Placeholder<29> _29;
92 template<typename _MemberPointer>
94 template<typename _Tp, typename _Class>
95 _Mem_fn<_Tp _Class::*>
96 mem_fn(_Tp _Class::*);
99 * Actual implementation of _Has_result_type, which uses SFINAE to
100 * determine if the type _Tp has a publicly-accessible member type
103 template<typename _Tp>
104 class _Has_result_type_helper : __sfinae_types
106 template<typename _Up>
110 template<typename _Up>
111 static __one __test(_Wrap_type<typename _Up::result_type>*);
113 template<typename _Up>
114 static __two __test(...);
117 static const bool value = sizeof(__test<_Tp>(0)) == 1;
120 template<typename _Tp>
121 struct _Has_result_type
122 : integral_constant<bool,
123 _Has_result_type_helper<typename remove_cv<_Tp>::type>::value>
129 /// If we have found a result_type, extract it.
130 template<bool _Has_result_type, typename _Functor>
131 struct _Maybe_get_result_type
134 template<typename _Functor>
135 struct _Maybe_get_result_type<true, _Functor>
137 typedef typename _Functor::result_type result_type;
141 * Base class for any function object that has a weak result type, as
142 * defined in 3.3/3 of TR1.
144 template<typename _Functor>
145 struct _Weak_result_type_impl
146 : _Maybe_get_result_type<_Has_result_type<_Functor>::value, _Functor>
150 /// Retrieve the result type for a function type.
151 template<typename _Res, typename... _ArgTypes>
152 struct _Weak_result_type_impl<_Res(_ArgTypes...)>
154 typedef _Res result_type;
157 /// Retrieve the result type for a function reference.
158 template<typename _Res, typename... _ArgTypes>
159 struct _Weak_result_type_impl<_Res(&)(_ArgTypes...)>
161 typedef _Res result_type;
164 /// Retrieve the result type for a function pointer.
165 template<typename _Res, typename... _ArgTypes>
166 struct _Weak_result_type_impl<_Res(*)(_ArgTypes...)>
168 typedef _Res result_type;
171 /// Retrieve result type for a member function pointer.
172 template<typename _Res, typename _Class, typename... _ArgTypes>
173 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...)>
175 typedef _Res result_type;
178 /// Retrieve result type for a const member function pointer.
179 template<typename _Res, typename _Class, typename... _ArgTypes>
180 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) const>
182 typedef _Res result_type;
185 /// Retrieve result type for a volatile member function pointer.
186 template<typename _Res, typename _Class, typename... _ArgTypes>
187 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) volatile>
189 typedef _Res result_type;
192 /// Retrieve result type for a const volatile member function pointer.
193 template<typename _Res, typename _Class, typename... _ArgTypes>
194 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...)const volatile>
196 typedef _Res result_type;
200 * Strip top-level cv-qualifiers from the function object and let
201 * _Weak_result_type_impl perform the real work.
203 template<typename _Functor>
204 struct _Weak_result_type
205 : _Weak_result_type_impl<typename remove_cv<_Functor>::type>
209 template<typename _Signature>
213 * Actual implementation of result_of. When _Has_result_type is
214 * true, gets its result from _Weak_result_type. Otherwise, uses
215 * the function object's member template result to extract the
218 template<bool _Has_result_type, typename _Signature>
219 struct _Result_of_impl;
221 // Handle member data pointers using _Mem_fn's logic
222 template<typename _Res, typename _Class, typename _T1>
223 struct _Result_of_impl<false, _Res _Class::*(_T1)>
225 typedef typename _Mem_fn<_Res _Class::*>
226 ::template _Result_type<_T1>::type type;
230 * Determine whether we can determine a result type from @c Functor
233 template<typename _Functor, typename... _ArgTypes>
234 class result_of<_Functor(_ArgTypes...)>
235 : public _Result_of_impl<
236 _Has_result_type<_Weak_result_type<_Functor> >::value,
237 _Functor(_ArgTypes...)>
241 /// We already know the result type for @c Functor; use it.
242 template<typename _Functor, typename... _ArgTypes>
243 struct _Result_of_impl<true, _Functor(_ArgTypes...)>
245 typedef typename _Weak_result_type<_Functor>::result_type type;
249 * We need to compute the result type for this invocation the hard
252 template<typename _Functor, typename... _ArgTypes>
253 struct _Result_of_impl<false, _Functor(_ArgTypes...)>
255 typedef typename _Functor
256 ::template result<_Functor(_ArgTypes...)>::type type;
260 * It is unsafe to access ::result when there are zero arguments, so we
261 * return @c void instead.
263 template<typename _Functor>
264 struct _Result_of_impl<false, _Functor()>
269 // Ignore warnings about std::unary_function and std::binary_function.
270 #pragma GCC diagnostic push
271 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
273 /// Determines if the type _Tp derives from unary_function.
274 template<typename _Tp>
275 struct _Derives_from_unary_function : __sfinae_types
278 template<typename _T1, typename _Res>
279 static __one __test(const volatile unary_function<_T1, _Res>*);
281 // It's tempting to change "..." to const volatile void*, but
282 // that fails when _Tp is a function type.
283 static __two __test(...);
286 static const bool value = sizeof(__test((_Tp*)0)) == 1;
289 /// Determines if the type _Tp derives from binary_function.
290 template<typename _Tp>
291 struct _Derives_from_binary_function : __sfinae_types
294 template<typename _T1, typename _T2, typename _Res>
295 static __one __test(const volatile binary_function<_T1, _T2, _Res>*);
297 // It's tempting to change "..." to const volatile void*, but
298 // that fails when _Tp is a function type.
299 static __two __test(...);
302 static const bool value = sizeof(__test((_Tp*)0)) == 1;
305 /// Turns a function type into a function pointer type
306 template<typename _Tp, bool _IsFunctionType = is_function<_Tp>::value>
307 struct _Function_to_function_pointer
312 template<typename _Tp>
313 struct _Function_to_function_pointer<_Tp, true>
319 * Invoke a function object, which may be either a member pointer or a
320 * function object. The first parameter will tell which.
322 template<typename _Functor, typename... _Args>
324 typename __gnu_cxx::__enable_if<
325 (!is_member_pointer<_Functor>::value
326 && !is_function<_Functor>::value
327 && !is_function<typename remove_pointer<_Functor>::type>::value),
328 typename result_of<_Functor(_Args...)>::type
330 __invoke(_Functor& __f, _Args&... __args)
332 return __f(__args...);
335 template<typename _Functor, typename... _Args>
337 typename __gnu_cxx::__enable_if<
338 (is_member_pointer<_Functor>::value
339 && !is_function<_Functor>::value
340 && !is_function<typename remove_pointer<_Functor>::type>::value),
341 typename result_of<_Functor(_Args...)>::type
343 __invoke(_Functor& __f, _Args&... __args)
345 return mem_fn(__f)(__args...);
348 // To pick up function references (that will become function pointers)
349 template<typename _Functor, typename... _Args>
351 typename __gnu_cxx::__enable_if<
352 (is_pointer<_Functor>::value
353 && is_function<typename remove_pointer<_Functor>::type>::value),
354 typename result_of<_Functor(_Args...)>::type
356 __invoke(_Functor __f, _Args&... __args)
358 return __f(__args...);
362 * Knowing which of unary_function and binary_function _Tp derives
363 * from, derives from the same and ensures that reference_wrapper
364 * will have a weak result type. See cases below.
366 template<bool _Unary, bool _Binary, typename _Tp>
367 struct _Reference_wrapper_base_impl;
369 // Not a unary_function or binary_function, so try a weak result type.
370 template<typename _Tp>
371 struct _Reference_wrapper_base_impl<false, false, _Tp>
372 : _Weak_result_type<_Tp>
375 // unary_function but not binary_function
376 template<typename _Tp>
377 struct _Reference_wrapper_base_impl<true, false, _Tp>
378 : unary_function<typename _Tp::argument_type,
379 typename _Tp::result_type>
382 // binary_function but not unary_function
383 template<typename _Tp>
384 struct _Reference_wrapper_base_impl<false, true, _Tp>
385 : binary_function<typename _Tp::first_argument_type,
386 typename _Tp::second_argument_type,
387 typename _Tp::result_type>
390 // Both unary_function and binary_function. Import result_type to
392 template<typename _Tp>
393 struct _Reference_wrapper_base_impl<true, true, _Tp>
394 : unary_function<typename _Tp::argument_type,
395 typename _Tp::result_type>,
396 binary_function<typename _Tp::first_argument_type,
397 typename _Tp::second_argument_type,
398 typename _Tp::result_type>
400 typedef typename _Tp::result_type result_type;
404 * Derives from unary_function or binary_function when it
405 * can. Specializations handle all of the easy cases. The primary
406 * template determines what to do with a class type, which may
407 * derive from both unary_function and binary_function.
409 template<typename _Tp>
410 struct _Reference_wrapper_base
411 : _Reference_wrapper_base_impl<
412 _Derives_from_unary_function<_Tp>::value,
413 _Derives_from_binary_function<_Tp>::value,
417 // - a function type (unary)
418 template<typename _Res, typename _T1>
419 struct _Reference_wrapper_base<_Res(_T1)>
420 : unary_function<_T1, _Res>
423 // - a function type (binary)
424 template<typename _Res, typename _T1, typename _T2>
425 struct _Reference_wrapper_base<_Res(_T1, _T2)>
426 : binary_function<_T1, _T2, _Res>
429 // - a function pointer type (unary)
430 template<typename _Res, typename _T1>
431 struct _Reference_wrapper_base<_Res(*)(_T1)>
432 : unary_function<_T1, _Res>
435 // - a function pointer type (binary)
436 template<typename _Res, typename _T1, typename _T2>
437 struct _Reference_wrapper_base<_Res(*)(_T1, _T2)>
438 : binary_function<_T1, _T2, _Res>
441 // - a pointer to member function type (unary, no qualifiers)
442 template<typename _Res, typename _T1>
443 struct _Reference_wrapper_base<_Res (_T1::*)()>
444 : unary_function<_T1*, _Res>
447 // - a pointer to member function type (binary, no qualifiers)
448 template<typename _Res, typename _T1, typename _T2>
449 struct _Reference_wrapper_base<_Res (_T1::*)(_T2)>
450 : binary_function<_T1*, _T2, _Res>
453 // - a pointer to member function type (unary, const)
454 template<typename _Res, typename _T1>
455 struct _Reference_wrapper_base<_Res (_T1::*)() const>
456 : unary_function<const _T1*, _Res>
459 // - a pointer to member function type (binary, const)
460 template<typename _Res, typename _T1, typename _T2>
461 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const>
462 : binary_function<const _T1*, _T2, _Res>
465 // - a pointer to member function type (unary, volatile)
466 template<typename _Res, typename _T1>
467 struct _Reference_wrapper_base<_Res (_T1::*)() volatile>
468 : unary_function<volatile _T1*, _Res>
471 // - a pointer to member function type (binary, volatile)
472 template<typename _Res, typename _T1, typename _T2>
473 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) volatile>
474 : binary_function<volatile _T1*, _T2, _Res>
477 // - a pointer to member function type (unary, const volatile)
478 template<typename _Res, typename _T1>
479 struct _Reference_wrapper_base<_Res (_T1::*)() const volatile>
480 : unary_function<const volatile _T1*, _Res>
483 // - a pointer to member function type (binary, const volatile)
484 template<typename _Res, typename _T1, typename _T2>
485 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const volatile>
486 : binary_function<const volatile _T1*, _T2, _Res>
489 /// reference_wrapper
490 template<typename _Tp>
491 class reference_wrapper
492 : public _Reference_wrapper_base<typename remove_cv<_Tp>::type>
494 // If _Tp is a function type, we can't form result_of<_Tp(...)>,
495 // so turn it into a function pointer type.
496 typedef typename _Function_to_function_pointer<_Tp>::type
504 reference_wrapper(_Tp& __indata)
505 : _M_data(std::__addressof(__indata))
508 reference_wrapper(const reference_wrapper<_Tp>& __inref):
509 _M_data(__inref._M_data)
513 operator=(const reference_wrapper<_Tp>& __inref)
515 _M_data = __inref._M_data;
519 operator _Tp&() const
520 { return this->get(); }
526 template<typename... _Args>
527 typename result_of<_M_func_type(_Args...)>::type
528 operator()(_Args&... __args) const
530 return __invoke(get(), __args...);
535 // Denotes a reference should be taken to a variable.
536 template<typename _Tp>
537 inline reference_wrapper<_Tp>
539 { return reference_wrapper<_Tp>(__t); }
541 // Denotes a const reference should be taken to a variable.
542 template<typename _Tp>
543 inline reference_wrapper<const _Tp>
545 { return reference_wrapper<const _Tp>(__t); }
547 template<typename _Tp>
548 inline reference_wrapper<_Tp>
549 ref(reference_wrapper<_Tp> __t)
550 { return ref(__t.get()); }
552 template<typename _Tp>
553 inline reference_wrapper<const _Tp>
554 cref(reference_wrapper<_Tp> __t)
555 { return cref(__t.get()); }
557 template<typename _Tp, bool>
558 struct _Mem_fn_const_or_non
560 typedef const _Tp& type;
563 template<typename _Tp>
564 struct _Mem_fn_const_or_non<_Tp, false>
570 * Derives from @c unary_function or @c binary_function, or perhaps
571 * nothing, depending on the number of arguments provided. The
572 * primary template is the basis case, which derives nothing.
574 template<typename _Res, typename... _ArgTypes>
575 struct _Maybe_unary_or_binary_function { };
577 /// Derives from @c unary_function, as appropriate.
578 template<typename _Res, typename _T1>
579 struct _Maybe_unary_or_binary_function<_Res, _T1>
580 : std::unary_function<_T1, _Res> { };
582 /// Derives from @c binary_function, as appropriate.
583 template<typename _Res, typename _T1, typename _T2>
584 struct _Maybe_unary_or_binary_function<_Res, _T1, _T2>
585 : std::binary_function<_T1, _T2, _Res> { };
587 /// Implementation of @c mem_fn for member function pointers.
588 template<typename _Res, typename _Class, typename... _ArgTypes>
589 class _Mem_fn<_Res (_Class::*)(_ArgTypes...)>
590 : public _Maybe_unary_or_binary_function<_Res, _Class*, _ArgTypes...>
592 typedef _Res (_Class::*_Functor)(_ArgTypes...);
594 template<typename _Tp>
596 _M_call(_Tp& __object, const volatile _Class *,
597 _ArgTypes... __args) const
598 { return (__object.*__pmf)(__args...); }
600 template<typename _Tp>
602 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
603 { return ((*__ptr).*__pmf)(__args...); }
606 typedef _Res result_type;
608 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
612 operator()(_Class& __object, _ArgTypes... __args) const
613 { return (__object.*__pmf)(__args...); }
617 operator()(_Class* __object, _ArgTypes... __args) const
618 { return (__object->*__pmf)(__args...); }
620 // Handle smart pointers, references and pointers to derived
621 template<typename _Tp>
623 operator()(_Tp& __object, _ArgTypes... __args) const
624 { return _M_call(__object, &__object, __args...); }
630 /// Implementation of @c mem_fn for const member function pointers.
631 template<typename _Res, typename _Class, typename... _ArgTypes>
632 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) const>
633 : public _Maybe_unary_or_binary_function<_Res, const _Class*,
636 typedef _Res (_Class::*_Functor)(_ArgTypes...) const;
638 template<typename _Tp>
640 _M_call(_Tp& __object, const volatile _Class *,
641 _ArgTypes... __args) const
642 { return (__object.*__pmf)(__args...); }
644 template<typename _Tp>
646 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
647 { return ((*__ptr).*__pmf)(__args...); }
650 typedef _Res result_type;
652 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
656 operator()(const _Class& __object, _ArgTypes... __args) const
657 { return (__object.*__pmf)(__args...); }
661 operator()(const _Class* __object, _ArgTypes... __args) const
662 { return (__object->*__pmf)(__args...); }
664 // Handle smart pointers, references and pointers to derived
665 template<typename _Tp>
666 _Res operator()(_Tp& __object, _ArgTypes... __args) const
667 { return _M_call(__object, &__object, __args...); }
673 /// Implementation of @c mem_fn for volatile member function pointers.
674 template<typename _Res, typename _Class, typename... _ArgTypes>
675 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) volatile>
676 : public _Maybe_unary_or_binary_function<_Res, volatile _Class*,
679 typedef _Res (_Class::*_Functor)(_ArgTypes...) volatile;
681 template<typename _Tp>
683 _M_call(_Tp& __object, const volatile _Class *,
684 _ArgTypes... __args) const
685 { return (__object.*__pmf)(__args...); }
687 template<typename _Tp>
689 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
690 { return ((*__ptr).*__pmf)(__args...); }
693 typedef _Res result_type;
695 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
699 operator()(volatile _Class& __object, _ArgTypes... __args) const
700 { return (__object.*__pmf)(__args...); }
704 operator()(volatile _Class* __object, _ArgTypes... __args) const
705 { return (__object->*__pmf)(__args...); }
707 // Handle smart pointers, references and pointers to derived
708 template<typename _Tp>
710 operator()(_Tp& __object, _ArgTypes... __args) const
711 { return _M_call(__object, &__object, __args...); }
717 /// Implementation of @c mem_fn for const volatile member function pointers.
718 template<typename _Res, typename _Class, typename... _ArgTypes>
719 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) const volatile>
720 : public _Maybe_unary_or_binary_function<_Res, const volatile _Class*,
723 typedef _Res (_Class::*_Functor)(_ArgTypes...) const volatile;
725 template<typename _Tp>
727 _M_call(_Tp& __object, const volatile _Class *,
728 _ArgTypes... __args) const
729 { return (__object.*__pmf)(__args...); }
731 template<typename _Tp>
733 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
734 { return ((*__ptr).*__pmf)(__args...); }
737 typedef _Res result_type;
739 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
743 operator()(const volatile _Class& __object, _ArgTypes... __args) const
744 { return (__object.*__pmf)(__args...); }
748 operator()(const volatile _Class* __object, _ArgTypes... __args) const
749 { return (__object->*__pmf)(__args...); }
751 // Handle smart pointers, references and pointers to derived
752 template<typename _Tp>
753 _Res operator()(_Tp& __object, _ArgTypes... __args) const
754 { return _M_call(__object, &__object, __args...); }
761 template<typename _Res, typename _Class>
762 class _Mem_fn<_Res _Class::*>
764 // This bit of genius is due to Peter Dimov, improved slightly by
766 template<typename _Tp>
768 _M_call(_Tp& __object, _Class *) const
769 { return __object.*__pm; }
771 template<typename _Tp, typename _Up>
773 _M_call(_Tp& __object, _Up * const *) const
774 { return (*__object).*__pm; }
776 template<typename _Tp, typename _Up>
778 _M_call(_Tp& __object, const _Up * const *) const
779 { return (*__object).*__pm; }
781 template<typename _Tp>
783 _M_call(_Tp& __object, const _Class *) const
784 { return __object.*__pm; }
786 template<typename _Tp>
788 _M_call(_Tp& __ptr, const volatile void*) const
789 { return (*__ptr).*__pm; }
791 template<typename _Tp> static _Tp& __get_ref();
793 template<typename _Tp>
794 static __sfinae_types::__one __check_const(_Tp&, _Class*);
795 template<typename _Tp, typename _Up>
796 static __sfinae_types::__one __check_const(_Tp&, _Up * const *);
797 template<typename _Tp, typename _Up>
798 static __sfinae_types::__two __check_const(_Tp&, const _Up * const *);
799 template<typename _Tp>
800 static __sfinae_types::__two __check_const(_Tp&, const _Class*);
801 template<typename _Tp>
802 static __sfinae_types::__two __check_const(_Tp&, const volatile void*);
805 template<typename _Tp>
807 : _Mem_fn_const_or_non<_Res,
808 (sizeof(__sfinae_types::__two)
809 == sizeof(__check_const<_Tp>(__get_ref<_Tp>(), (_Tp*)0)))>
812 template<typename _Signature>
815 template<typename _CVMem, typename _Tp>
816 struct result<_CVMem(_Tp)>
817 : public _Result_type<_Tp> { };
819 template<typename _CVMem, typename _Tp>
820 struct result<_CVMem(_Tp&)>
821 : public _Result_type<_Tp> { };
824 _Mem_fn(_Res _Class::*__pm) : __pm(__pm) { }
828 operator()(_Class& __object) const
829 { return __object.*__pm; }
832 operator()(const _Class& __object) const
833 { return __object.*__pm; }
837 operator()(_Class* __object) const
838 { return __object->*__pm; }
841 operator()(const _Class* __object) const
842 { return __object->*__pm; }
844 // Handle smart pointers and derived
845 template<typename _Tp>
846 typename _Result_type<_Tp>::type
847 operator()(_Tp& __unknown) const
848 { return _M_call(__unknown, &__unknown); }
855 * @brief Returns a function object that forwards to the member
858 template<typename _Tp, typename _Class>
859 inline _Mem_fn<_Tp _Class::*>
860 mem_fn(_Tp _Class::* __pm)
862 return _Mem_fn<_Tp _Class::*>(__pm);
866 * @brief Determines if the given type _Tp is a function object
867 * should be treated as a subexpression when evaluating calls to
868 * function objects returned by bind(). [TR1 3.6.1]
870 template<typename _Tp>
871 struct is_bind_expression
872 { static const bool value = false; };
874 template<typename _Tp>
875 const bool is_bind_expression<_Tp>::value;
878 * @brief Determines if the given type _Tp is a placeholder in a
879 * bind() expression and, if so, which placeholder it is. [TR1 3.6.2]
881 template<typename _Tp>
882 struct is_placeholder
883 { static const int value = 0; };
885 template<typename _Tp>
886 const int is_placeholder<_Tp>::value;
888 /// The type of placeholder objects defined by libstdc++.
889 using ::std::_Placeholder;
891 /** @namespace std::tr1::placeholders
892 * @brief Sub-namespace for tr1/functional.
894 namespace placeholders
896 // The C++11 std::placeholders are already exported from the library.
897 // Reusing them here avoids needing to export additional symbols for
898 // the TR1 placeholders, and avoids ODR violations due to defining
899 // them with internal linkage (as we used to do).
900 using namespace ::std::placeholders;
904 * Partial specialization of is_placeholder that provides the placeholder
905 * number for the placeholder objects defined by libstdc++.
908 struct is_placeholder<_Placeholder<_Num> >
909 : integral_constant<int, _Num>
913 struct is_placeholder<const _Placeholder<_Num> >
914 : integral_constant<int, _Num>
918 * Stores a tuple of indices. Used by bind() to extract the elements
921 template<int... _Indexes>
922 struct _Index_tuple { };
924 /// Builds an _Index_tuple<0, 1, 2, ..., _Num-1>.
925 template<std::size_t _Num, typename _Tuple = _Index_tuple<> >
926 struct _Build_index_tuple;
928 template<std::size_t _Num, int... _Indexes>
929 struct _Build_index_tuple<_Num, _Index_tuple<_Indexes...> >
930 : _Build_index_tuple<_Num - 1,
931 _Index_tuple<_Indexes..., sizeof...(_Indexes)> >
935 template<int... _Indexes>
936 struct _Build_index_tuple<0, _Index_tuple<_Indexes...> >
938 typedef _Index_tuple<_Indexes...> __type;
942 * Used by _Safe_tuple_element to indicate that there is no tuple
943 * element at this position.
945 struct _No_tuple_element;
948 * Implementation helper for _Safe_tuple_element. This primary
949 * template handles the case where it is safe to use @c
952 template<int __i, typename _Tuple, bool _IsSafe>
953 struct _Safe_tuple_element_impl
954 : tuple_element<__i, _Tuple> { };
957 * Implementation helper for _Safe_tuple_element. This partial
958 * specialization handles the case where it is not safe to use @c
959 * tuple_element. We just return @c _No_tuple_element.
961 template<int __i, typename _Tuple>
962 struct _Safe_tuple_element_impl<__i, _Tuple, false>
964 typedef _No_tuple_element type;
968 * Like tuple_element, but returns @c _No_tuple_element when
969 * tuple_element would return an error.
971 template<int __i, typename _Tuple>
972 struct _Safe_tuple_element
973 : _Safe_tuple_element_impl<__i, _Tuple,
974 (__i >= 0 && __i < tuple_size<_Tuple>::value)>
979 * Maps an argument to bind() into an actual argument to the bound
980 * function object [TR1 3.6.3/5]. Only the first parameter should
981 * be specified: the rest are used to determine among the various
982 * implementations. Note that, although this class is a function
983 * object, it isn't entirely normal because it takes only two
984 * parameters regardless of the number of parameters passed to the
985 * bind expression. The first parameter is the bound argument and
986 * the second parameter is a tuple containing references to the
987 * rest of the arguments.
989 template<typename _Arg,
990 bool _IsBindExp = is_bind_expression<_Arg>::value,
991 bool _IsPlaceholder = (is_placeholder<_Arg>::value > 0)>
995 * If the argument is reference_wrapper<_Tp>, returns the
996 * underlying reference. [TR1 3.6.3/5 bullet 1]
998 template<typename _Tp>
999 class _Mu<reference_wrapper<_Tp>, false, false>
1002 typedef _Tp& result_type;
1004 /* Note: This won't actually work for const volatile
1005 * reference_wrappers, because reference_wrapper::get() is const
1006 * but not volatile-qualified. This might be a defect in the TR.
1008 template<typename _CVRef, typename _Tuple>
1010 operator()(_CVRef& __arg, const _Tuple&) const volatile
1011 { return __arg.get(); }
1015 * If the argument is a bind expression, we invoke the underlying
1016 * function object with the same cv-qualifiers as we are given and
1017 * pass along all of our arguments (unwrapped). [TR1 3.6.3/5 bullet 2]
1019 template<typename _Arg>
1020 class _Mu<_Arg, true, false>
1023 template<typename _Signature> class result;
1025 // Determine the result type when we pass the arguments along. This
1026 // involves passing along the cv-qualifiers placed on _Mu and
1027 // unwrapping the argument bundle.
1028 template<typename _CVMu, typename _CVArg, typename... _Args>
1029 class result<_CVMu(_CVArg, tuple<_Args...>)>
1030 : public result_of<_CVArg(_Args...)> { };
1032 template<typename _CVArg, typename... _Args>
1033 typename result_of<_CVArg(_Args...)>::type
1034 operator()(_CVArg& __arg,
1035 const tuple<_Args...>& __tuple) const volatile
1037 // Construct an index tuple and forward to __call
1038 typedef typename _Build_index_tuple<sizeof...(_Args)>::__type
1040 return this->__call(__arg, __tuple, _Indexes());
1044 // Invokes the underlying function object __arg by unpacking all
1045 // of the arguments in the tuple.
1046 template<typename _CVArg, typename... _Args, int... _Indexes>
1047 typename result_of<_CVArg(_Args...)>::type
1048 __call(_CVArg& __arg, const tuple<_Args...>& __tuple,
1049 const _Index_tuple<_Indexes...>&) const volatile
1051 return __arg(tr1::get<_Indexes>(__tuple)...);
1056 * If the argument is a placeholder for the Nth argument, returns
1057 * a reference to the Nth argument to the bind function object.
1058 * [TR1 3.6.3/5 bullet 3]
1060 template<typename _Arg>
1061 class _Mu<_Arg, false, true>
1064 template<typename _Signature> class result;
1066 template<typename _CVMu, typename _CVArg, typename _Tuple>
1067 class result<_CVMu(_CVArg, _Tuple)>
1069 // Add a reference, if it hasn't already been done for us.
1070 // This allows us to be a little bit sloppy in constructing
1071 // the tuple that we pass to result_of<...>.
1072 typedef typename _Safe_tuple_element<(is_placeholder<_Arg>::value
1077 typedef typename add_reference<__base_type>::type type;
1080 template<typename _Tuple>
1081 typename result<_Mu(_Arg, _Tuple)>::type
1082 operator()(const volatile _Arg&, const _Tuple& __tuple) const volatile
1084 return ::std::tr1::get<(is_placeholder<_Arg>::value - 1)>(__tuple);
1089 * If the argument is just a value, returns a reference to that
1090 * value. The cv-qualifiers on the reference are the same as the
1091 * cv-qualifiers on the _Mu object. [TR1 3.6.3/5 bullet 4]
1093 template<typename _Arg>
1094 class _Mu<_Arg, false, false>
1097 template<typename _Signature> struct result;
1099 template<typename _CVMu, typename _CVArg, typename _Tuple>
1100 struct result<_CVMu(_CVArg, _Tuple)>
1102 typedef typename add_reference<_CVArg>::type type;
1105 // Pick up the cv-qualifiers of the argument
1106 template<typename _CVArg, typename _Tuple>
1108 operator()(_CVArg& __arg, const _Tuple&) const volatile
1113 * Maps member pointers into instances of _Mem_fn but leaves all
1114 * other function objects untouched. Used by tr1::bind(). The
1115 * primary template handles the non--member-pointer case.
1117 template<typename _Tp>
1118 struct _Maybe_wrap_member_pointer
1123 __do_wrap(const _Tp& __x)
1128 * Maps member pointers into instances of _Mem_fn but leaves all
1129 * other function objects untouched. Used by tr1::bind(). This
1130 * partial specialization handles the member pointer case.
1132 template<typename _Tp, typename _Class>
1133 struct _Maybe_wrap_member_pointer<_Tp _Class::*>
1135 typedef _Mem_fn<_Tp _Class::*> type;
1138 __do_wrap(_Tp _Class::* __pm)
1139 { return type(__pm); }
1142 /// Type of the function object returned from bind().
1143 template<typename _Signature>
1146 template<typename _Functor, typename... _Bound_args>
1147 class _Bind<_Functor(_Bound_args...)>
1148 : public _Weak_result_type<_Functor>
1150 typedef _Bind __self_type;
1151 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
1155 tuple<_Bound_args...> _M_bound_args;
1158 template<typename... _Args, int... _Indexes>
1160 _Functor(typename result_of<_Mu<_Bound_args>
1161 (_Bound_args, tuple<_Args...>)>::type...)
1163 __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>)
1165 return _M_f(_Mu<_Bound_args>()
1166 (tr1::get<_Indexes>(_M_bound_args), __args)...);
1170 template<typename... _Args, int... _Indexes>
1172 const _Functor(typename result_of<_Mu<_Bound_args>
1173 (const _Bound_args, tuple<_Args...>)
1175 __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>) const
1177 return _M_f(_Mu<_Bound_args>()
1178 (tr1::get<_Indexes>(_M_bound_args), __args)...);
1182 template<typename... _Args, int... _Indexes>
1184 volatile _Functor(typename result_of<_Mu<_Bound_args>
1185 (volatile _Bound_args, tuple<_Args...>)
1187 __call(const tuple<_Args...>& __args,
1188 _Index_tuple<_Indexes...>) volatile
1190 return _M_f(_Mu<_Bound_args>()
1191 (tr1::get<_Indexes>(_M_bound_args), __args)...);
1194 // Call as const volatile
1195 template<typename... _Args, int... _Indexes>
1197 const volatile _Functor(typename result_of<_Mu<_Bound_args>
1198 (const volatile _Bound_args,
1201 __call(const tuple<_Args...>& __args,
1202 _Index_tuple<_Indexes...>) const volatile
1204 return _M_f(_Mu<_Bound_args>()
1205 (tr1::get<_Indexes>(_M_bound_args), __args)...);
1209 explicit _Bind(_Functor __f, _Bound_args... __bound_args)
1210 : _M_f(__f), _M_bound_args(__bound_args...) { }
1213 template<typename... _Args>
1215 _Functor(typename result_of<_Mu<_Bound_args>
1216 (_Bound_args, tuple<_Args...>)>::type...)
1218 operator()(_Args&... __args)
1220 return this->__call(tr1::tie(__args...), _Bound_indexes());
1224 template<typename... _Args>
1226 const _Functor(typename result_of<_Mu<_Bound_args>
1227 (const _Bound_args, tuple<_Args...>)>::type...)
1229 operator()(_Args&... __args) const
1231 return this->__call(tr1::tie(__args...), _Bound_indexes());
1236 template<typename... _Args>
1238 volatile _Functor(typename result_of<_Mu<_Bound_args>
1239 (volatile _Bound_args, tuple<_Args...>)>::type...)
1241 operator()(_Args&... __args) volatile
1243 return this->__call(tr1::tie(__args...), _Bound_indexes());
1247 // Call as const volatile
1248 template<typename... _Args>
1250 const volatile _Functor(typename result_of<_Mu<_Bound_args>
1251 (const volatile _Bound_args,
1252 tuple<_Args...>)>::type...)
1254 operator()(_Args&... __args) const volatile
1256 return this->__call(tr1::tie(__args...), _Bound_indexes());
1260 /// Type of the function object returned from bind<R>().
1261 template<typename _Result, typename _Signature>
1262 struct _Bind_result;
1264 template<typename _Result, typename _Functor, typename... _Bound_args>
1265 class _Bind_result<_Result, _Functor(_Bound_args...)>
1267 typedef _Bind_result __self_type;
1268 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
1272 tuple<_Bound_args...> _M_bound_args;
1275 template<typename... _Args, int... _Indexes>
1277 __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>)
1279 return _M_f(_Mu<_Bound_args>()
1280 (tr1::get<_Indexes>(_M_bound_args), __args)...);
1284 template<typename... _Args, int... _Indexes>
1286 __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>) const
1288 return _M_f(_Mu<_Bound_args>()
1289 (tr1::get<_Indexes>(_M_bound_args), __args)...);
1293 template<typename... _Args, int... _Indexes>
1295 __call(const tuple<_Args...>& __args,
1296 _Index_tuple<_Indexes...>) volatile
1298 return _M_f(_Mu<_Bound_args>()
1299 (tr1::get<_Indexes>(_M_bound_args), __args)...);
1302 // Call as const volatile
1303 template<typename... _Args, int... _Indexes>
1305 __call(const tuple<_Args...>& __args,
1306 _Index_tuple<_Indexes...>) const volatile
1308 return _M_f(_Mu<_Bound_args>()
1309 (tr1::get<_Indexes>(_M_bound_args), __args)...);
1313 typedef _Result result_type;
1316 _Bind_result(_Functor __f, _Bound_args... __bound_args)
1317 : _M_f(__f), _M_bound_args(__bound_args...) { }
1320 template<typename... _Args>
1322 operator()(_Args&... __args)
1324 return this->__call(tr1::tie(__args...), _Bound_indexes());
1328 template<typename... _Args>
1330 operator()(_Args&... __args) const
1332 return this->__call(tr1::tie(__args...), _Bound_indexes());
1336 template<typename... _Args>
1338 operator()(_Args&... __args) volatile
1340 return this->__call(tr1::tie(__args...), _Bound_indexes());
1343 // Call as const volatile
1344 template<typename... _Args>
1346 operator()(_Args&... __args) const volatile
1348 return this->__call(tr1::tie(__args...), _Bound_indexes());
1352 /// Class template _Bind is always a bind expression.
1353 template<typename _Signature>
1354 struct is_bind_expression<_Bind<_Signature> >
1355 { static const bool value = true; };
1357 template<typename _Signature>
1358 const bool is_bind_expression<_Bind<_Signature> >::value;
1360 /// Class template _Bind is always a bind expression.
1361 template<typename _Signature>
1362 struct is_bind_expression<const _Bind<_Signature> >
1363 { static const bool value = true; };
1365 template<typename _Signature>
1366 const bool is_bind_expression<const _Bind<_Signature> >::value;
1368 /// Class template _Bind is always a bind expression.
1369 template<typename _Signature>
1370 struct is_bind_expression<volatile _Bind<_Signature> >
1371 { static const bool value = true; };
1373 template<typename _Signature>
1374 const bool is_bind_expression<volatile _Bind<_Signature> >::value;
1376 /// Class template _Bind is always a bind expression.
1377 template<typename _Signature>
1378 struct is_bind_expression<const volatile _Bind<_Signature> >
1379 { static const bool value = true; };
1381 template<typename _Signature>
1382 const bool is_bind_expression<const volatile _Bind<_Signature> >::value;
1384 /// Class template _Bind_result is always a bind expression.
1385 template<typename _Result, typename _Signature>
1386 struct is_bind_expression<_Bind_result<_Result, _Signature> >
1387 { static const bool value = true; };
1389 template<typename _Result, typename _Signature>
1390 const bool is_bind_expression<_Bind_result<_Result, _Signature> >::value;
1392 /// Class template _Bind_result is always a bind expression.
1393 template<typename _Result, typename _Signature>
1394 struct is_bind_expression<const _Bind_result<_Result, _Signature> >
1395 { static const bool value = true; };
1397 template<typename _Result, typename _Signature>
1399 is_bind_expression<const _Bind_result<_Result, _Signature> >::value;
1401 /// Class template _Bind_result is always a bind expression.
1402 template<typename _Result, typename _Signature>
1403 struct is_bind_expression<volatile _Bind_result<_Result, _Signature> >
1404 { static const bool value = true; };
1406 template<typename _Result, typename _Signature>
1408 is_bind_expression<volatile _Bind_result<_Result, _Signature> >::value;
1410 /// Class template _Bind_result is always a bind expression.
1411 template<typename _Result, typename _Signature>
1413 is_bind_expression<const volatile _Bind_result<_Result, _Signature> >
1414 { static const bool value = true; };
1416 template<typename _Result, typename _Signature>
1418 is_bind_expression<const volatile _Bind_result<_Result,
1419 _Signature> >::value;
1421 #if __cplusplus >= 201103L
1422 // Specialize tr1::is_bind_expression for std::bind closure types,
1423 // so that they can also work with tr1::bind.
1425 template<typename _Signature>
1426 struct is_bind_expression<std::_Bind<_Signature>>
1429 template<typename _Signature>
1430 struct is_bind_expression<const std::_Bind<_Signature>>
1433 template<typename _Signature>
1434 struct is_bind_expression<volatile std::_Bind<_Signature>>
1437 template<typename _Signature>
1438 struct is_bind_expression<const volatile std::_Bind<_Signature>>
1441 template<typename _Result, typename _Signature>
1442 struct is_bind_expression<std::_Bind_result<_Result, _Signature>>
1445 template<typename _Result, typename _Signature>
1446 struct is_bind_expression<const std::_Bind_result<_Result, _Signature>>
1449 template<typename _Result, typename _Signature>
1450 struct is_bind_expression<volatile std::_Bind_result<_Result, _Signature>>
1453 template<typename _Result, typename _Signature>
1454 struct is_bind_expression<const volatile std::_Bind_result<_Result,
1460 template<typename _Functor, typename... _ArgTypes>
1462 _Bind<typename _Maybe_wrap_member_pointer<_Functor>::type(_ArgTypes...)>
1463 bind(_Functor __f, _ArgTypes... __args)
1465 typedef _Maybe_wrap_member_pointer<_Functor> __maybe_type;
1466 typedef typename __maybe_type::type __functor_type;
1467 typedef _Bind<__functor_type(_ArgTypes...)> __result_type;
1468 return __result_type(__maybe_type::__do_wrap(__f), __args...);
1471 template<typename _Result, typename _Functor, typename... _ArgTypes>
1473 _Bind_result<_Result,
1474 typename _Maybe_wrap_member_pointer<_Functor>::type
1476 bind(_Functor __f, _ArgTypes... __args)
1478 typedef _Maybe_wrap_member_pointer<_Functor> __maybe_type;
1479 typedef typename __maybe_type::type __functor_type;
1480 typedef _Bind_result<_Result, __functor_type(_ArgTypes...)>
1482 return __result_type(__maybe_type::__do_wrap(__f), __args...);
1486 * @brief Exception class thrown when class template function's
1487 * operator() is called with an empty target.
1488 * @ingroup exceptions
1490 class bad_function_call : public std::exception { };
1493 * The integral constant expression 0 can be converted into a
1494 * pointer to this type. It is used by the function template to
1495 * accept NULL pointers.
1497 struct _M_clear_type;
1500 * Trait identifying @a location-invariant types, meaning that the
1501 * address of the object (or any of its members) will not escape.
1502 * Also implies a trivial copy constructor and assignment operator.
1504 template<typename _Tp>
1505 struct __is_location_invariant
1506 : integral_constant<bool,
1507 (is_pointer<_Tp>::value
1508 || is_member_pointer<_Tp>::value)>
1512 class _Undefined_class;
1517 const void* _M_const_object;
1518 void (*_M_function_pointer)();
1519 void (_Undefined_class::*_M_member_pointer)();
1524 void* _M_access() { return &_M_pod_data[0]; }
1525 const void* _M_access() const { return &_M_pod_data[0]; }
1527 template<typename _Tp>
1530 { return *static_cast<_Tp*>(_M_access()); }
1532 template<typename _Tp>
1535 { return *static_cast<const _Tp*>(_M_access()); }
1537 _Nocopy_types _M_unused;
1538 char _M_pod_data[sizeof(_Nocopy_types)];
1541 enum _Manager_operation
1549 // Simple type wrapper that helps avoid annoying const problems
1550 // when casting between void pointers and pointers-to-pointers.
1551 template<typename _Tp>
1552 struct _Simple_type_wrapper
1554 _Simple_type_wrapper(_Tp __value) : __value(__value) { }
1559 template<typename _Tp>
1560 struct __is_location_invariant<_Simple_type_wrapper<_Tp> >
1561 : __is_location_invariant<_Tp>
1565 // Converts a reference to a function object into a callable
1567 template<typename _Functor>
1569 __callable_functor(_Functor& __f)
1572 template<typename _Member, typename _Class>
1573 inline _Mem_fn<_Member _Class::*>
1574 __callable_functor(_Member _Class::* &__p)
1575 { return mem_fn(__p); }
1577 template<typename _Member, typename _Class>
1578 inline _Mem_fn<_Member _Class::*>
1579 __callable_functor(_Member _Class::* const &__p)
1580 { return mem_fn(__p); }
1582 template<typename _Signature>
1585 /// Base class of all polymorphic function object wrappers.
1586 class _Function_base
1589 static const std::size_t _M_max_size = sizeof(_Nocopy_types);
1590 static const std::size_t _M_max_align = __alignof__(_Nocopy_types);
1592 template<typename _Functor>
1596 static const bool __stored_locally =
1597 (__is_location_invariant<_Functor>::value
1598 && sizeof(_Functor) <= _M_max_size
1599 && __alignof__(_Functor) <= _M_max_align
1600 && (_M_max_align % __alignof__(_Functor) == 0));
1602 typedef integral_constant<bool, __stored_locally> _Local_storage;
1604 // Retrieve a pointer to the function object
1606 _M_get_pointer(const _Any_data& __source)
1608 const _Functor* __ptr =
1609 __stored_locally? std::__addressof(__source._M_access<_Functor>())
1610 /* have stored a pointer */ : __source._M_access<_Functor*>();
1611 return const_cast<_Functor*>(__ptr);
1614 // Clone a location-invariant function object that fits within
1615 // an _Any_data structure.
1617 _M_clone(_Any_data& __dest, const _Any_data& __source, true_type)
1619 new (__dest._M_access()) _Functor(__source._M_access<_Functor>());
1622 // Clone a function object that is not location-invariant or
1623 // that cannot fit into an _Any_data structure.
1625 _M_clone(_Any_data& __dest, const _Any_data& __source, false_type)
1627 __dest._M_access<_Functor*>() =
1628 new _Functor(*__source._M_access<_Functor*>());
1631 // Destroying a location-invariant object may still require
1634 _M_destroy(_Any_data& __victim, true_type)
1636 __victim._M_access<_Functor>().~_Functor();
1639 // Destroying an object located on the heap.
1641 _M_destroy(_Any_data& __victim, false_type)
1643 delete __victim._M_access<_Functor*>();
1648 _M_manager(_Any_data& __dest, const _Any_data& __source,
1649 _Manager_operation __op)
1654 case __get_type_info:
1655 __dest._M_access<const type_info*>() = &typeid(_Functor);
1658 case __get_functor_ptr:
1659 __dest._M_access<_Functor*>() = _M_get_pointer(__source);
1662 case __clone_functor:
1663 _M_clone(__dest, __source, _Local_storage());
1666 case __destroy_functor:
1667 _M_destroy(__dest, _Local_storage());
1674 _M_init_functor(_Any_data& __functor, const _Functor& __f)
1675 { _M_init_functor(__functor, __f, _Local_storage()); }
1677 template<typename _Signature>
1679 _M_not_empty_function(const function<_Signature>& __f)
1680 { return static_cast<bool>(__f); }
1682 template<typename _Tp>
1684 _M_not_empty_function(const _Tp*& __fp)
1687 template<typename _Class, typename _Tp>
1689 _M_not_empty_function(_Tp _Class::* const& __mp)
1692 template<typename _Tp>
1694 _M_not_empty_function(const _Tp&)
1699 _M_init_functor(_Any_data& __functor, const _Functor& __f, true_type)
1700 { new (__functor._M_access()) _Functor(__f); }
1703 _M_init_functor(_Any_data& __functor, const _Functor& __f, false_type)
1704 { __functor._M_access<_Functor*>() = new _Functor(__f); }
1707 template<typename _Functor>
1708 class _Ref_manager : public _Base_manager<_Functor*>
1710 typedef _Function_base::_Base_manager<_Functor*> _Base;
1714 _M_manager(_Any_data& __dest, const _Any_data& __source,
1715 _Manager_operation __op)
1720 case __get_type_info:
1721 __dest._M_access<const type_info*>() = &typeid(_Functor);
1724 case __get_functor_ptr:
1725 __dest._M_access<_Functor*>() = *_Base::_M_get_pointer(__source);
1726 return is_const<_Functor>::value;
1730 _Base::_M_manager(__dest, __source, __op);
1736 _M_init_functor(_Any_data& __functor, reference_wrapper<_Functor> __f)
1738 _Base::_M_init_functor(__functor, std::__addressof(__f.get()));
1742 _Function_base() : _M_manager(0) { }
1747 _M_manager(_M_functor, _M_functor, __destroy_functor);
1751 bool _M_empty() const { return !_M_manager; }
1753 typedef bool (*_Manager_type)(_Any_data&, const _Any_data&,
1754 _Manager_operation);
1756 _Any_data _M_functor;
1757 _Manager_type _M_manager;
1760 template<typename _Signature, typename _Functor>
1761 class _Function_handler;
1763 template<typename _Res, typename _Functor, typename... _ArgTypes>
1764 class _Function_handler<_Res(_ArgTypes...), _Functor>
1765 : public _Function_base::_Base_manager<_Functor>
1767 typedef _Function_base::_Base_manager<_Functor> _Base;
1771 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1773 return (*_Base::_M_get_pointer(__functor))(__args...);
1777 template<typename _Functor, typename... _ArgTypes>
1778 class _Function_handler<void(_ArgTypes...), _Functor>
1779 : public _Function_base::_Base_manager<_Functor>
1781 typedef _Function_base::_Base_manager<_Functor> _Base;
1785 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1787 (*_Base::_M_get_pointer(__functor))(__args...);
1791 template<typename _Res, typename _Functor, typename... _ArgTypes>
1792 class _Function_handler<_Res(_ArgTypes...), reference_wrapper<_Functor> >
1793 : public _Function_base::_Ref_manager<_Functor>
1795 typedef _Function_base::_Ref_manager<_Functor> _Base;
1799 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1802 __callable_functor(**_Base::_M_get_pointer(__functor))(__args...);
1806 template<typename _Functor, typename... _ArgTypes>
1807 class _Function_handler<void(_ArgTypes...), reference_wrapper<_Functor> >
1808 : public _Function_base::_Ref_manager<_Functor>
1810 typedef _Function_base::_Ref_manager<_Functor> _Base;
1814 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1816 __callable_functor(**_Base::_M_get_pointer(__functor))(__args...);
1820 template<typename _Class, typename _Member, typename _Res,
1821 typename... _ArgTypes>
1822 class _Function_handler<_Res(_ArgTypes...), _Member _Class::*>
1823 : public _Function_handler<void(_ArgTypes...), _Member _Class::*>
1825 typedef _Function_handler<void(_ArgTypes...), _Member _Class::*>
1830 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1833 mem_fn(_Base::_M_get_pointer(__functor)->__value)(__args...);
1837 template<typename _Class, typename _Member, typename... _ArgTypes>
1838 class _Function_handler<void(_ArgTypes...), _Member _Class::*>
1839 : public _Function_base::_Base_manager<
1840 _Simple_type_wrapper< _Member _Class::* > >
1842 typedef _Member _Class::* _Functor;
1843 typedef _Simple_type_wrapper<_Functor> _Wrapper;
1844 typedef _Function_base::_Base_manager<_Wrapper> _Base;
1848 _M_manager(_Any_data& __dest, const _Any_data& __source,
1849 _Manager_operation __op)
1854 case __get_type_info:
1855 __dest._M_access<const type_info*>() = &typeid(_Functor);
1858 case __get_functor_ptr:
1859 __dest._M_access<_Functor*>() =
1860 &_Base::_M_get_pointer(__source)->__value;
1864 _Base::_M_manager(__dest, __source, __op);
1870 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1872 tr1::mem_fn(_Base::_M_get_pointer(__functor)->__value)(__args...);
1877 template<typename _Res, typename... _ArgTypes>
1878 class function<_Res(_ArgTypes...)>
1879 : public _Maybe_unary_or_binary_function<_Res, _ArgTypes...>,
1880 private _Function_base
1882 #if __cplusplus < 201103L
1883 /// This class is used to implement the safe_bool idiom.
1886 _Hidden_type* _M_bool;
1889 /// This typedef is used to implement the safe_bool idiom.
1890 typedef _Hidden_type* _Hidden_type::* _Safe_bool;
1893 typedef _Res _Signature_type(_ArgTypes...);
1895 struct _Useless { };
1898 typedef _Res result_type;
1900 // [3.7.2.1] construct/copy/destroy
1903 * @brief Default construct creates an empty function call wrapper.
1904 * @post @c !(bool)*this
1906 function() : _Function_base() { }
1909 * @brief Default construct creates an empty function call wrapper.
1910 * @post @c !(bool)*this
1912 function(_M_clear_type*) : _Function_base() { }
1915 * @brief %Function copy constructor.
1916 * @param x A %function object with identical call signature.
1917 * @post @c (bool)*this == (bool)x
1919 * The newly-created %function contains a copy of the target of @a
1920 * x (if it has one).
1922 function(const function& __x);
1925 * @brief Builds a %function that targets a copy of the incoming
1927 * @param f A %function object that is callable with parameters of
1928 * type @c T1, @c T2, ..., @c TN and returns a value convertible
1931 * The newly-created %function object will target a copy of @a
1932 * f. If @a f is @c reference_wrapper<F>, then this function
1933 * object will contain a reference to the function object @c
1934 * f.get(). If @a f is a NULL function pointer or NULL
1935 * pointer-to-member, the newly-created object will be empty.
1937 * If @a f is a non-NULL function pointer or an object of type @c
1938 * reference_wrapper<F>, this function will not throw.
1940 template<typename _Functor>
1941 function(_Functor __f,
1942 typename __gnu_cxx::__enable_if<
1943 !is_integral<_Functor>::value, _Useless>::__type
1947 * @brief %Function assignment operator.
1948 * @param x A %function with identical call signature.
1949 * @post @c (bool)*this == (bool)x
1952 * The target of @a x is copied to @c *this. If @a x has no
1953 * target, then @c *this will be empty.
1955 * If @a x targets a function pointer or a reference to a function
1956 * object, then this operation will not throw an %exception.
1959 operator=(const function& __x)
1961 function(__x).swap(*this);
1966 * @brief %Function assignment to zero.
1967 * @post @c !(bool)*this
1970 * The target of @c *this is deallocated, leaving it empty.
1973 operator=(_M_clear_type*)
1977 _M_manager(_M_functor, _M_functor, __destroy_functor);
1985 * @brief %Function assignment to a new target.
1986 * @param f A %function object that is callable with parameters of
1987 * type @c T1, @c T2, ..., @c TN and returns a value convertible
1991 * This %function object wrapper will target a copy of @a
1992 * f. If @a f is @c reference_wrapper<F>, then this function
1993 * object will contain a reference to the function object @c
1994 * f.get(). If @a f is a NULL function pointer or NULL
1995 * pointer-to-member, @c this object will be empty.
1997 * If @a f is a non-NULL function pointer or an object of type @c
1998 * reference_wrapper<F>, this function will not throw.
2000 template<typename _Functor>
2001 typename __gnu_cxx::__enable_if<!is_integral<_Functor>::value,
2003 operator=(_Functor __f)
2005 function(__f).swap(*this);
2009 // [3.7.2.2] function modifiers
2012 * @brief Swap the targets of two %function objects.
2013 * @param f A %function with identical call signature.
2015 * Swap the targets of @c this function object and @a f. This
2016 * function will not throw an %exception.
2018 void swap(function& __x)
2020 std::swap(_M_functor, __x._M_functor);
2021 std::swap(_M_manager, __x._M_manager);
2022 std::swap(_M_invoker, __x._M_invoker);
2025 // [3.7.2.3] function capacity
2028 * @brief Determine if the %function wrapper has a target.
2030 * @return @c true when this %function object contains a target,
2031 * or @c false when it is empty.
2033 * This function will not throw an %exception.
2035 #if __cplusplus >= 201103L
2036 explicit operator bool() const
2037 { return !_M_empty(); }
2039 operator _Safe_bool() const
2044 return &_Hidden_type::_M_bool;
2048 // [3.7.2.4] function invocation
2051 * @brief Invokes the function targeted by @c *this.
2052 * @returns the result of the target.
2053 * @throws bad_function_call when @c !(bool)*this
2055 * The function call operator invokes the target function object
2056 * stored by @c this.
2058 _Res operator()(_ArgTypes... __args) const;
2061 // [3.7.2.5] function target access
2063 * @brief Determine the type of the target of this function object
2066 * @returns the type identifier of the target function object, or
2067 * @c typeid(void) if @c !(bool)*this.
2069 * This function will not throw an %exception.
2071 const type_info& target_type() const;
2074 * @brief Access the stored target function object.
2076 * @return Returns a pointer to the stored target function object,
2077 * if @c typeid(Functor).equals(target_type()); otherwise, a NULL
2080 * This function will not throw an %exception.
2082 template<typename _Functor> _Functor* target();
2085 template<typename _Functor> const _Functor* target() const;
2089 // [3.7.2.6] undefined operators
2090 template<typename _Function>
2091 void operator==(const function<_Function>&) const;
2092 template<typename _Function>
2093 void operator!=(const function<_Function>&) const;
2095 typedef _Res (*_Invoker_type)(const _Any_data&, _ArgTypes...);
2096 _Invoker_type _M_invoker;
2098 #pragma GCC diagnostic pop
2100 template<typename _Res, typename... _ArgTypes>
2101 function<_Res(_ArgTypes...)>::
2102 function(const function& __x)
2105 if (static_cast<bool>(__x))
2107 __x._M_manager(_M_functor, __x._M_functor, __clone_functor);
2108 _M_invoker = __x._M_invoker;
2109 _M_manager = __x._M_manager;
2113 template<typename _Res, typename... _ArgTypes>
2114 template<typename _Functor>
2115 function<_Res(_ArgTypes...)>::
2116 function(_Functor __f,
2117 typename __gnu_cxx::__enable_if<
2118 !is_integral<_Functor>::value, _Useless>::__type)
2121 typedef _Function_handler<_Signature_type, _Functor> _My_handler;
2123 if (_My_handler::_M_not_empty_function(__f))
2125 _My_handler::_M_init_functor(_M_functor, __f);
2126 _M_invoker = &_My_handler::_M_invoke;
2127 _M_manager = &_My_handler::_M_manager;
2131 template<typename _Res, typename... _ArgTypes>
2133 function<_Res(_ArgTypes...)>::
2134 operator()(_ArgTypes... __args) const
2137 _GLIBCXX_THROW_OR_ABORT(bad_function_call());
2138 return _M_invoker(_M_functor, __args...);
2142 template<typename _Res, typename... _ArgTypes>
2144 function<_Res(_ArgTypes...)>::
2149 _Any_data __typeinfo_result;
2150 _M_manager(__typeinfo_result, _M_functor, __get_type_info);
2151 return *__typeinfo_result._M_access<const type_info*>();
2154 return typeid(void);
2157 template<typename _Res, typename... _ArgTypes>
2158 template<typename _Functor>
2160 function<_Res(_ArgTypes...)>::
2163 if (typeid(_Functor) == target_type() && _M_manager)
2166 if (_M_manager(__ptr, _M_functor, __get_functor_ptr)
2167 && !is_const<_Functor>::value)
2170 return __ptr._M_access<_Functor*>();
2176 template<typename _Res, typename... _ArgTypes>
2177 template<typename _Functor>
2179 function<_Res(_ArgTypes...)>::
2182 if (typeid(_Functor) == target_type() && _M_manager)
2185 _M_manager(__ptr, _M_functor, __get_functor_ptr);
2186 return __ptr._M_access<const _Functor*>();
2193 // [3.7.2.7] null pointer comparisons
2196 * @brief Compares a polymorphic function object wrapper against 0
2197 * (the NULL pointer).
2198 * @returns @c true if the wrapper has no target, @c false otherwise
2200 * This function will not throw an %exception.
2202 template<typename _Signature>
2204 operator==(const function<_Signature>& __f, _M_clear_type*)
2205 { return !static_cast<bool>(__f); }
2208 template<typename _Signature>
2210 operator==(_M_clear_type*, const function<_Signature>& __f)
2211 { return !static_cast<bool>(__f); }
2214 * @brief Compares a polymorphic function object wrapper against 0
2215 * (the NULL pointer).
2216 * @returns @c false if the wrapper has no target, @c true otherwise
2218 * This function will not throw an %exception.
2220 template<typename _Signature>
2222 operator!=(const function<_Signature>& __f, _M_clear_type*)
2223 { return static_cast<bool>(__f); }
2226 template<typename _Signature>
2228 operator!=(_M_clear_type*, const function<_Signature>& __f)
2229 { return static_cast<bool>(__f); }
2231 // [3.7.2.8] specialized algorithms
2234 * @brief Swap the targets of two polymorphic function object wrappers.
2236 * This function will not throw an %exception.
2238 template<typename _Signature>
2240 swap(function<_Signature>& __x, function<_Signature>& __y)
2244 #if __cplusplus >= 201103L
2245 // Specialize std::is_bind_expression for tr1::bind closure types,
2246 // so that they can also work with std::bind.
2248 template<typename _Signature>
2249 struct is_bind_expression<tr1::_Bind<_Signature>>
2252 template<typename _Signature>
2253 struct is_bind_expression<const tr1::_Bind<_Signature>>
2256 template<typename _Signature>
2257 struct is_bind_expression<volatile tr1::_Bind<_Signature>>
2260 template<typename _Signature>
2261 struct is_bind_expression<const volatile tr1::_Bind<_Signature>>
2264 template<typename _Result, typename _Signature>
2265 struct is_bind_expression<tr1::_Bind_result<_Result, _Signature>>
2268 template<typename _Result, typename _Signature>
2269 struct is_bind_expression<const tr1::_Bind_result<_Result, _Signature>>
2272 template<typename _Result, typename _Signature>
2273 struct is_bind_expression<volatile tr1::_Bind_result<_Result, _Signature>>
2276 template<typename _Result, typename _Signature>
2277 struct is_bind_expression<const volatile tr1::_Bind_result<_Result,
2282 _GLIBCXX_END_NAMESPACE_VERSION
2285 #endif // _GLIBCXX_TR1_FUNCTIONAL