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 // Specialization needed to prevent "forming reference to void" errors when
1134 // bind<void>() is called, because argument deduction instantiates
1135 // _Maybe_wrap_member_pointer<void> outside the immediate context where
1138 struct _Maybe_wrap_member_pointer<void>
1143 /// Type of the function object returned from bind().
1144 template<typename _Signature>
1147 template<typename _Functor, typename... _Bound_args>
1148 class _Bind<_Functor(_Bound_args...)>
1149 : public _Weak_result_type<_Functor>
1151 typedef _Bind __self_type;
1152 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
1156 tuple<_Bound_args...> _M_bound_args;
1159 template<typename... _Args, int... _Indexes>
1161 _Functor(typename result_of<_Mu<_Bound_args>
1162 (_Bound_args, tuple<_Args...>)>::type...)
1164 __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>)
1166 return _M_f(_Mu<_Bound_args>()
1167 (get<_Indexes>(_M_bound_args), __args)...);
1171 template<typename... _Args, int... _Indexes>
1173 const _Functor(typename result_of<_Mu<_Bound_args>
1174 (const _Bound_args, tuple<_Args...>)
1176 __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>) const
1178 return _M_f(_Mu<_Bound_args>()
1179 (get<_Indexes>(_M_bound_args), __args)...);
1183 template<typename... _Args, int... _Indexes>
1185 volatile _Functor(typename result_of<_Mu<_Bound_args>
1186 (volatile _Bound_args, tuple<_Args...>)
1188 __call(const tuple<_Args...>& __args,
1189 _Index_tuple<_Indexes...>) volatile
1191 return _M_f(_Mu<_Bound_args>()
1192 (get<_Indexes>(_M_bound_args), __args)...);
1195 // Call as const volatile
1196 template<typename... _Args, int... _Indexes>
1198 const volatile _Functor(typename result_of<_Mu<_Bound_args>
1199 (const volatile _Bound_args,
1202 __call(const tuple<_Args...>& __args,
1203 _Index_tuple<_Indexes...>) const volatile
1205 return _M_f(_Mu<_Bound_args>()
1206 (get<_Indexes>(_M_bound_args), __args)...);
1210 explicit _Bind(_Functor __f, _Bound_args... __bound_args)
1211 : _M_f(__f), _M_bound_args(__bound_args...) { }
1214 template<typename... _Args>
1216 _Functor(typename result_of<_Mu<_Bound_args>
1217 (_Bound_args, tuple<_Args...>)>::type...)
1219 operator()(_Args&... __args)
1221 return this->__call(tie(__args...), _Bound_indexes());
1225 template<typename... _Args>
1227 const _Functor(typename result_of<_Mu<_Bound_args>
1228 (const _Bound_args, tuple<_Args...>)>::type...)
1230 operator()(_Args&... __args) const
1232 return this->__call(tie(__args...), _Bound_indexes());
1237 template<typename... _Args>
1239 volatile _Functor(typename result_of<_Mu<_Bound_args>
1240 (volatile _Bound_args, tuple<_Args...>)>::type...)
1242 operator()(_Args&... __args) volatile
1244 return this->__call(tie(__args...), _Bound_indexes());
1248 // Call as const volatile
1249 template<typename... _Args>
1251 const volatile _Functor(typename result_of<_Mu<_Bound_args>
1252 (const volatile _Bound_args,
1253 tuple<_Args...>)>::type...)
1255 operator()(_Args&... __args) const volatile
1257 return this->__call(tie(__args...), _Bound_indexes());
1261 /// Type of the function object returned from bind<R>().
1262 template<typename _Result, typename _Signature>
1263 struct _Bind_result;
1265 template<typename _Result, typename _Functor, typename... _Bound_args>
1266 class _Bind_result<_Result, _Functor(_Bound_args...)>
1268 typedef _Bind_result __self_type;
1269 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
1273 tuple<_Bound_args...> _M_bound_args;
1276 template<typename _Res>
1277 struct __enable_if_void : enable_if<is_void<_Res>::value, int> { };
1278 template<typename _Res>
1279 struct __disable_if_void : enable_if<!is_void<_Res>::value, int> { };
1282 template<typename _Res, typename... _Args, int... _Indexes>
1284 __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>,
1285 typename __disable_if_void<_Res>::type = 0)
1287 return _M_f(_Mu<_Bound_args>()
1288 (get<_Indexes>(_M_bound_args), __args)...);
1291 // Call unqualified, return void
1292 template<typename _Res, typename... _Args, int... _Indexes>
1294 __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>,
1295 typename __enable_if_void<_Res>::type = 0)
1297 _M_f(_Mu<_Bound_args>()
1298 (get<_Indexes>(_M_bound_args), __args)...);
1302 template<typename _Res, typename... _Args, int... _Indexes>
1304 __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>,
1305 typename __disable_if_void<_Res>::type = 0) const
1307 return _M_f(_Mu<_Bound_args>()
1308 (get<_Indexes>(_M_bound_args), __args)...);
1311 // Call as const, return void
1312 template<typename _Res, typename... _Args, int... _Indexes>
1314 __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>,
1315 typename __enable_if_void<_Res>::type = 0) const
1317 _M_f(_Mu<_Bound_args>()
1318 (get<_Indexes>(_M_bound_args), __args)...);
1322 template<typename _Res, typename... _Args, int... _Indexes>
1324 __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>,
1325 typename __disable_if_void<_Res>::type = 0) volatile
1327 return _M_f(_Mu<_Bound_args>()
1328 (get<_Indexes>(_M_bound_args), __args)...);
1331 // Call as volatile, return void
1332 template<typename _Res, typename... _Args, int... _Indexes>
1334 __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>,
1335 typename __enable_if_void<_Res>::type = 0) volatile
1337 _M_f(_Mu<_Bound_args>()
1338 (get<_Indexes>(_M_bound_args), __args)...);
1341 // Call as const volatile
1342 template<typename _Res, typename... _Args, int... _Indexes>
1344 __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>,
1345 typename __disable_if_void<_Res>::type = 0) const volatile
1347 return _M_f(_Mu<_Bound_args>()
1348 (get<_Indexes>(_M_bound_args), __args)...);
1351 // Call as const volatile, return void
1352 template<typename _Res, typename... _Args, int... _Indexes>
1354 __call(const tuple<_Args...>& __args,
1355 _Index_tuple<_Indexes...>,
1356 typename __enable_if_void<_Res>::type = 0) const volatile
1358 _M_f(_Mu<_Bound_args>()
1359 (get<_Indexes>(_M_bound_args), __args)...);
1363 typedef _Result result_type;
1366 _Bind_result(_Functor __f, _Bound_args... __bound_args)
1367 : _M_f(__f), _M_bound_args(__bound_args...) { }
1370 template<typename... _Args>
1372 operator()(_Args&... __args)
1374 return this->__call<_Result>(tie(__args...), _Bound_indexes());
1378 template<typename... _Args>
1380 operator()(_Args&... __args) const
1382 return this->__call<_Result>(tie(__args...), _Bound_indexes());
1386 template<typename... _Args>
1388 operator()(_Args&... __args) volatile
1390 return this->__call<_Result>(tie(__args...), _Bound_indexes());
1393 // Call as const volatile
1394 template<typename... _Args>
1396 operator()(_Args&... __args) const volatile
1398 return this->__call<_Result>(tie(__args...), _Bound_indexes());
1402 /// Class template _Bind is always a bind expression.
1403 template<typename _Signature>
1404 struct is_bind_expression<_Bind<_Signature> >
1405 { static const bool value = true; };
1407 template<typename _Signature>
1408 const bool is_bind_expression<_Bind<_Signature> >::value;
1410 /// Class template _Bind_result is always a bind expression.
1411 template<typename _Result, typename _Signature>
1412 struct is_bind_expression<_Bind_result<_Result, _Signature> >
1413 { static const bool value = true; };
1415 template<typename _Result, typename _Signature>
1416 const bool is_bind_expression<_Bind_result<_Result, _Signature> >::value;
1419 template<typename _Functor, typename... _ArgTypes>
1421 _Bind<typename _Maybe_wrap_member_pointer<_Functor>::type(_ArgTypes...)>
1422 bind(_Functor __f, _ArgTypes... __args)
1424 typedef _Maybe_wrap_member_pointer<_Functor> __maybe_type;
1425 typedef typename __maybe_type::type __functor_type;
1426 typedef _Bind<__functor_type(_ArgTypes...)> __result_type;
1427 return __result_type(__maybe_type::__do_wrap(__f), __args...);
1430 template<typename _Result, typename _Functor, typename... _ArgTypes>
1432 _Bind_result<_Result,
1433 typename _Maybe_wrap_member_pointer<_Functor>::type
1435 bind(_Functor __f, _ArgTypes... __args)
1437 typedef _Maybe_wrap_member_pointer<_Functor> __maybe_type;
1438 typedef typename __maybe_type::type __functor_type;
1439 typedef _Bind_result<_Result, __functor_type(_ArgTypes...)>
1441 return __result_type(__maybe_type::__do_wrap(__f), __args...);
1445 * @brief Exception class thrown when class template function's
1446 * operator() is called with an empty target.
1447 * @ingroup exceptions
1449 class bad_function_call : public std::exception { };
1452 * The integral constant expression 0 can be converted into a
1453 * pointer to this type. It is used by the function template to
1454 * accept NULL pointers.
1456 struct _M_clear_type;
1459 * Trait identifying "location-invariant" types, meaning that the
1460 * address of the object (or any of its members) will not escape.
1461 * Also implies a trivial copy constructor and assignment operator.
1463 template<typename _Tp>
1464 struct __is_location_invariant
1465 : integral_constant<bool,
1466 (is_pointer<_Tp>::value
1467 || is_member_pointer<_Tp>::value)>
1471 class _Undefined_class;
1476 const void* _M_const_object;
1477 void (*_M_function_pointer)();
1478 void (_Undefined_class::*_M_member_pointer)();
1483 void* _M_access() { return &_M_pod_data[0]; }
1484 const void* _M_access() const { return &_M_pod_data[0]; }
1486 template<typename _Tp>
1489 { return *static_cast<_Tp*>(_M_access()); }
1491 template<typename _Tp>
1494 { return *static_cast<const _Tp*>(_M_access()); }
1496 _Nocopy_types _M_unused;
1497 char _M_pod_data[sizeof(_Nocopy_types)];
1500 enum _Manager_operation
1508 // Simple type wrapper that helps avoid annoying const problems
1509 // when casting between void pointers and pointers-to-pointers.
1510 template<typename _Tp>
1511 struct _Simple_type_wrapper
1513 _Simple_type_wrapper(_Tp __value) : __value(__value) { }
1518 template<typename _Tp>
1519 struct __is_location_invariant<_Simple_type_wrapper<_Tp> >
1520 : __is_location_invariant<_Tp>
1524 // Converts a reference to a function object into a callable
1526 template<typename _Functor>
1528 __callable_functor(_Functor& __f)
1531 template<typename _Member, typename _Class>
1532 inline _Mem_fn<_Member _Class::*>
1533 __callable_functor(_Member _Class::* &__p)
1534 { return mem_fn(__p); }
1536 template<typename _Member, typename _Class>
1537 inline _Mem_fn<_Member _Class::*>
1538 __callable_functor(_Member _Class::* const &__p)
1539 { return mem_fn(__p); }
1541 template<typename _Signature>
1544 /// Base class of all polymorphic function object wrappers.
1545 class _Function_base
1548 static const std::size_t _M_max_size = sizeof(_Nocopy_types);
1549 static const std::size_t _M_max_align = __alignof__(_Nocopy_types);
1551 template<typename _Functor>
1555 static const bool __stored_locally =
1556 (__is_location_invariant<_Functor>::value
1557 && sizeof(_Functor) <= _M_max_size
1558 && __alignof__(_Functor) <= _M_max_align
1559 && (_M_max_align % __alignof__(_Functor) == 0));
1561 typedef integral_constant<bool, __stored_locally> _Local_storage;
1563 // Retrieve a pointer to the function object
1565 _M_get_pointer(const _Any_data& __source)
1567 const _Functor* __ptr =
1568 __stored_locally? &__source._M_access<_Functor>()
1569 /* have stored a pointer */ : __source._M_access<_Functor*>();
1570 return const_cast<_Functor*>(__ptr);
1573 // Clone a location-invariant function object that fits within
1574 // an _Any_data structure.
1576 _M_clone(_Any_data& __dest, const _Any_data& __source, true_type)
1578 new (__dest._M_access()) _Functor(__source._M_access<_Functor>());
1581 // Clone a function object that is not location-invariant or
1582 // that cannot fit into an _Any_data structure.
1584 _M_clone(_Any_data& __dest, const _Any_data& __source, false_type)
1586 __dest._M_access<_Functor*>() =
1587 new _Functor(*__source._M_access<_Functor*>());
1590 // Destroying a location-invariant object may still require
1593 _M_destroy(_Any_data& __victim, true_type)
1595 __victim._M_access<_Functor>().~_Functor();
1598 // Destroying an object located on the heap.
1600 _M_destroy(_Any_data& __victim, false_type)
1602 delete __victim._M_access<_Functor*>();
1607 _M_manager(_Any_data& __dest, const _Any_data& __source,
1608 _Manager_operation __op)
1613 case __get_type_info:
1614 __dest._M_access<const type_info*>() = &typeid(_Functor);
1617 case __get_functor_ptr:
1618 __dest._M_access<_Functor*>() = _M_get_pointer(__source);
1621 case __clone_functor:
1622 _M_clone(__dest, __source, _Local_storage());
1625 case __destroy_functor:
1626 _M_destroy(__dest, _Local_storage());
1633 _M_init_functor(_Any_data& __functor, _Functor&& __f)
1634 { _M_init_functor(__functor, std::move(__f), _Local_storage()); }
1636 template<typename _Signature>
1638 _M_not_empty_function(const function<_Signature>& __f)
1639 { return static_cast<bool>(__f); }
1641 template<typename _Tp>
1643 _M_not_empty_function(const _Tp*& __fp)
1646 template<typename _Class, typename _Tp>
1648 _M_not_empty_function(_Tp _Class::* const& __mp)
1651 template<typename _Tp>
1653 _M_not_empty_function(const _Tp&)
1658 _M_init_functor(_Any_data& __functor, _Functor&& __f, true_type)
1659 { new (__functor._M_access()) _Functor(std::move(__f)); }
1662 _M_init_functor(_Any_data& __functor, _Functor&& __f, false_type)
1663 { __functor._M_access<_Functor*>() = new _Functor(std::move(__f)); }
1666 template<typename _Functor>
1667 class _Ref_manager : public _Base_manager<_Functor*>
1669 typedef _Function_base::_Base_manager<_Functor*> _Base;
1673 _M_manager(_Any_data& __dest, const _Any_data& __source,
1674 _Manager_operation __op)
1679 case __get_type_info:
1680 __dest._M_access<const type_info*>() = &typeid(_Functor);
1683 case __get_functor_ptr:
1684 __dest._M_access<_Functor*>() = *_Base::_M_get_pointer(__source);
1685 return is_const<_Functor>::value;
1689 _Base::_M_manager(__dest, __source, __op);
1695 _M_init_functor(_Any_data& __functor, reference_wrapper<_Functor> __f)
1697 // TBD: Use address_of function instead.
1698 _Base::_M_init_functor(__functor, &__f.get());
1702 _Function_base() : _M_manager(0) { }
1707 _M_manager(_M_functor, _M_functor, __destroy_functor);
1711 bool _M_empty() const { return !_M_manager; }
1713 typedef bool (*_Manager_type)(_Any_data&, const _Any_data&,
1714 _Manager_operation);
1716 _Any_data _M_functor;
1717 _Manager_type _M_manager;
1720 template<typename _Signature, typename _Functor>
1721 class _Function_handler;
1723 template<typename _Res, typename _Functor, typename... _ArgTypes>
1724 class _Function_handler<_Res(_ArgTypes...), _Functor>
1725 : public _Function_base::_Base_manager<_Functor>
1727 typedef _Function_base::_Base_manager<_Functor> _Base;
1731 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1733 return (*_Base::_M_get_pointer(__functor))(
1734 std::forward<_ArgTypes>(__args)...);
1738 template<typename _Functor, typename... _ArgTypes>
1739 class _Function_handler<void(_ArgTypes...), _Functor>
1740 : public _Function_base::_Base_manager<_Functor>
1742 typedef _Function_base::_Base_manager<_Functor> _Base;
1746 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1748 (*_Base::_M_get_pointer(__functor))(
1749 std::forward<_ArgTypes>(__args)...);
1753 template<typename _Res, typename _Functor, typename... _ArgTypes>
1754 class _Function_handler<_Res(_ArgTypes...), reference_wrapper<_Functor> >
1755 : public _Function_base::_Ref_manager<_Functor>
1757 typedef _Function_base::_Ref_manager<_Functor> _Base;
1761 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1763 return __callable_functor(**_Base::_M_get_pointer(__functor))(
1764 std::forward<_ArgTypes>(__args)...);
1768 template<typename _Functor, typename... _ArgTypes>
1769 class _Function_handler<void(_ArgTypes...), reference_wrapper<_Functor> >
1770 : public _Function_base::_Ref_manager<_Functor>
1772 typedef _Function_base::_Ref_manager<_Functor> _Base;
1776 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1778 __callable_functor(**_Base::_M_get_pointer(__functor))(
1779 std::forward<_ArgTypes>(__args)...);
1783 template<typename _Class, typename _Member, typename _Res,
1784 typename... _ArgTypes>
1785 class _Function_handler<_Res(_ArgTypes...), _Member _Class::*>
1786 : public _Function_handler<void(_ArgTypes...), _Member _Class::*>
1788 typedef _Function_handler<void(_ArgTypes...), _Member _Class::*>
1793 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1795 return mem_fn(_Base::_M_get_pointer(__functor)->__value)(
1796 std::forward<_ArgTypes>(__args)...);
1800 template<typename _Class, typename _Member, typename... _ArgTypes>
1801 class _Function_handler<void(_ArgTypes...), _Member _Class::*>
1802 : public _Function_base::_Base_manager<
1803 _Simple_type_wrapper< _Member _Class::* > >
1805 typedef _Member _Class::* _Functor;
1806 typedef _Simple_type_wrapper<_Functor> _Wrapper;
1807 typedef _Function_base::_Base_manager<_Wrapper> _Base;
1811 _M_manager(_Any_data& __dest, const _Any_data& __source,
1812 _Manager_operation __op)
1817 case __get_type_info:
1818 __dest._M_access<const type_info*>() = &typeid(_Functor);
1821 case __get_functor_ptr:
1822 __dest._M_access<_Functor*>() =
1823 &_Base::_M_get_pointer(__source)->__value;
1827 _Base::_M_manager(__dest, __source, __op);
1833 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1835 mem_fn(_Base::_M_get_pointer(__functor)->__value)(
1836 std::forward<_ArgTypes>(__args)...);
1841 template<typename _Res, typename... _ArgTypes>
1842 class function<_Res(_ArgTypes...)>
1843 : public _Maybe_unary_or_binary_function<_Res, _ArgTypes...>,
1844 private _Function_base
1846 typedef _Res _Signature_type(_ArgTypes...);
1848 struct _Useless { };
1851 typedef _Res result_type;
1853 // [3.7.2.1] construct/copy/destroy
1856 * @brief Default construct creates an empty function call wrapper.
1857 * @post @c !(bool)*this
1860 function() : _Function_base() { }
1863 * @brief Default construct creates an empty function call wrapper.
1864 * @post @c !(bool)*this
1866 function(_M_clear_type*) : _Function_base() { }
1869 * @brief %Function copy constructor.
1870 * @param x A %function object with identical call signature.
1871 * @post @c (bool)*this == (bool)x
1873 * The newly-created %function contains a copy of the target of @a
1874 * x (if it has one).
1876 function(const function& __x);
1879 * @brief %Function move constructor.
1880 * @param x A %function object rvalue with identical call signature.
1882 * The newly-created %function contains the target of @a x
1885 function(function&& __x) : _Function_base()
1890 // TODO: needs allocator_arg_t
1893 * @brief Builds a %function that targets a copy of the incoming
1895 * @param f A %function object that is callable with parameters of
1896 * type @c T1, @c T2, ..., @c TN and returns a value convertible
1899 * The newly-created %function object will target a copy of @a
1900 * f. If @a f is @c reference_wrapper<F>, then this function
1901 * object will contain a reference to the function object @c
1902 * f.get(). If @a f is a NULL function pointer or NULL
1903 * pointer-to-member, the newly-created object will be empty.
1905 * If @a f is a non-NULL function pointer or an object of type @c
1906 * reference_wrapper<F>, this function will not throw.
1908 template<typename _Functor>
1909 function(_Functor __f,
1910 typename __gnu_cxx::__enable_if<
1911 !is_integral<_Functor>::value, _Useless>::__type
1915 * @brief %Function assignment operator.
1916 * @param x A %function with identical call signature.
1917 * @post @c (bool)*this == (bool)x
1920 * The target of @a x is copied to @c *this. If @a x has no
1921 * target, then @c *this will be empty.
1923 * If @a x targets a function pointer or a reference to a function
1924 * object, then this operation will not throw an %exception.
1927 operator=(const function& __x)
1929 function(__x).swap(*this);
1934 * @brief %Function move-assignment operator.
1935 * @param x A %function rvalue with identical call signature.
1938 * The target of @a x is moved to @c *this. If @a x has no
1939 * target, then @c *this will be empty.
1941 * If @a x targets a function pointer or a reference to a function
1942 * object, then this operation will not throw an %exception.
1945 operator=(function&& __x)
1947 function(std::move(__x)).swap(*this);
1952 * @brief %Function assignment to zero.
1953 * @post @c !(bool)*this
1956 * The target of @c *this is deallocated, leaving it empty.
1959 operator=(_M_clear_type*)
1963 _M_manager(_M_functor, _M_functor, __destroy_functor);
1971 * @brief %Function assignment to a new target.
1972 * @param f A %function object that is callable with parameters of
1973 * type @c T1, @c T2, ..., @c TN and returns a value convertible
1977 * This %function object wrapper will target a copy of @a
1978 * f. If @a f is @c reference_wrapper<F>, then this function
1979 * object will contain a reference to the function object @c
1980 * f.get(). If @a f is a NULL function pointer or NULL
1981 * pointer-to-member, @c this object will be empty.
1983 * If @a f is a non-NULL function pointer or an object of type @c
1984 * reference_wrapper<F>, this function will not throw.
1986 template<typename _Functor>
1987 typename __gnu_cxx::__enable_if<!is_integral<_Functor>::value,
1989 operator=(_Functor&& __f)
1991 function(std::forward<_Functor>(__f)).swap(*this);
1996 template<typename _Functor>
1997 typename __gnu_cxx::__enable_if<!is_integral<_Functor>::value,
1999 operator=(reference_wrapper<_Functor> __f)
2001 function(__f).swap(*this);
2005 // [3.7.2.2] function modifiers
2008 * @brief Swap the targets of two %function objects.
2009 * @param f A %function with identical call signature.
2011 * Swap the targets of @c this function object and @a f. This
2012 * function will not throw an %exception.
2014 void swap(function& __x)
2016 _Any_data __old_functor = _M_functor;
2017 _M_functor = __x._M_functor;
2018 __x._M_functor = __old_functor;
2019 _Manager_type __old_manager = _M_manager;
2020 _M_manager = __x._M_manager;
2021 __x._M_manager = __old_manager;
2022 _Invoker_type __old_invoker = _M_invoker;
2023 _M_invoker = __x._M_invoker;
2024 __x._M_invoker = __old_invoker;
2027 // TODO: needs allocator_arg_t
2029 template<typename _Functor, typename _Alloc>
2031 assign(_Functor&& __f, const _Alloc& __a)
2033 function(allocator_arg, __a,
2034 std::forward<_Functor>(__f)).swap(*this);
2038 // [3.7.2.3] function capacity
2041 * @brief Determine if the %function wrapper has a target.
2043 * @return @c true when this %function object contains a target,
2044 * or @c false when it is empty.
2046 * This function will not throw an %exception.
2048 explicit operator bool() const
2049 { return !_M_empty(); }
2051 // [3.7.2.4] function invocation
2054 * @brief Invokes the function targeted by @c *this.
2055 * @returns the result of the target.
2056 * @throws bad_function_call when @c !(bool)*this
2058 * The function call operator invokes the target function object
2059 * stored by @c this.
2061 _Res operator()(_ArgTypes... __args) const;
2064 // [3.7.2.5] function target access
2066 * @brief Determine the type of the target of this function object
2069 * @returns the type identifier of the target function object, or
2070 * @c typeid(void) if @c !(bool)*this.
2072 * This function will not throw an %exception.
2074 const type_info& target_type() const;
2077 * @brief Access the stored target function object.
2079 * @return Returns a pointer to the stored target function object,
2080 * if @c typeid(Functor).equals(target_type()); otherwise, a NULL
2083 * This function will not throw an %exception.
2085 template<typename _Functor> _Functor* target();
2088 template<typename _Functor> const _Functor* target() const;
2091 // deleted overloads
2092 template<typename _Res2, typename... _ArgTypes2>
2093 void operator==(const function<_Res2(_ArgTypes2...)>&) const = delete;
2094 template<typename _Res2, typename... _ArgTypes2>
2095 void operator!=(const function<_Res2(_ArgTypes2...)>&) const = delete;
2098 typedef _Res (*_Invoker_type)(const _Any_data&, _ArgTypes...);
2099 _Invoker_type _M_invoker;
2102 template<typename _Res, typename... _ArgTypes>
2103 function<_Res(_ArgTypes...)>::
2104 function(const function& __x)
2107 if (static_cast<bool>(__x))
2109 _M_invoker = __x._M_invoker;
2110 _M_manager = __x._M_manager;
2111 __x._M_manager(_M_functor, __x._M_functor, __clone_functor);
2115 template<typename _Res, typename... _ArgTypes>
2116 template<typename _Functor>
2117 function<_Res(_ArgTypes...)>::
2118 function(_Functor __f,
2119 typename __gnu_cxx::__enable_if<
2120 !is_integral<_Functor>::value, _Useless>::__type)
2123 typedef _Function_handler<_Signature_type, _Functor> _My_handler;
2125 if (_My_handler::_M_not_empty_function(__f))
2127 _M_invoker = &_My_handler::_M_invoke;
2128 _M_manager = &_My_handler::_M_manager;
2129 _My_handler::_M_init_functor(_M_functor, std::move(__f));
2133 template<typename _Res, typename... _ArgTypes>
2135 function<_Res(_ArgTypes...)>::
2136 operator()(_ArgTypes... __args) const
2139 __throw_bad_function_call();
2140 return _M_invoker(_M_functor, std::forward<_ArgTypes>(__args)...);
2144 template<typename _Res, typename... _ArgTypes>
2146 function<_Res(_ArgTypes...)>::
2151 _Any_data __typeinfo_result;
2152 _M_manager(__typeinfo_result, _M_functor, __get_type_info);
2153 return *__typeinfo_result._M_access<const type_info*>();
2156 return typeid(void);
2159 template<typename _Res, typename... _ArgTypes>
2160 template<typename _Functor>
2162 function<_Res(_ArgTypes...)>::
2165 if (typeid(_Functor) == target_type() && _M_manager)
2168 if (_M_manager(__ptr, _M_functor, __get_functor_ptr)
2169 && !is_const<_Functor>::value)
2172 return __ptr._M_access<_Functor*>();
2178 template<typename _Res, typename... _ArgTypes>
2179 template<typename _Functor>
2181 function<_Res(_ArgTypes...)>::
2184 if (typeid(_Functor) == target_type() && _M_manager)
2187 _M_manager(__ptr, _M_functor, __get_functor_ptr);
2188 return __ptr._M_access<const _Functor*>();
2195 // [20.7.15.2.6] null pointer comparisons
2198 * @brief Compares a polymorphic function object wrapper against 0
2199 * (the NULL pointer).
2200 * @returns @c true if the wrapper has no target, @c false otherwise
2202 * This function will not throw an %exception.
2204 template<typename _Res, typename... _Args>
2206 operator==(const function<_Res(_Args...)>& __f, _M_clear_type*)
2207 { return !static_cast<bool>(__f); }
2210 template<typename _Res, typename... _Args>
2212 operator==(_M_clear_type*, const function<_Res(_Args...)>& __f)
2213 { return !static_cast<bool>(__f); }
2216 * @brief Compares a polymorphic function object wrapper against 0
2217 * (the NULL pointer).
2218 * @returns @c false if the wrapper has no target, @c true otherwise
2220 * This function will not throw an %exception.
2222 template<typename _Res, typename... _Args>
2224 operator!=(const function<_Res(_Args...)>& __f, _M_clear_type*)
2225 { return static_cast<bool>(__f); }
2228 template<typename _Res, typename... _Args>
2230 operator!=(_M_clear_type*, const function<_Res(_Args...)>& __f)
2231 { return static_cast<bool>(__f); }
2233 // [20.7.15.2.7] specialized algorithms
2236 * @brief Swap the targets of two polymorphic function object wrappers.
2238 * This function will not throw an %exception.
2240 template<typename _Res, typename... _Args>
2242 swap(function<_Res(_Args...)>& __x, function<_Res(_Args...)>& __y)
2246 #endif // __GXX_EXPERIMENTAL_CXX0X__
2248 #endif // _GLIBCXX_FUNCTIONAL