1 // <functional> -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2009
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/>.
28 * Silicon Graphics Computer Systems, Inc.
30 * Permission to use, copy, modify, distribute and sell this software
31 * and its documentation for any purpose is hereby granted without fee,
32 * provided that the above copyright notice appear in all copies and
33 * that both that copyright notice and this permission notice appear
34 * in supporting documentation. Silicon Graphics makes no
35 * representations about the suitability of this software for any
36 * purpose. It is provided "as is" without express or implied warranty.
40 /** @file include/functional
41 * This is a Standard C++ Library header.
44 #ifndef _GLIBCXX_FUNCTIONAL
45 #define _GLIBCXX_FUNCTIONAL 1
47 #pragma GCC system_header
49 #include <bits/c++config.h>
50 #include <bits/stl_function.h>
52 #ifdef __GXX_EXPERIMENTAL_CXX0X__
57 #include <type_traits>
58 #include <bits/functional_hash.h>
59 #include <ext/type_traits.h>
63 template<typename _MemberPointer>
67 * Actual implementation of _Has_result_type, which uses SFINAE to
68 * determine if the type _Tp has a publicly-accessible member type
71 template<typename _Tp>
72 class _Has_result_type_helper : __sfinae_types
74 template<typename _Up>
78 template<typename _Up>
79 static __one __test(_Wrap_type<typename _Up::result_type>*);
81 template<typename _Up>
82 static __two __test(...);
85 static const bool value = sizeof(__test<_Tp>(0)) == 1;
88 template<typename _Tp>
89 struct _Has_result_type
90 : integral_constant<bool,
91 _Has_result_type_helper<typename remove_cv<_Tp>::type>::value>
97 /// If we have found a result_type, extract it.
98 template<bool _Has_result_type, typename _Functor>
99 struct _Maybe_get_result_type
102 template<typename _Functor>
103 struct _Maybe_get_result_type<true, _Functor>
105 typedef typename _Functor::result_type result_type;
109 * Base class for any function object that has a weak result type, as
110 * defined in 3.3/3 of TR1.
112 template<typename _Functor>
113 struct _Weak_result_type_impl
114 : _Maybe_get_result_type<_Has_result_type<_Functor>::value, _Functor>
118 /// Retrieve the result type for a function type.
119 template<typename _Res, typename... _ArgTypes>
120 struct _Weak_result_type_impl<_Res(_ArgTypes...)>
122 typedef _Res result_type;
125 /// Retrieve the result type for a function reference.
126 template<typename _Res, typename... _ArgTypes>
127 struct _Weak_result_type_impl<_Res(&)(_ArgTypes...)>
129 typedef _Res result_type;
132 /// Retrieve the result type for a function pointer.
133 template<typename _Res, typename... _ArgTypes>
134 struct _Weak_result_type_impl<_Res(*)(_ArgTypes...)>
136 typedef _Res result_type;
139 /// Retrieve result type for a member function pointer.
140 template<typename _Res, typename _Class, typename... _ArgTypes>
141 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...)>
143 typedef _Res result_type;
146 /// Retrieve result type for a const member function pointer.
147 template<typename _Res, typename _Class, typename... _ArgTypes>
148 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) const>
150 typedef _Res result_type;
153 /// Retrieve result type for a volatile member function pointer.
154 template<typename _Res, typename _Class, typename... _ArgTypes>
155 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) volatile>
157 typedef _Res result_type;
160 /// Retrieve result type for a const volatile member function pointer.
161 template<typename _Res, typename _Class, typename... _ArgTypes>
162 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...)const volatile>
164 typedef _Res result_type;
168 * Strip top-level cv-qualifiers from the function object and let
169 * _Weak_result_type_impl perform the real work.
171 template<typename _Functor>
172 struct _Weak_result_type
173 : _Weak_result_type_impl<typename remove_cv<_Functor>::type>
177 template<typename _Signature>
181 * Actual implementation of result_of. When _Has_result_type is
182 * true, gets its result from _Weak_result_type. Otherwise, uses
183 * the function object's member template result to extract the
186 template<bool _Has_result_type, typename _Signature>
187 struct _Result_of_impl;
189 // Handle member data pointers using _Mem_fn's logic
190 template<typename _Res, typename _Class, typename _T1>
191 struct _Result_of_impl<false, _Res _Class::*(_T1)>
193 typedef typename _Mem_fn<_Res _Class::*>
194 ::template _Result_type<_T1>::type type;
198 * Determine whether we can determine a result type from @c Functor
201 template<typename _Functor, typename... _ArgTypes>
202 class result_of<_Functor(_ArgTypes...)>
203 : public _Result_of_impl<
204 _Has_result_type<_Weak_result_type<_Functor> >::value,
205 _Functor(_ArgTypes...)>
209 /// We already know the result type for @c Functor; use it.
210 template<typename _Functor, typename... _ArgTypes>
211 struct _Result_of_impl<true, _Functor(_ArgTypes...)>
213 typedef typename _Weak_result_type<_Functor>::result_type type;
217 * We need to compute the result type for this invocation the hard
220 template<typename _Functor, typename... _ArgTypes>
221 struct _Result_of_impl<false, _Functor(_ArgTypes...)>
223 typedef typename _Functor
224 ::template result<_Functor(_ArgTypes...)>::type type;
228 * It is unsafe to access ::result when there are zero arguments, so we
229 * return @c void instead.
231 template<typename _Functor>
232 struct _Result_of_impl<false, _Functor()>
237 /// Determines if the type _Tp derives from unary_function.
238 template<typename _Tp>
239 struct _Derives_from_unary_function : __sfinae_types
242 template<typename _T1, typename _Res>
243 static __one __test(const volatile unary_function<_T1, _Res>*);
245 // It's tempting to change "..." to const volatile void*, but
246 // that fails when _Tp is a function type.
247 static __two __test(...);
250 static const bool value = sizeof(__test((_Tp*)0)) == 1;
253 /// Determines if the type _Tp derives from binary_function.
254 template<typename _Tp>
255 struct _Derives_from_binary_function : __sfinae_types
258 template<typename _T1, typename _T2, typename _Res>
259 static __one __test(const volatile binary_function<_T1, _T2, _Res>*);
261 // It's tempting to change "..." to const volatile void*, but
262 // that fails when _Tp is a function type.
263 static __two __test(...);
266 static const bool value = sizeof(__test((_Tp*)0)) == 1;
269 /// Turns a function type into a function pointer type
270 template<typename _Tp, bool _IsFunctionType = is_function<_Tp>::value>
271 struct _Function_to_function_pointer
276 template<typename _Tp>
277 struct _Function_to_function_pointer<_Tp, true>
283 * Invoke a function object, which may be either a member pointer or a
284 * function object. The first parameter will tell which.
286 template<typename _Functor, typename... _Args>
288 typename __gnu_cxx::__enable_if<
289 (!is_member_pointer<_Functor>::value
290 && !is_function<_Functor>::value
291 && !is_function<typename remove_pointer<_Functor>::type>::value),
292 typename result_of<_Functor(_Args...)>::type
294 __invoke(_Functor& __f, _Args&... __args)
296 return __f(__args...);
299 template<typename _Functor, typename... _Args>
301 typename __gnu_cxx::__enable_if<
302 (is_member_pointer<_Functor>::value
303 && !is_function<_Functor>::value
304 && !is_function<typename remove_pointer<_Functor>::type>::value),
305 typename result_of<_Functor(_Args...)>::type
307 __invoke(_Functor& __f, _Args&... __args)
309 return mem_fn(__f)(__args...);
312 // To pick up function references (that will become function pointers)
313 template<typename _Functor, typename... _Args>
315 typename __gnu_cxx::__enable_if<
316 (is_pointer<_Functor>::value
317 && is_function<typename remove_pointer<_Functor>::type>::value),
318 typename result_of<_Functor(_Args...)>::type
320 __invoke(_Functor __f, _Args&... __args)
322 return __f(__args...);
326 * Knowing which of unary_function and binary_function _Tp derives
327 * from, derives from the same and ensures that reference_wrapper
328 * will have a weak result type. See cases below.
330 template<bool _Unary, bool _Binary, typename _Tp>
331 struct _Reference_wrapper_base_impl;
333 // Not a unary_function or binary_function, so try a weak result type.
334 template<typename _Tp>
335 struct _Reference_wrapper_base_impl<false, false, _Tp>
336 : _Weak_result_type<_Tp>
339 // unary_function but not binary_function
340 template<typename _Tp>
341 struct _Reference_wrapper_base_impl<true, false, _Tp>
342 : unary_function<typename _Tp::argument_type,
343 typename _Tp::result_type>
346 // binary_function but not unary_function
347 template<typename _Tp>
348 struct _Reference_wrapper_base_impl<false, true, _Tp>
349 : binary_function<typename _Tp::first_argument_type,
350 typename _Tp::second_argument_type,
351 typename _Tp::result_type>
354 // Both unary_function and binary_function. Import result_type to
356 template<typename _Tp>
357 struct _Reference_wrapper_base_impl<true, true, _Tp>
358 : unary_function<typename _Tp::argument_type,
359 typename _Tp::result_type>,
360 binary_function<typename _Tp::first_argument_type,
361 typename _Tp::second_argument_type,
362 typename _Tp::result_type>
364 typedef typename _Tp::result_type result_type;
368 * Derives from unary_function or binary_function when it
369 * can. Specializations handle all of the easy cases. The primary
370 * template determines what to do with a class type, which may
371 * derive from both unary_function and binary_function.
373 template<typename _Tp>
374 struct _Reference_wrapper_base
375 : _Reference_wrapper_base_impl<
376 _Derives_from_unary_function<_Tp>::value,
377 _Derives_from_binary_function<_Tp>::value,
381 // - a function type (unary)
382 template<typename _Res, typename _T1>
383 struct _Reference_wrapper_base<_Res(_T1)>
384 : unary_function<_T1, _Res>
387 // - a function type (binary)
388 template<typename _Res, typename _T1, typename _T2>
389 struct _Reference_wrapper_base<_Res(_T1, _T2)>
390 : binary_function<_T1, _T2, _Res>
393 // - a function pointer type (unary)
394 template<typename _Res, typename _T1>
395 struct _Reference_wrapper_base<_Res(*)(_T1)>
396 : unary_function<_T1, _Res>
399 // - a function pointer type (binary)
400 template<typename _Res, typename _T1, typename _T2>
401 struct _Reference_wrapper_base<_Res(*)(_T1, _T2)>
402 : binary_function<_T1, _T2, _Res>
405 // - a pointer to member function type (unary, no qualifiers)
406 template<typename _Res, typename _T1>
407 struct _Reference_wrapper_base<_Res (_T1::*)()>
408 : unary_function<_T1*, _Res>
411 // - a pointer to member function type (binary, no qualifiers)
412 template<typename _Res, typename _T1, typename _T2>
413 struct _Reference_wrapper_base<_Res (_T1::*)(_T2)>
414 : binary_function<_T1*, _T2, _Res>
417 // - a pointer to member function type (unary, const)
418 template<typename _Res, typename _T1>
419 struct _Reference_wrapper_base<_Res (_T1::*)() const>
420 : unary_function<const _T1*, _Res>
423 // - a pointer to member function type (binary, const)
424 template<typename _Res, typename _T1, typename _T2>
425 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const>
426 : binary_function<const _T1*, _T2, _Res>
429 // - a pointer to member function type (unary, volatile)
430 template<typename _Res, typename _T1>
431 struct _Reference_wrapper_base<_Res (_T1::*)() volatile>
432 : unary_function<volatile _T1*, _Res>
435 // - a pointer to member function type (binary, volatile)
436 template<typename _Res, typename _T1, typename _T2>
437 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) volatile>
438 : binary_function<volatile _T1*, _T2, _Res>
441 // - a pointer to member function type (unary, const volatile)
442 template<typename _Res, typename _T1>
443 struct _Reference_wrapper_base<_Res (_T1::*)() const volatile>
444 : unary_function<const volatile _T1*, _Res>
447 // - a pointer to member function type (binary, const volatile)
448 template<typename _Res, typename _T1, typename _T2>
449 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const volatile>
450 : binary_function<const volatile _T1*, _T2, _Res>
453 /// reference_wrapper
454 template<typename _Tp>
455 class reference_wrapper
456 : public _Reference_wrapper_base<typename remove_cv<_Tp>::type>
458 // If _Tp is a function type, we can't form result_of<_Tp(...)>,
459 // so turn it into a function pointer type.
460 typedef typename _Function_to_function_pointer<_Tp>::type
468 reference_wrapper(_Tp& __indata): _M_data(&__indata)
471 reference_wrapper(const reference_wrapper<_Tp>& __inref):
472 _M_data(__inref._M_data)
476 operator=(const reference_wrapper<_Tp>& __inref)
478 _M_data = __inref._M_data;
482 operator _Tp&() const
483 { return this->get(); }
489 template<typename... _Args>
490 typename result_of<_M_func_type(_Args...)>::type
491 operator()(_Args&... __args) const
493 return __invoke(get(), __args...);
498 // Denotes a reference should be taken to a variable.
499 template<typename _Tp>
500 inline reference_wrapper<_Tp>
502 { return reference_wrapper<_Tp>(__t); }
504 // Denotes a const reference should be taken to a variable.
505 template<typename _Tp>
506 inline reference_wrapper<const _Tp>
508 { return reference_wrapper<const _Tp>(__t); }
510 template<typename _Tp>
511 inline reference_wrapper<_Tp>
512 ref(reference_wrapper<_Tp> __t)
513 { return ref(__t.get()); }
515 template<typename _Tp>
516 inline reference_wrapper<const _Tp>
517 cref(reference_wrapper<_Tp> __t)
518 { return cref(__t.get()); }
520 template<typename _Tp, bool>
521 struct _Mem_fn_const_or_non
523 typedef const _Tp& type;
526 template<typename _Tp>
527 struct _Mem_fn_const_or_non<_Tp, false>
533 * Derives from @c unary_function or @c binary_function, or perhaps
534 * nothing, depending on the number of arguments provided. The
535 * primary template is the basis case, which derives nothing.
537 template<typename _Res, typename... _ArgTypes>
538 struct _Maybe_unary_or_binary_function { };
540 /// Derives from @c unary_function, as appropriate.
541 template<typename _Res, typename _T1>
542 struct _Maybe_unary_or_binary_function<_Res, _T1>
543 : std::unary_function<_T1, _Res> { };
545 /// Derives from @c binary_function, as appropriate.
546 template<typename _Res, typename _T1, typename _T2>
547 struct _Maybe_unary_or_binary_function<_Res, _T1, _T2>
548 : std::binary_function<_T1, _T2, _Res> { };
550 /// Implementation of @c mem_fn for member function pointers.
551 template<typename _Res, typename _Class, typename... _ArgTypes>
552 class _Mem_fn<_Res (_Class::*)(_ArgTypes...)>
553 : public _Maybe_unary_or_binary_function<_Res, _Class*, _ArgTypes...>
555 typedef _Res (_Class::*_Functor)(_ArgTypes...);
557 template<typename _Tp>
559 _M_call(_Tp& __object, const volatile _Class *,
560 _ArgTypes... __args) const
561 { return (__object.*__pmf)(__args...); }
563 template<typename _Tp>
565 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
566 { return ((*__ptr).*__pmf)(__args...); }
569 typedef _Res result_type;
571 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
575 operator()(_Class& __object, _ArgTypes... __args) const
576 { return (__object.*__pmf)(__args...); }
580 operator()(_Class* __object, _ArgTypes... __args) const
581 { return (__object->*__pmf)(__args...); }
583 // Handle smart pointers, references and pointers to derived
584 template<typename _Tp>
586 operator()(_Tp& __object, _ArgTypes... __args) const
587 { return _M_call(__object, &__object, __args...); }
593 /// Implementation of @c mem_fn for const member function pointers.
594 template<typename _Res, typename _Class, typename... _ArgTypes>
595 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) const>
596 : public _Maybe_unary_or_binary_function<_Res, const _Class*,
599 typedef _Res (_Class::*_Functor)(_ArgTypes...) const;
601 template<typename _Tp>
603 _M_call(_Tp& __object, const volatile _Class *,
604 _ArgTypes... __args) const
605 { return (__object.*__pmf)(__args...); }
607 template<typename _Tp>
609 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
610 { return ((*__ptr).*__pmf)(__args...); }
613 typedef _Res result_type;
615 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
619 operator()(const _Class& __object, _ArgTypes... __args) const
620 { return (__object.*__pmf)(__args...); }
624 operator()(const _Class* __object, _ArgTypes... __args) const
625 { return (__object->*__pmf)(__args...); }
627 // Handle smart pointers, references and pointers to derived
628 template<typename _Tp>
629 _Res operator()(_Tp& __object, _ArgTypes... __args) const
630 { return _M_call(__object, &__object, __args...); }
636 /// Implementation of @c mem_fn for volatile member function pointers.
637 template<typename _Res, typename _Class, typename... _ArgTypes>
638 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) volatile>
639 : public _Maybe_unary_or_binary_function<_Res, volatile _Class*,
642 typedef _Res (_Class::*_Functor)(_ArgTypes...) volatile;
644 template<typename _Tp>
646 _M_call(_Tp& __object, const volatile _Class *,
647 _ArgTypes... __args) const
648 { return (__object.*__pmf)(__args...); }
650 template<typename _Tp>
652 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
653 { return ((*__ptr).*__pmf)(__args...); }
656 typedef _Res result_type;
658 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
662 operator()(volatile _Class& __object, _ArgTypes... __args) const
663 { return (__object.*__pmf)(__args...); }
667 operator()(volatile _Class* __object, _ArgTypes... __args) const
668 { return (__object->*__pmf)(__args...); }
670 // Handle smart pointers, references and pointers to derived
671 template<typename _Tp>
673 operator()(_Tp& __object, _ArgTypes... __args) const
674 { return _M_call(__object, &__object, __args...); }
680 /// Implementation of @c mem_fn for const volatile member function pointers.
681 template<typename _Res, typename _Class, typename... _ArgTypes>
682 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) const volatile>
683 : public _Maybe_unary_or_binary_function<_Res, const volatile _Class*,
686 typedef _Res (_Class::*_Functor)(_ArgTypes...) const volatile;
688 template<typename _Tp>
690 _M_call(_Tp& __object, const volatile _Class *,
691 _ArgTypes... __args) const
692 { return (__object.*__pmf)(__args...); }
694 template<typename _Tp>
696 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
697 { return ((*__ptr).*__pmf)(__args...); }
700 typedef _Res result_type;
702 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
706 operator()(const volatile _Class& __object, _ArgTypes... __args) const
707 { return (__object.*__pmf)(__args...); }
711 operator()(const volatile _Class* __object, _ArgTypes... __args) const
712 { return (__object->*__pmf)(__args...); }
714 // Handle smart pointers, references and pointers to derived
715 template<typename _Tp>
716 _Res operator()(_Tp& __object, _ArgTypes... __args) const
717 { return _M_call(__object, &__object, __args...); }
724 template<typename _Res, typename _Class>
725 class _Mem_fn<_Res _Class::*>
727 // This bit of genius is due to Peter Dimov, improved slightly by
729 template<typename _Tp>
731 _M_call(_Tp& __object, _Class *) const
732 { return __object.*__pm; }
734 template<typename _Tp, typename _Up>
736 _M_call(_Tp& __object, _Up * const *) const
737 { return (*__object).*__pm; }
739 template<typename _Tp, typename _Up>
741 _M_call(_Tp& __object, const _Up * const *) const
742 { return (*__object).*__pm; }
744 template<typename _Tp>
746 _M_call(_Tp& __object, const _Class *) const
747 { return __object.*__pm; }
749 template<typename _Tp>
751 _M_call(_Tp& __ptr, const volatile void*) const
752 { return (*__ptr).*__pm; }
754 template<typename _Tp> static _Tp& __get_ref();
756 template<typename _Tp>
757 static __sfinae_types::__one __check_const(_Tp&, _Class*);
758 template<typename _Tp, typename _Up>
759 static __sfinae_types::__one __check_const(_Tp&, _Up * const *);
760 template<typename _Tp, typename _Up>
761 static __sfinae_types::__two __check_const(_Tp&, const _Up * const *);
762 template<typename _Tp>
763 static __sfinae_types::__two __check_const(_Tp&, const _Class*);
764 template<typename _Tp>
765 static __sfinae_types::__two __check_const(_Tp&, const volatile void*);
768 template<typename _Tp>
770 : _Mem_fn_const_or_non<_Res,
771 (sizeof(__sfinae_types::__two)
772 == sizeof(__check_const<_Tp>(__get_ref<_Tp>(), (_Tp*)0)))>
775 template<typename _Signature>
778 template<typename _CVMem, typename _Tp>
779 struct result<_CVMem(_Tp)>
780 : public _Result_type<_Tp> { };
782 template<typename _CVMem, typename _Tp>
783 struct result<_CVMem(_Tp&)>
784 : public _Result_type<_Tp> { };
787 _Mem_fn(_Res _Class::*__pm) : __pm(__pm) { }
791 operator()(_Class& __object) const
792 { return __object.*__pm; }
795 operator()(const _Class& __object) const
796 { return __object.*__pm; }
800 operator()(_Class* __object) const
801 { return __object->*__pm; }
804 operator()(const _Class* __object) const
805 { return __object->*__pm; }
807 // Handle smart pointers and derived
808 template<typename _Tp>
809 typename _Result_type<_Tp>::type
810 operator()(_Tp& __unknown) const
811 { return _M_call(__unknown, &__unknown); }
818 * @brief Returns a function object that forwards to the member
821 template<typename _Tp, typename _Class>
822 inline _Mem_fn<_Tp _Class::*>
823 mem_fn(_Tp _Class::* __pm)
825 return _Mem_fn<_Tp _Class::*>(__pm);
829 * @brief Determines if the given type _Tp is a function object
830 * should be treated as a subexpression when evaluating calls to
831 * function objects returned by bind(). [TR1 3.6.1]
833 template<typename _Tp>
834 struct is_bind_expression
835 { static const bool value = false; };
837 template<typename _Tp>
838 const bool is_bind_expression<_Tp>::value;
841 * @brief Determines if the given type _Tp is a placeholder in a
842 * bind() expression and, if so, which placeholder it is. [TR1 3.6.2]
844 template<typename _Tp>
845 struct is_placeholder
846 { static const int value = 0; };
848 template<typename _Tp>
849 const int is_placeholder<_Tp>::value;
851 /// The type of placeholder objects defined by libstdc++.
852 template<int _Num> struct _Placeholder { };
854 /** @namespace std::placeholders
855 * @brief ISO C++ 0x entities sub namespace for functional.
857 * Define a large number of placeholders. There is no way to
858 * simplify this with variadic templates, because we're introducing
859 * unique names for each.
861 namespace placeholders
874 _Placeholder<10> _10;
875 _Placeholder<11> _11;
876 _Placeholder<12> _12;
877 _Placeholder<13> _13;
878 _Placeholder<14> _14;
879 _Placeholder<15> _15;
880 _Placeholder<16> _16;
881 _Placeholder<17> _17;
882 _Placeholder<18> _18;
883 _Placeholder<19> _19;
884 _Placeholder<20> _20;
885 _Placeholder<21> _21;
886 _Placeholder<22> _22;
887 _Placeholder<23> _23;
888 _Placeholder<24> _24;
889 _Placeholder<25> _25;
890 _Placeholder<26> _26;
891 _Placeholder<27> _27;
892 _Placeholder<28> _28;
893 _Placeholder<29> _29;
898 * Partial specialization of is_placeholder that provides the placeholder
899 * number for the placeholder objects defined by libstdc++.
902 struct is_placeholder<_Placeholder<_Num> >
903 { static const int value = _Num; };
906 const int is_placeholder<_Placeholder<_Num> >::value;
909 * Stores a tuple of indices. Used by bind() to extract the elements
912 template<int... _Indexes>
913 struct _Index_tuple { };
915 /// Builds an _Index_tuple<0, 1, 2, ..., _Num-1>.
916 template<std::size_t _Num, typename _Tuple = _Index_tuple<> >
917 struct _Build_index_tuple;
919 template<std::size_t _Num, int... _Indexes>
920 struct _Build_index_tuple<_Num, _Index_tuple<_Indexes...> >
921 : _Build_index_tuple<_Num - 1,
922 _Index_tuple<_Indexes..., sizeof...(_Indexes)> >
926 template<int... _Indexes>
927 struct _Build_index_tuple<0, _Index_tuple<_Indexes...> >
929 typedef _Index_tuple<_Indexes...> __type;
933 * Used by _Safe_tuple_element to indicate that there is no tuple
934 * element at this position.
936 struct _No_tuple_element;
939 * Implementation helper for _Safe_tuple_element. This primary
940 * template handles the case where it is safe to use @c
943 template<int __i, typename _Tuple, bool _IsSafe>
944 struct _Safe_tuple_element_impl
945 : tuple_element<__i, _Tuple> { };
948 * Implementation helper for _Safe_tuple_element. This partial
949 * specialization handles the case where it is not safe to use @c
950 * tuple_element. We just return @c _No_tuple_element.
952 template<int __i, typename _Tuple>
953 struct _Safe_tuple_element_impl<__i, _Tuple, false>
955 typedef _No_tuple_element type;
959 * Like tuple_element, but returns @c _No_tuple_element when
960 * tuple_element would return an error.
962 template<int __i, typename _Tuple>
963 struct _Safe_tuple_element
964 : _Safe_tuple_element_impl<__i, _Tuple,
965 (__i >= 0 && __i < tuple_size<_Tuple>::value)>
970 * Maps an argument to bind() into an actual argument to the bound
971 * function object [TR1 3.6.3/5]. Only the first parameter should
972 * be specified: the rest are used to determine among the various
973 * implementations. Note that, although this class is a function
974 * object, it isn't entirely normal because it takes only two
975 * parameters regardless of the number of parameters passed to the
976 * bind expression. The first parameter is the bound argument and
977 * the second parameter is a tuple containing references to the
978 * rest of the arguments.
980 template<typename _Arg,
981 bool _IsBindExp = is_bind_expression<_Arg>::value,
982 bool _IsPlaceholder = (is_placeholder<_Arg>::value > 0)>
986 * If the argument is reference_wrapper<_Tp>, returns the
987 * underlying reference. [TR1 3.6.3/5 bullet 1]
989 template<typename _Tp>
990 class _Mu<reference_wrapper<_Tp>, false, false>
993 typedef _Tp& result_type;
995 /* Note: This won't actually work for const volatile
996 * reference_wrappers, because reference_wrapper::get() is const
997 * but not volatile-qualified. This might be a defect in the TR.
999 template<typename _CVRef, typename _Tuple>
1001 operator()(_CVRef& __arg, const _Tuple&) const volatile
1002 { return __arg.get(); }
1006 * If the argument is a bind expression, we invoke the underlying
1007 * function object with the same cv-qualifiers as we are given and
1008 * pass along all of our arguments (unwrapped). [TR1 3.6.3/5 bullet 2]
1010 template<typename _Arg>
1011 class _Mu<_Arg, true, false>
1014 template<typename _Signature> class result;
1016 // Determine the result type when we pass the arguments along. This
1017 // involves passing along the cv-qualifiers placed on _Mu and
1018 // unwrapping the argument bundle.
1019 template<typename _CVMu, typename _CVArg, typename... _Args>
1020 class result<_CVMu(_CVArg, tuple<_Args...>)>
1021 : public result_of<_CVArg(_Args...)> { };
1023 template<typename _CVArg, typename... _Args>
1024 typename result_of<_CVArg(_Args...)>::type
1025 operator()(_CVArg& __arg,
1026 const tuple<_Args...>& __tuple) const volatile
1028 // Construct an index tuple and forward to __call
1029 typedef typename _Build_index_tuple<sizeof...(_Args)>::__type
1031 return this->__call(__arg, __tuple, _Indexes());
1035 // Invokes the underlying function object __arg by unpacking all
1036 // of the arguments in the tuple.
1037 template<typename _CVArg, typename... _Args, int... _Indexes>
1038 typename result_of<_CVArg(_Args...)>::type
1039 __call(_CVArg& __arg, const tuple<_Args...>& __tuple,
1040 const _Index_tuple<_Indexes...>&) const volatile
1042 return __arg(get<_Indexes>(__tuple)...);
1047 * If the argument is a placeholder for the Nth argument, returns
1048 * a reference to the Nth argument to the bind function object.
1049 * [TR1 3.6.3/5 bullet 3]
1051 template<typename _Arg>
1052 class _Mu<_Arg, false, true>
1055 template<typename _Signature> class result;
1057 template<typename _CVMu, typename _CVArg, typename _Tuple>
1058 class result<_CVMu(_CVArg, _Tuple)>
1060 // Add a reference, if it hasn't already been done for us.
1061 // This allows us to be a little bit sloppy in constructing
1062 // the tuple that we pass to result_of<...>.
1063 typedef typename _Safe_tuple_element<(is_placeholder<_Arg>::value
1068 typedef typename add_lvalue_reference<__base_type>::type type;
1071 template<typename _Tuple>
1072 typename result<_Mu(_Arg, _Tuple)>::type
1073 operator()(const volatile _Arg&, const _Tuple& __tuple) const volatile
1075 return ::std::get<(is_placeholder<_Arg>::value - 1)>(__tuple);
1080 * If the argument is just a value, returns a reference to that
1081 * value. The cv-qualifiers on the reference are the same as the
1082 * cv-qualifiers on the _Mu object. [TR1 3.6.3/5 bullet 4]
1084 template<typename _Arg>
1085 class _Mu<_Arg, false, false>
1088 template<typename _Signature> struct result;
1090 template<typename _CVMu, typename _CVArg, typename _Tuple>
1091 struct result<_CVMu(_CVArg, _Tuple)>
1093 typedef typename add_lvalue_reference<_CVArg>::type type;
1096 // Pick up the cv-qualifiers of the argument
1097 template<typename _CVArg, typename _Tuple>
1099 operator()(_CVArg& __arg, const _Tuple&) const volatile
1104 * Maps member pointers into instances of _Mem_fn but leaves all
1105 * other function objects untouched. Used by tr1::bind(). The
1106 * primary template handles the non--member-pointer case.
1108 template<typename _Tp>
1109 struct _Maybe_wrap_member_pointer
1114 __do_wrap(const _Tp& __x)
1119 * Maps member pointers into instances of _Mem_fn but leaves all
1120 * other function objects untouched. Used by tr1::bind(). This
1121 * partial specialization handles the member pointer case.
1123 template<typename _Tp, typename _Class>
1124 struct _Maybe_wrap_member_pointer<_Tp _Class::*>
1126 typedef _Mem_fn<_Tp _Class::*> type;
1129 __do_wrap(_Tp _Class::* __pm)
1130 { return type(__pm); }
1133 /// Type of the function object returned from bind().
1134 template<typename _Signature>
1137 template<typename _Functor, typename... _Bound_args>
1138 class _Bind<_Functor(_Bound_args...)>
1139 : public _Weak_result_type<_Functor>
1141 typedef _Bind __self_type;
1142 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
1146 tuple<_Bound_args...> _M_bound_args;
1149 template<typename... _Args, int... _Indexes>
1151 _Functor(typename result_of<_Mu<_Bound_args>
1152 (_Bound_args, tuple<_Args...>)>::type...)
1154 __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>)
1156 return _M_f(_Mu<_Bound_args>()
1157 (get<_Indexes>(_M_bound_args), __args)...);
1161 template<typename... _Args, int... _Indexes>
1163 const _Functor(typename result_of<_Mu<_Bound_args>
1164 (const _Bound_args, tuple<_Args...>)
1166 __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>) const
1168 return _M_f(_Mu<_Bound_args>()
1169 (get<_Indexes>(_M_bound_args), __args)...);
1173 template<typename... _Args, int... _Indexes>
1175 volatile _Functor(typename result_of<_Mu<_Bound_args>
1176 (volatile _Bound_args, tuple<_Args...>)
1178 __call(const tuple<_Args...>& __args,
1179 _Index_tuple<_Indexes...>) volatile
1181 return _M_f(_Mu<_Bound_args>()
1182 (get<_Indexes>(_M_bound_args), __args)...);
1185 // Call as const volatile
1186 template<typename... _Args, int... _Indexes>
1188 const volatile _Functor(typename result_of<_Mu<_Bound_args>
1189 (const volatile _Bound_args,
1192 __call(const tuple<_Args...>& __args,
1193 _Index_tuple<_Indexes...>) const volatile
1195 return _M_f(_Mu<_Bound_args>()
1196 (get<_Indexes>(_M_bound_args), __args)...);
1200 explicit _Bind(_Functor __f, _Bound_args... __bound_args)
1201 : _M_f(__f), _M_bound_args(__bound_args...) { }
1204 template<typename... _Args>
1206 _Functor(typename result_of<_Mu<_Bound_args>
1207 (_Bound_args, tuple<_Args...>)>::type...)
1209 operator()(_Args&... __args)
1211 return this->__call(tie(__args...), _Bound_indexes());
1215 template<typename... _Args>
1217 const _Functor(typename result_of<_Mu<_Bound_args>
1218 (const _Bound_args, tuple<_Args...>)>::type...)
1220 operator()(_Args&... __args) const
1222 return this->__call(tie(__args...), _Bound_indexes());
1227 template<typename... _Args>
1229 volatile _Functor(typename result_of<_Mu<_Bound_args>
1230 (volatile _Bound_args, tuple<_Args...>)>::type...)
1232 operator()(_Args&... __args) volatile
1234 return this->__call(tie(__args...), _Bound_indexes());
1238 // Call as const volatile
1239 template<typename... _Args>
1241 const volatile _Functor(typename result_of<_Mu<_Bound_args>
1242 (const volatile _Bound_args,
1243 tuple<_Args...>)>::type...)
1245 operator()(_Args&... __args) const volatile
1247 return this->__call(tie(__args...), _Bound_indexes());
1251 /// Type of the function object returned from bind<R>().
1252 template<typename _Result, typename _Signature>
1253 struct _Bind_result;
1255 template<typename _Result, typename _Functor, typename... _Bound_args>
1256 class _Bind_result<_Result, _Functor(_Bound_args...)>
1258 typedef _Bind_result __self_type;
1259 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
1263 tuple<_Bound_args...> _M_bound_args;
1266 template<typename... _Args, int... _Indexes>
1268 __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>)
1270 return _M_f(_Mu<_Bound_args>()
1271 (get<_Indexes>(_M_bound_args), __args)...);
1275 template<typename... _Args, int... _Indexes>
1277 __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>) const
1279 return _M_f(_Mu<_Bound_args>()
1280 (get<_Indexes>(_M_bound_args), __args)...);
1284 template<typename... _Args, int... _Indexes>
1286 __call(const tuple<_Args...>& __args,
1287 _Index_tuple<_Indexes...>) volatile
1289 return _M_f(_Mu<_Bound_args>()
1290 (get<_Indexes>(_M_bound_args), __args)...);
1293 // Call as const volatile
1294 template<typename... _Args, int... _Indexes>
1296 __call(const tuple<_Args...>& __args,
1297 _Index_tuple<_Indexes...>) const volatile
1299 return _M_f(_Mu<_Bound_args>()
1300 (get<_Indexes>(_M_bound_args), __args)...);
1304 typedef _Result result_type;
1307 _Bind_result(_Functor __f, _Bound_args... __bound_args)
1308 : _M_f(__f), _M_bound_args(__bound_args...) { }
1311 template<typename... _Args>
1313 operator()(_Args&... __args)
1315 return this->__call(tie(__args...), _Bound_indexes());
1319 template<typename... _Args>
1321 operator()(_Args&... __args) const
1323 return this->__call(tie(__args...), _Bound_indexes());
1327 template<typename... _Args>
1329 operator()(_Args&... __args) volatile
1331 return this->__call(tie(__args...), _Bound_indexes());
1334 // Call as const volatile
1335 template<typename... _Args>
1337 operator()(_Args&... __args) const volatile
1339 return this->__call(tie(__args...), _Bound_indexes());
1343 /// Class template _Bind is always a bind expression.
1344 template<typename _Signature>
1345 struct is_bind_expression<_Bind<_Signature> >
1346 { static const bool value = true; };
1348 template<typename _Signature>
1349 const bool is_bind_expression<_Bind<_Signature> >::value;
1351 /// Class template _Bind_result is always a bind expression.
1352 template<typename _Result, typename _Signature>
1353 struct is_bind_expression<_Bind_result<_Result, _Signature> >
1354 { static const bool value = true; };
1356 template<typename _Result, typename _Signature>
1357 const bool is_bind_expression<_Bind_result<_Result, _Signature> >::value;
1360 template<typename _Functor, typename... _ArgTypes>
1362 _Bind<typename _Maybe_wrap_member_pointer<_Functor>::type(_ArgTypes...)>
1363 bind(_Functor __f, _ArgTypes... __args)
1365 typedef _Maybe_wrap_member_pointer<_Functor> __maybe_type;
1366 typedef typename __maybe_type::type __functor_type;
1367 typedef _Bind<__functor_type(_ArgTypes...)> __result_type;
1368 return __result_type(__maybe_type::__do_wrap(__f), __args...);
1371 template<typename _Result, typename _Functor, typename... _ArgTypes>
1373 _Bind_result<_Result,
1374 typename _Maybe_wrap_member_pointer<_Functor>::type
1376 bind(_Functor __f, _ArgTypes... __args)
1378 typedef _Maybe_wrap_member_pointer<_Functor> __maybe_type;
1379 typedef typename __maybe_type::type __functor_type;
1380 typedef _Bind_result<_Result, __functor_type(_ArgTypes...)>
1382 return __result_type(__maybe_type::__do_wrap(__f), __args...);
1386 * @brief Exception class thrown when class template function's
1387 * operator() is called with an empty target.
1388 * @ingroup exceptions
1390 class bad_function_call : public std::exception { };
1393 * The integral constant expression 0 can be converted into a
1394 * pointer to this type. It is used by the function template to
1395 * accept NULL pointers.
1397 struct _M_clear_type;
1400 * Trait identifying "location-invariant" types, meaning that the
1401 * address of the object (or any of its members) will not escape.
1402 * Also implies a trivial copy constructor and assignment operator.
1404 template<typename _Tp>
1405 struct __is_location_invariant
1406 : integral_constant<bool,
1407 (is_pointer<_Tp>::value
1408 || is_member_pointer<_Tp>::value)>
1412 class _Undefined_class;
1417 const void* _M_const_object;
1418 void (*_M_function_pointer)();
1419 void (_Undefined_class::*_M_member_pointer)();
1424 void* _M_access() { return &_M_pod_data[0]; }
1425 const void* _M_access() const { return &_M_pod_data[0]; }
1427 template<typename _Tp>
1430 { return *static_cast<_Tp*>(_M_access()); }
1432 template<typename _Tp>
1435 { return *static_cast<const _Tp*>(_M_access()); }
1437 _Nocopy_types _M_unused;
1438 char _M_pod_data[sizeof(_Nocopy_types)];
1441 enum _Manager_operation
1449 // Simple type wrapper that helps avoid annoying const problems
1450 // when casting between void pointers and pointers-to-pointers.
1451 template<typename _Tp>
1452 struct _Simple_type_wrapper
1454 _Simple_type_wrapper(_Tp __value) : __value(__value) { }
1459 template<typename _Tp>
1460 struct __is_location_invariant<_Simple_type_wrapper<_Tp> >
1461 : __is_location_invariant<_Tp>
1465 // Converts a reference to a function object into a callable
1467 template<typename _Functor>
1469 __callable_functor(_Functor& __f)
1472 template<typename _Member, typename _Class>
1473 inline _Mem_fn<_Member _Class::*>
1474 __callable_functor(_Member _Class::* &__p)
1475 { return mem_fn(__p); }
1477 template<typename _Member, typename _Class>
1478 inline _Mem_fn<_Member _Class::*>
1479 __callable_functor(_Member _Class::* const &__p)
1480 { return mem_fn(__p); }
1482 template<typename _Signature>
1485 /// Base class of all polymorphic function object wrappers.
1486 class _Function_base
1489 static const std::size_t _M_max_size = sizeof(_Nocopy_types);
1490 static const std::size_t _M_max_align = __alignof__(_Nocopy_types);
1492 template<typename _Functor>
1496 static const bool __stored_locally =
1497 (__is_location_invariant<_Functor>::value
1498 && sizeof(_Functor) <= _M_max_size
1499 && __alignof__(_Functor) <= _M_max_align
1500 && (_M_max_align % __alignof__(_Functor) == 0));
1502 typedef integral_constant<bool, __stored_locally> _Local_storage;
1504 // Retrieve a pointer to the function object
1506 _M_get_pointer(const _Any_data& __source)
1508 const _Functor* __ptr =
1509 __stored_locally? &__source._M_access<_Functor>()
1510 /* have stored a pointer */ : __source._M_access<_Functor*>();
1511 return const_cast<_Functor*>(__ptr);
1514 // Clone a location-invariant function object that fits within
1515 // an _Any_data structure.
1517 _M_clone(_Any_data& __dest, const _Any_data& __source, true_type)
1519 new (__dest._M_access()) _Functor(__source._M_access<_Functor>());
1522 // Clone a function object that is not location-invariant or
1523 // that cannot fit into an _Any_data structure.
1525 _M_clone(_Any_data& __dest, const _Any_data& __source, false_type)
1527 __dest._M_access<_Functor*>() =
1528 new _Functor(*__source._M_access<_Functor*>());
1531 // Destroying a location-invariant object may still require
1534 _M_destroy(_Any_data& __victim, true_type)
1536 __victim._M_access<_Functor>().~_Functor();
1539 // Destroying an object located on the heap.
1541 _M_destroy(_Any_data& __victim, false_type)
1543 delete __victim._M_access<_Functor*>();
1548 _M_manager(_Any_data& __dest, const _Any_data& __source,
1549 _Manager_operation __op)
1554 case __get_type_info:
1555 __dest._M_access<const type_info*>() = &typeid(_Functor);
1558 case __get_functor_ptr:
1559 __dest._M_access<_Functor*>() = _M_get_pointer(__source);
1562 case __clone_functor:
1563 _M_clone(__dest, __source, _Local_storage());
1566 case __destroy_functor:
1567 _M_destroy(__dest, _Local_storage());
1574 _M_init_functor(_Any_data& __functor, _Functor&& __f)
1575 { _M_init_functor(__functor, std::move(__f), _Local_storage()); }
1577 template<typename _Signature>
1579 _M_not_empty_function(const function<_Signature>& __f)
1580 { return static_cast<bool>(__f); }
1582 template<typename _Tp>
1584 _M_not_empty_function(const _Tp*& __fp)
1587 template<typename _Class, typename _Tp>
1589 _M_not_empty_function(_Tp _Class::* const& __mp)
1592 template<typename _Tp>
1594 _M_not_empty_function(const _Tp&)
1599 _M_init_functor(_Any_data& __functor, _Functor&& __f, true_type)
1600 { new (__functor._M_access()) _Functor(std::move(__f)); }
1603 _M_init_functor(_Any_data& __functor, _Functor&& __f, false_type)
1604 { __functor._M_access<_Functor*>() = new _Functor(std::move(__f)); }
1607 template<typename _Functor>
1608 class _Ref_manager : public _Base_manager<_Functor*>
1610 typedef _Function_base::_Base_manager<_Functor*> _Base;
1614 _M_manager(_Any_data& __dest, const _Any_data& __source,
1615 _Manager_operation __op)
1620 case __get_type_info:
1621 __dest._M_access<const type_info*>() = &typeid(_Functor);
1624 case __get_functor_ptr:
1625 __dest._M_access<_Functor*>() = *_Base::_M_get_pointer(__source);
1626 return is_const<_Functor>::value;
1630 _Base::_M_manager(__dest, __source, __op);
1636 _M_init_functor(_Any_data& __functor, reference_wrapper<_Functor> __f)
1638 // TBD: Use address_of function instead.
1639 _Base::_M_init_functor(__functor, &__f.get());
1643 _Function_base() : _M_manager(0) { }
1648 _M_manager(_M_functor, _M_functor, __destroy_functor);
1652 bool _M_empty() const { return !_M_manager; }
1654 typedef bool (*_Manager_type)(_Any_data&, const _Any_data&,
1655 _Manager_operation);
1657 _Any_data _M_functor;
1658 _Manager_type _M_manager;
1661 template<typename _Signature, typename _Functor>
1662 class _Function_handler;
1664 template<typename _Res, typename _Functor, typename... _ArgTypes>
1665 class _Function_handler<_Res(_ArgTypes...), _Functor>
1666 : public _Function_base::_Base_manager<_Functor>
1668 typedef _Function_base::_Base_manager<_Functor> _Base;
1672 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1674 return (*_Base::_M_get_pointer(__functor))(
1675 std::forward<_ArgTypes>(__args)...);
1679 template<typename _Functor, typename... _ArgTypes>
1680 class _Function_handler<void(_ArgTypes...), _Functor>
1681 : public _Function_base::_Base_manager<_Functor>
1683 typedef _Function_base::_Base_manager<_Functor> _Base;
1687 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1689 (*_Base::_M_get_pointer(__functor))(
1690 std::forward<_ArgTypes>(__args)...);
1694 template<typename _Res, typename _Functor, typename... _ArgTypes>
1695 class _Function_handler<_Res(_ArgTypes...), reference_wrapper<_Functor> >
1696 : public _Function_base::_Ref_manager<_Functor>
1698 typedef _Function_base::_Ref_manager<_Functor> _Base;
1702 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1704 return __callable_functor(**_Base::_M_get_pointer(__functor))(
1705 std::forward<_ArgTypes>(__args)...);
1709 template<typename _Functor, typename... _ArgTypes>
1710 class _Function_handler<void(_ArgTypes...), reference_wrapper<_Functor> >
1711 : public _Function_base::_Ref_manager<_Functor>
1713 typedef _Function_base::_Ref_manager<_Functor> _Base;
1717 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1719 __callable_functor(**_Base::_M_get_pointer(__functor))(
1720 std::forward<_ArgTypes>(__args)...);
1724 template<typename _Class, typename _Member, typename _Res,
1725 typename... _ArgTypes>
1726 class _Function_handler<_Res(_ArgTypes...), _Member _Class::*>
1727 : public _Function_handler<void(_ArgTypes...), _Member _Class::*>
1729 typedef _Function_handler<void(_ArgTypes...), _Member _Class::*>
1734 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1736 return mem_fn(_Base::_M_get_pointer(__functor)->__value)(
1737 std::forward<_ArgTypes>(__args)...);
1741 template<typename _Class, typename _Member, typename... _ArgTypes>
1742 class _Function_handler<void(_ArgTypes...), _Member _Class::*>
1743 : public _Function_base::_Base_manager<
1744 _Simple_type_wrapper< _Member _Class::* > >
1746 typedef _Member _Class::* _Functor;
1747 typedef _Simple_type_wrapper<_Functor> _Wrapper;
1748 typedef _Function_base::_Base_manager<_Wrapper> _Base;
1752 _M_manager(_Any_data& __dest, const _Any_data& __source,
1753 _Manager_operation __op)
1758 case __get_type_info:
1759 __dest._M_access<const type_info*>() = &typeid(_Functor);
1762 case __get_functor_ptr:
1763 __dest._M_access<_Functor*>() =
1764 &_Base::_M_get_pointer(__source)->__value;
1768 _Base::_M_manager(__dest, __source, __op);
1774 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1776 mem_fn(_Base::_M_get_pointer(__functor)->__value)(
1777 std::forward<_ArgTypes>(__args)...);
1782 template<typename _Res, typename... _ArgTypes>
1783 class function<_Res(_ArgTypes...)>
1784 : public _Maybe_unary_or_binary_function<_Res, _ArgTypes...>,
1785 private _Function_base
1787 typedef _Res _Signature_type(_ArgTypes...);
1789 struct _Useless { };
1792 typedef _Res result_type;
1794 // [3.7.2.1] construct/copy/destroy
1797 * @brief Default construct creates an empty function call wrapper.
1798 * @post @c !(bool)*this
1801 function() : _Function_base() { }
1804 * @brief Default construct creates an empty function call wrapper.
1805 * @post @c !(bool)*this
1807 function(_M_clear_type*) : _Function_base() { }
1810 * @brief %Function copy constructor.
1811 * @param x A %function object with identical call signature.
1812 * @post @c (bool)*this == (bool)x
1814 * The newly-created %function contains a copy of the target of @a
1815 * x (if it has one).
1817 function(const function& __x);
1820 * @brief %Function move constructor.
1821 * @param x A %function object rvalue with identical call signature.
1823 * The newly-created %function contains the target of @a x
1826 function(function&& __x) : _Function_base()
1831 // TODO: needs allocator_arg_t
1834 * @brief Builds a %function that targets a copy of the incoming
1836 * @param f A %function object that is callable with parameters of
1837 * type @c T1, @c T2, ..., @c TN and returns a value convertible
1840 * The newly-created %function object will target a copy of @a
1841 * f. If @a f is @c reference_wrapper<F>, then this function
1842 * object will contain a reference to the function object @c
1843 * f.get(). If @a f is a NULL function pointer or NULL
1844 * pointer-to-member, the newly-created object will be empty.
1846 * If @a f is a non-NULL function pointer or an object of type @c
1847 * reference_wrapper<F>, this function will not throw.
1849 template<typename _Functor>
1850 function(_Functor __f,
1851 typename __gnu_cxx::__enable_if<
1852 !is_integral<_Functor>::value, _Useless>::__type
1856 * @brief %Function assignment operator.
1857 * @param x A %function with identical call signature.
1858 * @post @c (bool)*this == (bool)x
1861 * The target of @a x is copied to @c *this. If @a x has no
1862 * target, then @c *this will be empty.
1864 * If @a x targets a function pointer or a reference to a function
1865 * object, then this operation will not throw an %exception.
1868 operator=(const function& __x)
1870 function(__x).swap(*this);
1875 * @brief %Function move-assignment operator.
1876 * @param x A %function rvalue with identical call signature.
1879 * The target of @a x is moved to @c *this. If @a x has no
1880 * target, then @c *this will be empty.
1882 * If @a x targets a function pointer or a reference to a function
1883 * object, then this operation will not throw an %exception.
1886 operator=(function&& __x)
1888 function(std::move(__x)).swap(*this);
1893 * @brief %Function assignment to zero.
1894 * @post @c !(bool)*this
1897 * The target of @c *this is deallocated, leaving it empty.
1900 operator=(_M_clear_type*)
1904 _M_manager(_M_functor, _M_functor, __destroy_functor);
1912 * @brief %Function assignment to a new target.
1913 * @param f A %function object that is callable with parameters of
1914 * type @c T1, @c T2, ..., @c TN and returns a value convertible
1918 * This %function object wrapper will target a copy of @a
1919 * f. If @a f is @c reference_wrapper<F>, then this function
1920 * object will contain a reference to the function object @c
1921 * f.get(). If @a f is a NULL function pointer or NULL
1922 * pointer-to-member, @c this object will be empty.
1924 * If @a f is a non-NULL function pointer or an object of type @c
1925 * reference_wrapper<F>, this function will not throw.
1927 template<typename _Functor>
1928 typename __gnu_cxx::__enable_if<!is_integral<_Functor>::value,
1930 operator=(_Functor&& __f)
1932 function(std::forward<_Functor>(__f)).swap(*this);
1937 template<typename _Functor>
1938 typename __gnu_cxx::__enable_if<!is_integral<_Functor>::value,
1940 operator=(reference_wrapper<_Functor> __f)
1942 function(__f).swap(*this);
1946 // [3.7.2.2] function modifiers
1949 * @brief Swap the targets of two %function objects.
1950 * @param f A %function with identical call signature.
1952 * Swap the targets of @c this function object and @a f. This
1953 * function will not throw an %exception.
1955 void swap(function& __x)
1957 _Any_data __old_functor = _M_functor;
1958 _M_functor = __x._M_functor;
1959 __x._M_functor = __old_functor;
1960 _Manager_type __old_manager = _M_manager;
1961 _M_manager = __x._M_manager;
1962 __x._M_manager = __old_manager;
1963 _Invoker_type __old_invoker = _M_invoker;
1964 _M_invoker = __x._M_invoker;
1965 __x._M_invoker = __old_invoker;
1968 // TODO: needs allocator_arg_t
1970 template<typename _Functor, typename _Alloc>
1972 assign(_Functor&& __f, const _Alloc& __a)
1974 function(allocator_arg, __a,
1975 std::forward<_Functor>(__f)).swap(*this);
1979 // [3.7.2.3] function capacity
1982 * @brief Determine if the %function wrapper has a target.
1984 * @return @c true when this %function object contains a target,
1985 * or @c false when it is empty.
1987 * This function will not throw an %exception.
1989 explicit operator bool() const
1990 { return !_M_empty(); }
1992 // [3.7.2.4] function invocation
1995 * @brief Invokes the function targeted by @c *this.
1996 * @returns the result of the target.
1997 * @throws bad_function_call when @c !(bool)*this
1999 * The function call operator invokes the target function object
2000 * stored by @c this.
2002 _Res operator()(_ArgTypes... __args) const;
2005 // [3.7.2.5] function target access
2007 * @brief Determine the type of the target of this function object
2010 * @returns the type identifier of the target function object, or
2011 * @c typeid(void) if @c !(bool)*this.
2013 * This function will not throw an %exception.
2015 const type_info& target_type() const;
2018 * @brief Access the stored target function object.
2020 * @return Returns a pointer to the stored target function object,
2021 * if @c typeid(Functor).equals(target_type()); otherwise, a NULL
2024 * This function will not throw an %exception.
2026 template<typename _Functor> _Functor* target();
2029 template<typename _Functor> const _Functor* target() const;
2032 // deleted overloads
2033 template<typename _Res2, typename... _ArgTypes2>
2034 void operator==(const function<_Res2(_ArgTypes2...)>&) const = delete;
2035 template<typename _Res2, typename... _ArgTypes2>
2036 void operator!=(const function<_Res2(_ArgTypes2...)>&) const = delete;
2039 typedef _Res (*_Invoker_type)(const _Any_data&, _ArgTypes...);
2040 _Invoker_type _M_invoker;
2043 template<typename _Res, typename... _ArgTypes>
2044 function<_Res(_ArgTypes...)>::
2045 function(const function& __x)
2048 if (static_cast<bool>(__x))
2050 _M_invoker = __x._M_invoker;
2051 _M_manager = __x._M_manager;
2052 __x._M_manager(_M_functor, __x._M_functor, __clone_functor);
2056 template<typename _Res, typename... _ArgTypes>
2057 template<typename _Functor>
2058 function<_Res(_ArgTypes...)>::
2059 function(_Functor __f,
2060 typename __gnu_cxx::__enable_if<
2061 !is_integral<_Functor>::value, _Useless>::__type)
2064 typedef _Function_handler<_Signature_type, _Functor> _My_handler;
2066 if (_My_handler::_M_not_empty_function(__f))
2068 _M_invoker = &_My_handler::_M_invoke;
2069 _M_manager = &_My_handler::_M_manager;
2070 _My_handler::_M_init_functor(_M_functor, std::move(__f));
2074 template<typename _Res, typename... _ArgTypes>
2076 function<_Res(_ArgTypes...)>::
2077 operator()(_ArgTypes... __args) const
2080 __throw_bad_function_call();
2081 return _M_invoker(_M_functor, std::forward<_ArgTypes>(__args)...);
2085 template<typename _Res, typename... _ArgTypes>
2087 function<_Res(_ArgTypes...)>::
2092 _Any_data __typeinfo_result;
2093 _M_manager(__typeinfo_result, _M_functor, __get_type_info);
2094 return *__typeinfo_result._M_access<const type_info*>();
2097 return typeid(void);
2100 template<typename _Res, typename... _ArgTypes>
2101 template<typename _Functor>
2103 function<_Res(_ArgTypes...)>::
2106 if (typeid(_Functor) == target_type() && _M_manager)
2109 if (_M_manager(__ptr, _M_functor, __get_functor_ptr)
2110 && !is_const<_Functor>::value)
2113 return __ptr._M_access<_Functor*>();
2119 template<typename _Res, typename... _ArgTypes>
2120 template<typename _Functor>
2122 function<_Res(_ArgTypes...)>::
2125 if (typeid(_Functor) == target_type() && _M_manager)
2128 _M_manager(__ptr, _M_functor, __get_functor_ptr);
2129 return __ptr._M_access<const _Functor*>();
2136 // [20.7.15.2.6] null pointer comparisons
2139 * @brief Compares a polymorphic function object wrapper against 0
2140 * (the NULL pointer).
2141 * @returns @c true if the wrapper has no target, @c false otherwise
2143 * This function will not throw an %exception.
2145 template<typename _Res, typename... _Args>
2147 operator==(const function<_Res(_Args...)>& __f, _M_clear_type*)
2148 { return !static_cast<bool>(__f); }
2151 template<typename _Res, typename... _Args>
2153 operator==(_M_clear_type*, const function<_Res(_Args...)>& __f)
2154 { return !static_cast<bool>(__f); }
2157 * @brief Compares a polymorphic function object wrapper against 0
2158 * (the NULL pointer).
2159 * @returns @c false if the wrapper has no target, @c true otherwise
2161 * This function will not throw an %exception.
2163 template<typename _Res, typename... _Args>
2165 operator!=(const function<_Res(_Args...)>& __f, _M_clear_type*)
2166 { return static_cast<bool>(__f); }
2169 template<typename _Res, typename... _Args>
2171 operator!=(_M_clear_type*, const function<_Res(_Args...)>& __f)
2172 { return static_cast<bool>(__f); }
2174 // [20.7.15.2.7] specialized algorithms
2177 * @brief Swap the targets of two polymorphic function object wrappers.
2179 * This function will not throw an %exception.
2181 template<typename _Res, typename... _Args>
2183 swap(function<_Res(_Args...)>& __x, function<_Res(_Args...)>& __y)
2187 #endif // __GXX_EXPERIMENTAL_CXX0X__
2189 #endif // _GLIBCXX_FUNCTIONAL