1 // TR1 functional header -*- C++ -*-
3 // Copyright (C) 2004-2018 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/c++config.h>
35 #include <bits/stl_function.h>
40 #include <tr1/type_traits>
41 #include <bits/stringfwd.h>
42 #include <tr1/functional_hash.h>
43 #include <ext/type_traits.h>
44 #include <bits/move.h> // for std::__addressof
45 #if __cplusplus >= 201103L
46 # include <type_traits> // for integral_constant, true_type, false_type
49 namespace std _GLIBCXX_VISIBILITY(default)
51 _GLIBCXX_BEGIN_NAMESPACE_VERSION
52 #if __cplusplus >= 201103L
53 template<int> struct _Placeholder;
54 template<typename> class _Bind;
55 template<typename, typename> class _Bind_result;
60 template<typename _MemberPointer>
62 template<typename _Tp, typename _Class>
63 _Mem_fn<_Tp _Class::*>
64 mem_fn(_Tp _Class::*);
67 * Actual implementation of _Has_result_type, which uses SFINAE to
68 * determine if the type _Tp has a publicly-accessible member type
71 template<typename _Tp>
72 class _Has_result_type_helper : __sfinae_types
74 template<typename _Up>
78 template<typename _Up>
79 static __one __test(_Wrap_type<typename _Up::result_type>*);
81 template<typename _Up>
82 static __two __test(...);
85 static const bool value = sizeof(__test<_Tp>(0)) == 1;
88 template<typename _Tp>
89 struct _Has_result_type
90 : integral_constant<bool,
91 _Has_result_type_helper<typename remove_cv<_Tp>::type>::value>
97 /// If we have found a result_type, extract it.
98 template<bool _Has_result_type, typename _Functor>
99 struct _Maybe_get_result_type
102 template<typename _Functor>
103 struct _Maybe_get_result_type<true, _Functor>
105 typedef typename _Functor::result_type result_type;
109 * Base class for any function object that has a weak result type, as
110 * defined in 3.3/3 of TR1.
112 template<typename _Functor>
113 struct _Weak_result_type_impl
114 : _Maybe_get_result_type<_Has_result_type<_Functor>::value, _Functor>
118 /// Retrieve the result type for a function type.
119 template<typename _Res, typename... _ArgTypes>
120 struct _Weak_result_type_impl<_Res(_ArgTypes...)>
122 typedef _Res result_type;
125 /// Retrieve the result type for a function reference.
126 template<typename _Res, typename... _ArgTypes>
127 struct _Weak_result_type_impl<_Res(&)(_ArgTypes...)>
129 typedef _Res result_type;
132 /// Retrieve the result type for a function pointer.
133 template<typename _Res, typename... _ArgTypes>
134 struct _Weak_result_type_impl<_Res(*)(_ArgTypes...)>
136 typedef _Res result_type;
139 /// Retrieve result type for a member function pointer.
140 template<typename _Res, typename _Class, typename... _ArgTypes>
141 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...)>
143 typedef _Res result_type;
146 /// Retrieve result type for a const member function pointer.
147 template<typename _Res, typename _Class, typename... _ArgTypes>
148 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) const>
150 typedef _Res result_type;
153 /// Retrieve result type for a volatile member function pointer.
154 template<typename _Res, typename _Class, typename... _ArgTypes>
155 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) volatile>
157 typedef _Res result_type;
160 /// Retrieve result type for a const volatile member function pointer.
161 template<typename _Res, typename _Class, typename... _ArgTypes>
162 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...)const volatile>
164 typedef _Res result_type;
168 * Strip top-level cv-qualifiers from the function object and let
169 * _Weak_result_type_impl perform the real work.
171 template<typename _Functor>
172 struct _Weak_result_type
173 : _Weak_result_type_impl<typename remove_cv<_Functor>::type>
177 template<typename _Signature>
181 * Actual implementation of result_of. When _Has_result_type is
182 * true, gets its result from _Weak_result_type. Otherwise, uses
183 * the function object's member template result to extract the
186 template<bool _Has_result_type, typename _Signature>
187 struct _Result_of_impl;
189 // Handle member data pointers using _Mem_fn's logic
190 template<typename _Res, typename _Class, typename _T1>
191 struct _Result_of_impl<false, _Res _Class::*(_T1)>
193 typedef typename _Mem_fn<_Res _Class::*>
194 ::template _Result_type<_T1>::type type;
198 * Determine whether we can determine a result type from @c Functor
201 template<typename _Functor, typename... _ArgTypes>
202 class result_of<_Functor(_ArgTypes...)>
203 : public _Result_of_impl<
204 _Has_result_type<_Weak_result_type<_Functor> >::value,
205 _Functor(_ArgTypes...)>
209 /// We already know the result type for @c Functor; use it.
210 template<typename _Functor, typename... _ArgTypes>
211 struct _Result_of_impl<true, _Functor(_ArgTypes...)>
213 typedef typename _Weak_result_type<_Functor>::result_type type;
217 * We need to compute the result type for this invocation the hard
220 template<typename _Functor, typename... _ArgTypes>
221 struct _Result_of_impl<false, _Functor(_ArgTypes...)>
223 typedef typename _Functor
224 ::template result<_Functor(_ArgTypes...)>::type type;
228 * It is unsafe to access ::result when there are zero arguments, so we
229 * return @c void instead.
231 template<typename _Functor>
232 struct _Result_of_impl<false, _Functor()>
237 /// Determines if the type _Tp derives from unary_function.
238 template<typename _Tp>
239 struct _Derives_from_unary_function : __sfinae_types
242 template<typename _T1, typename _Res>
243 static __one __test(const volatile unary_function<_T1, _Res>*);
245 // It's tempting to change "..." to const volatile void*, but
246 // that fails when _Tp is a function type.
247 static __two __test(...);
250 static const bool value = sizeof(__test((_Tp*)0)) == 1;
253 /// Determines if the type _Tp derives from binary_function.
254 template<typename _Tp>
255 struct _Derives_from_binary_function : __sfinae_types
258 template<typename _T1, typename _T2, typename _Res>
259 static __one __test(const volatile binary_function<_T1, _T2, _Res>*);
261 // It's tempting to change "..." to const volatile void*, but
262 // that fails when _Tp is a function type.
263 static __two __test(...);
266 static const bool value = sizeof(__test((_Tp*)0)) == 1;
269 /// Turns a function type into a function pointer type
270 template<typename _Tp, bool _IsFunctionType = is_function<_Tp>::value>
271 struct _Function_to_function_pointer
276 template<typename _Tp>
277 struct _Function_to_function_pointer<_Tp, true>
283 * Invoke a function object, which may be either a member pointer or a
284 * function object. The first parameter will tell which.
286 template<typename _Functor, typename... _Args>
288 typename __gnu_cxx::__enable_if<
289 (!is_member_pointer<_Functor>::value
290 && !is_function<_Functor>::value
291 && !is_function<typename remove_pointer<_Functor>::type>::value),
292 typename result_of<_Functor(_Args...)>::type
294 __invoke(_Functor& __f, _Args&... __args)
296 return __f(__args...);
299 template<typename _Functor, typename... _Args>
301 typename __gnu_cxx::__enable_if<
302 (is_member_pointer<_Functor>::value
303 && !is_function<_Functor>::value
304 && !is_function<typename remove_pointer<_Functor>::type>::value),
305 typename result_of<_Functor(_Args...)>::type
307 __invoke(_Functor& __f, _Args&... __args)
309 return mem_fn(__f)(__args...);
312 // To pick up function references (that will become function pointers)
313 template<typename _Functor, typename... _Args>
315 typename __gnu_cxx::__enable_if<
316 (is_pointer<_Functor>::value
317 && is_function<typename remove_pointer<_Functor>::type>::value),
318 typename result_of<_Functor(_Args...)>::type
320 __invoke(_Functor __f, _Args&... __args)
322 return __f(__args...);
326 * Knowing which of unary_function and binary_function _Tp derives
327 * from, derives from the same and ensures that reference_wrapper
328 * will have a weak result type. See cases below.
330 template<bool _Unary, bool _Binary, typename _Tp>
331 struct _Reference_wrapper_base_impl;
333 // Not a unary_function or binary_function, so try a weak result type.
334 template<typename _Tp>
335 struct _Reference_wrapper_base_impl<false, false, _Tp>
336 : _Weak_result_type<_Tp>
339 // unary_function but not binary_function
340 template<typename _Tp>
341 struct _Reference_wrapper_base_impl<true, false, _Tp>
342 : unary_function<typename _Tp::argument_type,
343 typename _Tp::result_type>
346 // binary_function but not unary_function
347 template<typename _Tp>
348 struct _Reference_wrapper_base_impl<false, true, _Tp>
349 : binary_function<typename _Tp::first_argument_type,
350 typename _Tp::second_argument_type,
351 typename _Tp::result_type>
354 // Both unary_function and binary_function. Import result_type to
356 template<typename _Tp>
357 struct _Reference_wrapper_base_impl<true, true, _Tp>
358 : unary_function<typename _Tp::argument_type,
359 typename _Tp::result_type>,
360 binary_function<typename _Tp::first_argument_type,
361 typename _Tp::second_argument_type,
362 typename _Tp::result_type>
364 typedef typename _Tp::result_type result_type;
368 * Derives from unary_function or binary_function when it
369 * can. Specializations handle all of the easy cases. The primary
370 * template determines what to do with a class type, which may
371 * derive from both unary_function and binary_function.
373 template<typename _Tp>
374 struct _Reference_wrapper_base
375 : _Reference_wrapper_base_impl<
376 _Derives_from_unary_function<_Tp>::value,
377 _Derives_from_binary_function<_Tp>::value,
381 // - a function type (unary)
382 template<typename _Res, typename _T1>
383 struct _Reference_wrapper_base<_Res(_T1)>
384 : unary_function<_T1, _Res>
387 // - a function type (binary)
388 template<typename _Res, typename _T1, typename _T2>
389 struct _Reference_wrapper_base<_Res(_T1, _T2)>
390 : binary_function<_T1, _T2, _Res>
393 // - a function pointer type (unary)
394 template<typename _Res, typename _T1>
395 struct _Reference_wrapper_base<_Res(*)(_T1)>
396 : unary_function<_T1, _Res>
399 // - a function pointer type (binary)
400 template<typename _Res, typename _T1, typename _T2>
401 struct _Reference_wrapper_base<_Res(*)(_T1, _T2)>
402 : binary_function<_T1, _T2, _Res>
405 // - a pointer to member function type (unary, no qualifiers)
406 template<typename _Res, typename _T1>
407 struct _Reference_wrapper_base<_Res (_T1::*)()>
408 : unary_function<_T1*, _Res>
411 // - a pointer to member function type (binary, no qualifiers)
412 template<typename _Res, typename _T1, typename _T2>
413 struct _Reference_wrapper_base<_Res (_T1::*)(_T2)>
414 : binary_function<_T1*, _T2, _Res>
417 // - a pointer to member function type (unary, const)
418 template<typename _Res, typename _T1>
419 struct _Reference_wrapper_base<_Res (_T1::*)() const>
420 : unary_function<const _T1*, _Res>
423 // - a pointer to member function type (binary, const)
424 template<typename _Res, typename _T1, typename _T2>
425 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const>
426 : binary_function<const _T1*, _T2, _Res>
429 // - a pointer to member function type (unary, volatile)
430 template<typename _Res, typename _T1>
431 struct _Reference_wrapper_base<_Res (_T1::*)() volatile>
432 : unary_function<volatile _T1*, _Res>
435 // - a pointer to member function type (binary, volatile)
436 template<typename _Res, typename _T1, typename _T2>
437 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) volatile>
438 : binary_function<volatile _T1*, _T2, _Res>
441 // - a pointer to member function type (unary, const volatile)
442 template<typename _Res, typename _T1>
443 struct _Reference_wrapper_base<_Res (_T1::*)() const volatile>
444 : unary_function<const volatile _T1*, _Res>
447 // - a pointer to member function type (binary, const volatile)
448 template<typename _Res, typename _T1, typename _T2>
449 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const volatile>
450 : binary_function<const volatile _T1*, _T2, _Res>
453 /// reference_wrapper
454 template<typename _Tp>
455 class reference_wrapper
456 : public _Reference_wrapper_base<typename remove_cv<_Tp>::type>
458 // If _Tp is a function type, we can't form result_of<_Tp(...)>,
459 // so turn it into a function pointer type.
460 typedef typename _Function_to_function_pointer<_Tp>::type
468 reference_wrapper(_Tp& __indata)
469 : _M_data(std::__addressof(__indata))
472 reference_wrapper(const reference_wrapper<_Tp>& __inref):
473 _M_data(__inref._M_data)
477 operator=(const reference_wrapper<_Tp>& __inref)
479 _M_data = __inref._M_data;
483 operator _Tp&() const
484 { return this->get(); }
490 template<typename... _Args>
491 typename result_of<_M_func_type(_Args...)>::type
492 operator()(_Args&... __args) const
494 return __invoke(get(), __args...);
499 // Denotes a reference should be taken to a variable.
500 template<typename _Tp>
501 inline reference_wrapper<_Tp>
503 { return reference_wrapper<_Tp>(__t); }
505 // Denotes a const reference should be taken to a variable.
506 template<typename _Tp>
507 inline reference_wrapper<const _Tp>
509 { return reference_wrapper<const _Tp>(__t); }
511 template<typename _Tp>
512 inline reference_wrapper<_Tp>
513 ref(reference_wrapper<_Tp> __t)
514 { return ref(__t.get()); }
516 template<typename _Tp>
517 inline reference_wrapper<const _Tp>
518 cref(reference_wrapper<_Tp> __t)
519 { return cref(__t.get()); }
521 template<typename _Tp, bool>
522 struct _Mem_fn_const_or_non
524 typedef const _Tp& type;
527 template<typename _Tp>
528 struct _Mem_fn_const_or_non<_Tp, false>
534 * Derives from @c unary_function or @c binary_function, or perhaps
535 * nothing, depending on the number of arguments provided. The
536 * primary template is the basis case, which derives nothing.
538 template<typename _Res, typename... _ArgTypes>
539 struct _Maybe_unary_or_binary_function { };
541 /// Derives from @c unary_function, as appropriate.
542 template<typename _Res, typename _T1>
543 struct _Maybe_unary_or_binary_function<_Res, _T1>
544 : std::unary_function<_T1, _Res> { };
546 /// Derives from @c binary_function, as appropriate.
547 template<typename _Res, typename _T1, typename _T2>
548 struct _Maybe_unary_or_binary_function<_Res, _T1, _T2>
549 : std::binary_function<_T1, _T2, _Res> { };
551 /// Implementation of @c mem_fn for member function pointers.
552 template<typename _Res, typename _Class, typename... _ArgTypes>
553 class _Mem_fn<_Res (_Class::*)(_ArgTypes...)>
554 : public _Maybe_unary_or_binary_function<_Res, _Class*, _ArgTypes...>
556 typedef _Res (_Class::*_Functor)(_ArgTypes...);
558 template<typename _Tp>
560 _M_call(_Tp& __object, const volatile _Class *,
561 _ArgTypes... __args) const
562 { return (__object.*__pmf)(__args...); }
564 template<typename _Tp>
566 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
567 { return ((*__ptr).*__pmf)(__args...); }
570 typedef _Res result_type;
572 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
576 operator()(_Class& __object, _ArgTypes... __args) const
577 { return (__object.*__pmf)(__args...); }
581 operator()(_Class* __object, _ArgTypes... __args) const
582 { return (__object->*__pmf)(__args...); }
584 // Handle smart pointers, references and pointers to derived
585 template<typename _Tp>
587 operator()(_Tp& __object, _ArgTypes... __args) const
588 { return _M_call(__object, &__object, __args...); }
594 /// Implementation of @c mem_fn for const member function pointers.
595 template<typename _Res, typename _Class, typename... _ArgTypes>
596 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) const>
597 : public _Maybe_unary_or_binary_function<_Res, const _Class*,
600 typedef _Res (_Class::*_Functor)(_ArgTypes...) const;
602 template<typename _Tp>
604 _M_call(_Tp& __object, const volatile _Class *,
605 _ArgTypes... __args) const
606 { return (__object.*__pmf)(__args...); }
608 template<typename _Tp>
610 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
611 { return ((*__ptr).*__pmf)(__args...); }
614 typedef _Res result_type;
616 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
620 operator()(const _Class& __object, _ArgTypes... __args) const
621 { return (__object.*__pmf)(__args...); }
625 operator()(const _Class* __object, _ArgTypes... __args) const
626 { return (__object->*__pmf)(__args...); }
628 // Handle smart pointers, references and pointers to derived
629 template<typename _Tp>
630 _Res operator()(_Tp& __object, _ArgTypes... __args) const
631 { return _M_call(__object, &__object, __args...); }
637 /// Implementation of @c mem_fn for volatile member function pointers.
638 template<typename _Res, typename _Class, typename... _ArgTypes>
639 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) volatile>
640 : public _Maybe_unary_or_binary_function<_Res, volatile _Class*,
643 typedef _Res (_Class::*_Functor)(_ArgTypes...) volatile;
645 template<typename _Tp>
647 _M_call(_Tp& __object, const volatile _Class *,
648 _ArgTypes... __args) const
649 { return (__object.*__pmf)(__args...); }
651 template<typename _Tp>
653 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
654 { return ((*__ptr).*__pmf)(__args...); }
657 typedef _Res result_type;
659 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
663 operator()(volatile _Class& __object, _ArgTypes... __args) const
664 { return (__object.*__pmf)(__args...); }
668 operator()(volatile _Class* __object, _ArgTypes... __args) const
669 { return (__object->*__pmf)(__args...); }
671 // Handle smart pointers, references and pointers to derived
672 template<typename _Tp>
674 operator()(_Tp& __object, _ArgTypes... __args) const
675 { return _M_call(__object, &__object, __args...); }
681 /// Implementation of @c mem_fn for const volatile member function pointers.
682 template<typename _Res, typename _Class, typename... _ArgTypes>
683 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) const volatile>
684 : public _Maybe_unary_or_binary_function<_Res, const volatile _Class*,
687 typedef _Res (_Class::*_Functor)(_ArgTypes...) const volatile;
689 template<typename _Tp>
691 _M_call(_Tp& __object, const volatile _Class *,
692 _ArgTypes... __args) const
693 { return (__object.*__pmf)(__args...); }
695 template<typename _Tp>
697 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
698 { return ((*__ptr).*__pmf)(__args...); }
701 typedef _Res result_type;
703 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
707 operator()(const volatile _Class& __object, _ArgTypes... __args) const
708 { return (__object.*__pmf)(__args...); }
712 operator()(const volatile _Class* __object, _ArgTypes... __args) const
713 { return (__object->*__pmf)(__args...); }
715 // Handle smart pointers, references and pointers to derived
716 template<typename _Tp>
717 _Res operator()(_Tp& __object, _ArgTypes... __args) const
718 { return _M_call(__object, &__object, __args...); }
725 template<typename _Res, typename _Class>
726 class _Mem_fn<_Res _Class::*>
728 // This bit of genius is due to Peter Dimov, improved slightly by
730 template<typename _Tp>
732 _M_call(_Tp& __object, _Class *) const
733 { return __object.*__pm; }
735 template<typename _Tp, typename _Up>
737 _M_call(_Tp& __object, _Up * const *) const
738 { return (*__object).*__pm; }
740 template<typename _Tp, typename _Up>
742 _M_call(_Tp& __object, const _Up * const *) const
743 { return (*__object).*__pm; }
745 template<typename _Tp>
747 _M_call(_Tp& __object, const _Class *) const
748 { return __object.*__pm; }
750 template<typename _Tp>
752 _M_call(_Tp& __ptr, const volatile void*) const
753 { return (*__ptr).*__pm; }
755 template<typename _Tp> static _Tp& __get_ref();
757 template<typename _Tp>
758 static __sfinae_types::__one __check_const(_Tp&, _Class*);
759 template<typename _Tp, typename _Up>
760 static __sfinae_types::__one __check_const(_Tp&, _Up * const *);
761 template<typename _Tp, typename _Up>
762 static __sfinae_types::__two __check_const(_Tp&, const _Up * const *);
763 template<typename _Tp>
764 static __sfinae_types::__two __check_const(_Tp&, const _Class*);
765 template<typename _Tp>
766 static __sfinae_types::__two __check_const(_Tp&, const volatile void*);
769 template<typename _Tp>
771 : _Mem_fn_const_or_non<_Res,
772 (sizeof(__sfinae_types::__two)
773 == sizeof(__check_const<_Tp>(__get_ref<_Tp>(), (_Tp*)0)))>
776 template<typename _Signature>
779 template<typename _CVMem, typename _Tp>
780 struct result<_CVMem(_Tp)>
781 : public _Result_type<_Tp> { };
783 template<typename _CVMem, typename _Tp>
784 struct result<_CVMem(_Tp&)>
785 : public _Result_type<_Tp> { };
788 _Mem_fn(_Res _Class::*__pm) : __pm(__pm) { }
792 operator()(_Class& __object) const
793 { return __object.*__pm; }
796 operator()(const _Class& __object) const
797 { return __object.*__pm; }
801 operator()(_Class* __object) const
802 { return __object->*__pm; }
805 operator()(const _Class* __object) const
806 { return __object->*__pm; }
808 // Handle smart pointers and derived
809 template<typename _Tp>
810 typename _Result_type<_Tp>::type
811 operator()(_Tp& __unknown) const
812 { return _M_call(__unknown, &__unknown); }
819 * @brief Returns a function object that forwards to the member
822 template<typename _Tp, typename _Class>
823 inline _Mem_fn<_Tp _Class::*>
824 mem_fn(_Tp _Class::* __pm)
826 return _Mem_fn<_Tp _Class::*>(__pm);
830 * @brief Determines if the given type _Tp is a function object
831 * should be treated as a subexpression when evaluating calls to
832 * function objects returned by bind(). [TR1 3.6.1]
834 template<typename _Tp>
835 struct is_bind_expression
836 { static const bool value = false; };
838 template<typename _Tp>
839 const bool is_bind_expression<_Tp>::value;
842 * @brief Determines if the given type _Tp is a placeholder in a
843 * bind() expression and, if so, which placeholder it is. [TR1 3.6.2]
845 template<typename _Tp>
846 struct is_placeholder
847 { static const int value = 0; };
849 template<typename _Tp>
850 const int is_placeholder<_Tp>::value;
852 /// The type of placeholder objects defined by libstdc++.
853 template<int _Num> struct _Placeholder { };
855 /** @namespace std::tr1::placeholders
856 * @brief Sub-namespace for tr1/functional.
858 namespace placeholders
860 /* Define a large number of placeholders. There is no way to
861 * simplify this with variadic templates, because we're introducing
862 * unique names for each.
875 _Placeholder<10> _10;
876 _Placeholder<11> _11;
877 _Placeholder<12> _12;
878 _Placeholder<13> _13;
879 _Placeholder<14> _14;
880 _Placeholder<15> _15;
881 _Placeholder<16> _16;
882 _Placeholder<17> _17;
883 _Placeholder<18> _18;
884 _Placeholder<19> _19;
885 _Placeholder<20> _20;
886 _Placeholder<21> _21;
887 _Placeholder<22> _22;
888 _Placeholder<23> _23;
889 _Placeholder<24> _24;
890 _Placeholder<25> _25;
891 _Placeholder<26> _26;
892 _Placeholder<27> _27;
893 _Placeholder<28> _28;
894 _Placeholder<29> _29;
899 * Partial specialization of is_placeholder that provides the placeholder
900 * number for the placeholder objects defined by libstdc++.
903 struct is_placeholder<_Placeholder<_Num> >
904 { static const int value = _Num; };
907 const int is_placeholder<_Placeholder<_Num> >::value;
909 #if __cplusplus >= 201103L
911 struct is_placeholder<std::_Placeholder<_Num>>
912 : std::integral_constant<int, _Num>
916 struct is_placeholder<const std::_Placeholder<_Num>>
917 : std::integral_constant<int, _Num>
922 * Stores a tuple of indices. Used by bind() to extract the elements
925 template<int... _Indexes>
926 struct _Index_tuple { };
928 /// Builds an _Index_tuple<0, 1, 2, ..., _Num-1>.
929 template<std::size_t _Num, typename _Tuple = _Index_tuple<> >
930 struct _Build_index_tuple;
932 template<std::size_t _Num, int... _Indexes>
933 struct _Build_index_tuple<_Num, _Index_tuple<_Indexes...> >
934 : _Build_index_tuple<_Num - 1,
935 _Index_tuple<_Indexes..., sizeof...(_Indexes)> >
939 template<int... _Indexes>
940 struct _Build_index_tuple<0, _Index_tuple<_Indexes...> >
942 typedef _Index_tuple<_Indexes...> __type;
946 * Used by _Safe_tuple_element to indicate that there is no tuple
947 * element at this position.
949 struct _No_tuple_element;
952 * Implementation helper for _Safe_tuple_element. This primary
953 * template handles the case where it is safe to use @c
956 template<int __i, typename _Tuple, bool _IsSafe>
957 struct _Safe_tuple_element_impl
958 : tuple_element<__i, _Tuple> { };
961 * Implementation helper for _Safe_tuple_element. This partial
962 * specialization handles the case where it is not safe to use @c
963 * tuple_element. We just return @c _No_tuple_element.
965 template<int __i, typename _Tuple>
966 struct _Safe_tuple_element_impl<__i, _Tuple, false>
968 typedef _No_tuple_element type;
972 * Like tuple_element, but returns @c _No_tuple_element when
973 * tuple_element would return an error.
975 template<int __i, typename _Tuple>
976 struct _Safe_tuple_element
977 : _Safe_tuple_element_impl<__i, _Tuple,
978 (__i >= 0 && __i < tuple_size<_Tuple>::value)>
983 * Maps an argument to bind() into an actual argument to the bound
984 * function object [TR1 3.6.3/5]. Only the first parameter should
985 * be specified: the rest are used to determine among the various
986 * implementations. Note that, although this class is a function
987 * object, it isn't entirely normal because it takes only two
988 * parameters regardless of the number of parameters passed to the
989 * bind expression. The first parameter is the bound argument and
990 * the second parameter is a tuple containing references to the
991 * rest of the arguments.
993 template<typename _Arg,
994 bool _IsBindExp = is_bind_expression<_Arg>::value,
995 bool _IsPlaceholder = (is_placeholder<_Arg>::value > 0)>
999 * If the argument is reference_wrapper<_Tp>, returns the
1000 * underlying reference. [TR1 3.6.3/5 bullet 1]
1002 template<typename _Tp>
1003 class _Mu<reference_wrapper<_Tp>, false, false>
1006 typedef _Tp& result_type;
1008 /* Note: This won't actually work for const volatile
1009 * reference_wrappers, because reference_wrapper::get() is const
1010 * but not volatile-qualified. This might be a defect in the TR.
1012 template<typename _CVRef, typename _Tuple>
1014 operator()(_CVRef& __arg, const _Tuple&) const volatile
1015 { return __arg.get(); }
1019 * If the argument is a bind expression, we invoke the underlying
1020 * function object with the same cv-qualifiers as we are given and
1021 * pass along all of our arguments (unwrapped). [TR1 3.6.3/5 bullet 2]
1023 template<typename _Arg>
1024 class _Mu<_Arg, true, false>
1027 template<typename _Signature> class result;
1029 // Determine the result type when we pass the arguments along. This
1030 // involves passing along the cv-qualifiers placed on _Mu and
1031 // unwrapping the argument bundle.
1032 template<typename _CVMu, typename _CVArg, typename... _Args>
1033 class result<_CVMu(_CVArg, tuple<_Args...>)>
1034 : public result_of<_CVArg(_Args...)> { };
1036 template<typename _CVArg, typename... _Args>
1037 typename result_of<_CVArg(_Args...)>::type
1038 operator()(_CVArg& __arg,
1039 const tuple<_Args...>& __tuple) const volatile
1041 // Construct an index tuple and forward to __call
1042 typedef typename _Build_index_tuple<sizeof...(_Args)>::__type
1044 return this->__call(__arg, __tuple, _Indexes());
1048 // Invokes the underlying function object __arg by unpacking all
1049 // of the arguments in the tuple.
1050 template<typename _CVArg, typename... _Args, int... _Indexes>
1051 typename result_of<_CVArg(_Args...)>::type
1052 __call(_CVArg& __arg, const tuple<_Args...>& __tuple,
1053 const _Index_tuple<_Indexes...>&) const volatile
1055 return __arg(tr1::get<_Indexes>(__tuple)...);
1060 * If the argument is a placeholder for the Nth argument, returns
1061 * a reference to the Nth argument to the bind function object.
1062 * [TR1 3.6.3/5 bullet 3]
1064 template<typename _Arg>
1065 class _Mu<_Arg, false, true>
1068 template<typename _Signature> class result;
1070 template<typename _CVMu, typename _CVArg, typename _Tuple>
1071 class result<_CVMu(_CVArg, _Tuple)>
1073 // Add a reference, if it hasn't already been done for us.
1074 // This allows us to be a little bit sloppy in constructing
1075 // the tuple that we pass to result_of<...>.
1076 typedef typename _Safe_tuple_element<(is_placeholder<_Arg>::value
1081 typedef typename add_reference<__base_type>::type type;
1084 template<typename _Tuple>
1085 typename result<_Mu(_Arg, _Tuple)>::type
1086 operator()(const volatile _Arg&, const _Tuple& __tuple) const volatile
1088 return ::std::tr1::get<(is_placeholder<_Arg>::value - 1)>(__tuple);
1093 * If the argument is just a value, returns a reference to that
1094 * value. The cv-qualifiers on the reference are the same as the
1095 * cv-qualifiers on the _Mu object. [TR1 3.6.3/5 bullet 4]
1097 template<typename _Arg>
1098 class _Mu<_Arg, false, false>
1101 template<typename _Signature> struct result;
1103 template<typename _CVMu, typename _CVArg, typename _Tuple>
1104 struct result<_CVMu(_CVArg, _Tuple)>
1106 typedef typename add_reference<_CVArg>::type type;
1109 // Pick up the cv-qualifiers of the argument
1110 template<typename _CVArg, typename _Tuple>
1112 operator()(_CVArg& __arg, const _Tuple&) const volatile
1117 * Maps member pointers into instances of _Mem_fn but leaves all
1118 * other function objects untouched. Used by tr1::bind(). The
1119 * primary template handles the non--member-pointer case.
1121 template<typename _Tp>
1122 struct _Maybe_wrap_member_pointer
1127 __do_wrap(const _Tp& __x)
1132 * Maps member pointers into instances of _Mem_fn but leaves all
1133 * other function objects untouched. Used by tr1::bind(). This
1134 * partial specialization handles the member pointer case.
1136 template<typename _Tp, typename _Class>
1137 struct _Maybe_wrap_member_pointer<_Tp _Class::*>
1139 typedef _Mem_fn<_Tp _Class::*> type;
1142 __do_wrap(_Tp _Class::* __pm)
1143 { return type(__pm); }
1146 /// Type of the function object returned from bind().
1147 template<typename _Signature>
1150 template<typename _Functor, typename... _Bound_args>
1151 class _Bind<_Functor(_Bound_args...)>
1152 : public _Weak_result_type<_Functor>
1154 typedef _Bind __self_type;
1155 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
1159 tuple<_Bound_args...> _M_bound_args;
1162 template<typename... _Args, int... _Indexes>
1164 _Functor(typename result_of<_Mu<_Bound_args>
1165 (_Bound_args, tuple<_Args...>)>::type...)
1167 __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>)
1169 return _M_f(_Mu<_Bound_args>()
1170 (tr1::get<_Indexes>(_M_bound_args), __args)...);
1174 template<typename... _Args, int... _Indexes>
1176 const _Functor(typename result_of<_Mu<_Bound_args>
1177 (const _Bound_args, tuple<_Args...>)
1179 __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>) const
1181 return _M_f(_Mu<_Bound_args>()
1182 (tr1::get<_Indexes>(_M_bound_args), __args)...);
1186 template<typename... _Args, int... _Indexes>
1188 volatile _Functor(typename result_of<_Mu<_Bound_args>
1189 (volatile _Bound_args, tuple<_Args...>)
1191 __call(const tuple<_Args...>& __args,
1192 _Index_tuple<_Indexes...>) volatile
1194 return _M_f(_Mu<_Bound_args>()
1195 (tr1::get<_Indexes>(_M_bound_args), __args)...);
1198 // Call as const volatile
1199 template<typename... _Args, int... _Indexes>
1201 const volatile _Functor(typename result_of<_Mu<_Bound_args>
1202 (const volatile _Bound_args,
1205 __call(const tuple<_Args...>& __args,
1206 _Index_tuple<_Indexes...>) const volatile
1208 return _M_f(_Mu<_Bound_args>()
1209 (tr1::get<_Indexes>(_M_bound_args), __args)...);
1213 explicit _Bind(_Functor __f, _Bound_args... __bound_args)
1214 : _M_f(__f), _M_bound_args(__bound_args...) { }
1217 template<typename... _Args>
1219 _Functor(typename result_of<_Mu<_Bound_args>
1220 (_Bound_args, tuple<_Args...>)>::type...)
1222 operator()(_Args&... __args)
1224 return this->__call(tr1::tie(__args...), _Bound_indexes());
1228 template<typename... _Args>
1230 const _Functor(typename result_of<_Mu<_Bound_args>
1231 (const _Bound_args, tuple<_Args...>)>::type...)
1233 operator()(_Args&... __args) const
1235 return this->__call(tr1::tie(__args...), _Bound_indexes());
1240 template<typename... _Args>
1242 volatile _Functor(typename result_of<_Mu<_Bound_args>
1243 (volatile _Bound_args, tuple<_Args...>)>::type...)
1245 operator()(_Args&... __args) volatile
1247 return this->__call(tr1::tie(__args...), _Bound_indexes());
1251 // Call as const volatile
1252 template<typename... _Args>
1254 const volatile _Functor(typename result_of<_Mu<_Bound_args>
1255 (const volatile _Bound_args,
1256 tuple<_Args...>)>::type...)
1258 operator()(_Args&... __args) const volatile
1260 return this->__call(tr1::tie(__args...), _Bound_indexes());
1264 /// Type of the function object returned from bind<R>().
1265 template<typename _Result, typename _Signature>
1266 struct _Bind_result;
1268 template<typename _Result, typename _Functor, typename... _Bound_args>
1269 class _Bind_result<_Result, _Functor(_Bound_args...)>
1271 typedef _Bind_result __self_type;
1272 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
1276 tuple<_Bound_args...> _M_bound_args;
1279 template<typename... _Args, int... _Indexes>
1281 __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>)
1283 return _M_f(_Mu<_Bound_args>()
1284 (tr1::get<_Indexes>(_M_bound_args), __args)...);
1288 template<typename... _Args, int... _Indexes>
1290 __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>) const
1292 return _M_f(_Mu<_Bound_args>()
1293 (tr1::get<_Indexes>(_M_bound_args), __args)...);
1297 template<typename... _Args, int... _Indexes>
1299 __call(const tuple<_Args...>& __args,
1300 _Index_tuple<_Indexes...>) volatile
1302 return _M_f(_Mu<_Bound_args>()
1303 (tr1::get<_Indexes>(_M_bound_args), __args)...);
1306 // Call as const volatile
1307 template<typename... _Args, int... _Indexes>
1309 __call(const tuple<_Args...>& __args,
1310 _Index_tuple<_Indexes...>) const volatile
1312 return _M_f(_Mu<_Bound_args>()
1313 (tr1::get<_Indexes>(_M_bound_args), __args)...);
1317 typedef _Result result_type;
1320 _Bind_result(_Functor __f, _Bound_args... __bound_args)
1321 : _M_f(__f), _M_bound_args(__bound_args...) { }
1324 template<typename... _Args>
1326 operator()(_Args&... __args)
1328 return this->__call(tr1::tie(__args...), _Bound_indexes());
1332 template<typename... _Args>
1334 operator()(_Args&... __args) const
1336 return this->__call(tr1::tie(__args...), _Bound_indexes());
1340 template<typename... _Args>
1342 operator()(_Args&... __args) volatile
1344 return this->__call(tr1::tie(__args...), _Bound_indexes());
1347 // Call as const volatile
1348 template<typename... _Args>
1350 operator()(_Args&... __args) const volatile
1352 return this->__call(tr1::tie(__args...), _Bound_indexes());
1356 /// Class template _Bind is always a bind expression.
1357 template<typename _Signature>
1358 struct is_bind_expression<_Bind<_Signature> >
1359 { static const bool value = true; };
1361 template<typename _Signature>
1362 const bool is_bind_expression<_Bind<_Signature> >::value;
1364 /// Class template _Bind is always a bind expression.
1365 template<typename _Signature>
1366 struct is_bind_expression<const _Bind<_Signature> >
1367 { static const bool value = true; };
1369 template<typename _Signature>
1370 const bool is_bind_expression<const _Bind<_Signature> >::value;
1372 /// Class template _Bind is always a bind expression.
1373 template<typename _Signature>
1374 struct is_bind_expression<volatile _Bind<_Signature> >
1375 { static const bool value = true; };
1377 template<typename _Signature>
1378 const bool is_bind_expression<volatile _Bind<_Signature> >::value;
1380 /// Class template _Bind is always a bind expression.
1381 template<typename _Signature>
1382 struct is_bind_expression<const volatile _Bind<_Signature> >
1383 { static const bool value = true; };
1385 template<typename _Signature>
1386 const bool is_bind_expression<const volatile _Bind<_Signature> >::value;
1388 /// Class template _Bind_result is always a bind expression.
1389 template<typename _Result, typename _Signature>
1390 struct is_bind_expression<_Bind_result<_Result, _Signature> >
1391 { static const bool value = true; };
1393 template<typename _Result, typename _Signature>
1394 const bool is_bind_expression<_Bind_result<_Result, _Signature> >::value;
1396 /// Class template _Bind_result is always a bind expression.
1397 template<typename _Result, typename _Signature>
1398 struct is_bind_expression<const _Bind_result<_Result, _Signature> >
1399 { static const bool value = true; };
1401 template<typename _Result, typename _Signature>
1403 is_bind_expression<const _Bind_result<_Result, _Signature> >::value;
1405 /// Class template _Bind_result is always a bind expression.
1406 template<typename _Result, typename _Signature>
1407 struct is_bind_expression<volatile _Bind_result<_Result, _Signature> >
1408 { static const bool value = true; };
1410 template<typename _Result, typename _Signature>
1412 is_bind_expression<volatile _Bind_result<_Result, _Signature> >::value;
1414 /// Class template _Bind_result is always a bind expression.
1415 template<typename _Result, typename _Signature>
1417 is_bind_expression<const volatile _Bind_result<_Result, _Signature> >
1418 { static const bool value = true; };
1420 template<typename _Result, typename _Signature>
1422 is_bind_expression<const volatile _Bind_result<_Result,
1423 _Signature> >::value;
1425 #if __cplusplus >= 201103L
1426 template<typename _Signature>
1427 struct is_bind_expression<std::_Bind<_Signature>>
1430 template<typename _Signature>
1431 struct is_bind_expression<const std::_Bind<_Signature>>
1434 template<typename _Signature>
1435 struct is_bind_expression<volatile std::_Bind<_Signature>>
1438 template<typename _Signature>
1439 struct is_bind_expression<const volatile std::_Bind<_Signature>>
1442 template<typename _Result, typename _Signature>
1443 struct is_bind_expression<std::_Bind_result<_Result, _Signature>>
1446 template<typename _Result, typename _Signature>
1447 struct is_bind_expression<const std::_Bind_result<_Result, _Signature>>
1450 template<typename _Result, typename _Signature>
1451 struct is_bind_expression<volatile std::_Bind_result<_Result, _Signature>>
1454 template<typename _Result, typename _Signature>
1455 struct is_bind_expression<const volatile std::_Bind_result<_Result,
1461 template<typename _Functor, typename... _ArgTypes>
1463 _Bind<typename _Maybe_wrap_member_pointer<_Functor>::type(_ArgTypes...)>
1464 bind(_Functor __f, _ArgTypes... __args)
1466 typedef _Maybe_wrap_member_pointer<_Functor> __maybe_type;
1467 typedef typename __maybe_type::type __functor_type;
1468 typedef _Bind<__functor_type(_ArgTypes...)> __result_type;
1469 return __result_type(__maybe_type::__do_wrap(__f), __args...);
1472 template<typename _Result, typename _Functor, typename... _ArgTypes>
1474 _Bind_result<_Result,
1475 typename _Maybe_wrap_member_pointer<_Functor>::type
1477 bind(_Functor __f, _ArgTypes... __args)
1479 typedef _Maybe_wrap_member_pointer<_Functor> __maybe_type;
1480 typedef typename __maybe_type::type __functor_type;
1481 typedef _Bind_result<_Result, __functor_type(_ArgTypes...)>
1483 return __result_type(__maybe_type::__do_wrap(__f), __args...);
1487 * @brief Exception class thrown when class template function's
1488 * operator() is called with an empty target.
1489 * @ingroup exceptions
1491 class bad_function_call : public std::exception { };
1494 * The integral constant expression 0 can be converted into a
1495 * pointer to this type. It is used by the function template to
1496 * accept NULL pointers.
1498 struct _M_clear_type;
1501 * Trait identifying @a location-invariant types, meaning that the
1502 * address of the object (or any of its members) will not escape.
1503 * Also implies a trivial copy constructor and assignment operator.
1505 template<typename _Tp>
1506 struct __is_location_invariant
1507 : integral_constant<bool,
1508 (is_pointer<_Tp>::value
1509 || is_member_pointer<_Tp>::value)>
1513 class _Undefined_class;
1518 const void* _M_const_object;
1519 void (*_M_function_pointer)();
1520 void (_Undefined_class::*_M_member_pointer)();
1525 void* _M_access() { return &_M_pod_data[0]; }
1526 const void* _M_access() const { return &_M_pod_data[0]; }
1528 template<typename _Tp>
1531 { return *static_cast<_Tp*>(_M_access()); }
1533 template<typename _Tp>
1536 { return *static_cast<const _Tp*>(_M_access()); }
1538 _Nocopy_types _M_unused;
1539 char _M_pod_data[sizeof(_Nocopy_types)];
1542 enum _Manager_operation
1550 // Simple type wrapper that helps avoid annoying const problems
1551 // when casting between void pointers and pointers-to-pointers.
1552 template<typename _Tp>
1553 struct _Simple_type_wrapper
1555 _Simple_type_wrapper(_Tp __value) : __value(__value) { }
1560 template<typename _Tp>
1561 struct __is_location_invariant<_Simple_type_wrapper<_Tp> >
1562 : __is_location_invariant<_Tp>
1566 // Converts a reference to a function object into a callable
1568 template<typename _Functor>
1570 __callable_functor(_Functor& __f)
1573 template<typename _Member, typename _Class>
1574 inline _Mem_fn<_Member _Class::*>
1575 __callable_functor(_Member _Class::* &__p)
1576 { return mem_fn(__p); }
1578 template<typename _Member, typename _Class>
1579 inline _Mem_fn<_Member _Class::*>
1580 __callable_functor(_Member _Class::* const &__p)
1581 { return mem_fn(__p); }
1583 template<typename _Signature>
1586 /// Base class of all polymorphic function object wrappers.
1587 class _Function_base
1590 static const std::size_t _M_max_size = sizeof(_Nocopy_types);
1591 static const std::size_t _M_max_align = __alignof__(_Nocopy_types);
1593 template<typename _Functor>
1597 static const bool __stored_locally =
1598 (__is_location_invariant<_Functor>::value
1599 && sizeof(_Functor) <= _M_max_size
1600 && __alignof__(_Functor) <= _M_max_align
1601 && (_M_max_align % __alignof__(_Functor) == 0));
1603 typedef integral_constant<bool, __stored_locally> _Local_storage;
1605 // Retrieve a pointer to the function object
1607 _M_get_pointer(const _Any_data& __source)
1609 const _Functor* __ptr =
1610 __stored_locally? std::__addressof(__source._M_access<_Functor>())
1611 /* have stored a pointer */ : __source._M_access<_Functor*>();
1612 return const_cast<_Functor*>(__ptr);
1615 // Clone a location-invariant function object that fits within
1616 // an _Any_data structure.
1618 _M_clone(_Any_data& __dest, const _Any_data& __source, true_type)
1620 new (__dest._M_access()) _Functor(__source._M_access<_Functor>());
1623 // Clone a function object that is not location-invariant or
1624 // that cannot fit into an _Any_data structure.
1626 _M_clone(_Any_data& __dest, const _Any_data& __source, false_type)
1628 __dest._M_access<_Functor*>() =
1629 new _Functor(*__source._M_access<_Functor*>());
1632 // Destroying a location-invariant object may still require
1635 _M_destroy(_Any_data& __victim, true_type)
1637 __victim._M_access<_Functor>().~_Functor();
1640 // Destroying an object located on the heap.
1642 _M_destroy(_Any_data& __victim, false_type)
1644 delete __victim._M_access<_Functor*>();
1649 _M_manager(_Any_data& __dest, const _Any_data& __source,
1650 _Manager_operation __op)
1655 case __get_type_info:
1656 __dest._M_access<const type_info*>() = &typeid(_Functor);
1659 case __get_functor_ptr:
1660 __dest._M_access<_Functor*>() = _M_get_pointer(__source);
1663 case __clone_functor:
1664 _M_clone(__dest, __source, _Local_storage());
1667 case __destroy_functor:
1668 _M_destroy(__dest, _Local_storage());
1675 _M_init_functor(_Any_data& __functor, const _Functor& __f)
1676 { _M_init_functor(__functor, __f, _Local_storage()); }
1678 template<typename _Signature>
1680 _M_not_empty_function(const function<_Signature>& __f)
1681 { return static_cast<bool>(__f); }
1683 template<typename _Tp>
1685 _M_not_empty_function(const _Tp*& __fp)
1688 template<typename _Class, typename _Tp>
1690 _M_not_empty_function(_Tp _Class::* const& __mp)
1693 template<typename _Tp>
1695 _M_not_empty_function(const _Tp&)
1700 _M_init_functor(_Any_data& __functor, const _Functor& __f, true_type)
1701 { new (__functor._M_access()) _Functor(__f); }
1704 _M_init_functor(_Any_data& __functor, const _Functor& __f, false_type)
1705 { __functor._M_access<_Functor*>() = new _Functor(__f); }
1708 template<typename _Functor>
1709 class _Ref_manager : public _Base_manager<_Functor*>
1711 typedef _Function_base::_Base_manager<_Functor*> _Base;
1715 _M_manager(_Any_data& __dest, const _Any_data& __source,
1716 _Manager_operation __op)
1721 case __get_type_info:
1722 __dest._M_access<const type_info*>() = &typeid(_Functor);
1725 case __get_functor_ptr:
1726 __dest._M_access<_Functor*>() = *_Base::_M_get_pointer(__source);
1727 return is_const<_Functor>::value;
1731 _Base::_M_manager(__dest, __source, __op);
1737 _M_init_functor(_Any_data& __functor, reference_wrapper<_Functor> __f)
1739 _Base::_M_init_functor(__functor, std::__addressof(__f.get()));
1743 _Function_base() : _M_manager(0) { }
1748 _M_manager(_M_functor, _M_functor, __destroy_functor);
1752 bool _M_empty() const { return !_M_manager; }
1754 typedef bool (*_Manager_type)(_Any_data&, const _Any_data&,
1755 _Manager_operation);
1757 _Any_data _M_functor;
1758 _Manager_type _M_manager;
1761 template<typename _Signature, typename _Functor>
1762 class _Function_handler;
1764 template<typename _Res, typename _Functor, typename... _ArgTypes>
1765 class _Function_handler<_Res(_ArgTypes...), _Functor>
1766 : public _Function_base::_Base_manager<_Functor>
1768 typedef _Function_base::_Base_manager<_Functor> _Base;
1772 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1774 return (*_Base::_M_get_pointer(__functor))(__args...);
1778 template<typename _Functor, typename... _ArgTypes>
1779 class _Function_handler<void(_ArgTypes...), _Functor>
1780 : public _Function_base::_Base_manager<_Functor>
1782 typedef _Function_base::_Base_manager<_Functor> _Base;
1786 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1788 (*_Base::_M_get_pointer(__functor))(__args...);
1792 template<typename _Res, typename _Functor, typename... _ArgTypes>
1793 class _Function_handler<_Res(_ArgTypes...), reference_wrapper<_Functor> >
1794 : public _Function_base::_Ref_manager<_Functor>
1796 typedef _Function_base::_Ref_manager<_Functor> _Base;
1800 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1803 __callable_functor(**_Base::_M_get_pointer(__functor))(__args...);
1807 template<typename _Functor, typename... _ArgTypes>
1808 class _Function_handler<void(_ArgTypes...), reference_wrapper<_Functor> >
1809 : public _Function_base::_Ref_manager<_Functor>
1811 typedef _Function_base::_Ref_manager<_Functor> _Base;
1815 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1817 __callable_functor(**_Base::_M_get_pointer(__functor))(__args...);
1821 template<typename _Class, typename _Member, typename _Res,
1822 typename... _ArgTypes>
1823 class _Function_handler<_Res(_ArgTypes...), _Member _Class::*>
1824 : public _Function_handler<void(_ArgTypes...), _Member _Class::*>
1826 typedef _Function_handler<void(_ArgTypes...), _Member _Class::*>
1831 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1834 mem_fn(_Base::_M_get_pointer(__functor)->__value)(__args...);
1838 template<typename _Class, typename _Member, typename... _ArgTypes>
1839 class _Function_handler<void(_ArgTypes...), _Member _Class::*>
1840 : public _Function_base::_Base_manager<
1841 _Simple_type_wrapper< _Member _Class::* > >
1843 typedef _Member _Class::* _Functor;
1844 typedef _Simple_type_wrapper<_Functor> _Wrapper;
1845 typedef _Function_base::_Base_manager<_Wrapper> _Base;
1849 _M_manager(_Any_data& __dest, const _Any_data& __source,
1850 _Manager_operation __op)
1855 case __get_type_info:
1856 __dest._M_access<const type_info*>() = &typeid(_Functor);
1859 case __get_functor_ptr:
1860 __dest._M_access<_Functor*>() =
1861 &_Base::_M_get_pointer(__source)->__value;
1865 _Base::_M_manager(__dest, __source, __op);
1871 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1873 tr1::mem_fn(_Base::_M_get_pointer(__functor)->__value)(__args...);
1878 template<typename _Res, typename... _ArgTypes>
1879 class function<_Res(_ArgTypes...)>
1880 : public _Maybe_unary_or_binary_function<_Res, _ArgTypes...>,
1881 private _Function_base
1883 #if __cplusplus < 201103L
1884 /// This class is used to implement the safe_bool idiom.
1887 _Hidden_type* _M_bool;
1890 /// This typedef is used to implement the safe_bool idiom.
1891 typedef _Hidden_type* _Hidden_type::* _Safe_bool;
1894 typedef _Res _Signature_type(_ArgTypes...);
1896 struct _Useless { };
1899 typedef _Res result_type;
1901 // [3.7.2.1] construct/copy/destroy
1904 * @brief Default construct creates an empty function call wrapper.
1905 * @post @c !(bool)*this
1907 function() : _Function_base() { }
1910 * @brief Default construct creates an empty function call wrapper.
1911 * @post @c !(bool)*this
1913 function(_M_clear_type*) : _Function_base() { }
1916 * @brief %Function copy constructor.
1917 * @param x A %function object with identical call signature.
1918 * @post @c (bool)*this == (bool)x
1920 * The newly-created %function contains a copy of the target of @a
1921 * x (if it has one).
1923 function(const function& __x);
1926 * @brief Builds a %function that targets a copy of the incoming
1928 * @param f A %function object that is callable with parameters of
1929 * type @c T1, @c T2, ..., @c TN and returns a value convertible
1932 * The newly-created %function object will target a copy of @a
1933 * f. If @a f is @c reference_wrapper<F>, then this function
1934 * object will contain a reference to the function object @c
1935 * f.get(). If @a f is a NULL function pointer or NULL
1936 * pointer-to-member, the newly-created object will be empty.
1938 * If @a f is a non-NULL function pointer or an object of type @c
1939 * reference_wrapper<F>, this function will not throw.
1941 template<typename _Functor>
1942 function(_Functor __f,
1943 typename __gnu_cxx::__enable_if<
1944 !is_integral<_Functor>::value, _Useless>::__type
1948 * @brief %Function assignment operator.
1949 * @param x A %function with identical call signature.
1950 * @post @c (bool)*this == (bool)x
1953 * The target of @a x is copied to @c *this. If @a x has no
1954 * target, then @c *this will be empty.
1956 * If @a x targets a function pointer or a reference to a function
1957 * object, then this operation will not throw an %exception.
1960 operator=(const function& __x)
1962 function(__x).swap(*this);
1967 * @brief %Function assignment to zero.
1968 * @post @c !(bool)*this
1971 * The target of @c *this is deallocated, leaving it empty.
1974 operator=(_M_clear_type*)
1978 _M_manager(_M_functor, _M_functor, __destroy_functor);
1986 * @brief %Function assignment to a new target.
1987 * @param f A %function object that is callable with parameters of
1988 * type @c T1, @c T2, ..., @c TN and returns a value convertible
1992 * This %function object wrapper will target a copy of @a
1993 * f. If @a f is @c reference_wrapper<F>, then this function
1994 * object will contain a reference to the function object @c
1995 * f.get(). If @a f is a NULL function pointer or NULL
1996 * pointer-to-member, @c this object will be empty.
1998 * If @a f is a non-NULL function pointer or an object of type @c
1999 * reference_wrapper<F>, this function will not throw.
2001 template<typename _Functor>
2002 typename __gnu_cxx::__enable_if<!is_integral<_Functor>::value,
2004 operator=(_Functor __f)
2006 function(__f).swap(*this);
2010 // [3.7.2.2] function modifiers
2013 * @brief Swap the targets of two %function objects.
2014 * @param f A %function with identical call signature.
2016 * Swap the targets of @c this function object and @a f. This
2017 * function will not throw an %exception.
2019 void swap(function& __x)
2021 std::swap(_M_functor, __x._M_functor);
2022 std::swap(_M_manager, __x._M_manager);
2023 std::swap(_M_invoker, __x._M_invoker);
2026 // [3.7.2.3] function capacity
2029 * @brief Determine if the %function wrapper has a target.
2031 * @return @c true when this %function object contains a target,
2032 * or @c false when it is empty.
2034 * This function will not throw an %exception.
2036 #if __cplusplus >= 201103L
2037 explicit operator bool() const
2038 { return !_M_empty(); }
2040 operator _Safe_bool() const
2045 return &_Hidden_type::_M_bool;
2049 // [3.7.2.4] function invocation
2052 * @brief Invokes the function targeted by @c *this.
2053 * @returns the result of the target.
2054 * @throws bad_function_call when @c !(bool)*this
2056 * The function call operator invokes the target function object
2057 * stored by @c this.
2059 _Res operator()(_ArgTypes... __args) const;
2062 // [3.7.2.5] function target access
2064 * @brief Determine the type of the target of this function object
2067 * @returns the type identifier of the target function object, or
2068 * @c typeid(void) if @c !(bool)*this.
2070 * This function will not throw an %exception.
2072 const type_info& target_type() const;
2075 * @brief Access the stored target function object.
2077 * @return Returns a pointer to the stored target function object,
2078 * if @c typeid(Functor).equals(target_type()); otherwise, a NULL
2081 * This function will not throw an %exception.
2083 template<typename _Functor> _Functor* target();
2086 template<typename _Functor> const _Functor* target() const;
2090 // [3.7.2.6] undefined operators
2091 template<typename _Function>
2092 void operator==(const function<_Function>&) const;
2093 template<typename _Function>
2094 void operator!=(const function<_Function>&) const;
2096 typedef _Res (*_Invoker_type)(const _Any_data&, _ArgTypes...);
2097 _Invoker_type _M_invoker;
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
2246 template<typename> struct is_placeholder;
2249 struct is_placeholder<tr1::_Placeholder<_Num>>
2250 : integral_constant<int, _Num>
2254 struct is_placeholder<const tr1::_Placeholder<_Num>>
2255 : integral_constant<int, _Num>
2258 template<typename> struct is_bind_expression;
2260 template<typename _Signature>
2261 struct is_bind_expression<tr1::_Bind<_Signature>>
2264 template<typename _Signature>
2265 struct is_bind_expression<const tr1::_Bind<_Signature>>
2268 template<typename _Signature>
2269 struct is_bind_expression<volatile tr1::_Bind<_Signature>>
2272 template<typename _Signature>
2273 struct is_bind_expression<const volatile tr1::_Bind<_Signature>>
2276 template<typename _Result, typename _Signature>
2277 struct is_bind_expression<tr1::_Bind_result<_Result, _Signature>>
2280 template<typename _Result, typename _Signature>
2281 struct is_bind_expression<const tr1::_Bind_result<_Result, _Signature>>
2284 template<typename _Result, typename _Signature>
2285 struct is_bind_expression<volatile tr1::_Bind_result<_Result, _Signature>>
2288 template<typename _Result, typename _Signature>
2289 struct is_bind_expression<const volatile tr1::_Bind_result<_Result,
2294 _GLIBCXX_END_NAMESPACE_VERSION
2297 #endif // _GLIBCXX_TR1_FUNCTIONAL