1 // TR1 functional header -*- C++ -*-
3 // Copyright (C) 2004, 2005, 2006, 2007, 2009, 2010, 2011
4 // Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 // <http://www.gnu.org/licenses/>.
26 /** @file tr1/functional
27 * This is a TR1 C++ Library header.
30 #ifndef _GLIBCXX_TR1_FUNCTIONAL
31 #define _GLIBCXX_TR1_FUNCTIONAL 1
33 #pragma GCC system_header
35 #include <bits/c++config.h>
36 #include <bits/stl_function.h>
41 #include <tr1/type_traits>
42 #include <bits/stringfwd.h>
43 #include <tr1/functional_hash.h>
44 #include <ext/type_traits.h>
45 #include <bits/move.h> // for std::__addressof
47 namespace std _GLIBCXX_VISIBILITY(default)
51 _GLIBCXX_BEGIN_NAMESPACE_VERSION
53 template<typename _MemberPointer>
55 template<typename _Tp, typename _Class>
56 _Mem_fn<_Tp _Class::*>
57 mem_fn(_Tp _Class::*);
60 * Actual implementation of _Has_result_type, which uses SFINAE to
61 * determine if the type _Tp has a publicly-accessible member type
64 template<typename _Tp>
65 class _Has_result_type_helper : __sfinae_types
67 template<typename _Up>
71 template<typename _Up>
72 static __one __test(_Wrap_type<typename _Up::result_type>*);
74 template<typename _Up>
75 static __two __test(...);
78 static const bool value = sizeof(__test<_Tp>(0)) == 1;
81 template<typename _Tp>
82 struct _Has_result_type
83 : integral_constant<bool,
84 _Has_result_type_helper<typename remove_cv<_Tp>::type>::value>
90 /// If we have found a result_type, extract it.
91 template<bool _Has_result_type, typename _Functor>
92 struct _Maybe_get_result_type
95 template<typename _Functor>
96 struct _Maybe_get_result_type<true, _Functor>
98 typedef typename _Functor::result_type result_type;
102 * Base class for any function object that has a weak result type, as
103 * defined in 3.3/3 of TR1.
105 template<typename _Functor>
106 struct _Weak_result_type_impl
107 : _Maybe_get_result_type<_Has_result_type<_Functor>::value, _Functor>
111 /// Retrieve the result type for a function type.
112 template<typename _Res, typename... _ArgTypes>
113 struct _Weak_result_type_impl<_Res(_ArgTypes...)>
115 typedef _Res result_type;
118 /// Retrieve the result type for a function reference.
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 pointer.
126 template<typename _Res, typename... _ArgTypes>
127 struct _Weak_result_type_impl<_Res(*)(_ArgTypes...)>
129 typedef _Res result_type;
132 /// Retrieve result type for a member function pointer.
133 template<typename _Res, typename _Class, typename... _ArgTypes>
134 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...)>
136 typedef _Res result_type;
139 /// Retrieve result type for a const member function pointer.
140 template<typename _Res, typename _Class, typename... _ArgTypes>
141 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) const>
143 typedef _Res result_type;
146 /// Retrieve result type for a volatile member function pointer.
147 template<typename _Res, typename _Class, typename... _ArgTypes>
148 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) volatile>
150 typedef _Res result_type;
153 /// Retrieve result type for a const volatile member function pointer.
154 template<typename _Res, typename _Class, typename... _ArgTypes>
155 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...)const volatile>
157 typedef _Res result_type;
161 * Strip top-level cv-qualifiers from the function object and let
162 * _Weak_result_type_impl perform the real work.
164 template<typename _Functor>
165 struct _Weak_result_type
166 : _Weak_result_type_impl<typename remove_cv<_Functor>::type>
170 template<typename _Signature>
174 * Actual implementation of result_of. When _Has_result_type is
175 * true, gets its result from _Weak_result_type. Otherwise, uses
176 * the function object's member template result to extract the
179 template<bool _Has_result_type, typename _Signature>
180 struct _Result_of_impl;
182 // Handle member data pointers using _Mem_fn's logic
183 template<typename _Res, typename _Class, typename _T1>
184 struct _Result_of_impl<false, _Res _Class::*(_T1)>
186 typedef typename _Mem_fn<_Res _Class::*>
187 ::template _Result_type<_T1>::type type;
191 * Determine whether we can determine a result type from @c Functor
194 template<typename _Functor, typename... _ArgTypes>
195 class result_of<_Functor(_ArgTypes...)>
196 : public _Result_of_impl<
197 _Has_result_type<_Weak_result_type<_Functor> >::value,
198 _Functor(_ArgTypes...)>
202 /// We already know the result type for @c Functor; use it.
203 template<typename _Functor, typename... _ArgTypes>
204 struct _Result_of_impl<true, _Functor(_ArgTypes...)>
206 typedef typename _Weak_result_type<_Functor>::result_type type;
210 * We need to compute the result type for this invocation the hard
213 template<typename _Functor, typename... _ArgTypes>
214 struct _Result_of_impl<false, _Functor(_ArgTypes...)>
216 typedef typename _Functor
217 ::template result<_Functor(_ArgTypes...)>::type type;
221 * It is unsafe to access ::result when there are zero arguments, so we
222 * return @c void instead.
224 template<typename _Functor>
225 struct _Result_of_impl<false, _Functor()>
230 /// Determines if the type _Tp derives from unary_function.
231 template<typename _Tp>
232 struct _Derives_from_unary_function : __sfinae_types
235 template<typename _T1, typename _Res>
236 static __one __test(const volatile unary_function<_T1, _Res>*);
238 // It's tempting to change "..." to const volatile void*, but
239 // that fails when _Tp is a function type.
240 static __two __test(...);
243 static const bool value = sizeof(__test((_Tp*)0)) == 1;
246 /// Determines if the type _Tp derives from binary_function.
247 template<typename _Tp>
248 struct _Derives_from_binary_function : __sfinae_types
251 template<typename _T1, typename _T2, typename _Res>
252 static __one __test(const volatile binary_function<_T1, _T2, _Res>*);
254 // It's tempting to change "..." to const volatile void*, but
255 // that fails when _Tp is a function type.
256 static __two __test(...);
259 static const bool value = sizeof(__test((_Tp*)0)) == 1;
262 /// Turns a function type into a function pointer type
263 template<typename _Tp, bool _IsFunctionType = is_function<_Tp>::value>
264 struct _Function_to_function_pointer
269 template<typename _Tp>
270 struct _Function_to_function_pointer<_Tp, true>
276 * Invoke a function object, which may be either a member pointer or a
277 * function object. The first parameter will tell which.
279 template<typename _Functor, typename... _Args>
281 typename __gnu_cxx::__enable_if<
282 (!is_member_pointer<_Functor>::value
283 && !is_function<_Functor>::value
284 && !is_function<typename remove_pointer<_Functor>::type>::value),
285 typename result_of<_Functor(_Args...)>::type
287 __invoke(_Functor& __f, _Args&... __args)
289 return __f(__args...);
292 template<typename _Functor, typename... _Args>
294 typename __gnu_cxx::__enable_if<
295 (is_member_pointer<_Functor>::value
296 && !is_function<_Functor>::value
297 && !is_function<typename remove_pointer<_Functor>::type>::value),
298 typename result_of<_Functor(_Args...)>::type
300 __invoke(_Functor& __f, _Args&... __args)
302 return mem_fn(__f)(__args...);
305 // To pick up function references (that will become function pointers)
306 template<typename _Functor, typename... _Args>
308 typename __gnu_cxx::__enable_if<
309 (is_pointer<_Functor>::value
310 && is_function<typename remove_pointer<_Functor>::type>::value),
311 typename result_of<_Functor(_Args...)>::type
313 __invoke(_Functor __f, _Args&... __args)
315 return __f(__args...);
319 * Knowing which of unary_function and binary_function _Tp derives
320 * from, derives from the same and ensures that reference_wrapper
321 * will have a weak result type. See cases below.
323 template<bool _Unary, bool _Binary, typename _Tp>
324 struct _Reference_wrapper_base_impl;
326 // Not a unary_function or binary_function, so try a weak result type.
327 template<typename _Tp>
328 struct _Reference_wrapper_base_impl<false, false, _Tp>
329 : _Weak_result_type<_Tp>
332 // unary_function but not binary_function
333 template<typename _Tp>
334 struct _Reference_wrapper_base_impl<true, false, _Tp>
335 : unary_function<typename _Tp::argument_type,
336 typename _Tp::result_type>
339 // binary_function but not unary_function
340 template<typename _Tp>
341 struct _Reference_wrapper_base_impl<false, true, _Tp>
342 : binary_function<typename _Tp::first_argument_type,
343 typename _Tp::second_argument_type,
344 typename _Tp::result_type>
347 // Both unary_function and binary_function. Import result_type to
349 template<typename _Tp>
350 struct _Reference_wrapper_base_impl<true, true, _Tp>
351 : unary_function<typename _Tp::argument_type,
352 typename _Tp::result_type>,
353 binary_function<typename _Tp::first_argument_type,
354 typename _Tp::second_argument_type,
355 typename _Tp::result_type>
357 typedef typename _Tp::result_type result_type;
361 * Derives from unary_function or binary_function when it
362 * can. Specializations handle all of the easy cases. The primary
363 * template determines what to do with a class type, which may
364 * derive from both unary_function and binary_function.
366 template<typename _Tp>
367 struct _Reference_wrapper_base
368 : _Reference_wrapper_base_impl<
369 _Derives_from_unary_function<_Tp>::value,
370 _Derives_from_binary_function<_Tp>::value,
374 // - a function type (unary)
375 template<typename _Res, typename _T1>
376 struct _Reference_wrapper_base<_Res(_T1)>
377 : unary_function<_T1, _Res>
380 // - a function type (binary)
381 template<typename _Res, typename _T1, typename _T2>
382 struct _Reference_wrapper_base<_Res(_T1, _T2)>
383 : binary_function<_T1, _T2, _Res>
386 // - a function pointer type (unary)
387 template<typename _Res, typename _T1>
388 struct _Reference_wrapper_base<_Res(*)(_T1)>
389 : unary_function<_T1, _Res>
392 // - a function pointer type (binary)
393 template<typename _Res, typename _T1, typename _T2>
394 struct _Reference_wrapper_base<_Res(*)(_T1, _T2)>
395 : binary_function<_T1, _T2, _Res>
398 // - a pointer to member function type (unary, no qualifiers)
399 template<typename _Res, typename _T1>
400 struct _Reference_wrapper_base<_Res (_T1::*)()>
401 : unary_function<_T1*, _Res>
404 // - a pointer to member function type (binary, no qualifiers)
405 template<typename _Res, typename _T1, typename _T2>
406 struct _Reference_wrapper_base<_Res (_T1::*)(_T2)>
407 : binary_function<_T1*, _T2, _Res>
410 // - a pointer to member function type (unary, const)
411 template<typename _Res, typename _T1>
412 struct _Reference_wrapper_base<_Res (_T1::*)() const>
413 : unary_function<const _T1*, _Res>
416 // - a pointer to member function type (binary, const)
417 template<typename _Res, typename _T1, typename _T2>
418 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const>
419 : binary_function<const _T1*, _T2, _Res>
422 // - a pointer to member function type (unary, volatile)
423 template<typename _Res, typename _T1>
424 struct _Reference_wrapper_base<_Res (_T1::*)() volatile>
425 : unary_function<volatile _T1*, _Res>
428 // - a pointer to member function type (binary, volatile)
429 template<typename _Res, typename _T1, typename _T2>
430 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) volatile>
431 : binary_function<volatile _T1*, _T2, _Res>
434 // - a pointer to member function type (unary, const volatile)
435 template<typename _Res, typename _T1>
436 struct _Reference_wrapper_base<_Res (_T1::*)() const volatile>
437 : unary_function<const volatile _T1*, _Res>
440 // - a pointer to member function type (binary, const volatile)
441 template<typename _Res, typename _T1, typename _T2>
442 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const volatile>
443 : binary_function<const volatile _T1*, _T2, _Res>
446 /// reference_wrapper
447 template<typename _Tp>
448 class reference_wrapper
449 : public _Reference_wrapper_base<typename remove_cv<_Tp>::type>
451 // If _Tp is a function type, we can't form result_of<_Tp(...)>,
452 // so turn it into a function pointer type.
453 typedef typename _Function_to_function_pointer<_Tp>::type
461 reference_wrapper(_Tp& __indata)
462 : _M_data(std::__addressof(__indata))
465 reference_wrapper(const reference_wrapper<_Tp>& __inref):
466 _M_data(__inref._M_data)
470 operator=(const reference_wrapper<_Tp>& __inref)
472 _M_data = __inref._M_data;
476 operator _Tp&() const
477 { return this->get(); }
483 template<typename... _Args>
484 typename result_of<_M_func_type(_Args...)>::type
485 operator()(_Args&... __args) const
487 return __invoke(get(), __args...);
492 // Denotes a reference should be taken to a variable.
493 template<typename _Tp>
494 inline reference_wrapper<_Tp>
496 { return reference_wrapper<_Tp>(__t); }
498 // Denotes a const reference should be taken to a variable.
499 template<typename _Tp>
500 inline reference_wrapper<const _Tp>
502 { return reference_wrapper<const _Tp>(__t); }
504 template<typename _Tp>
505 inline reference_wrapper<_Tp>
506 ref(reference_wrapper<_Tp> __t)
507 { return ref(__t.get()); }
509 template<typename _Tp>
510 inline reference_wrapper<const _Tp>
511 cref(reference_wrapper<_Tp> __t)
512 { return cref(__t.get()); }
514 template<typename _Tp, bool>
515 struct _Mem_fn_const_or_non
517 typedef const _Tp& type;
520 template<typename _Tp>
521 struct _Mem_fn_const_or_non<_Tp, false>
527 * Derives from @c unary_function or @c binary_function, or perhaps
528 * nothing, depending on the number of arguments provided. The
529 * primary template is the basis case, which derives nothing.
531 template<typename _Res, typename... _ArgTypes>
532 struct _Maybe_unary_or_binary_function { };
534 /// Derives from @c unary_function, as appropriate.
535 template<typename _Res, typename _T1>
536 struct _Maybe_unary_or_binary_function<_Res, _T1>
537 : std::unary_function<_T1, _Res> { };
539 /// Derives from @c binary_function, as appropriate.
540 template<typename _Res, typename _T1, typename _T2>
541 struct _Maybe_unary_or_binary_function<_Res, _T1, _T2>
542 : std::binary_function<_T1, _T2, _Res> { };
544 /// Implementation of @c mem_fn for member function pointers.
545 template<typename _Res, typename _Class, typename... _ArgTypes>
546 class _Mem_fn<_Res (_Class::*)(_ArgTypes...)>
547 : public _Maybe_unary_or_binary_function<_Res, _Class*, _ArgTypes...>
549 typedef _Res (_Class::*_Functor)(_ArgTypes...);
551 template<typename _Tp>
553 _M_call(_Tp& __object, const volatile _Class *,
554 _ArgTypes... __args) const
555 { return (__object.*__pmf)(__args...); }
557 template<typename _Tp>
559 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
560 { return ((*__ptr).*__pmf)(__args...); }
563 typedef _Res result_type;
565 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
569 operator()(_Class& __object, _ArgTypes... __args) const
570 { return (__object.*__pmf)(__args...); }
574 operator()(_Class* __object, _ArgTypes... __args) const
575 { return (__object->*__pmf)(__args...); }
577 // Handle smart pointers, references and pointers to derived
578 template<typename _Tp>
580 operator()(_Tp& __object, _ArgTypes... __args) const
581 { return _M_call(__object, &__object, __args...); }
587 /// Implementation of @c mem_fn for const member function pointers.
588 template<typename _Res, typename _Class, typename... _ArgTypes>
589 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) const>
590 : public _Maybe_unary_or_binary_function<_Res, const _Class*,
593 typedef _Res (_Class::*_Functor)(_ArgTypes...) const;
595 template<typename _Tp>
597 _M_call(_Tp& __object, const volatile _Class *,
598 _ArgTypes... __args) const
599 { return (__object.*__pmf)(__args...); }
601 template<typename _Tp>
603 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
604 { return ((*__ptr).*__pmf)(__args...); }
607 typedef _Res result_type;
609 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
613 operator()(const _Class& __object, _ArgTypes... __args) const
614 { return (__object.*__pmf)(__args...); }
618 operator()(const _Class* __object, _ArgTypes... __args) const
619 { return (__object->*__pmf)(__args...); }
621 // Handle smart pointers, references and pointers to derived
622 template<typename _Tp>
623 _Res operator()(_Tp& __object, _ArgTypes... __args) const
624 { return _M_call(__object, &__object, __args...); }
630 /// Implementation of @c mem_fn for volatile member function pointers.
631 template<typename _Res, typename _Class, typename... _ArgTypes>
632 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) volatile>
633 : public _Maybe_unary_or_binary_function<_Res, volatile _Class*,
636 typedef _Res (_Class::*_Functor)(_ArgTypes...) volatile;
638 template<typename _Tp>
640 _M_call(_Tp& __object, const volatile _Class *,
641 _ArgTypes... __args) const
642 { return (__object.*__pmf)(__args...); }
644 template<typename _Tp>
646 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
647 { return ((*__ptr).*__pmf)(__args...); }
650 typedef _Res result_type;
652 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
656 operator()(volatile _Class& __object, _ArgTypes... __args) const
657 { return (__object.*__pmf)(__args...); }
661 operator()(volatile _Class* __object, _ArgTypes... __args) const
662 { return (__object->*__pmf)(__args...); }
664 // Handle smart pointers, references and pointers to derived
665 template<typename _Tp>
667 operator()(_Tp& __object, _ArgTypes... __args) const
668 { return _M_call(__object, &__object, __args...); }
674 /// Implementation of @c mem_fn for const volatile member function pointers.
675 template<typename _Res, typename _Class, typename... _ArgTypes>
676 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) const volatile>
677 : public _Maybe_unary_or_binary_function<_Res, const volatile _Class*,
680 typedef _Res (_Class::*_Functor)(_ArgTypes...) const volatile;
682 template<typename _Tp>
684 _M_call(_Tp& __object, const volatile _Class *,
685 _ArgTypes... __args) const
686 { return (__object.*__pmf)(__args...); }
688 template<typename _Tp>
690 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
691 { return ((*__ptr).*__pmf)(__args...); }
694 typedef _Res result_type;
696 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
700 operator()(const volatile _Class& __object, _ArgTypes... __args) const
701 { return (__object.*__pmf)(__args...); }
705 operator()(const volatile _Class* __object, _ArgTypes... __args) const
706 { return (__object->*__pmf)(__args...); }
708 // Handle smart pointers, references and pointers to derived
709 template<typename _Tp>
710 _Res operator()(_Tp& __object, _ArgTypes... __args) const
711 { return _M_call(__object, &__object, __args...); }
718 template<typename _Res, typename _Class>
719 class _Mem_fn<_Res _Class::*>
721 // This bit of genius is due to Peter Dimov, improved slightly by
723 template<typename _Tp>
725 _M_call(_Tp& __object, _Class *) const
726 { return __object.*__pm; }
728 template<typename _Tp, typename _Up>
730 _M_call(_Tp& __object, _Up * const *) const
731 { return (*__object).*__pm; }
733 template<typename _Tp, typename _Up>
735 _M_call(_Tp& __object, const _Up * const *) const
736 { return (*__object).*__pm; }
738 template<typename _Tp>
740 _M_call(_Tp& __object, const _Class *) const
741 { return __object.*__pm; }
743 template<typename _Tp>
745 _M_call(_Tp& __ptr, const volatile void*) const
746 { return (*__ptr).*__pm; }
748 template<typename _Tp> static _Tp& __get_ref();
750 template<typename _Tp>
751 static __sfinae_types::__one __check_const(_Tp&, _Class*);
752 template<typename _Tp, typename _Up>
753 static __sfinae_types::__one __check_const(_Tp&, _Up * const *);
754 template<typename _Tp, typename _Up>
755 static __sfinae_types::__two __check_const(_Tp&, const _Up * const *);
756 template<typename _Tp>
757 static __sfinae_types::__two __check_const(_Tp&, const _Class*);
758 template<typename _Tp>
759 static __sfinae_types::__two __check_const(_Tp&, const volatile void*);
762 template<typename _Tp>
764 : _Mem_fn_const_or_non<_Res,
765 (sizeof(__sfinae_types::__two)
766 == sizeof(__check_const<_Tp>(__get_ref<_Tp>(), (_Tp*)0)))>
769 template<typename _Signature>
772 template<typename _CVMem, typename _Tp>
773 struct result<_CVMem(_Tp)>
774 : public _Result_type<_Tp> { };
776 template<typename _CVMem, typename _Tp>
777 struct result<_CVMem(_Tp&)>
778 : public _Result_type<_Tp> { };
781 _Mem_fn(_Res _Class::*__pm) : __pm(__pm) { }
785 operator()(_Class& __object) const
786 { return __object.*__pm; }
789 operator()(const _Class& __object) const
790 { return __object.*__pm; }
794 operator()(_Class* __object) const
795 { return __object->*__pm; }
798 operator()(const _Class* __object) const
799 { return __object->*__pm; }
801 // Handle smart pointers and derived
802 template<typename _Tp>
803 typename _Result_type<_Tp>::type
804 operator()(_Tp& __unknown) const
805 { return _M_call(__unknown, &__unknown); }
812 * @brief Returns a function object that forwards to the member
815 template<typename _Tp, typename _Class>
816 inline _Mem_fn<_Tp _Class::*>
817 mem_fn(_Tp _Class::* __pm)
819 return _Mem_fn<_Tp _Class::*>(__pm);
823 * @brief Determines if the given type _Tp is a function object
824 * should be treated as a subexpression when evaluating calls to
825 * function objects returned by bind(). [TR1 3.6.1]
827 template<typename _Tp>
828 struct is_bind_expression
829 { static const bool value = false; };
831 template<typename _Tp>
832 const bool is_bind_expression<_Tp>::value;
835 * @brief Determines if the given type _Tp is a placeholder in a
836 * bind() expression and, if so, which placeholder it is. [TR1 3.6.2]
838 template<typename _Tp>
839 struct is_placeholder
840 { static const int value = 0; };
842 template<typename _Tp>
843 const int is_placeholder<_Tp>::value;
845 /// The type of placeholder objects defined by libstdc++.
846 template<int _Num> struct _Placeholder { };
848 _GLIBCXX_END_NAMESPACE_VERSION
850 /** @namespace std::placeholders
851 * @brief ISO C++ 0x entities sub namespace for functional.
853 * Define a large number of placeholders. There is no way to
854 * simplify this with variadic templates, because we're introducing
855 * unique names for each.
857 namespace placeholders
859 _GLIBCXX_BEGIN_NAMESPACE_VERSION
871 _Placeholder<10> _10;
872 _Placeholder<11> _11;
873 _Placeholder<12> _12;
874 _Placeholder<13> _13;
875 _Placeholder<14> _14;
876 _Placeholder<15> _15;
877 _Placeholder<16> _16;
878 _Placeholder<17> _17;
879 _Placeholder<18> _18;
880 _Placeholder<19> _19;
881 _Placeholder<20> _20;
882 _Placeholder<21> _21;
883 _Placeholder<22> _22;
884 _Placeholder<23> _23;
885 _Placeholder<24> _24;
886 _Placeholder<25> _25;
887 _Placeholder<26> _26;
888 _Placeholder<27> _27;
889 _Placeholder<28> _28;
890 _Placeholder<29> _29;
892 _GLIBCXX_END_NAMESPACE_VERSION
895 _GLIBCXX_BEGIN_NAMESPACE_VERSION
897 * Partial specialization of is_placeholder that provides the placeholder
898 * number for the placeholder objects defined by libstdc++.
901 struct is_placeholder<_Placeholder<_Num> >
902 { static const int value = _Num; };
905 const int is_placeholder<_Placeholder<_Num> >::value;
908 * Stores a tuple of indices. Used by bind() to extract the elements
911 template<int... _Indexes>
912 struct _Index_tuple { };
914 /// Builds an _Index_tuple<0, 1, 2, ..., _Num-1>.
915 template<std::size_t _Num, typename _Tuple = _Index_tuple<> >
916 struct _Build_index_tuple;
918 template<std::size_t _Num, int... _Indexes>
919 struct _Build_index_tuple<_Num, _Index_tuple<_Indexes...> >
920 : _Build_index_tuple<_Num - 1,
921 _Index_tuple<_Indexes..., sizeof...(_Indexes)> >
925 template<int... _Indexes>
926 struct _Build_index_tuple<0, _Index_tuple<_Indexes...> >
928 typedef _Index_tuple<_Indexes...> __type;
932 * Used by _Safe_tuple_element to indicate that there is no tuple
933 * element at this position.
935 struct _No_tuple_element;
938 * Implementation helper for _Safe_tuple_element. This primary
939 * template handles the case where it is safe to use @c
942 template<int __i, typename _Tuple, bool _IsSafe>
943 struct _Safe_tuple_element_impl
944 : tuple_element<__i, _Tuple> { };
947 * Implementation helper for _Safe_tuple_element. This partial
948 * specialization handles the case where it is not safe to use @c
949 * tuple_element. We just return @c _No_tuple_element.
951 template<int __i, typename _Tuple>
952 struct _Safe_tuple_element_impl<__i, _Tuple, false>
954 typedef _No_tuple_element type;
958 * Like tuple_element, but returns @c _No_tuple_element when
959 * tuple_element would return an error.
961 template<int __i, typename _Tuple>
962 struct _Safe_tuple_element
963 : _Safe_tuple_element_impl<__i, _Tuple,
964 (__i >= 0 && __i < tuple_size<_Tuple>::value)>
969 * Maps an argument to bind() into an actual argument to the bound
970 * function object [TR1 3.6.3/5]. Only the first parameter should
971 * be specified: the rest are used to determine among the various
972 * implementations. Note that, although this class is a function
973 * object, it isn't entirely normal because it takes only two
974 * parameters regardless of the number of parameters passed to the
975 * bind expression. The first parameter is the bound argument and
976 * the second parameter is a tuple containing references to the
977 * rest of the arguments.
979 template<typename _Arg,
980 bool _IsBindExp = is_bind_expression<_Arg>::value,
981 bool _IsPlaceholder = (is_placeholder<_Arg>::value > 0)>
985 * If the argument is reference_wrapper<_Tp>, returns the
986 * underlying reference. [TR1 3.6.3/5 bullet 1]
988 template<typename _Tp>
989 class _Mu<reference_wrapper<_Tp>, false, false>
992 typedef _Tp& result_type;
994 /* Note: This won't actually work for const volatile
995 * reference_wrappers, because reference_wrapper::get() is const
996 * but not volatile-qualified. This might be a defect in the TR.
998 template<typename _CVRef, typename _Tuple>
1000 operator()(_CVRef& __arg, const _Tuple&) const volatile
1001 { return __arg.get(); }
1005 * If the argument is a bind expression, we invoke the underlying
1006 * function object with the same cv-qualifiers as we are given and
1007 * pass along all of our arguments (unwrapped). [TR1 3.6.3/5 bullet 2]
1009 template<typename _Arg>
1010 class _Mu<_Arg, true, false>
1013 template<typename _Signature> class result;
1015 // Determine the result type when we pass the arguments along. This
1016 // involves passing along the cv-qualifiers placed on _Mu and
1017 // unwrapping the argument bundle.
1018 template<typename _CVMu, typename _CVArg, typename... _Args>
1019 class result<_CVMu(_CVArg, tuple<_Args...>)>
1020 : public result_of<_CVArg(_Args...)> { };
1022 template<typename _CVArg, typename... _Args>
1023 typename result_of<_CVArg(_Args...)>::type
1024 operator()(_CVArg& __arg,
1025 const tuple<_Args...>& __tuple) const volatile
1027 // Construct an index tuple and forward to __call
1028 typedef typename _Build_index_tuple<sizeof...(_Args)>::__type
1030 return this->__call(__arg, __tuple, _Indexes());
1034 // Invokes the underlying function object __arg by unpacking all
1035 // of the arguments in the tuple.
1036 template<typename _CVArg, typename... _Args, int... _Indexes>
1037 typename result_of<_CVArg(_Args...)>::type
1038 __call(_CVArg& __arg, const tuple<_Args...>& __tuple,
1039 const _Index_tuple<_Indexes...>&) const volatile
1041 return __arg(tr1::get<_Indexes>(__tuple)...);
1046 * If the argument is a placeholder for the Nth argument, returns
1047 * a reference to the Nth argument to the bind function object.
1048 * [TR1 3.6.3/5 bullet 3]
1050 template<typename _Arg>
1051 class _Mu<_Arg, false, true>
1054 template<typename _Signature> class result;
1056 template<typename _CVMu, typename _CVArg, typename _Tuple>
1057 class result<_CVMu(_CVArg, _Tuple)>
1059 // Add a reference, if it hasn't already been done for us.
1060 // This allows us to be a little bit sloppy in constructing
1061 // the tuple that we pass to result_of<...>.
1062 typedef typename _Safe_tuple_element<(is_placeholder<_Arg>::value
1067 typedef typename add_reference<__base_type>::type type;
1070 template<typename _Tuple>
1071 typename result<_Mu(_Arg, _Tuple)>::type
1072 operator()(const volatile _Arg&, const _Tuple& __tuple) const volatile
1074 return ::std::tr1::get<(is_placeholder<_Arg>::value - 1)>(__tuple);
1079 * If the argument is just a value, returns a reference to that
1080 * value. The cv-qualifiers on the reference are the same as the
1081 * cv-qualifiers on the _Mu object. [TR1 3.6.3/5 bullet 4]
1083 template<typename _Arg>
1084 class _Mu<_Arg, false, false>
1087 template<typename _Signature> struct result;
1089 template<typename _CVMu, typename _CVArg, typename _Tuple>
1090 struct result<_CVMu(_CVArg, _Tuple)>
1092 typedef typename add_reference<_CVArg>::type type;
1095 // Pick up the cv-qualifiers of the argument
1096 template<typename _CVArg, typename _Tuple>
1098 operator()(_CVArg& __arg, const _Tuple&) const volatile
1103 * Maps member pointers into instances of _Mem_fn but leaves all
1104 * other function objects untouched. Used by tr1::bind(). The
1105 * primary template handles the non--member-pointer case.
1107 template<typename _Tp>
1108 struct _Maybe_wrap_member_pointer
1113 __do_wrap(const _Tp& __x)
1118 * Maps member pointers into instances of _Mem_fn but leaves all
1119 * other function objects untouched. Used by tr1::bind(). This
1120 * partial specialization handles the member pointer case.
1122 template<typename _Tp, typename _Class>
1123 struct _Maybe_wrap_member_pointer<_Tp _Class::*>
1125 typedef _Mem_fn<_Tp _Class::*> type;
1128 __do_wrap(_Tp _Class::* __pm)
1129 { return type(__pm); }
1132 /// Type of the function object returned from bind().
1133 template<typename _Signature>
1136 template<typename _Functor, typename... _Bound_args>
1137 class _Bind<_Functor(_Bound_args...)>
1138 : public _Weak_result_type<_Functor>
1140 typedef _Bind __self_type;
1141 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
1145 tuple<_Bound_args...> _M_bound_args;
1148 template<typename... _Args, int... _Indexes>
1150 _Functor(typename result_of<_Mu<_Bound_args>
1151 (_Bound_args, tuple<_Args...>)>::type...)
1153 __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>)
1155 return _M_f(_Mu<_Bound_args>()
1156 (tr1::get<_Indexes>(_M_bound_args), __args)...);
1160 template<typename... _Args, int... _Indexes>
1162 const _Functor(typename result_of<_Mu<_Bound_args>
1163 (const _Bound_args, tuple<_Args...>)
1165 __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>) const
1167 return _M_f(_Mu<_Bound_args>()
1168 (tr1::get<_Indexes>(_M_bound_args), __args)...);
1172 template<typename... _Args, int... _Indexes>
1174 volatile _Functor(typename result_of<_Mu<_Bound_args>
1175 (volatile _Bound_args, tuple<_Args...>)
1177 __call(const tuple<_Args...>& __args,
1178 _Index_tuple<_Indexes...>) volatile
1180 return _M_f(_Mu<_Bound_args>()
1181 (tr1::get<_Indexes>(_M_bound_args), __args)...);
1184 // Call as const volatile
1185 template<typename... _Args, int... _Indexes>
1187 const volatile _Functor(typename result_of<_Mu<_Bound_args>
1188 (const volatile _Bound_args,
1191 __call(const tuple<_Args...>& __args,
1192 _Index_tuple<_Indexes...>) const volatile
1194 return _M_f(_Mu<_Bound_args>()
1195 (tr1::get<_Indexes>(_M_bound_args), __args)...);
1199 explicit _Bind(_Functor __f, _Bound_args... __bound_args)
1200 : _M_f(__f), _M_bound_args(__bound_args...) { }
1203 template<typename... _Args>
1205 _Functor(typename result_of<_Mu<_Bound_args>
1206 (_Bound_args, tuple<_Args...>)>::type...)
1208 operator()(_Args&... __args)
1210 return this->__call(tr1::tie(__args...), _Bound_indexes());
1214 template<typename... _Args>
1216 const _Functor(typename result_of<_Mu<_Bound_args>
1217 (const _Bound_args, tuple<_Args...>)>::type...)
1219 operator()(_Args&... __args) const
1221 return this->__call(tr1::tie(__args...), _Bound_indexes());
1226 template<typename... _Args>
1228 volatile _Functor(typename result_of<_Mu<_Bound_args>
1229 (volatile _Bound_args, tuple<_Args...>)>::type...)
1231 operator()(_Args&... __args) volatile
1233 return this->__call(tr1::tie(__args...), _Bound_indexes());
1237 // Call as const volatile
1238 template<typename... _Args>
1240 const volatile _Functor(typename result_of<_Mu<_Bound_args>
1241 (const volatile _Bound_args,
1242 tuple<_Args...>)>::type...)
1244 operator()(_Args&... __args) const volatile
1246 return this->__call(tr1::tie(__args...), _Bound_indexes());
1250 /// Type of the function object returned from bind<R>().
1251 template<typename _Result, typename _Signature>
1252 struct _Bind_result;
1254 template<typename _Result, typename _Functor, typename... _Bound_args>
1255 class _Bind_result<_Result, _Functor(_Bound_args...)>
1257 typedef _Bind_result __self_type;
1258 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
1262 tuple<_Bound_args...> _M_bound_args;
1265 template<typename... _Args, int... _Indexes>
1267 __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>)
1269 return _M_f(_Mu<_Bound_args>()
1270 (tr1::get<_Indexes>(_M_bound_args), __args)...);
1274 template<typename... _Args, int... _Indexes>
1276 __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>) const
1278 return _M_f(_Mu<_Bound_args>()
1279 (tr1::get<_Indexes>(_M_bound_args), __args)...);
1283 template<typename... _Args, int... _Indexes>
1285 __call(const tuple<_Args...>& __args,
1286 _Index_tuple<_Indexes...>) volatile
1288 return _M_f(_Mu<_Bound_args>()
1289 (tr1::get<_Indexes>(_M_bound_args), __args)...);
1292 // Call as const volatile
1293 template<typename... _Args, int... _Indexes>
1295 __call(const tuple<_Args...>& __args,
1296 _Index_tuple<_Indexes...>) const volatile
1298 return _M_f(_Mu<_Bound_args>()
1299 (tr1::get<_Indexes>(_M_bound_args), __args)...);
1303 typedef _Result result_type;
1306 _Bind_result(_Functor __f, _Bound_args... __bound_args)
1307 : _M_f(__f), _M_bound_args(__bound_args...) { }
1310 template<typename... _Args>
1312 operator()(_Args&... __args)
1314 return this->__call(tr1::tie(__args...), _Bound_indexes());
1318 template<typename... _Args>
1320 operator()(_Args&... __args) const
1322 return this->__call(tr1::tie(__args...), _Bound_indexes());
1326 template<typename... _Args>
1328 operator()(_Args&... __args) volatile
1330 return this->__call(tr1::tie(__args...), _Bound_indexes());
1333 // Call as const volatile
1334 template<typename... _Args>
1336 operator()(_Args&... __args) const volatile
1338 return this->__call(tr1::tie(__args...), _Bound_indexes());
1342 /// Class template _Bind is always a bind expression.
1343 template<typename _Signature>
1344 struct is_bind_expression<_Bind<_Signature> >
1345 { static const bool value = true; };
1347 template<typename _Signature>
1348 const bool is_bind_expression<_Bind<_Signature> >::value;
1350 /// Class template _Bind_result is always a bind expression.
1351 template<typename _Result, typename _Signature>
1352 struct is_bind_expression<_Bind_result<_Result, _Signature> >
1353 { static const bool value = true; };
1355 template<typename _Result, typename _Signature>
1356 const bool is_bind_expression<_Bind_result<_Result, _Signature> >::value;
1359 template<typename _Functor, typename... _ArgTypes>
1361 _Bind<typename _Maybe_wrap_member_pointer<_Functor>::type(_ArgTypes...)>
1362 bind(_Functor __f, _ArgTypes... __args)
1364 typedef _Maybe_wrap_member_pointer<_Functor> __maybe_type;
1365 typedef typename __maybe_type::type __functor_type;
1366 typedef _Bind<__functor_type(_ArgTypes...)> __result_type;
1367 return __result_type(__maybe_type::__do_wrap(__f), __args...);
1370 template<typename _Result, typename _Functor, typename... _ArgTypes>
1372 _Bind_result<_Result,
1373 typename _Maybe_wrap_member_pointer<_Functor>::type
1375 bind(_Functor __f, _ArgTypes... __args)
1377 typedef _Maybe_wrap_member_pointer<_Functor> __maybe_type;
1378 typedef typename __maybe_type::type __functor_type;
1379 typedef _Bind_result<_Result, __functor_type(_ArgTypes...)>
1381 return __result_type(__maybe_type::__do_wrap(__f), __args...);
1385 * @brief Exception class thrown when class template function's
1386 * operator() is called with an empty target.
1387 * @ingroup exceptions
1389 class bad_function_call : public std::exception { };
1392 * The integral constant expression 0 can be converted into a
1393 * pointer to this type. It is used by the function template to
1394 * accept NULL pointers.
1396 struct _M_clear_type;
1399 * Trait identifying @a location-invariant types, meaning that the
1400 * address of the object (or any of its members) will not escape.
1401 * Also implies a trivial copy constructor and assignment operator.
1403 template<typename _Tp>
1404 struct __is_location_invariant
1405 : integral_constant<bool,
1406 (is_pointer<_Tp>::value
1407 || is_member_pointer<_Tp>::value)>
1411 class _Undefined_class;
1416 const void* _M_const_object;
1417 void (*_M_function_pointer)();
1418 void (_Undefined_class::*_M_member_pointer)();
1423 void* _M_access() { return &_M_pod_data[0]; }
1424 const void* _M_access() const { return &_M_pod_data[0]; }
1426 template<typename _Tp>
1429 { return *static_cast<_Tp*>(_M_access()); }
1431 template<typename _Tp>
1434 { return *static_cast<const _Tp*>(_M_access()); }
1436 _Nocopy_types _M_unused;
1437 char _M_pod_data[sizeof(_Nocopy_types)];
1440 enum _Manager_operation
1448 // Simple type wrapper that helps avoid annoying const problems
1449 // when casting between void pointers and pointers-to-pointers.
1450 template<typename _Tp>
1451 struct _Simple_type_wrapper
1453 _Simple_type_wrapper(_Tp __value) : __value(__value) { }
1458 template<typename _Tp>
1459 struct __is_location_invariant<_Simple_type_wrapper<_Tp> >
1460 : __is_location_invariant<_Tp>
1464 // Converts a reference to a function object into a callable
1466 template<typename _Functor>
1468 __callable_functor(_Functor& __f)
1471 template<typename _Member, typename _Class>
1472 inline _Mem_fn<_Member _Class::*>
1473 __callable_functor(_Member _Class::* &__p)
1474 { return mem_fn(__p); }
1476 template<typename _Member, typename _Class>
1477 inline _Mem_fn<_Member _Class::*>
1478 __callable_functor(_Member _Class::* const &__p)
1479 { return mem_fn(__p); }
1481 template<typename _Signature>
1484 /// Base class of all polymorphic function object wrappers.
1485 class _Function_base
1488 static const std::size_t _M_max_size = sizeof(_Nocopy_types);
1489 static const std::size_t _M_max_align = __alignof__(_Nocopy_types);
1491 template<typename _Functor>
1495 static const bool __stored_locally =
1496 (__is_location_invariant<_Functor>::value
1497 && sizeof(_Functor) <= _M_max_size
1498 && __alignof__(_Functor) <= _M_max_align
1499 && (_M_max_align % __alignof__(_Functor) == 0));
1501 typedef integral_constant<bool, __stored_locally> _Local_storage;
1503 // Retrieve a pointer to the function object
1505 _M_get_pointer(const _Any_data& __source)
1507 const _Functor* __ptr =
1508 __stored_locally? &__source._M_access<_Functor>()
1509 /* have stored a pointer */ : __source._M_access<_Functor*>();
1510 return const_cast<_Functor*>(__ptr);
1513 // Clone a location-invariant function object that fits within
1514 // an _Any_data structure.
1516 _M_clone(_Any_data& __dest, const _Any_data& __source, true_type)
1518 new (__dest._M_access()) _Functor(__source._M_access<_Functor>());
1521 // Clone a function object that is not location-invariant or
1522 // that cannot fit into an _Any_data structure.
1524 _M_clone(_Any_data& __dest, const _Any_data& __source, false_type)
1526 __dest._M_access<_Functor*>() =
1527 new _Functor(*__source._M_access<_Functor*>());
1530 // Destroying a location-invariant object may still require
1533 _M_destroy(_Any_data& __victim, true_type)
1535 __victim._M_access<_Functor>().~_Functor();
1538 // Destroying an object located on the heap.
1540 _M_destroy(_Any_data& __victim, false_type)
1542 delete __victim._M_access<_Functor*>();
1547 _M_manager(_Any_data& __dest, const _Any_data& __source,
1548 _Manager_operation __op)
1553 case __get_type_info:
1554 __dest._M_access<const type_info*>() = &typeid(_Functor);
1557 case __get_functor_ptr:
1558 __dest._M_access<_Functor*>() = _M_get_pointer(__source);
1561 case __clone_functor:
1562 _M_clone(__dest, __source, _Local_storage());
1565 case __destroy_functor:
1566 _M_destroy(__dest, _Local_storage());
1573 _M_init_functor(_Any_data& __functor, const _Functor& __f)
1574 { _M_init_functor(__functor, __f, _Local_storage()); }
1576 template<typename _Signature>
1578 _M_not_empty_function(const function<_Signature>& __f)
1579 { return static_cast<bool>(__f); }
1581 template<typename _Tp>
1583 _M_not_empty_function(const _Tp*& __fp)
1586 template<typename _Class, typename _Tp>
1588 _M_not_empty_function(_Tp _Class::* const& __mp)
1591 template<typename _Tp>
1593 _M_not_empty_function(const _Tp&)
1598 _M_init_functor(_Any_data& __functor, const _Functor& __f, true_type)
1599 { new (__functor._M_access()) _Functor(__f); }
1602 _M_init_functor(_Any_data& __functor, const _Functor& __f, false_type)
1603 { __functor._M_access<_Functor*>() = new _Functor(__f); }
1606 template<typename _Functor>
1607 class _Ref_manager : public _Base_manager<_Functor*>
1609 typedef _Function_base::_Base_manager<_Functor*> _Base;
1613 _M_manager(_Any_data& __dest, const _Any_data& __source,
1614 _Manager_operation __op)
1619 case __get_type_info:
1620 __dest._M_access<const type_info*>() = &typeid(_Functor);
1623 case __get_functor_ptr:
1624 __dest._M_access<_Functor*>() = *_Base::_M_get_pointer(__source);
1625 return is_const<_Functor>::value;
1629 _Base::_M_manager(__dest, __source, __op);
1635 _M_init_functor(_Any_data& __functor, reference_wrapper<_Functor> __f)
1637 // TBD: Use address_of function instead.
1638 _Base::_M_init_functor(__functor, &__f.get());
1642 _Function_base() : _M_manager(0) { }
1647 _M_manager(_M_functor, _M_functor, __destroy_functor);
1651 bool _M_empty() const { return !_M_manager; }
1653 typedef bool (*_Manager_type)(_Any_data&, const _Any_data&,
1654 _Manager_operation);
1656 _Any_data _M_functor;
1657 _Manager_type _M_manager;
1660 template<typename _Signature, typename _Functor>
1661 class _Function_handler;
1663 template<typename _Res, typename _Functor, typename... _ArgTypes>
1664 class _Function_handler<_Res(_ArgTypes...), _Functor>
1665 : public _Function_base::_Base_manager<_Functor>
1667 typedef _Function_base::_Base_manager<_Functor> _Base;
1671 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1673 return (*_Base::_M_get_pointer(__functor))(__args...);
1677 template<typename _Functor, typename... _ArgTypes>
1678 class _Function_handler<void(_ArgTypes...), _Functor>
1679 : public _Function_base::_Base_manager<_Functor>
1681 typedef _Function_base::_Base_manager<_Functor> _Base;
1685 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1687 (*_Base::_M_get_pointer(__functor))(__args...);
1691 template<typename _Res, typename _Functor, typename... _ArgTypes>
1692 class _Function_handler<_Res(_ArgTypes...), reference_wrapper<_Functor> >
1693 : public _Function_base::_Ref_manager<_Functor>
1695 typedef _Function_base::_Ref_manager<_Functor> _Base;
1699 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1702 __callable_functor(**_Base::_M_get_pointer(__functor))(__args...);
1706 template<typename _Functor, typename... _ArgTypes>
1707 class _Function_handler<void(_ArgTypes...), reference_wrapper<_Functor> >
1708 : public _Function_base::_Ref_manager<_Functor>
1710 typedef _Function_base::_Ref_manager<_Functor> _Base;
1714 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1716 __callable_functor(**_Base::_M_get_pointer(__functor))(__args...);
1720 template<typename _Class, typename _Member, typename _Res,
1721 typename... _ArgTypes>
1722 class _Function_handler<_Res(_ArgTypes...), _Member _Class::*>
1723 : public _Function_handler<void(_ArgTypes...), _Member _Class::*>
1725 typedef _Function_handler<void(_ArgTypes...), _Member _Class::*>
1730 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1733 mem_fn(_Base::_M_get_pointer(__functor)->__value)(__args...);
1737 template<typename _Class, typename _Member, typename... _ArgTypes>
1738 class _Function_handler<void(_ArgTypes...), _Member _Class::*>
1739 : public _Function_base::_Base_manager<
1740 _Simple_type_wrapper< _Member _Class::* > >
1742 typedef _Member _Class::* _Functor;
1743 typedef _Simple_type_wrapper<_Functor> _Wrapper;
1744 typedef _Function_base::_Base_manager<_Wrapper> _Base;
1748 _M_manager(_Any_data& __dest, const _Any_data& __source,
1749 _Manager_operation __op)
1754 case __get_type_info:
1755 __dest._M_access<const type_info*>() = &typeid(_Functor);
1758 case __get_functor_ptr:
1759 __dest._M_access<_Functor*>() =
1760 &_Base::_M_get_pointer(__source)->__value;
1764 _Base::_M_manager(__dest, __source, __op);
1770 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1772 tr1::mem_fn(_Base::_M_get_pointer(__functor)->__value)(__args...);
1777 template<typename _Res, typename... _ArgTypes>
1778 class function<_Res(_ArgTypes...)>
1779 : public _Maybe_unary_or_binary_function<_Res, _ArgTypes...>,
1780 private _Function_base
1782 #ifndef __GXX_EXPERIMENTAL_CXX0X__
1783 /// This class is used to implement the safe_bool idiom.
1786 _Hidden_type* _M_bool;
1789 /// This typedef is used to implement the safe_bool idiom.
1790 typedef _Hidden_type* _Hidden_type::* _Safe_bool;
1793 typedef _Res _Signature_type(_ArgTypes...);
1795 struct _Useless { };
1798 typedef _Res result_type;
1800 // [3.7.2.1] construct/copy/destroy
1803 * @brief Default construct creates an empty function call wrapper.
1804 * @post @c !(bool)*this
1806 function() : _Function_base() { }
1809 * @brief Default construct creates an empty function call wrapper.
1810 * @post @c !(bool)*this
1812 function(_M_clear_type*) : _Function_base() { }
1815 * @brief %Function copy constructor.
1816 * @param x A %function object with identical call signature.
1817 * @post @c (bool)*this == (bool)x
1819 * The newly-created %function contains a copy of the target of @a
1820 * x (if it has one).
1822 function(const function& __x);
1825 * @brief Builds a %function that targets a copy of the incoming
1827 * @param f A %function object that is callable with parameters of
1828 * type @c T1, @c T2, ..., @c TN and returns a value convertible
1831 * The newly-created %function object will target a copy of @a
1832 * f. If @a f is @c reference_wrapper<F>, then this function
1833 * object will contain a reference to the function object @c
1834 * f.get(). If @a f is a NULL function pointer or NULL
1835 * pointer-to-member, the newly-created object will be empty.
1837 * If @a f is a non-NULL function pointer or an object of type @c
1838 * reference_wrapper<F>, this function will not throw.
1840 template<typename _Functor>
1841 function(_Functor __f,
1842 typename __gnu_cxx::__enable_if<
1843 !is_integral<_Functor>::value, _Useless>::__type
1847 * @brief %Function assignment operator.
1848 * @param x A %function with identical call signature.
1849 * @post @c (bool)*this == (bool)x
1852 * The target of @a x is copied to @c *this. If @a x has no
1853 * target, then @c *this will be empty.
1855 * If @a x targets a function pointer or a reference to a function
1856 * object, then this operation will not throw an %exception.
1859 operator=(const function& __x)
1861 function(__x).swap(*this);
1866 * @brief %Function assignment to zero.
1867 * @post @c !(bool)*this
1870 * The target of @c *this is deallocated, leaving it empty.
1873 operator=(_M_clear_type*)
1877 _M_manager(_M_functor, _M_functor, __destroy_functor);
1885 * @brief %Function assignment to a new target.
1886 * @param f A %function object that is callable with parameters of
1887 * type @c T1, @c T2, ..., @c TN and returns a value convertible
1891 * This %function object wrapper will target a copy of @a
1892 * f. If @a f is @c reference_wrapper<F>, then this function
1893 * object will contain a reference to the function object @c
1894 * f.get(). If @a f is a NULL function pointer or NULL
1895 * pointer-to-member, @c this object will be empty.
1897 * If @a f is a non-NULL function pointer or an object of type @c
1898 * reference_wrapper<F>, this function will not throw.
1900 template<typename _Functor>
1901 typename __gnu_cxx::__enable_if<!is_integral<_Functor>::value,
1903 operator=(_Functor __f)
1905 function(__f).swap(*this);
1909 // [3.7.2.2] function modifiers
1912 * @brief Swap the targets of two %function objects.
1913 * @param f A %function with identical call signature.
1915 * Swap the targets of @c this function object and @a f. This
1916 * function will not throw an %exception.
1918 void swap(function& __x)
1920 std::swap(_M_functor, __x._M_functor);
1921 std::swap(_M_manager, __x._M_manager);
1922 std::swap(_M_invoker, __x._M_invoker);
1925 // [3.7.2.3] function capacity
1928 * @brief Determine if the %function wrapper has a target.
1930 * @return @c true when this %function object contains a target,
1931 * or @c false when it is empty.
1933 * This function will not throw an %exception.
1935 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1936 explicit operator bool() const
1937 { return !_M_empty(); }
1939 operator _Safe_bool() const
1944 return &_Hidden_type::_M_bool;
1948 // [3.7.2.4] function invocation
1951 * @brief Invokes the function targeted by @c *this.
1952 * @returns the result of the target.
1953 * @throws bad_function_call when @c !(bool)*this
1955 * The function call operator invokes the target function object
1956 * stored by @c this.
1958 _Res operator()(_ArgTypes... __args) const;
1961 // [3.7.2.5] function target access
1963 * @brief Determine the type of the target of this function object
1966 * @returns the type identifier of the target function object, or
1967 * @c typeid(void) if @c !(bool)*this.
1969 * This function will not throw an %exception.
1971 const type_info& target_type() const;
1974 * @brief Access the stored target function object.
1976 * @return Returns a pointer to the stored target function object,
1977 * if @c typeid(Functor).equals(target_type()); otherwise, a NULL
1980 * This function will not throw an %exception.
1982 template<typename _Functor> _Functor* target();
1985 template<typename _Functor> const _Functor* target() const;
1989 // [3.7.2.6] undefined operators
1990 template<typename _Function>
1991 void operator==(const function<_Function>&) const;
1992 template<typename _Function>
1993 void operator!=(const function<_Function>&) const;
1995 typedef _Res (*_Invoker_type)(const _Any_data&, _ArgTypes...);
1996 _Invoker_type _M_invoker;
1999 template<typename _Res, typename... _ArgTypes>
2000 function<_Res(_ArgTypes...)>::
2001 function(const function& __x)
2004 if (static_cast<bool>(__x))
2006 _M_invoker = __x._M_invoker;
2007 _M_manager = __x._M_manager;
2008 __x._M_manager(_M_functor, __x._M_functor, __clone_functor);
2012 template<typename _Res, typename... _ArgTypes>
2013 template<typename _Functor>
2014 function<_Res(_ArgTypes...)>::
2015 function(_Functor __f,
2016 typename __gnu_cxx::__enable_if<
2017 !is_integral<_Functor>::value, _Useless>::__type)
2020 typedef _Function_handler<_Signature_type, _Functor> _My_handler;
2022 if (_My_handler::_M_not_empty_function(__f))
2024 _M_invoker = &_My_handler::_M_invoke;
2025 _M_manager = &_My_handler::_M_manager;
2026 _My_handler::_M_init_functor(_M_functor, __f);
2030 template<typename _Res, typename... _ArgTypes>
2032 function<_Res(_ArgTypes...)>::
2033 operator()(_ArgTypes... __args) const
2038 throw bad_function_call();
2043 return _M_invoker(_M_functor, __args...);
2047 template<typename _Res, typename... _ArgTypes>
2049 function<_Res(_ArgTypes...)>::
2054 _Any_data __typeinfo_result;
2055 _M_manager(__typeinfo_result, _M_functor, __get_type_info);
2056 return *__typeinfo_result._M_access<const type_info*>();
2059 return typeid(void);
2062 template<typename _Res, typename... _ArgTypes>
2063 template<typename _Functor>
2065 function<_Res(_ArgTypes...)>::
2068 if (typeid(_Functor) == target_type() && _M_manager)
2071 if (_M_manager(__ptr, _M_functor, __get_functor_ptr)
2072 && !is_const<_Functor>::value)
2075 return __ptr._M_access<_Functor*>();
2081 template<typename _Res, typename... _ArgTypes>
2082 template<typename _Functor>
2084 function<_Res(_ArgTypes...)>::
2087 if (typeid(_Functor) == target_type() && _M_manager)
2090 _M_manager(__ptr, _M_functor, __get_functor_ptr);
2091 return __ptr._M_access<const _Functor*>();
2098 // [3.7.2.7] null pointer comparisons
2101 * @brief Compares a polymorphic function object wrapper against 0
2102 * (the NULL pointer).
2103 * @returns @c true if the wrapper has no target, @c false otherwise
2105 * This function will not throw an %exception.
2107 template<typename _Signature>
2109 operator==(const function<_Signature>& __f, _M_clear_type*)
2110 { return !static_cast<bool>(__f); }
2113 template<typename _Signature>
2115 operator==(_M_clear_type*, const function<_Signature>& __f)
2116 { return !static_cast<bool>(__f); }
2119 * @brief Compares a polymorphic function object wrapper against 0
2120 * (the NULL pointer).
2121 * @returns @c false if the wrapper has no target, @c true otherwise
2123 * This function will not throw an %exception.
2125 template<typename _Signature>
2127 operator!=(const function<_Signature>& __f, _M_clear_type*)
2128 { return static_cast<bool>(__f); }
2131 template<typename _Signature>
2133 operator!=(_M_clear_type*, const function<_Signature>& __f)
2134 { return static_cast<bool>(__f); }
2136 // [3.7.2.8] specialized algorithms
2139 * @brief Swap the targets of two polymorphic function object wrappers.
2141 * This function will not throw an %exception.
2143 template<typename _Signature>
2145 swap(function<_Signature>& __x, function<_Signature>& __y)
2148 _GLIBCXX_END_NAMESPACE_VERSION
2152 #endif // _GLIBCXX_TR1_FUNCTIONAL