1 // TR1 functional header -*- C++ -*-
3 // Copyright (C) 2004-2015 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 #if __cplusplus >= 201103L
52 _GLIBCXX_BEGIN_NAMESPACE_VERSION
53 template<int> struct _Placeholder;
54 template<typename> class _Bind;
55 template<typename, typename> class _Bind_result;
56 _GLIBCXX_END_NAMESPACE_VERSION
61 _GLIBCXX_BEGIN_NAMESPACE_VERSION
63 template<typename _MemberPointer>
65 template<typename _Tp, typename _Class>
66 _Mem_fn<_Tp _Class::*>
67 mem_fn(_Tp _Class::*);
70 * Actual implementation of _Has_result_type, which uses SFINAE to
71 * determine if the type _Tp has a publicly-accessible member type
74 template<typename _Tp>
75 class _Has_result_type_helper : __sfinae_types
77 template<typename _Up>
81 template<typename _Up>
82 static __one __test(_Wrap_type<typename _Up::result_type>*);
84 template<typename _Up>
85 static __two __test(...);
88 static const bool value = sizeof(__test<_Tp>(0)) == 1;
91 template<typename _Tp>
92 struct _Has_result_type
93 : integral_constant<bool,
94 _Has_result_type_helper<typename remove_cv<_Tp>::type>::value>
100 /// If we have found a result_type, extract it.
101 template<bool _Has_result_type, typename _Functor>
102 struct _Maybe_get_result_type
105 template<typename _Functor>
106 struct _Maybe_get_result_type<true, _Functor>
108 typedef typename _Functor::result_type result_type;
112 * Base class for any function object that has a weak result type, as
113 * defined in 3.3/3 of TR1.
115 template<typename _Functor>
116 struct _Weak_result_type_impl
117 : _Maybe_get_result_type<_Has_result_type<_Functor>::value, _Functor>
121 /// Retrieve the result type for a function type.
122 template<typename _Res, typename... _ArgTypes>
123 struct _Weak_result_type_impl<_Res(_ArgTypes...)>
125 typedef _Res result_type;
128 /// Retrieve the result type for a function reference.
129 template<typename _Res, typename... _ArgTypes>
130 struct _Weak_result_type_impl<_Res(&)(_ArgTypes...)>
132 typedef _Res result_type;
135 /// Retrieve the result type for a function pointer.
136 template<typename _Res, typename... _ArgTypes>
137 struct _Weak_result_type_impl<_Res(*)(_ArgTypes...)>
139 typedef _Res result_type;
142 /// Retrieve result type for a member function pointer.
143 template<typename _Res, typename _Class, typename... _ArgTypes>
144 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...)>
146 typedef _Res result_type;
149 /// Retrieve result type for a const member function pointer.
150 template<typename _Res, typename _Class, typename... _ArgTypes>
151 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) const>
153 typedef _Res result_type;
156 /// Retrieve result type for a volatile member function pointer.
157 template<typename _Res, typename _Class, typename... _ArgTypes>
158 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) volatile>
160 typedef _Res result_type;
163 /// Retrieve result type for a const volatile member function pointer.
164 template<typename _Res, typename _Class, typename... _ArgTypes>
165 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...)const volatile>
167 typedef _Res result_type;
171 * Strip top-level cv-qualifiers from the function object and let
172 * _Weak_result_type_impl perform the real work.
174 template<typename _Functor>
175 struct _Weak_result_type
176 : _Weak_result_type_impl<typename remove_cv<_Functor>::type>
180 template<typename _Signature>
184 * Actual implementation of result_of. When _Has_result_type is
185 * true, gets its result from _Weak_result_type. Otherwise, uses
186 * the function object's member template result to extract the
189 template<bool _Has_result_type, typename _Signature>
190 struct _Result_of_impl;
192 // Handle member data pointers using _Mem_fn's logic
193 template<typename _Res, typename _Class, typename _T1>
194 struct _Result_of_impl<false, _Res _Class::*(_T1)>
196 typedef typename _Mem_fn<_Res _Class::*>
197 ::template _Result_type<_T1>::type type;
201 * Determine whether we can determine a result type from @c Functor
204 template<typename _Functor, typename... _ArgTypes>
205 class result_of<_Functor(_ArgTypes...)>
206 : public _Result_of_impl<
207 _Has_result_type<_Weak_result_type<_Functor> >::value,
208 _Functor(_ArgTypes...)>
212 /// We already know the result type for @c Functor; use it.
213 template<typename _Functor, typename... _ArgTypes>
214 struct _Result_of_impl<true, _Functor(_ArgTypes...)>
216 typedef typename _Weak_result_type<_Functor>::result_type type;
220 * We need to compute the result type for this invocation the hard
223 template<typename _Functor, typename... _ArgTypes>
224 struct _Result_of_impl<false, _Functor(_ArgTypes...)>
226 typedef typename _Functor
227 ::template result<_Functor(_ArgTypes...)>::type type;
231 * It is unsafe to access ::result when there are zero arguments, so we
232 * return @c void instead.
234 template<typename _Functor>
235 struct _Result_of_impl<false, _Functor()>
240 /// Determines if the type _Tp derives from unary_function.
241 template<typename _Tp>
242 struct _Derives_from_unary_function : __sfinae_types
245 template<typename _T1, typename _Res>
246 static __one __test(const volatile unary_function<_T1, _Res>*);
248 // It's tempting to change "..." to const volatile void*, but
249 // that fails when _Tp is a function type.
250 static __two __test(...);
253 static const bool value = sizeof(__test((_Tp*)0)) == 1;
256 /// Determines if the type _Tp derives from binary_function.
257 template<typename _Tp>
258 struct _Derives_from_binary_function : __sfinae_types
261 template<typename _T1, typename _T2, typename _Res>
262 static __one __test(const volatile binary_function<_T1, _T2, _Res>*);
264 // It's tempting to change "..." to const volatile void*, but
265 // that fails when _Tp is a function type.
266 static __two __test(...);
269 static const bool value = sizeof(__test((_Tp*)0)) == 1;
272 /// Turns a function type into a function pointer type
273 template<typename _Tp, bool _IsFunctionType = is_function<_Tp>::value>
274 struct _Function_to_function_pointer
279 template<typename _Tp>
280 struct _Function_to_function_pointer<_Tp, true>
286 * Invoke a function object, which may be either a member pointer or a
287 * function object. The first parameter will tell which.
289 template<typename _Functor, typename... _Args>
291 typename __gnu_cxx::__enable_if<
292 (!is_member_pointer<_Functor>::value
293 && !is_function<_Functor>::value
294 && !is_function<typename remove_pointer<_Functor>::type>::value),
295 typename result_of<_Functor(_Args...)>::type
297 __invoke(_Functor& __f, _Args&... __args)
299 return __f(__args...);
302 template<typename _Functor, typename... _Args>
304 typename __gnu_cxx::__enable_if<
305 (is_member_pointer<_Functor>::value
306 && !is_function<_Functor>::value
307 && !is_function<typename remove_pointer<_Functor>::type>::value),
308 typename result_of<_Functor(_Args...)>::type
310 __invoke(_Functor& __f, _Args&... __args)
312 return mem_fn(__f)(__args...);
315 // To pick up function references (that will become function pointers)
316 template<typename _Functor, typename... _Args>
318 typename __gnu_cxx::__enable_if<
319 (is_pointer<_Functor>::value
320 && is_function<typename remove_pointer<_Functor>::type>::value),
321 typename result_of<_Functor(_Args...)>::type
323 __invoke(_Functor __f, _Args&... __args)
325 return __f(__args...);
329 * Knowing which of unary_function and binary_function _Tp derives
330 * from, derives from the same and ensures that reference_wrapper
331 * will have a weak result type. See cases below.
333 template<bool _Unary, bool _Binary, typename _Tp>
334 struct _Reference_wrapper_base_impl;
336 // Not a unary_function or binary_function, so try a weak result type.
337 template<typename _Tp>
338 struct _Reference_wrapper_base_impl<false, false, _Tp>
339 : _Weak_result_type<_Tp>
342 // unary_function but not binary_function
343 template<typename _Tp>
344 struct _Reference_wrapper_base_impl<true, false, _Tp>
345 : unary_function<typename _Tp::argument_type,
346 typename _Tp::result_type>
349 // binary_function but not unary_function
350 template<typename _Tp>
351 struct _Reference_wrapper_base_impl<false, true, _Tp>
352 : binary_function<typename _Tp::first_argument_type,
353 typename _Tp::second_argument_type,
354 typename _Tp::result_type>
357 // Both unary_function and binary_function. Import result_type to
359 template<typename _Tp>
360 struct _Reference_wrapper_base_impl<true, true, _Tp>
361 : unary_function<typename _Tp::argument_type,
362 typename _Tp::result_type>,
363 binary_function<typename _Tp::first_argument_type,
364 typename _Tp::second_argument_type,
365 typename _Tp::result_type>
367 typedef typename _Tp::result_type result_type;
371 * Derives from unary_function or binary_function when it
372 * can. Specializations handle all of the easy cases. The primary
373 * template determines what to do with a class type, which may
374 * derive from both unary_function and binary_function.
376 template<typename _Tp>
377 struct _Reference_wrapper_base
378 : _Reference_wrapper_base_impl<
379 _Derives_from_unary_function<_Tp>::value,
380 _Derives_from_binary_function<_Tp>::value,
384 // - a function type (unary)
385 template<typename _Res, typename _T1>
386 struct _Reference_wrapper_base<_Res(_T1)>
387 : unary_function<_T1, _Res>
390 // - a function type (binary)
391 template<typename _Res, typename _T1, typename _T2>
392 struct _Reference_wrapper_base<_Res(_T1, _T2)>
393 : binary_function<_T1, _T2, _Res>
396 // - a function pointer type (unary)
397 template<typename _Res, typename _T1>
398 struct _Reference_wrapper_base<_Res(*)(_T1)>
399 : unary_function<_T1, _Res>
402 // - a function pointer type (binary)
403 template<typename _Res, typename _T1, typename _T2>
404 struct _Reference_wrapper_base<_Res(*)(_T1, _T2)>
405 : binary_function<_T1, _T2, _Res>
408 // - a pointer to member function type (unary, no qualifiers)
409 template<typename _Res, typename _T1>
410 struct _Reference_wrapper_base<_Res (_T1::*)()>
411 : unary_function<_T1*, _Res>
414 // - a pointer to member function type (binary, no qualifiers)
415 template<typename _Res, typename _T1, typename _T2>
416 struct _Reference_wrapper_base<_Res (_T1::*)(_T2)>
417 : binary_function<_T1*, _T2, _Res>
420 // - a pointer to member function type (unary, const)
421 template<typename _Res, typename _T1>
422 struct _Reference_wrapper_base<_Res (_T1::*)() const>
423 : unary_function<const _T1*, _Res>
426 // - a pointer to member function type (binary, const)
427 template<typename _Res, typename _T1, typename _T2>
428 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const>
429 : binary_function<const _T1*, _T2, _Res>
432 // - a pointer to member function type (unary, volatile)
433 template<typename _Res, typename _T1>
434 struct _Reference_wrapper_base<_Res (_T1::*)() volatile>
435 : unary_function<volatile _T1*, _Res>
438 // - a pointer to member function type (binary, volatile)
439 template<typename _Res, typename _T1, typename _T2>
440 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) volatile>
441 : binary_function<volatile _T1*, _T2, _Res>
444 // - a pointer to member function type (unary, const volatile)
445 template<typename _Res, typename _T1>
446 struct _Reference_wrapper_base<_Res (_T1::*)() const volatile>
447 : unary_function<const volatile _T1*, _Res>
450 // - a pointer to member function type (binary, const volatile)
451 template<typename _Res, typename _T1, typename _T2>
452 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const volatile>
453 : binary_function<const volatile _T1*, _T2, _Res>
456 /// reference_wrapper
457 template<typename _Tp>
458 class reference_wrapper
459 : public _Reference_wrapper_base<typename remove_cv<_Tp>::type>
461 // If _Tp is a function type, we can't form result_of<_Tp(...)>,
462 // so turn it into a function pointer type.
463 typedef typename _Function_to_function_pointer<_Tp>::type
471 reference_wrapper(_Tp& __indata)
472 : _M_data(std::__addressof(__indata))
475 reference_wrapper(const reference_wrapper<_Tp>& __inref):
476 _M_data(__inref._M_data)
480 operator=(const reference_wrapper<_Tp>& __inref)
482 _M_data = __inref._M_data;
486 operator _Tp&() const
487 { return this->get(); }
493 template<typename... _Args>
494 typename result_of<_M_func_type(_Args...)>::type
495 operator()(_Args&... __args) const
497 return __invoke(get(), __args...);
502 // Denotes a reference should be taken to a variable.
503 template<typename _Tp>
504 inline reference_wrapper<_Tp>
506 { return reference_wrapper<_Tp>(__t); }
508 // Denotes a const reference should be taken to a variable.
509 template<typename _Tp>
510 inline reference_wrapper<const _Tp>
512 { return reference_wrapper<const _Tp>(__t); }
514 template<typename _Tp>
515 inline reference_wrapper<_Tp>
516 ref(reference_wrapper<_Tp> __t)
517 { return ref(__t.get()); }
519 template<typename _Tp>
520 inline reference_wrapper<const _Tp>
521 cref(reference_wrapper<_Tp> __t)
522 { return cref(__t.get()); }
524 template<typename _Tp, bool>
525 struct _Mem_fn_const_or_non
527 typedef const _Tp& type;
530 template<typename _Tp>
531 struct _Mem_fn_const_or_non<_Tp, false>
537 * Derives from @c unary_function or @c binary_function, or perhaps
538 * nothing, depending on the number of arguments provided. The
539 * primary template is the basis case, which derives nothing.
541 template<typename _Res, typename... _ArgTypes>
542 struct _Maybe_unary_or_binary_function { };
544 /// Derives from @c unary_function, as appropriate.
545 template<typename _Res, typename _T1>
546 struct _Maybe_unary_or_binary_function<_Res, _T1>
547 : std::unary_function<_T1, _Res> { };
549 /// Derives from @c binary_function, as appropriate.
550 template<typename _Res, typename _T1, typename _T2>
551 struct _Maybe_unary_or_binary_function<_Res, _T1, _T2>
552 : std::binary_function<_T1, _T2, _Res> { };
554 /// Implementation of @c mem_fn for member function pointers.
555 template<typename _Res, typename _Class, typename... _ArgTypes>
556 class _Mem_fn<_Res (_Class::*)(_ArgTypes...)>
557 : public _Maybe_unary_or_binary_function<_Res, _Class*, _ArgTypes...>
559 typedef _Res (_Class::*_Functor)(_ArgTypes...);
561 template<typename _Tp>
563 _M_call(_Tp& __object, const volatile _Class *,
564 _ArgTypes... __args) const
565 { return (__object.*__pmf)(__args...); }
567 template<typename _Tp>
569 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
570 { return ((*__ptr).*__pmf)(__args...); }
573 typedef _Res result_type;
575 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
579 operator()(_Class& __object, _ArgTypes... __args) const
580 { return (__object.*__pmf)(__args...); }
584 operator()(_Class* __object, _ArgTypes... __args) const
585 { return (__object->*__pmf)(__args...); }
587 // Handle smart pointers, references and pointers to derived
588 template<typename _Tp>
590 operator()(_Tp& __object, _ArgTypes... __args) const
591 { return _M_call(__object, &__object, __args...); }
597 /// Implementation of @c mem_fn for const member function pointers.
598 template<typename _Res, typename _Class, typename... _ArgTypes>
599 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) const>
600 : public _Maybe_unary_or_binary_function<_Res, const _Class*,
603 typedef _Res (_Class::*_Functor)(_ArgTypes...) const;
605 template<typename _Tp>
607 _M_call(_Tp& __object, const volatile _Class *,
608 _ArgTypes... __args) const
609 { return (__object.*__pmf)(__args...); }
611 template<typename _Tp>
613 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
614 { return ((*__ptr).*__pmf)(__args...); }
617 typedef _Res result_type;
619 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
623 operator()(const _Class& __object, _ArgTypes... __args) const
624 { return (__object.*__pmf)(__args...); }
628 operator()(const _Class* __object, _ArgTypes... __args) const
629 { return (__object->*__pmf)(__args...); }
631 // Handle smart pointers, references and pointers to derived
632 template<typename _Tp>
633 _Res operator()(_Tp& __object, _ArgTypes... __args) const
634 { return _M_call(__object, &__object, __args...); }
640 /// Implementation of @c mem_fn for volatile member function pointers.
641 template<typename _Res, typename _Class, typename... _ArgTypes>
642 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) volatile>
643 : public _Maybe_unary_or_binary_function<_Res, volatile _Class*,
646 typedef _Res (_Class::*_Functor)(_ArgTypes...) volatile;
648 template<typename _Tp>
650 _M_call(_Tp& __object, const volatile _Class *,
651 _ArgTypes... __args) const
652 { return (__object.*__pmf)(__args...); }
654 template<typename _Tp>
656 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
657 { return ((*__ptr).*__pmf)(__args...); }
660 typedef _Res result_type;
662 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
666 operator()(volatile _Class& __object, _ArgTypes... __args) const
667 { return (__object.*__pmf)(__args...); }
671 operator()(volatile _Class* __object, _ArgTypes... __args) const
672 { return (__object->*__pmf)(__args...); }
674 // Handle smart pointers, references and pointers to derived
675 template<typename _Tp>
677 operator()(_Tp& __object, _ArgTypes... __args) const
678 { return _M_call(__object, &__object, __args...); }
684 /// Implementation of @c mem_fn for const volatile member function pointers.
685 template<typename _Res, typename _Class, typename... _ArgTypes>
686 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) const volatile>
687 : public _Maybe_unary_or_binary_function<_Res, const volatile _Class*,
690 typedef _Res (_Class::*_Functor)(_ArgTypes...) const volatile;
692 template<typename _Tp>
694 _M_call(_Tp& __object, const volatile _Class *,
695 _ArgTypes... __args) const
696 { return (__object.*__pmf)(__args...); }
698 template<typename _Tp>
700 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
701 { return ((*__ptr).*__pmf)(__args...); }
704 typedef _Res result_type;
706 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
710 operator()(const volatile _Class& __object, _ArgTypes... __args) const
711 { return (__object.*__pmf)(__args...); }
715 operator()(const volatile _Class* __object, _ArgTypes... __args) const
716 { return (__object->*__pmf)(__args...); }
718 // Handle smart pointers, references and pointers to derived
719 template<typename _Tp>
720 _Res operator()(_Tp& __object, _ArgTypes... __args) const
721 { return _M_call(__object, &__object, __args...); }
728 template<typename _Res, typename _Class>
729 class _Mem_fn<_Res _Class::*>
731 // This bit of genius is due to Peter Dimov, improved slightly by
733 template<typename _Tp>
735 _M_call(_Tp& __object, _Class *) const
736 { return __object.*__pm; }
738 template<typename _Tp, typename _Up>
740 _M_call(_Tp& __object, _Up * const *) const
741 { return (*__object).*__pm; }
743 template<typename _Tp, typename _Up>
745 _M_call(_Tp& __object, const _Up * const *) const
746 { return (*__object).*__pm; }
748 template<typename _Tp>
750 _M_call(_Tp& __object, const _Class *) const
751 { return __object.*__pm; }
753 template<typename _Tp>
755 _M_call(_Tp& __ptr, const volatile void*) const
756 { return (*__ptr).*__pm; }
758 template<typename _Tp> static _Tp& __get_ref();
760 template<typename _Tp>
761 static __sfinae_types::__one __check_const(_Tp&, _Class*);
762 template<typename _Tp, typename _Up>
763 static __sfinae_types::__one __check_const(_Tp&, _Up * const *);
764 template<typename _Tp, typename _Up>
765 static __sfinae_types::__two __check_const(_Tp&, const _Up * const *);
766 template<typename _Tp>
767 static __sfinae_types::__two __check_const(_Tp&, const _Class*);
768 template<typename _Tp>
769 static __sfinae_types::__two __check_const(_Tp&, const volatile void*);
772 template<typename _Tp>
774 : _Mem_fn_const_or_non<_Res,
775 (sizeof(__sfinae_types::__two)
776 == sizeof(__check_const<_Tp>(__get_ref<_Tp>(), (_Tp*)0)))>
779 template<typename _Signature>
782 template<typename _CVMem, typename _Tp>
783 struct result<_CVMem(_Tp)>
784 : public _Result_type<_Tp> { };
786 template<typename _CVMem, typename _Tp>
787 struct result<_CVMem(_Tp&)>
788 : public _Result_type<_Tp> { };
791 _Mem_fn(_Res _Class::*__pm) : __pm(__pm) { }
795 operator()(_Class& __object) const
796 { return __object.*__pm; }
799 operator()(const _Class& __object) const
800 { return __object.*__pm; }
804 operator()(_Class* __object) const
805 { return __object->*__pm; }
808 operator()(const _Class* __object) const
809 { return __object->*__pm; }
811 // Handle smart pointers and derived
812 template<typename _Tp>
813 typename _Result_type<_Tp>::type
814 operator()(_Tp& __unknown) const
815 { return _M_call(__unknown, &__unknown); }
822 * @brief Returns a function object that forwards to the member
825 template<typename _Tp, typename _Class>
826 inline _Mem_fn<_Tp _Class::*>
827 mem_fn(_Tp _Class::* __pm)
829 return _Mem_fn<_Tp _Class::*>(__pm);
833 * @brief Determines if the given type _Tp is a function object
834 * should be treated as a subexpression when evaluating calls to
835 * function objects returned by bind(). [TR1 3.6.1]
837 template<typename _Tp>
838 struct is_bind_expression
839 { static const bool value = false; };
841 template<typename _Tp>
842 const bool is_bind_expression<_Tp>::value;
845 * @brief Determines if the given type _Tp is a placeholder in a
846 * bind() expression and, if so, which placeholder it is. [TR1 3.6.2]
848 template<typename _Tp>
849 struct is_placeholder
850 { static const int value = 0; };
852 template<typename _Tp>
853 const int is_placeholder<_Tp>::value;
855 /// The type of placeholder objects defined by libstdc++.
856 template<int _Num> struct _Placeholder { };
858 _GLIBCXX_END_NAMESPACE_VERSION
860 /** @namespace std::tr1::placeholders
861 * @brief Sub-namespace for tr1/functional.
863 namespace placeholders
865 _GLIBCXX_BEGIN_NAMESPACE_VERSION
866 /* Define a large number of placeholders. There is no way to
867 * simplify this with variadic templates, because we're introducing
868 * unique names for each.
881 _Placeholder<10> _10;
882 _Placeholder<11> _11;
883 _Placeholder<12> _12;
884 _Placeholder<13> _13;
885 _Placeholder<14> _14;
886 _Placeholder<15> _15;
887 _Placeholder<16> _16;
888 _Placeholder<17> _17;
889 _Placeholder<18> _18;
890 _Placeholder<19> _19;
891 _Placeholder<20> _20;
892 _Placeholder<21> _21;
893 _Placeholder<22> _22;
894 _Placeholder<23> _23;
895 _Placeholder<24> _24;
896 _Placeholder<25> _25;
897 _Placeholder<26> _26;
898 _Placeholder<27> _27;
899 _Placeholder<28> _28;
900 _Placeholder<29> _29;
902 _GLIBCXX_END_NAMESPACE_VERSION
905 _GLIBCXX_BEGIN_NAMESPACE_VERSION
907 * Partial specialization of is_placeholder that provides the placeholder
908 * number for the placeholder objects defined by libstdc++.
911 struct is_placeholder<_Placeholder<_Num> >
912 { static const int value = _Num; };
915 const int is_placeholder<_Placeholder<_Num> >::value;
917 #if __cplusplus >= 201103L
919 struct is_placeholder<std::_Placeholder<_Num>>
920 : std::integral_constant<int, _Num>
924 struct is_placeholder<const std::_Placeholder<_Num>>
925 : std::integral_constant<int, _Num>
930 * Stores a tuple of indices. Used by bind() to extract the elements
933 template<int... _Indexes>
934 struct _Index_tuple { };
936 /// Builds an _Index_tuple<0, 1, 2, ..., _Num-1>.
937 template<std::size_t _Num, typename _Tuple = _Index_tuple<> >
938 struct _Build_index_tuple;
940 template<std::size_t _Num, int... _Indexes>
941 struct _Build_index_tuple<_Num, _Index_tuple<_Indexes...> >
942 : _Build_index_tuple<_Num - 1,
943 _Index_tuple<_Indexes..., sizeof...(_Indexes)> >
947 template<int... _Indexes>
948 struct _Build_index_tuple<0, _Index_tuple<_Indexes...> >
950 typedef _Index_tuple<_Indexes...> __type;
954 * Used by _Safe_tuple_element to indicate that there is no tuple
955 * element at this position.
957 struct _No_tuple_element;
960 * Implementation helper for _Safe_tuple_element. This primary
961 * template handles the case where it is safe to use @c
964 template<int __i, typename _Tuple, bool _IsSafe>
965 struct _Safe_tuple_element_impl
966 : tuple_element<__i, _Tuple> { };
969 * Implementation helper for _Safe_tuple_element. This partial
970 * specialization handles the case where it is not safe to use @c
971 * tuple_element. We just return @c _No_tuple_element.
973 template<int __i, typename _Tuple>
974 struct _Safe_tuple_element_impl<__i, _Tuple, false>
976 typedef _No_tuple_element type;
980 * Like tuple_element, but returns @c _No_tuple_element when
981 * tuple_element would return an error.
983 template<int __i, typename _Tuple>
984 struct _Safe_tuple_element
985 : _Safe_tuple_element_impl<__i, _Tuple,
986 (__i >= 0 && __i < tuple_size<_Tuple>::value)>
991 * Maps an argument to bind() into an actual argument to the bound
992 * function object [TR1 3.6.3/5]. Only the first parameter should
993 * be specified: the rest are used to determine among the various
994 * implementations. Note that, although this class is a function
995 * object, it isn't entirely normal because it takes only two
996 * parameters regardless of the number of parameters passed to the
997 * bind expression. The first parameter is the bound argument and
998 * the second parameter is a tuple containing references to the
999 * rest of the arguments.
1001 template<typename _Arg,
1002 bool _IsBindExp = is_bind_expression<_Arg>::value,
1003 bool _IsPlaceholder = (is_placeholder<_Arg>::value > 0)>
1007 * If the argument is reference_wrapper<_Tp>, returns the
1008 * underlying reference. [TR1 3.6.3/5 bullet 1]
1010 template<typename _Tp>
1011 class _Mu<reference_wrapper<_Tp>, false, false>
1014 typedef _Tp& result_type;
1016 /* Note: This won't actually work for const volatile
1017 * reference_wrappers, because reference_wrapper::get() is const
1018 * but not volatile-qualified. This might be a defect in the TR.
1020 template<typename _CVRef, typename _Tuple>
1022 operator()(_CVRef& __arg, const _Tuple&) const volatile
1023 { return __arg.get(); }
1027 * If the argument is a bind expression, we invoke the underlying
1028 * function object with the same cv-qualifiers as we are given and
1029 * pass along all of our arguments (unwrapped). [TR1 3.6.3/5 bullet 2]
1031 template<typename _Arg>
1032 class _Mu<_Arg, true, false>
1035 template<typename _Signature> class result;
1037 // Determine the result type when we pass the arguments along. This
1038 // involves passing along the cv-qualifiers placed on _Mu and
1039 // unwrapping the argument bundle.
1040 template<typename _CVMu, typename _CVArg, typename... _Args>
1041 class result<_CVMu(_CVArg, tuple<_Args...>)>
1042 : public result_of<_CVArg(_Args...)> { };
1044 template<typename _CVArg, typename... _Args>
1045 typename result_of<_CVArg(_Args...)>::type
1046 operator()(_CVArg& __arg,
1047 const tuple<_Args...>& __tuple) const volatile
1049 // Construct an index tuple and forward to __call
1050 typedef typename _Build_index_tuple<sizeof...(_Args)>::__type
1052 return this->__call(__arg, __tuple, _Indexes());
1056 // Invokes the underlying function object __arg by unpacking all
1057 // of the arguments in the tuple.
1058 template<typename _CVArg, typename... _Args, int... _Indexes>
1059 typename result_of<_CVArg(_Args...)>::type
1060 __call(_CVArg& __arg, const tuple<_Args...>& __tuple,
1061 const _Index_tuple<_Indexes...>&) const volatile
1063 return __arg(tr1::get<_Indexes>(__tuple)...);
1068 * If the argument is a placeholder for the Nth argument, returns
1069 * a reference to the Nth argument to the bind function object.
1070 * [TR1 3.6.3/5 bullet 3]
1072 template<typename _Arg>
1073 class _Mu<_Arg, false, true>
1076 template<typename _Signature> class result;
1078 template<typename _CVMu, typename _CVArg, typename _Tuple>
1079 class result<_CVMu(_CVArg, _Tuple)>
1081 // Add a reference, if it hasn't already been done for us.
1082 // This allows us to be a little bit sloppy in constructing
1083 // the tuple that we pass to result_of<...>.
1084 typedef typename _Safe_tuple_element<(is_placeholder<_Arg>::value
1089 typedef typename add_reference<__base_type>::type type;
1092 template<typename _Tuple>
1093 typename result<_Mu(_Arg, _Tuple)>::type
1094 operator()(const volatile _Arg&, const _Tuple& __tuple) const volatile
1096 return ::std::tr1::get<(is_placeholder<_Arg>::value - 1)>(__tuple);
1101 * If the argument is just a value, returns a reference to that
1102 * value. The cv-qualifiers on the reference are the same as the
1103 * cv-qualifiers on the _Mu object. [TR1 3.6.3/5 bullet 4]
1105 template<typename _Arg>
1106 class _Mu<_Arg, false, false>
1109 template<typename _Signature> struct result;
1111 template<typename _CVMu, typename _CVArg, typename _Tuple>
1112 struct result<_CVMu(_CVArg, _Tuple)>
1114 typedef typename add_reference<_CVArg>::type type;
1117 // Pick up the cv-qualifiers of the argument
1118 template<typename _CVArg, typename _Tuple>
1120 operator()(_CVArg& __arg, const _Tuple&) const volatile
1125 * Maps member pointers into instances of _Mem_fn but leaves all
1126 * other function objects untouched. Used by tr1::bind(). The
1127 * primary template handles the non--member-pointer case.
1129 template<typename _Tp>
1130 struct _Maybe_wrap_member_pointer
1135 __do_wrap(const _Tp& __x)
1140 * Maps member pointers into instances of _Mem_fn but leaves all
1141 * other function objects untouched. Used by tr1::bind(). This
1142 * partial specialization handles the member pointer case.
1144 template<typename _Tp, typename _Class>
1145 struct _Maybe_wrap_member_pointer<_Tp _Class::*>
1147 typedef _Mem_fn<_Tp _Class::*> type;
1150 __do_wrap(_Tp _Class::* __pm)
1151 { return type(__pm); }
1154 /// Type of the function object returned from bind().
1155 template<typename _Signature>
1158 template<typename _Functor, typename... _Bound_args>
1159 class _Bind<_Functor(_Bound_args...)>
1160 : public _Weak_result_type<_Functor>
1162 typedef _Bind __self_type;
1163 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
1167 tuple<_Bound_args...> _M_bound_args;
1170 template<typename... _Args, int... _Indexes>
1172 _Functor(typename result_of<_Mu<_Bound_args>
1173 (_Bound_args, tuple<_Args...>)>::type...)
1175 __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>)
1177 return _M_f(_Mu<_Bound_args>()
1178 (tr1::get<_Indexes>(_M_bound_args), __args)...);
1182 template<typename... _Args, int... _Indexes>
1184 const _Functor(typename result_of<_Mu<_Bound_args>
1185 (const _Bound_args, tuple<_Args...>)
1187 __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>) const
1189 return _M_f(_Mu<_Bound_args>()
1190 (tr1::get<_Indexes>(_M_bound_args), __args)...);
1194 template<typename... _Args, int... _Indexes>
1196 volatile _Functor(typename result_of<_Mu<_Bound_args>
1197 (volatile _Bound_args, tuple<_Args...>)
1199 __call(const tuple<_Args...>& __args,
1200 _Index_tuple<_Indexes...>) volatile
1202 return _M_f(_Mu<_Bound_args>()
1203 (tr1::get<_Indexes>(_M_bound_args), __args)...);
1206 // Call as const volatile
1207 template<typename... _Args, int... _Indexes>
1209 const volatile _Functor(typename result_of<_Mu<_Bound_args>
1210 (const volatile _Bound_args,
1213 __call(const tuple<_Args...>& __args,
1214 _Index_tuple<_Indexes...>) const volatile
1216 return _M_f(_Mu<_Bound_args>()
1217 (tr1::get<_Indexes>(_M_bound_args), __args)...);
1221 explicit _Bind(_Functor __f, _Bound_args... __bound_args)
1222 : _M_f(__f), _M_bound_args(__bound_args...) { }
1225 template<typename... _Args>
1227 _Functor(typename result_of<_Mu<_Bound_args>
1228 (_Bound_args, tuple<_Args...>)>::type...)
1230 operator()(_Args&... __args)
1232 return this->__call(tr1::tie(__args...), _Bound_indexes());
1236 template<typename... _Args>
1238 const _Functor(typename result_of<_Mu<_Bound_args>
1239 (const _Bound_args, tuple<_Args...>)>::type...)
1241 operator()(_Args&... __args) const
1243 return this->__call(tr1::tie(__args...), _Bound_indexes());
1248 template<typename... _Args>
1250 volatile _Functor(typename result_of<_Mu<_Bound_args>
1251 (volatile _Bound_args, tuple<_Args...>)>::type...)
1253 operator()(_Args&... __args) volatile
1255 return this->__call(tr1::tie(__args...), _Bound_indexes());
1259 // Call as const volatile
1260 template<typename... _Args>
1262 const volatile _Functor(typename result_of<_Mu<_Bound_args>
1263 (const volatile _Bound_args,
1264 tuple<_Args...>)>::type...)
1266 operator()(_Args&... __args) const volatile
1268 return this->__call(tr1::tie(__args...), _Bound_indexes());
1272 /// Type of the function object returned from bind<R>().
1273 template<typename _Result, typename _Signature>
1274 struct _Bind_result;
1276 template<typename _Result, typename _Functor, typename... _Bound_args>
1277 class _Bind_result<_Result, _Functor(_Bound_args...)>
1279 typedef _Bind_result __self_type;
1280 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
1284 tuple<_Bound_args...> _M_bound_args;
1287 template<typename... _Args, int... _Indexes>
1289 __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>)
1291 return _M_f(_Mu<_Bound_args>()
1292 (tr1::get<_Indexes>(_M_bound_args), __args)...);
1296 template<typename... _Args, int... _Indexes>
1298 __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>) const
1300 return _M_f(_Mu<_Bound_args>()
1301 (tr1::get<_Indexes>(_M_bound_args), __args)...);
1305 template<typename... _Args, int... _Indexes>
1307 __call(const tuple<_Args...>& __args,
1308 _Index_tuple<_Indexes...>) volatile
1310 return _M_f(_Mu<_Bound_args>()
1311 (tr1::get<_Indexes>(_M_bound_args), __args)...);
1314 // Call as const volatile
1315 template<typename... _Args, int... _Indexes>
1317 __call(const tuple<_Args...>& __args,
1318 _Index_tuple<_Indexes...>) const volatile
1320 return _M_f(_Mu<_Bound_args>()
1321 (tr1::get<_Indexes>(_M_bound_args), __args)...);
1325 typedef _Result result_type;
1328 _Bind_result(_Functor __f, _Bound_args... __bound_args)
1329 : _M_f(__f), _M_bound_args(__bound_args...) { }
1332 template<typename... _Args>
1334 operator()(_Args&... __args)
1336 return this->__call(tr1::tie(__args...), _Bound_indexes());
1340 template<typename... _Args>
1342 operator()(_Args&... __args) const
1344 return this->__call(tr1::tie(__args...), _Bound_indexes());
1348 template<typename... _Args>
1350 operator()(_Args&... __args) volatile
1352 return this->__call(tr1::tie(__args...), _Bound_indexes());
1355 // Call as const volatile
1356 template<typename... _Args>
1358 operator()(_Args&... __args) const volatile
1360 return this->__call(tr1::tie(__args...), _Bound_indexes());
1364 /// Class template _Bind is always a bind expression.
1365 template<typename _Signature>
1366 struct is_bind_expression<_Bind<_Signature> >
1367 { static const bool value = true; };
1369 template<typename _Signature>
1370 const bool is_bind_expression<_Bind<_Signature> >::value;
1372 /// Class template _Bind is always a bind expression.
1373 template<typename _Signature>
1374 struct is_bind_expression<const _Bind<_Signature> >
1375 { static const bool value = true; };
1377 template<typename _Signature>
1378 const bool is_bind_expression<const _Bind<_Signature> >::value;
1380 /// Class template _Bind is always a bind expression.
1381 template<typename _Signature>
1382 struct is_bind_expression<volatile _Bind<_Signature> >
1383 { static const bool value = true; };
1385 template<typename _Signature>
1386 const bool is_bind_expression<volatile _Bind<_Signature> >::value;
1388 /// Class template _Bind is always a bind expression.
1389 template<typename _Signature>
1390 struct is_bind_expression<const volatile _Bind<_Signature> >
1391 { static const bool value = true; };
1393 template<typename _Signature>
1394 const bool is_bind_expression<const volatile _Bind<_Signature> >::value;
1396 /// Class template _Bind_result is always a bind expression.
1397 template<typename _Result, typename _Signature>
1398 struct is_bind_expression<_Bind_result<_Result, _Signature> >
1399 { static const bool value = true; };
1401 template<typename _Result, typename _Signature>
1402 const bool is_bind_expression<_Bind_result<_Result, _Signature> >::value;
1404 /// Class template _Bind_result is always a bind expression.
1405 template<typename _Result, typename _Signature>
1406 struct is_bind_expression<const _Bind_result<_Result, _Signature> >
1407 { static const bool value = true; };
1409 template<typename _Result, typename _Signature>
1411 is_bind_expression<const _Bind_result<_Result, _Signature> >::value;
1413 /// Class template _Bind_result is always a bind expression.
1414 template<typename _Result, typename _Signature>
1415 struct is_bind_expression<volatile _Bind_result<_Result, _Signature> >
1416 { static const bool value = true; };
1418 template<typename _Result, typename _Signature>
1420 is_bind_expression<volatile _Bind_result<_Result, _Signature> >::value;
1422 /// Class template _Bind_result is always a bind expression.
1423 template<typename _Result, typename _Signature>
1425 is_bind_expression<const volatile _Bind_result<_Result, _Signature> >
1426 { static const bool value = true; };
1428 template<typename _Result, typename _Signature>
1430 is_bind_expression<const volatile _Bind_result<_Result,
1431 _Signature> >::value;
1433 #if __cplusplus >= 201103L
1434 template<typename _Signature>
1435 struct is_bind_expression<std::_Bind<_Signature>>
1438 template<typename _Signature>
1439 struct is_bind_expression<const std::_Bind<_Signature>>
1442 template<typename _Signature>
1443 struct is_bind_expression<volatile std::_Bind<_Signature>>
1446 template<typename _Signature>
1447 struct is_bind_expression<const volatile std::_Bind<_Signature>>
1450 template<typename _Result, typename _Signature>
1451 struct is_bind_expression<std::_Bind_result<_Result, _Signature>>
1454 template<typename _Result, typename _Signature>
1455 struct is_bind_expression<const std::_Bind_result<_Result, _Signature>>
1458 template<typename _Result, typename _Signature>
1459 struct is_bind_expression<volatile std::_Bind_result<_Result, _Signature>>
1462 template<typename _Result, typename _Signature>
1463 struct is_bind_expression<const volatile std::_Bind_result<_Result,
1469 template<typename _Functor, typename... _ArgTypes>
1471 _Bind<typename _Maybe_wrap_member_pointer<_Functor>::type(_ArgTypes...)>
1472 bind(_Functor __f, _ArgTypes... __args)
1474 typedef _Maybe_wrap_member_pointer<_Functor> __maybe_type;
1475 typedef typename __maybe_type::type __functor_type;
1476 typedef _Bind<__functor_type(_ArgTypes...)> __result_type;
1477 return __result_type(__maybe_type::__do_wrap(__f), __args...);
1480 template<typename _Result, typename _Functor, typename... _ArgTypes>
1482 _Bind_result<_Result,
1483 typename _Maybe_wrap_member_pointer<_Functor>::type
1485 bind(_Functor __f, _ArgTypes... __args)
1487 typedef _Maybe_wrap_member_pointer<_Functor> __maybe_type;
1488 typedef typename __maybe_type::type __functor_type;
1489 typedef _Bind_result<_Result, __functor_type(_ArgTypes...)>
1491 return __result_type(__maybe_type::__do_wrap(__f), __args...);
1495 * @brief Exception class thrown when class template function's
1496 * operator() is called with an empty target.
1497 * @ingroup exceptions
1499 class bad_function_call : public std::exception { };
1502 * The integral constant expression 0 can be converted into a
1503 * pointer to this type. It is used by the function template to
1504 * accept NULL pointers.
1506 struct _M_clear_type;
1509 * Trait identifying @a location-invariant types, meaning that the
1510 * address of the object (or any of its members) will not escape.
1511 * Also implies a trivial copy constructor and assignment operator.
1513 template<typename _Tp>
1514 struct __is_location_invariant
1515 : integral_constant<bool,
1516 (is_pointer<_Tp>::value
1517 || is_member_pointer<_Tp>::value)>
1521 class _Undefined_class;
1526 const void* _M_const_object;
1527 void (*_M_function_pointer)();
1528 void (_Undefined_class::*_M_member_pointer)();
1533 void* _M_access() { return &_M_pod_data[0]; }
1534 const void* _M_access() const { return &_M_pod_data[0]; }
1536 template<typename _Tp>
1539 { return *static_cast<_Tp*>(_M_access()); }
1541 template<typename _Tp>
1544 { return *static_cast<const _Tp*>(_M_access()); }
1546 _Nocopy_types _M_unused;
1547 char _M_pod_data[sizeof(_Nocopy_types)];
1550 enum _Manager_operation
1558 // Simple type wrapper that helps avoid annoying const problems
1559 // when casting between void pointers and pointers-to-pointers.
1560 template<typename _Tp>
1561 struct _Simple_type_wrapper
1563 _Simple_type_wrapper(_Tp __value) : __value(__value) { }
1568 template<typename _Tp>
1569 struct __is_location_invariant<_Simple_type_wrapper<_Tp> >
1570 : __is_location_invariant<_Tp>
1574 // Converts a reference to a function object into a callable
1576 template<typename _Functor>
1578 __callable_functor(_Functor& __f)
1581 template<typename _Member, typename _Class>
1582 inline _Mem_fn<_Member _Class::*>
1583 __callable_functor(_Member _Class::* &__p)
1584 { return mem_fn(__p); }
1586 template<typename _Member, typename _Class>
1587 inline _Mem_fn<_Member _Class::*>
1588 __callable_functor(_Member _Class::* const &__p)
1589 { return mem_fn(__p); }
1591 template<typename _Signature>
1594 /// Base class of all polymorphic function object wrappers.
1595 class _Function_base
1598 static const std::size_t _M_max_size = sizeof(_Nocopy_types);
1599 static const std::size_t _M_max_align = __alignof__(_Nocopy_types);
1601 template<typename _Functor>
1605 static const bool __stored_locally =
1606 (__is_location_invariant<_Functor>::value
1607 && sizeof(_Functor) <= _M_max_size
1608 && __alignof__(_Functor) <= _M_max_align
1609 && (_M_max_align % __alignof__(_Functor) == 0));
1611 typedef integral_constant<bool, __stored_locally> _Local_storage;
1613 // Retrieve a pointer to the function object
1615 _M_get_pointer(const _Any_data& __source)
1617 const _Functor* __ptr =
1618 __stored_locally? std::__addressof(__source._M_access<_Functor>())
1619 /* have stored a pointer */ : __source._M_access<_Functor*>();
1620 return const_cast<_Functor*>(__ptr);
1623 // Clone a location-invariant function object that fits within
1624 // an _Any_data structure.
1626 _M_clone(_Any_data& __dest, const _Any_data& __source, true_type)
1628 new (__dest._M_access()) _Functor(__source._M_access<_Functor>());
1631 // Clone a function object that is not location-invariant or
1632 // that cannot fit into an _Any_data structure.
1634 _M_clone(_Any_data& __dest, const _Any_data& __source, false_type)
1636 __dest._M_access<_Functor*>() =
1637 new _Functor(*__source._M_access<_Functor*>());
1640 // Destroying a location-invariant object may still require
1643 _M_destroy(_Any_data& __victim, true_type)
1645 __victim._M_access<_Functor>().~_Functor();
1648 // Destroying an object located on the heap.
1650 _M_destroy(_Any_data& __victim, false_type)
1652 delete __victim._M_access<_Functor*>();
1657 _M_manager(_Any_data& __dest, const _Any_data& __source,
1658 _Manager_operation __op)
1663 case __get_type_info:
1664 __dest._M_access<const type_info*>() = &typeid(_Functor);
1667 case __get_functor_ptr:
1668 __dest._M_access<_Functor*>() = _M_get_pointer(__source);
1671 case __clone_functor:
1672 _M_clone(__dest, __source, _Local_storage());
1675 case __destroy_functor:
1676 _M_destroy(__dest, _Local_storage());
1683 _M_init_functor(_Any_data& __functor, const _Functor& __f)
1684 { _M_init_functor(__functor, __f, _Local_storage()); }
1686 template<typename _Signature>
1688 _M_not_empty_function(const function<_Signature>& __f)
1689 { return static_cast<bool>(__f); }
1691 template<typename _Tp>
1693 _M_not_empty_function(const _Tp*& __fp)
1696 template<typename _Class, typename _Tp>
1698 _M_not_empty_function(_Tp _Class::* const& __mp)
1701 template<typename _Tp>
1703 _M_not_empty_function(const _Tp&)
1708 _M_init_functor(_Any_data& __functor, const _Functor& __f, true_type)
1709 { new (__functor._M_access()) _Functor(__f); }
1712 _M_init_functor(_Any_data& __functor, const _Functor& __f, false_type)
1713 { __functor._M_access<_Functor*>() = new _Functor(__f); }
1716 template<typename _Functor>
1717 class _Ref_manager : public _Base_manager<_Functor*>
1719 typedef _Function_base::_Base_manager<_Functor*> _Base;
1723 _M_manager(_Any_data& __dest, const _Any_data& __source,
1724 _Manager_operation __op)
1729 case __get_type_info:
1730 __dest._M_access<const type_info*>() = &typeid(_Functor);
1733 case __get_functor_ptr:
1734 __dest._M_access<_Functor*>() = *_Base::_M_get_pointer(__source);
1735 return is_const<_Functor>::value;
1739 _Base::_M_manager(__dest, __source, __op);
1745 _M_init_functor(_Any_data& __functor, reference_wrapper<_Functor> __f)
1747 _Base::_M_init_functor(__functor, std::__addressof(__f.get()));
1751 _Function_base() : _M_manager(0) { }
1756 _M_manager(_M_functor, _M_functor, __destroy_functor);
1760 bool _M_empty() const { return !_M_manager; }
1762 typedef bool (*_Manager_type)(_Any_data&, const _Any_data&,
1763 _Manager_operation);
1765 _Any_data _M_functor;
1766 _Manager_type _M_manager;
1769 template<typename _Signature, typename _Functor>
1770 class _Function_handler;
1772 template<typename _Res, typename _Functor, typename... _ArgTypes>
1773 class _Function_handler<_Res(_ArgTypes...), _Functor>
1774 : public _Function_base::_Base_manager<_Functor>
1776 typedef _Function_base::_Base_manager<_Functor> _Base;
1780 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1782 return (*_Base::_M_get_pointer(__functor))(__args...);
1786 template<typename _Functor, typename... _ArgTypes>
1787 class _Function_handler<void(_ArgTypes...), _Functor>
1788 : public _Function_base::_Base_manager<_Functor>
1790 typedef _Function_base::_Base_manager<_Functor> _Base;
1794 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1796 (*_Base::_M_get_pointer(__functor))(__args...);
1800 template<typename _Res, typename _Functor, typename... _ArgTypes>
1801 class _Function_handler<_Res(_ArgTypes...), reference_wrapper<_Functor> >
1802 : public _Function_base::_Ref_manager<_Functor>
1804 typedef _Function_base::_Ref_manager<_Functor> _Base;
1808 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1811 __callable_functor(**_Base::_M_get_pointer(__functor))(__args...);
1815 template<typename _Functor, typename... _ArgTypes>
1816 class _Function_handler<void(_ArgTypes...), reference_wrapper<_Functor> >
1817 : public _Function_base::_Ref_manager<_Functor>
1819 typedef _Function_base::_Ref_manager<_Functor> _Base;
1823 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1825 __callable_functor(**_Base::_M_get_pointer(__functor))(__args...);
1829 template<typename _Class, typename _Member, typename _Res,
1830 typename... _ArgTypes>
1831 class _Function_handler<_Res(_ArgTypes...), _Member _Class::*>
1832 : public _Function_handler<void(_ArgTypes...), _Member _Class::*>
1834 typedef _Function_handler<void(_ArgTypes...), _Member _Class::*>
1839 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1842 mem_fn(_Base::_M_get_pointer(__functor)->__value)(__args...);
1846 template<typename _Class, typename _Member, typename... _ArgTypes>
1847 class _Function_handler<void(_ArgTypes...), _Member _Class::*>
1848 : public _Function_base::_Base_manager<
1849 _Simple_type_wrapper< _Member _Class::* > >
1851 typedef _Member _Class::* _Functor;
1852 typedef _Simple_type_wrapper<_Functor> _Wrapper;
1853 typedef _Function_base::_Base_manager<_Wrapper> _Base;
1857 _M_manager(_Any_data& __dest, const _Any_data& __source,
1858 _Manager_operation __op)
1863 case __get_type_info:
1864 __dest._M_access<const type_info*>() = &typeid(_Functor);
1867 case __get_functor_ptr:
1868 __dest._M_access<_Functor*>() =
1869 &_Base::_M_get_pointer(__source)->__value;
1873 _Base::_M_manager(__dest, __source, __op);
1879 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1881 tr1::mem_fn(_Base::_M_get_pointer(__functor)->__value)(__args...);
1886 template<typename _Res, typename... _ArgTypes>
1887 class function<_Res(_ArgTypes...)>
1888 : public _Maybe_unary_or_binary_function<_Res, _ArgTypes...>,
1889 private _Function_base
1891 #if __cplusplus < 201103L
1892 /// This class is used to implement the safe_bool idiom.
1895 _Hidden_type* _M_bool;
1898 /// This typedef is used to implement the safe_bool idiom.
1899 typedef _Hidden_type* _Hidden_type::* _Safe_bool;
1902 typedef _Res _Signature_type(_ArgTypes...);
1904 struct _Useless { };
1907 typedef _Res result_type;
1909 // [3.7.2.1] construct/copy/destroy
1912 * @brief Default construct creates an empty function call wrapper.
1913 * @post @c !(bool)*this
1915 function() : _Function_base() { }
1918 * @brief Default construct creates an empty function call wrapper.
1919 * @post @c !(bool)*this
1921 function(_M_clear_type*) : _Function_base() { }
1924 * @brief %Function copy constructor.
1925 * @param x A %function object with identical call signature.
1926 * @post @c (bool)*this == (bool)x
1928 * The newly-created %function contains a copy of the target of @a
1929 * x (if it has one).
1931 function(const function& __x);
1934 * @brief Builds a %function that targets a copy of the incoming
1936 * @param f A %function object that is callable with parameters of
1937 * type @c T1, @c T2, ..., @c TN and returns a value convertible
1940 * The newly-created %function object will target a copy of @a
1941 * f. If @a f is @c reference_wrapper<F>, then this function
1942 * object will contain a reference to the function object @c
1943 * f.get(). If @a f is a NULL function pointer or NULL
1944 * pointer-to-member, the newly-created object will be empty.
1946 * If @a f is a non-NULL function pointer or an object of type @c
1947 * reference_wrapper<F>, this function will not throw.
1949 template<typename _Functor>
1950 function(_Functor __f,
1951 typename __gnu_cxx::__enable_if<
1952 !is_integral<_Functor>::value, _Useless>::__type
1956 * @brief %Function assignment operator.
1957 * @param x A %function with identical call signature.
1958 * @post @c (bool)*this == (bool)x
1961 * The target of @a x is copied to @c *this. If @a x has no
1962 * target, then @c *this will be empty.
1964 * If @a x targets a function pointer or a reference to a function
1965 * object, then this operation will not throw an %exception.
1968 operator=(const function& __x)
1970 function(__x).swap(*this);
1975 * @brief %Function assignment to zero.
1976 * @post @c !(bool)*this
1979 * The target of @c *this is deallocated, leaving it empty.
1982 operator=(_M_clear_type*)
1986 _M_manager(_M_functor, _M_functor, __destroy_functor);
1994 * @brief %Function assignment to a new target.
1995 * @param f A %function object that is callable with parameters of
1996 * type @c T1, @c T2, ..., @c TN and returns a value convertible
2000 * This %function object wrapper will target a copy of @a
2001 * f. If @a f is @c reference_wrapper<F>, then this function
2002 * object will contain a reference to the function object @c
2003 * f.get(). If @a f is a NULL function pointer or NULL
2004 * pointer-to-member, @c this object will be empty.
2006 * If @a f is a non-NULL function pointer or an object of type @c
2007 * reference_wrapper<F>, this function will not throw.
2009 template<typename _Functor>
2010 typename __gnu_cxx::__enable_if<!is_integral<_Functor>::value,
2012 operator=(_Functor __f)
2014 function(__f).swap(*this);
2018 // [3.7.2.2] function modifiers
2021 * @brief Swap the targets of two %function objects.
2022 * @param f A %function with identical call signature.
2024 * Swap the targets of @c this function object and @a f. This
2025 * function will not throw an %exception.
2027 void swap(function& __x)
2029 std::swap(_M_functor, __x._M_functor);
2030 std::swap(_M_manager, __x._M_manager);
2031 std::swap(_M_invoker, __x._M_invoker);
2034 // [3.7.2.3] function capacity
2037 * @brief Determine if the %function wrapper has a target.
2039 * @return @c true when this %function object contains a target,
2040 * or @c false when it is empty.
2042 * This function will not throw an %exception.
2044 #if __cplusplus >= 201103L
2045 explicit operator bool() const
2046 { return !_M_empty(); }
2048 operator _Safe_bool() const
2053 return &_Hidden_type::_M_bool;
2057 // [3.7.2.4] function invocation
2060 * @brief Invokes the function targeted by @c *this.
2061 * @returns the result of the target.
2062 * @throws bad_function_call when @c !(bool)*this
2064 * The function call operator invokes the target function object
2065 * stored by @c this.
2067 _Res operator()(_ArgTypes... __args) const;
2070 // [3.7.2.5] function target access
2072 * @brief Determine the type of the target of this function object
2075 * @returns the type identifier of the target function object, or
2076 * @c typeid(void) if @c !(bool)*this.
2078 * This function will not throw an %exception.
2080 const type_info& target_type() const;
2083 * @brief Access the stored target function object.
2085 * @return Returns a pointer to the stored target function object,
2086 * if @c typeid(Functor).equals(target_type()); otherwise, a NULL
2089 * This function will not throw an %exception.
2091 template<typename _Functor> _Functor* target();
2094 template<typename _Functor> const _Functor* target() const;
2098 // [3.7.2.6] undefined operators
2099 template<typename _Function>
2100 void operator==(const function<_Function>&) const;
2101 template<typename _Function>
2102 void operator!=(const function<_Function>&) const;
2104 typedef _Res (*_Invoker_type)(const _Any_data&, _ArgTypes...);
2105 _Invoker_type _M_invoker;
2108 template<typename _Res, typename... _ArgTypes>
2109 function<_Res(_ArgTypes...)>::
2110 function(const function& __x)
2113 if (static_cast<bool>(__x))
2115 __x._M_manager(_M_functor, __x._M_functor, __clone_functor);
2116 _M_invoker = __x._M_invoker;
2117 _M_manager = __x._M_manager;
2121 template<typename _Res, typename... _ArgTypes>
2122 template<typename _Functor>
2123 function<_Res(_ArgTypes...)>::
2124 function(_Functor __f,
2125 typename __gnu_cxx::__enable_if<
2126 !is_integral<_Functor>::value, _Useless>::__type)
2129 typedef _Function_handler<_Signature_type, _Functor> _My_handler;
2131 if (_My_handler::_M_not_empty_function(__f))
2133 _My_handler::_M_init_functor(_M_functor, __f);
2134 _M_invoker = &_My_handler::_M_invoke;
2135 _M_manager = &_My_handler::_M_manager;
2139 template<typename _Res, typename... _ArgTypes>
2141 function<_Res(_ArgTypes...)>::
2142 operator()(_ArgTypes... __args) const
2145 _GLIBCXX_THROW_OR_ABORT(bad_function_call());
2146 return _M_invoker(_M_functor, __args...);
2150 template<typename _Res, typename... _ArgTypes>
2152 function<_Res(_ArgTypes...)>::
2157 _Any_data __typeinfo_result;
2158 _M_manager(__typeinfo_result, _M_functor, __get_type_info);
2159 return *__typeinfo_result._M_access<const type_info*>();
2162 return typeid(void);
2165 template<typename _Res, typename... _ArgTypes>
2166 template<typename _Functor>
2168 function<_Res(_ArgTypes...)>::
2171 if (typeid(_Functor) == target_type() && _M_manager)
2174 if (_M_manager(__ptr, _M_functor, __get_functor_ptr)
2175 && !is_const<_Functor>::value)
2178 return __ptr._M_access<_Functor*>();
2184 template<typename _Res, typename... _ArgTypes>
2185 template<typename _Functor>
2187 function<_Res(_ArgTypes...)>::
2190 if (typeid(_Functor) == target_type() && _M_manager)
2193 _M_manager(__ptr, _M_functor, __get_functor_ptr);
2194 return __ptr._M_access<const _Functor*>();
2201 // [3.7.2.7] null pointer comparisons
2204 * @brief Compares a polymorphic function object wrapper against 0
2205 * (the NULL pointer).
2206 * @returns @c true if the wrapper has no target, @c false otherwise
2208 * This function will not throw an %exception.
2210 template<typename _Signature>
2212 operator==(const function<_Signature>& __f, _M_clear_type*)
2213 { return !static_cast<bool>(__f); }
2216 template<typename _Signature>
2218 operator==(_M_clear_type*, const function<_Signature>& __f)
2219 { return !static_cast<bool>(__f); }
2222 * @brief Compares a polymorphic function object wrapper against 0
2223 * (the NULL pointer).
2224 * @returns @c false if the wrapper has no target, @c true otherwise
2226 * This function will not throw an %exception.
2228 template<typename _Signature>
2230 operator!=(const function<_Signature>& __f, _M_clear_type*)
2231 { return static_cast<bool>(__f); }
2234 template<typename _Signature>
2236 operator!=(_M_clear_type*, const function<_Signature>& __f)
2237 { return static_cast<bool>(__f); }
2239 // [3.7.2.8] specialized algorithms
2242 * @brief Swap the targets of two polymorphic function object wrappers.
2244 * This function will not throw an %exception.
2246 template<typename _Signature>
2248 swap(function<_Signature>& __x, function<_Signature>& __y)
2251 _GLIBCXX_END_NAMESPACE_VERSION
2254 #if __cplusplus >= 201103L
2255 _GLIBCXX_BEGIN_NAMESPACE_VERSION
2257 template<typename> struct is_placeholder;
2260 struct is_placeholder<tr1::_Placeholder<_Num>>
2261 : integral_constant<int, _Num>
2265 struct is_placeholder<const tr1::_Placeholder<_Num>>
2266 : integral_constant<int, _Num>
2269 template<typename> struct is_bind_expression;
2271 template<typename _Signature>
2272 struct is_bind_expression<tr1::_Bind<_Signature>>
2275 template<typename _Signature>
2276 struct is_bind_expression<const tr1::_Bind<_Signature>>
2279 template<typename _Signature>
2280 struct is_bind_expression<volatile tr1::_Bind<_Signature>>
2283 template<typename _Signature>
2284 struct is_bind_expression<const volatile tr1::_Bind<_Signature>>
2287 template<typename _Result, typename _Signature>
2288 struct is_bind_expression<tr1::_Bind_result<_Result, _Signature>>
2291 template<typename _Result, typename _Signature>
2292 struct is_bind_expression<const tr1::_Bind_result<_Result, _Signature>>
2295 template<typename _Result, typename _Signature>
2296 struct is_bind_expression<volatile tr1::_Bind_result<_Result, _Signature>>
2299 template<typename _Result, typename _Signature>
2300 struct is_bind_expression<const volatile tr1::_Bind_result<_Result,
2304 _GLIBCXX_END_NAMESPACE_VERSION
2308 #endif // _GLIBCXX_TR1_FUNCTIONAL