1 // <functional> -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
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/functexcept.h>
59 #include <bits/functional_hash.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>
94 /// If we have found a result_type, extract it.
95 template<bool _Has_result_type, typename _Functor>
96 struct _Maybe_get_result_type
99 template<typename _Functor>
100 struct _Maybe_get_result_type<true, _Functor>
102 typedef typename _Functor::result_type result_type;
106 * Base class for any function object that has a weak result type, as
107 * defined in 3.3/3 of TR1.
109 template<typename _Functor>
110 struct _Weak_result_type_impl
111 : _Maybe_get_result_type<_Has_result_type<_Functor>::value, _Functor>
114 /// Retrieve the result type for a function type.
115 template<typename _Res, typename... _ArgTypes>
116 struct _Weak_result_type_impl<_Res(_ArgTypes...)>
118 typedef _Res result_type;
121 /// Retrieve the result type for a function reference.
122 template<typename _Res, typename... _ArgTypes>
123 struct _Weak_result_type_impl<_Res(&)(_ArgTypes...)>
125 typedef _Res result_type;
128 /// Retrieve the result type for a function pointer.
129 template<typename _Res, typename... _ArgTypes>
130 struct _Weak_result_type_impl<_Res(*)(_ArgTypes...)>
132 typedef _Res result_type;
135 /// Retrieve result type for a member function pointer.
136 template<typename _Res, typename _Class, typename... _ArgTypes>
137 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...)>
139 typedef _Res result_type;
142 /// Retrieve result type for a const member function pointer.
143 template<typename _Res, typename _Class, typename... _ArgTypes>
144 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) const>
146 typedef _Res result_type;
149 /// Retrieve result type for a volatile member function pointer.
150 template<typename _Res, typename _Class, typename... _ArgTypes>
151 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) volatile>
153 typedef _Res result_type;
156 /// Retrieve result type for a const volatile member function pointer.
157 template<typename _Res, typename _Class, typename... _ArgTypes>
158 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...)const volatile>
160 typedef _Res result_type;
164 * Strip top-level cv-qualifiers from the function object and let
165 * _Weak_result_type_impl perform the real work.
167 template<typename _Functor>
168 struct _Weak_result_type
169 : _Weak_result_type_impl<typename remove_cv<_Functor>::type>
172 /// Determines if the type _Tp derives from unary_function.
173 template<typename _Tp>
174 struct _Derives_from_unary_function : __sfinae_types
177 template<typename _T1, typename _Res>
178 static __one __test(const volatile unary_function<_T1, _Res>*);
180 // It's tempting to change "..." to const volatile void*, but
181 // that fails when _Tp is a function type.
182 static __two __test(...);
185 static const bool value = sizeof(__test((_Tp*)0)) == 1;
188 /// Determines if the type _Tp derives from binary_function.
189 template<typename _Tp>
190 struct _Derives_from_binary_function : __sfinae_types
193 template<typename _T1, typename _T2, typename _Res>
194 static __one __test(const volatile binary_function<_T1, _T2, _Res>*);
196 // It's tempting to change "..." to const volatile void*, but
197 // that fails when _Tp is a function type.
198 static __two __test(...);
201 static const bool value = sizeof(__test((_Tp*)0)) == 1;
204 /// Turns a function type into a function pointer type
205 template<typename _Tp, bool _IsFunctionType = is_function<_Tp>::value>
206 struct _Function_to_function_pointer
211 template<typename _Tp>
212 struct _Function_to_function_pointer<_Tp, true>
218 * Invoke a function object, which may be either a member pointer or a
219 * function object. The first parameter will tell which.
221 template<typename _Functor, typename... _Args>
224 (!is_member_pointer<_Functor>::value
225 && !is_function<_Functor>::value
226 && !is_function<typename remove_pointer<_Functor>::type>::value),
227 typename result_of<_Functor(_Args...)>::type
229 __invoke(_Functor& __f, _Args&&... __args)
231 return __f(std::forward<_Args>(__args)...);
234 // To pick up function references (that will become function pointers)
235 template<typename _Functor, typename... _Args>
238 (is_pointer<_Functor>::value
239 && is_function<typename remove_pointer<_Functor>::type>::value),
240 typename result_of<_Functor(_Args...)>::type
242 __invoke(_Functor __f, _Args&&... __args)
244 return __f(std::forward<_Args>(__args)...);
248 * Knowing which of unary_function and binary_function _Tp derives
249 * from, derives from the same and ensures that reference_wrapper
250 * will have a weak result type. See cases below.
252 template<bool _Unary, bool _Binary, typename _Tp>
253 struct _Reference_wrapper_base_impl;
255 // Not a unary_function or binary_function, so try a weak result type.
256 template<typename _Tp>
257 struct _Reference_wrapper_base_impl<false, false, _Tp>
258 : _Weak_result_type<_Tp>
261 // unary_function but not binary_function
262 template<typename _Tp>
263 struct _Reference_wrapper_base_impl<true, false, _Tp>
264 : unary_function<typename _Tp::argument_type,
265 typename _Tp::result_type>
268 // binary_function but not unary_function
269 template<typename _Tp>
270 struct _Reference_wrapper_base_impl<false, true, _Tp>
271 : binary_function<typename _Tp::first_argument_type,
272 typename _Tp::second_argument_type,
273 typename _Tp::result_type>
276 // Both unary_function and binary_function. Import result_type to
278 template<typename _Tp>
279 struct _Reference_wrapper_base_impl<true, true, _Tp>
280 : unary_function<typename _Tp::argument_type,
281 typename _Tp::result_type>,
282 binary_function<typename _Tp::first_argument_type,
283 typename _Tp::second_argument_type,
284 typename _Tp::result_type>
286 typedef typename _Tp::result_type result_type;
290 * Derives from unary_function or binary_function when it
291 * can. Specializations handle all of the easy cases. The primary
292 * template determines what to do with a class type, which may
293 * derive from both unary_function and binary_function.
295 template<typename _Tp>
296 struct _Reference_wrapper_base
297 : _Reference_wrapper_base_impl<
298 _Derives_from_unary_function<_Tp>::value,
299 _Derives_from_binary_function<_Tp>::value,
303 // - a function type (unary)
304 template<typename _Res, typename _T1>
305 struct _Reference_wrapper_base<_Res(_T1)>
306 : unary_function<_T1, _Res>
309 // - a function type (binary)
310 template<typename _Res, typename _T1, typename _T2>
311 struct _Reference_wrapper_base<_Res(_T1, _T2)>
312 : binary_function<_T1, _T2, _Res>
315 // - a function pointer type (unary)
316 template<typename _Res, typename _T1>
317 struct _Reference_wrapper_base<_Res(*)(_T1)>
318 : unary_function<_T1, _Res>
321 // - a function pointer type (binary)
322 template<typename _Res, typename _T1, typename _T2>
323 struct _Reference_wrapper_base<_Res(*)(_T1, _T2)>
324 : binary_function<_T1, _T2, _Res>
327 // - a pointer to member function type (unary, no qualifiers)
328 template<typename _Res, typename _T1>
329 struct _Reference_wrapper_base<_Res (_T1::*)()>
330 : unary_function<_T1*, _Res>
333 // - a pointer to member function type (binary, no qualifiers)
334 template<typename _Res, typename _T1, typename _T2>
335 struct _Reference_wrapper_base<_Res (_T1::*)(_T2)>
336 : binary_function<_T1*, _T2, _Res>
339 // - a pointer to member function type (unary, const)
340 template<typename _Res, typename _T1>
341 struct _Reference_wrapper_base<_Res (_T1::*)() const>
342 : unary_function<const _T1*, _Res>
345 // - a pointer to member function type (binary, const)
346 template<typename _Res, typename _T1, typename _T2>
347 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const>
348 : binary_function<const _T1*, _T2, _Res>
351 // - a pointer to member function type (unary, volatile)
352 template<typename _Res, typename _T1>
353 struct _Reference_wrapper_base<_Res (_T1::*)() volatile>
354 : unary_function<volatile _T1*, _Res>
357 // - a pointer to member function type (binary, volatile)
358 template<typename _Res, typename _T1, typename _T2>
359 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) volatile>
360 : binary_function<volatile _T1*, _T2, _Res>
363 // - a pointer to member function type (unary, const volatile)
364 template<typename _Res, typename _T1>
365 struct _Reference_wrapper_base<_Res (_T1::*)() const volatile>
366 : unary_function<const volatile _T1*, _Res>
369 // - a pointer to member function type (binary, const volatile)
370 template<typename _Res, typename _T1, typename _T2>
371 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const volatile>
372 : binary_function<const volatile _T1*, _T2, _Res>
376 * @brief Primary class template for reference_wrapper.
380 template<typename _Tp>
381 class reference_wrapper
382 : public _Reference_wrapper_base<typename remove_cv<_Tp>::type>
384 // If _Tp is a function type, we can't form result_of<_Tp(...)>,
385 // so turn it into a function pointer type.
386 typedef typename _Function_to_function_pointer<_Tp>::type
393 reference_wrapper(_Tp& __indata): _M_data(&__indata)
396 reference_wrapper(_Tp&&) = delete;
398 reference_wrapper(const reference_wrapper<_Tp>& __inref):
399 _M_data(__inref._M_data)
403 operator=(const reference_wrapper<_Tp>& __inref)
405 _M_data = __inref._M_data;
409 operator _Tp&() const
410 { return this->get(); }
416 template<typename... _Args>
417 typename result_of<_M_func_type(_Args...)>::type
418 operator()(_Args&&... __args) const
420 return __invoke(get(), std::forward<_Args>(__args)...);
425 /// Denotes a reference should be taken to a variable.
426 template<typename _Tp>
427 inline reference_wrapper<_Tp>
429 { return reference_wrapper<_Tp>(__t); }
431 /// Denotes a const reference should be taken to a variable.
432 template<typename _Tp>
433 inline reference_wrapper<const _Tp>
435 { return reference_wrapper<const _Tp>(__t); }
437 /// Partial specialization.
438 template<typename _Tp>
439 inline reference_wrapper<_Tp>
440 ref(reference_wrapper<_Tp> __t)
441 { return ref(__t.get()); }
443 /// Partial specialization.
444 template<typename _Tp>
445 inline reference_wrapper<const _Tp>
446 cref(reference_wrapper<_Tp> __t)
447 { return cref(__t.get()); }
451 template<typename _Tp, bool>
452 struct _Mem_fn_const_or_non
454 typedef const _Tp& type;
457 template<typename _Tp>
458 struct _Mem_fn_const_or_non<_Tp, false>
464 * Derives from @c unary_function or @c binary_function, or perhaps
465 * nothing, depending on the number of arguments provided. The
466 * primary template is the basis case, which derives nothing.
468 template<typename _Res, typename... _ArgTypes>
469 struct _Maybe_unary_or_binary_function { };
471 /// Derives from @c unary_function, as appropriate.
472 template<typename _Res, typename _T1>
473 struct _Maybe_unary_or_binary_function<_Res, _T1>
474 : std::unary_function<_T1, _Res> { };
476 /// Derives from @c binary_function, as appropriate.
477 template<typename _Res, typename _T1, typename _T2>
478 struct _Maybe_unary_or_binary_function<_Res, _T1, _T2>
479 : std::binary_function<_T1, _T2, _Res> { };
481 /// Implementation of @c mem_fn for member function pointers.
482 template<typename _Res, typename _Class, typename... _ArgTypes>
483 class _Mem_fn<_Res (_Class::*)(_ArgTypes...)>
484 : public _Maybe_unary_or_binary_function<_Res, _Class*, _ArgTypes...>
486 typedef _Res (_Class::*_Functor)(_ArgTypes...);
488 template<typename _Tp>
490 _M_call(_Tp& __object, const volatile _Class *,
491 _ArgTypes... __args) const
492 { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); }
494 template<typename _Tp>
496 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
497 { return ((*__ptr).*__pmf)(std::forward<_ArgTypes>(__args)...); }
500 typedef _Res result_type;
502 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
506 operator()(_Class& __object, _ArgTypes... __args) const
507 { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); }
511 operator()(_Class* __object, _ArgTypes... __args) const
512 { return (__object->*__pmf)(std::forward<_ArgTypes>(__args)...); }
514 // Handle smart pointers, references and pointers to derived
515 template<typename _Tp>
517 operator()(_Tp& __object, _ArgTypes... __args) const
519 return _M_call(__object, &__object,
520 std::forward<_ArgTypes>(__args)...);
527 /// Implementation of @c mem_fn for const member function pointers.
528 template<typename _Res, typename _Class, typename... _ArgTypes>
529 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) const>
530 : public _Maybe_unary_or_binary_function<_Res, const _Class*,
533 typedef _Res (_Class::*_Functor)(_ArgTypes...) const;
535 template<typename _Tp>
537 _M_call(_Tp& __object, const volatile _Class *,
538 _ArgTypes... __args) const
539 { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); }
541 template<typename _Tp>
543 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
544 { return ((*__ptr).*__pmf)(std::forward<_ArgTypes>(__args)...); }
547 typedef _Res result_type;
549 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
553 operator()(const _Class& __object, _ArgTypes... __args) const
554 { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); }
558 operator()(const _Class* __object, _ArgTypes... __args) const
559 { return (__object->*__pmf)(std::forward<_ArgTypes>(__args)...); }
561 // Handle smart pointers, references and pointers to derived
562 template<typename _Tp>
563 _Res operator()(_Tp& __object, _ArgTypes... __args) const
565 return _M_call(__object, &__object,
566 std::forward<_ArgTypes>(__args)...);
573 /// Implementation of @c mem_fn for volatile member function pointers.
574 template<typename _Res, typename _Class, typename... _ArgTypes>
575 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) volatile>
576 : public _Maybe_unary_or_binary_function<_Res, volatile _Class*,
579 typedef _Res (_Class::*_Functor)(_ArgTypes...) volatile;
581 template<typename _Tp>
583 _M_call(_Tp& __object, const volatile _Class *,
584 _ArgTypes... __args) const
585 { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); }
587 template<typename _Tp>
589 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
590 { return ((*__ptr).*__pmf)(std::forward<_ArgTypes>(__args)...); }
593 typedef _Res result_type;
595 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
599 operator()(volatile _Class& __object, _ArgTypes... __args) const
600 { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); }
604 operator()(volatile _Class* __object, _ArgTypes... __args) const
605 { return (__object->*__pmf)(std::forward<_ArgTypes>(__args)...); }
607 // Handle smart pointers, references and pointers to derived
608 template<typename _Tp>
610 operator()(_Tp& __object, _ArgTypes... __args) const
612 return _M_call(__object, &__object,
613 std::forward<_ArgTypes>(__args)...);
620 /// Implementation of @c mem_fn for const volatile member function pointers.
621 template<typename _Res, typename _Class, typename... _ArgTypes>
622 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) const volatile>
623 : public _Maybe_unary_or_binary_function<_Res, const volatile _Class*,
626 typedef _Res (_Class::*_Functor)(_ArgTypes...) const volatile;
628 template<typename _Tp>
630 _M_call(_Tp& __object, const volatile _Class *,
631 _ArgTypes... __args) const
632 { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); }
634 template<typename _Tp>
636 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
637 { return ((*__ptr).*__pmf)(std::forward<_ArgTypes>(__args)...); }
640 typedef _Res result_type;
642 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
646 operator()(const volatile _Class& __object, _ArgTypes... __args) const
647 { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); }
651 operator()(const volatile _Class* __object, _ArgTypes... __args) const
652 { return (__object->*__pmf)(std::forward<_ArgTypes>(__args)...); }
654 // Handle smart pointers, references and pointers to derived
655 template<typename _Tp>
656 _Res operator()(_Tp& __object, _ArgTypes... __args) const
658 return _M_call(__object, &__object,
659 std::forward<_ArgTypes>(__args)...);
667 template<typename _Res, typename _Class>
668 class _Mem_fn<_Res _Class::*>
670 // This bit of genius is due to Peter Dimov, improved slightly by
672 template<typename _Tp>
674 _M_call(_Tp& __object, _Class *) const
675 { return __object.*__pm; }
677 template<typename _Tp, typename _Up>
679 _M_call(_Tp& __object, _Up * const *) const
680 { return (*__object).*__pm; }
682 template<typename _Tp, typename _Up>
684 _M_call(_Tp& __object, const _Up * const *) const
685 { return (*__object).*__pm; }
687 template<typename _Tp>
689 _M_call(_Tp& __object, const _Class *) const
690 { return __object.*__pm; }
692 template<typename _Tp>
694 _M_call(_Tp& __ptr, const volatile void*) const
695 { return (*__ptr).*__pm; }
697 template<typename _Tp> static _Tp& __get_ref();
699 template<typename _Tp>
700 static __sfinae_types::__one __check_const(_Tp&, _Class*);
701 template<typename _Tp, typename _Up>
702 static __sfinae_types::__one __check_const(_Tp&, _Up * const *);
703 template<typename _Tp, typename _Up>
704 static __sfinae_types::__two __check_const(_Tp&, const _Up * const *);
705 template<typename _Tp>
706 static __sfinae_types::__two __check_const(_Tp&, const _Class*);
707 template<typename _Tp>
708 static __sfinae_types::__two __check_const(_Tp&, const volatile void*);
711 template<typename _Tp>
713 : _Mem_fn_const_or_non<_Res,
714 (sizeof(__sfinae_types::__two)
715 == sizeof(__check_const<_Tp>(__get_ref<_Tp>(), (_Tp*)0)))>
718 template<typename _Signature>
721 template<typename _CVMem, typename _Tp>
722 struct result<_CVMem(_Tp)>
723 : public _Result_type<_Tp> { };
725 template<typename _CVMem, typename _Tp>
726 struct result<_CVMem(_Tp&)>
727 : public _Result_type<_Tp> { };
730 _Mem_fn(_Res _Class::*__pm) : __pm(__pm) { }
734 operator()(_Class& __object) const
735 { return __object.*__pm; }
738 operator()(const _Class& __object) const
739 { return __object.*__pm; }
743 operator()(_Class* __object) const
744 { return __object->*__pm; }
747 operator()(const _Class* __object) const
748 { return __object->*__pm; }
750 // Handle smart pointers and derived
751 template<typename _Tp>
752 typename _Result_type<_Tp>::type
753 operator()(_Tp& __unknown) const
754 { return _M_call(__unknown, &__unknown); }
761 * @brief Returns a function object that forwards to the member
765 template<typename _Tp, typename _Class>
766 inline _Mem_fn<_Tp _Class::*>
767 mem_fn(_Tp _Class::* __pm)
769 return _Mem_fn<_Tp _Class::*>(__pm);
773 * @brief Determines if the given type _Tp is a function object
774 * should be treated as a subexpression when evaluating calls to
775 * function objects returned by bind(). [TR1 3.6.1]
778 template<typename _Tp>
779 struct is_bind_expression
780 : public false_type { };
783 * @brief Determines if the given type _Tp is a placeholder in a
784 * bind() expression and, if so, which placeholder it is. [TR1 3.6.2]
787 template<typename _Tp>
788 struct is_placeholder
789 : public integral_constant<int, 0>
792 /// The type of placeholder objects defined by libstdc++.
793 template<int _Num> struct _Placeholder { };
795 /** @namespace std::placeholders
796 * @brief ISO C++ 0x entities sub namespace for functional.
799 * Define a large number of placeholders. There is no way to
800 * simplify this with variadic templates, because we're introducing
801 * unique names for each.
803 namespace placeholders
816 _Placeholder<10> _10;
817 _Placeholder<11> _11;
818 _Placeholder<12> _12;
819 _Placeholder<13> _13;
820 _Placeholder<14> _14;
821 _Placeholder<15> _15;
822 _Placeholder<16> _16;
823 _Placeholder<17> _17;
824 _Placeholder<18> _18;
825 _Placeholder<19> _19;
826 _Placeholder<20> _20;
827 _Placeholder<21> _21;
828 _Placeholder<22> _22;
829 _Placeholder<23> _23;
830 _Placeholder<24> _24;
831 _Placeholder<25> _25;
832 _Placeholder<26> _26;
833 _Placeholder<27> _27;
834 _Placeholder<28> _28;
835 _Placeholder<29> _29;
840 * Partial specialization of is_placeholder that provides the placeholder
841 * number for the placeholder objects defined by libstdc++.
845 struct is_placeholder<_Placeholder<_Num> >
846 : public integral_constant<int, _Num>
850 * Used by _Safe_tuple_element to indicate that there is no tuple
851 * element at this position.
853 struct _No_tuple_element;
856 * Implementation helper for _Safe_tuple_element. This primary
857 * template handles the case where it is safe to use @c
860 template<int __i, typename _Tuple, bool _IsSafe>
861 struct _Safe_tuple_element_impl
862 : tuple_element<__i, _Tuple> { };
865 * Implementation helper for _Safe_tuple_element. This partial
866 * specialization handles the case where it is not safe to use @c
867 * tuple_element. We just return @c _No_tuple_element.
869 template<int __i, typename _Tuple>
870 struct _Safe_tuple_element_impl<__i, _Tuple, false>
872 typedef _No_tuple_element type;
876 * Like tuple_element, but returns @c _No_tuple_element when
877 * tuple_element would return an error.
879 template<int __i, typename _Tuple>
880 struct _Safe_tuple_element
881 : _Safe_tuple_element_impl<__i, _Tuple,
882 (__i >= 0 && __i < tuple_size<_Tuple>::value)>
886 * Maps an argument to bind() into an actual argument to the bound
887 * function object [TR1 3.6.3/5]. Only the first parameter should
888 * be specified: the rest are used to determine among the various
889 * implementations. Note that, although this class is a function
890 * object, it isn't entirely normal because it takes only two
891 * parameters regardless of the number of parameters passed to the
892 * bind expression. The first parameter is the bound argument and
893 * the second parameter is a tuple containing references to the
894 * rest of the arguments.
896 template<typename _Arg,
897 bool _IsBindExp = is_bind_expression<_Arg>::value,
898 bool _IsPlaceholder = (is_placeholder<_Arg>::value > 0)>
902 * If the argument is reference_wrapper<_Tp>, returns the
903 * underlying reference. [TR1 3.6.3/5 bullet 1]
905 template<typename _Tp>
906 class _Mu<reference_wrapper<_Tp>, false, false>
909 typedef _Tp& result_type;
911 /* Note: This won't actually work for const volatile
912 * reference_wrappers, because reference_wrapper::get() is const
913 * but not volatile-qualified. This might be a defect in the TR.
915 template<typename _CVRef, typename _Tuple>
917 operator()(_CVRef& __arg, _Tuple&&) const volatile
918 { return __arg.get(); }
922 * If the argument is a bind expression, we invoke the underlying
923 * function object with the same cv-qualifiers as we are given and
924 * pass along all of our arguments (unwrapped). [TR1 3.6.3/5 bullet 2]
926 template<typename _Arg>
927 class _Mu<_Arg, true, false>
930 template<typename _Signature> class result;
932 // Determine the result type when we pass the arguments along. This
933 // involves passing along the cv-qualifiers placed on _Mu and
934 // unwrapping the argument bundle.
935 template<typename _CVMu, typename _CVArg, typename... _Args>
936 class result<_CVMu(_CVArg, tuple<_Args...>)>
937 : public result_of<_CVArg(_Args...)> { };
939 template<typename _CVArg, typename... _Args>
940 typename result_of<_CVArg(_Args...)>::type
941 operator()(_CVArg& __arg,
942 tuple<_Args...>&& __tuple) const volatile
944 // Construct an index tuple and forward to __call
945 typedef typename _Build_index_tuple<sizeof...(_Args)>::__type
947 return this->__call(__arg, std::move(__tuple), _Indexes());
951 // Invokes the underlying function object __arg by unpacking all
952 // of the arguments in the tuple.
953 template<typename _CVArg, typename... _Args, int... _Indexes>
954 typename result_of<_CVArg(_Args...)>::type
955 __call(_CVArg& __arg, tuple<_Args...>&& __tuple,
956 const _Index_tuple<_Indexes...>&) const volatile
958 return __arg(std::forward<_Args>(get<_Indexes>(__tuple))...);
963 * If the argument is a placeholder for the Nth argument, returns
964 * a reference to the Nth argument to the bind function object.
965 * [TR1 3.6.3/5 bullet 3]
967 template<typename _Arg>
968 class _Mu<_Arg, false, true>
971 template<typename _Signature> class result;
973 template<typename _CVMu, typename _CVArg, typename _Tuple>
974 class result<_CVMu(_CVArg, _Tuple)>
976 // Add a reference, if it hasn't already been done for us.
977 // This allows us to be a little bit sloppy in constructing
978 // the tuple that we pass to result_of<...>.
979 typedef typename _Safe_tuple_element<(is_placeholder<_Arg>::value
984 typedef typename add_rvalue_reference<__base_type>::type type;
987 template<typename _Tuple>
988 typename result<_Mu(_Arg, _Tuple)>::type
989 operator()(const volatile _Arg&, _Tuple&& __tuple) const volatile
991 return std::forward<typename result<_Mu(_Arg, _Tuple)>::type>(
992 ::std::get<(is_placeholder<_Arg>::value - 1)>(__tuple));
997 * If the argument is just a value, returns a reference to that
998 * value. The cv-qualifiers on the reference are the same as the
999 * cv-qualifiers on the _Mu object. [TR1 3.6.3/5 bullet 4]
1001 template<typename _Arg>
1002 class _Mu<_Arg, false, false>
1005 template<typename _Signature> struct result;
1007 template<typename _CVMu, typename _CVArg, typename _Tuple>
1008 struct result<_CVMu(_CVArg, _Tuple)>
1010 typedef typename add_lvalue_reference<_CVArg>::type type;
1013 // Pick up the cv-qualifiers of the argument
1014 template<typename _CVArg, typename _Tuple>
1016 operator()(_CVArg&& __arg, _Tuple&&) const volatile
1017 { return std::forward<_CVArg>(__arg); }
1021 * Maps member pointers into instances of _Mem_fn but leaves all
1022 * other function objects untouched. Used by tr1::bind(). The
1023 * primary template handles the non--member-pointer case.
1025 template<typename _Tp>
1026 struct _Maybe_wrap_member_pointer
1031 __do_wrap(const _Tp& __x)
1036 * Maps member pointers into instances of _Mem_fn but leaves all
1037 * other function objects untouched. Used by tr1::bind(). This
1038 * partial specialization handles the member pointer case.
1040 template<typename _Tp, typename _Class>
1041 struct _Maybe_wrap_member_pointer<_Tp _Class::*>
1043 typedef _Mem_fn<_Tp _Class::*> type;
1046 __do_wrap(_Tp _Class::* __pm)
1047 { return type(__pm); }
1050 // Specialization needed to prevent "forming reference to void" errors when
1051 // bind<void>() is called, because argument deduction instantiates
1052 // _Maybe_wrap_member_pointer<void> outside the immediate context where
1055 struct _Maybe_wrap_member_pointer<void>
1060 /// Type of the function object returned from bind().
1061 template<typename _Signature>
1064 template<typename _Functor, typename... _Bound_args>
1065 class _Bind<_Functor(_Bound_args...)>
1066 : public _Weak_result_type<_Functor>
1068 typedef _Bind __self_type;
1069 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
1073 tuple<_Bound_args...> _M_bound_args;
1076 template<typename _Result, typename... _Args, int... _Indexes>
1078 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>)
1080 return _M_f(_Mu<_Bound_args>()
1081 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1085 template<typename _Result, typename... _Args, int... _Indexes>
1087 __call_c(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>) const
1089 return _M_f(_Mu<_Bound_args>()
1090 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1095 template<typename _Result, typename... _Args, int... _Indexes>
1097 __call_v(tuple<_Args...>&& __args,
1098 _Index_tuple<_Indexes...>) volatile
1100 return _M_f(_Mu<_Bound_args>()
1101 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1104 // Call as const volatile
1105 template<typename _Result, typename... _Args, int... _Indexes>
1107 __call_c_v(tuple<_Args...>&& __args,
1108 _Index_tuple<_Indexes...>) const volatile
1110 return _M_f(_Mu<_Bound_args>()
1111 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1116 explicit _Bind(_Functor __f, _Bound_args... __bound_args)
1117 : _M_f(std::forward<_Functor>(__f)),
1118 _M_bound_args(std::forward<_Bound_args>(__bound_args)...)
1122 template<typename... _Args, typename _Result
1123 = decltype( std::declval<_Functor>()(
1124 _Mu<_Bound_args>()( std::declval<_Bound_args&>(),
1125 std::declval<tuple<_Args...>&&>() )... ) )>
1127 operator()(_Args&&... __args)
1129 return this->__call<_Result>(tuple<_Args...>
1130 (std::forward<_Args>(__args)...),
1135 template<typename... _Args, typename _Result
1136 = decltype( std::declval<const _Functor>()(
1137 _Mu<_Bound_args>()( std::declval<const _Bound_args&>(),
1138 std::declval<tuple<_Args...>&&>() )... ) )>
1140 operator()(_Args&&... __args) const
1142 return this->__call_c<_Result>(tuple<_Args...>
1143 (std::forward<_Args>(__args)...),
1149 template<typename... _Args, typename _Result
1150 = decltype( std::declval<volatile _Functor>()(
1151 _Mu<_Bound_args>()( std::declval<volatile _Bound_args&>(),
1152 std::declval<tuple<_Args...>&&>() )... ) )>
1154 operator()(_Args&&... __args) volatile
1156 return this->__call_v<_Result>(tuple<_Args...>
1157 (std::forward<_Args>(__args)...),
1161 // Call as const volatile
1162 template<typename... _Args, typename _Result
1163 = decltype( std::declval<const volatile _Functor>()(
1164 _Mu<_Bound_args>()( std::declval<const volatile _Bound_args&>(),
1165 std::declval<tuple<_Args...>&&>() )... ) )>
1167 operator()(_Args&&... __args) const volatile
1169 return this->__call_c_v<_Result>(tuple<_Args...>
1170 (std::forward<_Args>(__args)...),
1176 /// Type of the function object returned from bind<R>().
1177 template<typename _Result, typename _Signature>
1178 struct _Bind_result;
1180 template<typename _Result, typename _Functor, typename... _Bound_args>
1181 class _Bind_result<_Result, _Functor(_Bound_args...)>
1183 typedef _Bind_result __self_type;
1184 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
1188 tuple<_Bound_args...> _M_bound_args;
1191 template<typename _Res>
1192 struct __enable_if_void : enable_if<is_void<_Res>::value, int> { };
1193 template<typename _Res>
1194 struct __disable_if_void : enable_if<!is_void<_Res>::value, int> { };
1197 template<typename _Res, typename... _Args, int... _Indexes>
1199 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1200 typename __disable_if_void<_Res>::type = 0)
1202 return _M_f(_Mu<_Bound_args>()
1203 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1206 // Call unqualified, return void
1207 template<typename _Res, typename... _Args, int... _Indexes>
1209 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1210 typename __enable_if_void<_Res>::type = 0)
1212 _M_f(_Mu<_Bound_args>()
1213 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1217 template<typename _Res, typename... _Args, int... _Indexes>
1219 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1220 typename __disable_if_void<_Res>::type = 0) const
1222 return _M_f(_Mu<_Bound_args>()
1223 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1226 // Call as const, return void
1227 template<typename _Res, typename... _Args, int... _Indexes>
1229 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1230 typename __enable_if_void<_Res>::type = 0) const
1232 _M_f(_Mu<_Bound_args>()
1233 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1237 template<typename _Res, typename... _Args, int... _Indexes>
1239 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1240 typename __disable_if_void<_Res>::type = 0) volatile
1242 return _M_f(_Mu<_Bound_args>()
1243 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1246 // Call as volatile, return void
1247 template<typename _Res, typename... _Args, int... _Indexes>
1249 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1250 typename __enable_if_void<_Res>::type = 0) volatile
1252 _M_f(_Mu<_Bound_args>()
1253 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1256 // Call as const volatile
1257 template<typename _Res, typename... _Args, int... _Indexes>
1259 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1260 typename __disable_if_void<_Res>::type = 0) const volatile
1262 return _M_f(_Mu<_Bound_args>()
1263 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1266 // Call as const volatile, return void
1267 template<typename _Res, typename... _Args, int... _Indexes>
1269 __call(tuple<_Args...>&& __args,
1270 _Index_tuple<_Indexes...>,
1271 typename __enable_if_void<_Res>::type = 0) const volatile
1273 _M_f(_Mu<_Bound_args>()
1274 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1278 typedef _Result result_type;
1281 _Bind_result(_Functor __f, _Bound_args... __bound_args)
1282 : _M_f(std::forward<_Functor>(__f)),
1283 _M_bound_args(std::forward<_Bound_args>(__bound_args)...)
1287 template<typename... _Args>
1289 operator()(_Args&&... __args)
1291 return this->__call<_Result>(
1292 tuple<_Args...>(std::forward<_Args...>(__args)...),
1297 template<typename... _Args>
1299 operator()(_Args&&... __args) const
1301 return this->__call<_Result>(
1302 tuple<_Args...>(std::forward<_Args...>(__args)...),
1307 template<typename... _Args>
1309 operator()(_Args&&... __args) volatile
1311 return this->__call<_Result>(
1312 tuple<_Args...>(std::forward<_Args...>(__args)...),
1316 // Call as const volatile
1317 template<typename... _Args>
1319 operator()(_Args&&... __args) const volatile
1321 return this->__call<_Result>(
1322 tuple<_Args...>(std::forward<_Args...>(__args)...),
1328 * @brief Class template _Bind is always a bind expression.
1331 template<typename _Signature>
1332 struct is_bind_expression<_Bind<_Signature> >
1333 : public true_type { };
1336 * @brief Class template _Bind is always a bind expression.
1339 template<typename _Result, typename _Signature>
1340 struct is_bind_expression<_Bind_result<_Result, _Signature> >
1341 : public true_type { };
1344 * @brief Function template for std::bind.
1347 template<typename _Functor, typename... _ArgTypes>
1349 _Bind<typename _Maybe_wrap_member_pointer<_Functor>::type(_ArgTypes...)>
1350 bind(_Functor __f, _ArgTypes... __args)
1352 typedef _Maybe_wrap_member_pointer<_Functor> __maybe_type;
1353 typedef typename __maybe_type::type __functor_type;
1354 typedef _Bind<__functor_type(_ArgTypes...)> __result_type;
1355 return __result_type(__maybe_type::__do_wrap(__f),
1356 std::forward<_ArgTypes>(__args)...);
1360 * @brief Function template for std::bind.
1363 template<typename _Result, typename _Functor, typename... _ArgTypes>
1365 _Bind_result<_Result,
1366 typename _Maybe_wrap_member_pointer<_Functor>::type
1368 bind(_Functor __f, _ArgTypes... __args)
1370 typedef _Maybe_wrap_member_pointer<_Functor> __maybe_type;
1371 typedef typename __maybe_type::type __functor_type;
1372 typedef _Bind_result<_Result, __functor_type(_ArgTypes...)>
1374 return __result_type(__maybe_type::__do_wrap(__f),
1375 std::forward<_ArgTypes>(__args)...);
1379 * @brief Exception class thrown when class template function's
1380 * operator() is called with an empty target.
1381 * @ingroup exceptions
1383 class bad_function_call : public std::exception { };
1386 * The integral constant expression 0 can be converted into a
1387 * pointer to this type. It is used by the function template to
1388 * accept NULL pointers.
1390 struct _M_clear_type;
1393 * Trait identifying "location-invariant" types, meaning that the
1394 * address of the object (or any of its members) will not escape.
1395 * Also implies a trivial copy constructor and assignment operator.
1397 template<typename _Tp>
1398 struct __is_location_invariant
1399 : integral_constant<bool, (is_pointer<_Tp>::value
1400 || is_member_pointer<_Tp>::value)>
1403 class _Undefined_class;
1408 const void* _M_const_object;
1409 void (*_M_function_pointer)();
1410 void (_Undefined_class::*_M_member_pointer)();
1415 void* _M_access() { return &_M_pod_data[0]; }
1416 const void* _M_access() const { return &_M_pod_data[0]; }
1418 template<typename _Tp>
1421 { return *static_cast<_Tp*>(_M_access()); }
1423 template<typename _Tp>
1426 { return *static_cast<const _Tp*>(_M_access()); }
1428 _Nocopy_types _M_unused;
1429 char _M_pod_data[sizeof(_Nocopy_types)];
1432 enum _Manager_operation
1440 // Simple type wrapper that helps avoid annoying const problems
1441 // when casting between void pointers and pointers-to-pointers.
1442 template<typename _Tp>
1443 struct _Simple_type_wrapper
1445 _Simple_type_wrapper(_Tp __value) : __value(__value) { }
1450 template<typename _Tp>
1451 struct __is_location_invariant<_Simple_type_wrapper<_Tp> >
1452 : __is_location_invariant<_Tp>
1455 // Converts a reference to a function object into a callable
1457 template<typename _Functor>
1459 __callable_functor(_Functor& __f)
1462 template<typename _Member, typename _Class>
1463 inline _Mem_fn<_Member _Class::*>
1464 __callable_functor(_Member _Class::* &__p)
1465 { return mem_fn(__p); }
1467 template<typename _Member, typename _Class>
1468 inline _Mem_fn<_Member _Class::*>
1469 __callable_functor(_Member _Class::* const &__p)
1470 { return mem_fn(__p); }
1472 template<typename _Signature>
1475 /// Base class of all polymorphic function object wrappers.
1476 class _Function_base
1479 static const std::size_t _M_max_size = sizeof(_Nocopy_types);
1480 static const std::size_t _M_max_align = __alignof__(_Nocopy_types);
1482 template<typename _Functor>
1486 static const bool __stored_locally =
1487 (__is_location_invariant<_Functor>::value
1488 && sizeof(_Functor) <= _M_max_size
1489 && __alignof__(_Functor) <= _M_max_align
1490 && (_M_max_align % __alignof__(_Functor) == 0));
1492 typedef integral_constant<bool, __stored_locally> _Local_storage;
1494 // Retrieve a pointer to the function object
1496 _M_get_pointer(const _Any_data& __source)
1498 const _Functor* __ptr =
1499 __stored_locally? &__source._M_access<_Functor>()
1500 /* have stored a pointer */ : __source._M_access<_Functor*>();
1501 return const_cast<_Functor*>(__ptr);
1504 // Clone a location-invariant function object that fits within
1505 // an _Any_data structure.
1507 _M_clone(_Any_data& __dest, const _Any_data& __source, true_type)
1509 new (__dest._M_access()) _Functor(__source._M_access<_Functor>());
1512 // Clone a function object that is not location-invariant or
1513 // that cannot fit into an _Any_data structure.
1515 _M_clone(_Any_data& __dest, const _Any_data& __source, false_type)
1517 __dest._M_access<_Functor*>() =
1518 new _Functor(*__source._M_access<_Functor*>());
1521 // Destroying a location-invariant object may still require
1524 _M_destroy(_Any_data& __victim, true_type)
1526 __victim._M_access<_Functor>().~_Functor();
1529 // Destroying an object located on the heap.
1531 _M_destroy(_Any_data& __victim, false_type)
1533 delete __victim._M_access<_Functor*>();
1538 _M_manager(_Any_data& __dest, const _Any_data& __source,
1539 _Manager_operation __op)
1544 case __get_type_info:
1545 __dest._M_access<const type_info*>() = &typeid(_Functor);
1548 case __get_functor_ptr:
1549 __dest._M_access<_Functor*>() = _M_get_pointer(__source);
1552 case __clone_functor:
1553 _M_clone(__dest, __source, _Local_storage());
1556 case __destroy_functor:
1557 _M_destroy(__dest, _Local_storage());
1564 _M_init_functor(_Any_data& __functor, _Functor&& __f)
1565 { _M_init_functor(__functor, std::move(__f), _Local_storage()); }
1567 template<typename _Signature>
1569 _M_not_empty_function(const function<_Signature>& __f)
1570 { return static_cast<bool>(__f); }
1572 template<typename _Tp>
1574 _M_not_empty_function(const _Tp*& __fp)
1577 template<typename _Class, typename _Tp>
1579 _M_not_empty_function(_Tp _Class::* const& __mp)
1582 template<typename _Tp>
1584 _M_not_empty_function(const _Tp&)
1589 _M_init_functor(_Any_data& __functor, _Functor&& __f, true_type)
1590 { new (__functor._M_access()) _Functor(std::move(__f)); }
1593 _M_init_functor(_Any_data& __functor, _Functor&& __f, false_type)
1594 { __functor._M_access<_Functor*>() = new _Functor(std::move(__f)); }
1597 template<typename _Functor>
1598 class _Ref_manager : public _Base_manager<_Functor*>
1600 typedef _Function_base::_Base_manager<_Functor*> _Base;
1604 _M_manager(_Any_data& __dest, const _Any_data& __source,
1605 _Manager_operation __op)
1610 case __get_type_info:
1611 __dest._M_access<const type_info*>() = &typeid(_Functor);
1614 case __get_functor_ptr:
1615 __dest._M_access<_Functor*>() = *_Base::_M_get_pointer(__source);
1616 return is_const<_Functor>::value;
1620 _Base::_M_manager(__dest, __source, __op);
1626 _M_init_functor(_Any_data& __functor, reference_wrapper<_Functor> __f)
1628 // TBD: Use address_of function instead.
1629 _Base::_M_init_functor(__functor, &__f.get());
1633 _Function_base() : _M_manager(0) { }
1638 _M_manager(_M_functor, _M_functor, __destroy_functor);
1642 bool _M_empty() const { return !_M_manager; }
1644 typedef bool (*_Manager_type)(_Any_data&, const _Any_data&,
1645 _Manager_operation);
1647 _Any_data _M_functor;
1648 _Manager_type _M_manager;
1651 template<typename _Signature, typename _Functor>
1652 class _Function_handler;
1654 template<typename _Res, typename _Functor, typename... _ArgTypes>
1655 class _Function_handler<_Res(_ArgTypes...), _Functor>
1656 : public _Function_base::_Base_manager<_Functor>
1658 typedef _Function_base::_Base_manager<_Functor> _Base;
1662 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1664 return (*_Base::_M_get_pointer(__functor))(
1665 std::forward<_ArgTypes>(__args)...);
1669 template<typename _Functor, typename... _ArgTypes>
1670 class _Function_handler<void(_ArgTypes...), _Functor>
1671 : public _Function_base::_Base_manager<_Functor>
1673 typedef _Function_base::_Base_manager<_Functor> _Base;
1677 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1679 (*_Base::_M_get_pointer(__functor))(
1680 std::forward<_ArgTypes>(__args)...);
1684 template<typename _Res, typename _Functor, typename... _ArgTypes>
1685 class _Function_handler<_Res(_ArgTypes...), reference_wrapper<_Functor> >
1686 : public _Function_base::_Ref_manager<_Functor>
1688 typedef _Function_base::_Ref_manager<_Functor> _Base;
1692 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1694 return __callable_functor(**_Base::_M_get_pointer(__functor))(
1695 std::forward<_ArgTypes>(__args)...);
1699 template<typename _Functor, typename... _ArgTypes>
1700 class _Function_handler<void(_ArgTypes...), reference_wrapper<_Functor> >
1701 : public _Function_base::_Ref_manager<_Functor>
1703 typedef _Function_base::_Ref_manager<_Functor> _Base;
1707 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1709 __callable_functor(**_Base::_M_get_pointer(__functor))(
1710 std::forward<_ArgTypes>(__args)...);
1714 template<typename _Class, typename _Member, typename _Res,
1715 typename... _ArgTypes>
1716 class _Function_handler<_Res(_ArgTypes...), _Member _Class::*>
1717 : public _Function_handler<void(_ArgTypes...), _Member _Class::*>
1719 typedef _Function_handler<void(_ArgTypes...), _Member _Class::*>
1724 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1726 return mem_fn(_Base::_M_get_pointer(__functor)->__value)(
1727 std::forward<_ArgTypes>(__args)...);
1731 template<typename _Class, typename _Member, typename... _ArgTypes>
1732 class _Function_handler<void(_ArgTypes...), _Member _Class::*>
1733 : public _Function_base::_Base_manager<
1734 _Simple_type_wrapper< _Member _Class::* > >
1736 typedef _Member _Class::* _Functor;
1737 typedef _Simple_type_wrapper<_Functor> _Wrapper;
1738 typedef _Function_base::_Base_manager<_Wrapper> _Base;
1742 _M_manager(_Any_data& __dest, const _Any_data& __source,
1743 _Manager_operation __op)
1748 case __get_type_info:
1749 __dest._M_access<const type_info*>() = &typeid(_Functor);
1752 case __get_functor_ptr:
1753 __dest._M_access<_Functor*>() =
1754 &_Base::_M_get_pointer(__source)->__value;
1758 _Base::_M_manager(__dest, __source, __op);
1764 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1766 mem_fn(_Base::_M_get_pointer(__functor)->__value)(
1767 std::forward<_ArgTypes>(__args)...);
1772 * @brief Primary class template for std::function.
1775 * Polymorphic function wrapper.
1777 template<typename _Res, typename... _ArgTypes>
1778 class function<_Res(_ArgTypes...)>
1779 : public _Maybe_unary_or_binary_function<_Res, _ArgTypes...>,
1780 private _Function_base
1782 typedef _Res _Signature_type(_ArgTypes...);
1784 struct _Useless { };
1787 typedef _Res result_type;
1789 // [3.7.2.1] construct/copy/destroy
1792 * @brief Default construct creates an empty function call wrapper.
1793 * @post @c !(bool)*this
1796 function() : _Function_base() { }
1799 * @brief Default construct creates an empty function call wrapper.
1800 * @post @c !(bool)*this
1802 function(_M_clear_type*) : _Function_base() { }
1805 * @brief %Function copy constructor.
1806 * @param x A %function object with identical call signature.
1807 * @post @c (bool)*this == (bool)x
1809 * The newly-created %function contains a copy of the target of @a
1810 * x (if it has one).
1812 function(const function& __x);
1815 * @brief %Function move constructor.
1816 * @param x A %function object rvalue with identical call signature.
1818 * The newly-created %function contains the target of @a x
1821 function(function&& __x) : _Function_base()
1826 // TODO: needs allocator_arg_t
1829 * @brief Builds a %function that targets a copy of the incoming
1831 * @param f A %function object that is callable with parameters of
1832 * type @c T1, @c T2, ..., @c TN and returns a value convertible
1835 * The newly-created %function object will target a copy of @a
1836 * f. If @a f is @c reference_wrapper<F>, then this function
1837 * object will contain a reference to the function object @c
1838 * f.get(). If @a f is a NULL function pointer or NULL
1839 * pointer-to-member, the newly-created object will be empty.
1841 * If @a f is a non-NULL function pointer or an object of type @c
1842 * reference_wrapper<F>, this function will not throw.
1844 template<typename _Functor>
1845 function(_Functor __f,
1847 !is_integral<_Functor>::value, _Useless>::type
1851 * @brief %Function assignment operator.
1852 * @param x A %function with identical call signature.
1853 * @post @c (bool)*this == (bool)x
1856 * The target of @a x is copied to @c *this. If @a x has no
1857 * target, then @c *this will be empty.
1859 * If @a x targets a function pointer or a reference to a function
1860 * object, then this operation will not throw an %exception.
1863 operator=(const function& __x)
1865 function(__x).swap(*this);
1870 * @brief %Function move-assignment operator.
1871 * @param x A %function rvalue with identical call signature.
1874 * The target of @a x is moved to @c *this. If @a x has no
1875 * target, then @c *this will be empty.
1877 * If @a x targets a function pointer or a reference to a function
1878 * object, then this operation will not throw an %exception.
1881 operator=(function&& __x)
1883 function(std::move(__x)).swap(*this);
1888 * @brief %Function assignment to zero.
1889 * @post @c !(bool)*this
1892 * The target of @c *this is deallocated, leaving it empty.
1895 operator=(_M_clear_type*)
1899 _M_manager(_M_functor, _M_functor, __destroy_functor);
1907 * @brief %Function assignment to a new target.
1908 * @param f A %function object that is callable with parameters of
1909 * type @c T1, @c T2, ..., @c TN and returns a value convertible
1913 * This %function object wrapper will target a copy of @a
1914 * f. If @a f is @c reference_wrapper<F>, then this function
1915 * object will contain a reference to the function object @c
1916 * f.get(). If @a f is a NULL function pointer or NULL
1917 * pointer-to-member, @c this object will be empty.
1919 * If @a f is a non-NULL function pointer or an object of type @c
1920 * reference_wrapper<F>, this function will not throw.
1922 template<typename _Functor>
1923 typename enable_if<!is_integral<_Functor>::value, function&>::type
1924 operator=(_Functor&& __f)
1926 function(std::forward<_Functor>(__f)).swap(*this);
1931 template<typename _Functor>
1932 typename enable_if<!is_integral<_Functor>::value, function&>::type
1933 operator=(reference_wrapper<_Functor> __f)
1935 function(__f).swap(*this);
1939 // [3.7.2.2] function modifiers
1942 * @brief Swap the targets of two %function objects.
1943 * @param f A %function with identical call signature.
1945 * Swap the targets of @c this function object and @a f. This
1946 * function will not throw an %exception.
1948 void swap(function& __x)
1950 /* We cannot perform direct assignments of the _M_functor
1951 parts as they are of type _Any_data and have a different
1952 dynamic type. Doing so would violate type-based aliasing
1953 rules and lead to spurious miscompilations.
1954 Instead perform a bytewise exchange of the memory of
1956 ??? A wordwise exchange honoring alignment of _M_functor
1957 would be more efficient. See PR42845. */
1958 for (unsigned i = 0; i < sizeof (_M_functor._M_pod_data); ++i)
1959 std::swap (_M_functor._M_pod_data[i], __x._M_functor._M_pod_data[i]);
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 // Out-of-line member definitions.
2044 template<typename _Res, typename... _ArgTypes>
2045 function<_Res(_ArgTypes...)>::
2046 function(const function& __x)
2049 if (static_cast<bool>(__x))
2051 _M_invoker = __x._M_invoker;
2052 _M_manager = __x._M_manager;
2053 __x._M_manager(_M_functor, __x._M_functor, __clone_functor);
2057 template<typename _Res, typename... _ArgTypes>
2058 template<typename _Functor>
2059 function<_Res(_ArgTypes...)>::
2060 function(_Functor __f,
2062 !is_integral<_Functor>::value, _Useless>::type)
2065 typedef _Function_handler<_Signature_type, _Functor> _My_handler;
2067 if (_My_handler::_M_not_empty_function(__f))
2069 _M_invoker = &_My_handler::_M_invoke;
2070 _M_manager = &_My_handler::_M_manager;
2071 _My_handler::_M_init_functor(_M_functor, std::move(__f));
2075 template<typename _Res, typename... _ArgTypes>
2077 function<_Res(_ArgTypes...)>::
2078 operator()(_ArgTypes... __args) const
2081 __throw_bad_function_call();
2082 return _M_invoker(_M_functor, std::forward<_ArgTypes>(__args)...);
2086 template<typename _Res, typename... _ArgTypes>
2088 function<_Res(_ArgTypes...)>::
2093 _Any_data __typeinfo_result;
2094 _M_manager(__typeinfo_result, _M_functor, __get_type_info);
2095 return *__typeinfo_result._M_access<const type_info*>();
2098 return typeid(void);
2101 template<typename _Res, typename... _ArgTypes>
2102 template<typename _Functor>
2104 function<_Res(_ArgTypes...)>::
2107 if (typeid(_Functor) == target_type() && _M_manager)
2110 if (_M_manager(__ptr, _M_functor, __get_functor_ptr)
2111 && !is_const<_Functor>::value)
2114 return __ptr._M_access<_Functor*>();
2120 template<typename _Res, typename... _ArgTypes>
2121 template<typename _Functor>
2123 function<_Res(_ArgTypes...)>::
2126 if (typeid(_Functor) == target_type() && _M_manager)
2129 _M_manager(__ptr, _M_functor, __get_functor_ptr);
2130 return __ptr._M_access<const _Functor*>();
2137 // [20.7.15.2.6] null pointer comparisons
2140 * @brief Compares a polymorphic function object wrapper against 0
2141 * (the NULL pointer).
2142 * @returns @c true if the wrapper has no target, @c false otherwise
2144 * This function will not throw an %exception.
2146 template<typename _Res, typename... _Args>
2148 operator==(const function<_Res(_Args...)>& __f, _M_clear_type*)
2149 { return !static_cast<bool>(__f); }
2152 template<typename _Res, typename... _Args>
2154 operator==(_M_clear_type*, const function<_Res(_Args...)>& __f)
2155 { return !static_cast<bool>(__f); }
2158 * @brief Compares a polymorphic function object wrapper against 0
2159 * (the NULL pointer).
2160 * @returns @c false if the wrapper has no target, @c true otherwise
2162 * This function will not throw an %exception.
2164 template<typename _Res, typename... _Args>
2166 operator!=(const function<_Res(_Args...)>& __f, _M_clear_type*)
2167 { return static_cast<bool>(__f); }
2170 template<typename _Res, typename... _Args>
2172 operator!=(_M_clear_type*, const function<_Res(_Args...)>& __f)
2173 { return static_cast<bool>(__f); }
2175 // [20.7.15.2.7] specialized algorithms
2178 * @brief Swap the targets of two polymorphic function object wrappers.
2180 * This function will not throw an %exception.
2182 template<typename _Res, typename... _Args>
2184 swap(function<_Res(_Args...)>& __x, function<_Res(_Args...)>& __y)
2188 #endif // __GXX_EXPERIMENTAL_CXX0X__
2190 #endif // _GLIBCXX_FUNCTIONAL