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)
394 : _M_data(std::__addressof(__indata))
397 reference_wrapper(_Tp&&) = delete;
399 reference_wrapper(const reference_wrapper<_Tp>& __inref):
400 _M_data(__inref._M_data)
404 operator=(const reference_wrapper<_Tp>& __inref)
406 _M_data = __inref._M_data;
410 operator _Tp&() const
411 { return this->get(); }
417 template<typename... _Args>
418 typename result_of<_M_func_type(_Args...)>::type
419 operator()(_Args&&... __args) const
421 return __invoke(get(), std::forward<_Args>(__args)...);
426 /// Denotes a reference should be taken to a variable.
427 template<typename _Tp>
428 inline reference_wrapper<_Tp>
430 { return reference_wrapper<_Tp>(__t); }
432 /// Denotes a const reference should be taken to a variable.
433 template<typename _Tp>
434 inline reference_wrapper<const _Tp>
436 { return reference_wrapper<const _Tp>(__t); }
438 /// Partial specialization.
439 template<typename _Tp>
440 inline reference_wrapper<_Tp>
441 ref(reference_wrapper<_Tp> __t)
442 { return ref(__t.get()); }
444 /// Partial specialization.
445 template<typename _Tp>
446 inline reference_wrapper<const _Tp>
447 cref(reference_wrapper<_Tp> __t)
448 { return cref(__t.get()); }
452 template<typename _Tp, bool>
453 struct _Mem_fn_const_or_non
455 typedef const _Tp& type;
458 template<typename _Tp>
459 struct _Mem_fn_const_or_non<_Tp, false>
465 * Derives from @c unary_function or @c binary_function, or perhaps
466 * nothing, depending on the number of arguments provided. The
467 * primary template is the basis case, which derives nothing.
469 template<typename _Res, typename... _ArgTypes>
470 struct _Maybe_unary_or_binary_function { };
472 /// Derives from @c unary_function, as appropriate.
473 template<typename _Res, typename _T1>
474 struct _Maybe_unary_or_binary_function<_Res, _T1>
475 : std::unary_function<_T1, _Res> { };
477 /// Derives from @c binary_function, as appropriate.
478 template<typename _Res, typename _T1, typename _T2>
479 struct _Maybe_unary_or_binary_function<_Res, _T1, _T2>
480 : std::binary_function<_T1, _T2, _Res> { };
482 /// Implementation of @c mem_fn for member function pointers.
483 template<typename _Res, typename _Class, typename... _ArgTypes>
484 class _Mem_fn<_Res (_Class::*)(_ArgTypes...)>
485 : public _Maybe_unary_or_binary_function<_Res, _Class*, _ArgTypes...>
487 typedef _Res (_Class::*_Functor)(_ArgTypes...);
489 template<typename _Tp>
491 _M_call(_Tp& __object, const volatile _Class *,
492 _ArgTypes... __args) const
493 { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); }
495 template<typename _Tp>
497 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
498 { return ((*__ptr).*__pmf)(std::forward<_ArgTypes>(__args)...); }
501 typedef _Res result_type;
503 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
507 operator()(_Class& __object, _ArgTypes... __args) const
508 { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); }
512 operator()(_Class* __object, _ArgTypes... __args) const
513 { return (__object->*__pmf)(std::forward<_ArgTypes>(__args)...); }
515 // Handle smart pointers, references and pointers to derived
516 template<typename _Tp>
518 operator()(_Tp& __object, _ArgTypes... __args) const
520 return _M_call(__object, &__object,
521 std::forward<_ArgTypes>(__args)...);
528 /// Implementation of @c mem_fn for const member function pointers.
529 template<typename _Res, typename _Class, typename... _ArgTypes>
530 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) const>
531 : public _Maybe_unary_or_binary_function<_Res, const _Class*,
534 typedef _Res (_Class::*_Functor)(_ArgTypes...) const;
536 template<typename _Tp>
538 _M_call(_Tp& __object, const volatile _Class *,
539 _ArgTypes... __args) const
540 { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); }
542 template<typename _Tp>
544 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
545 { return ((*__ptr).*__pmf)(std::forward<_ArgTypes>(__args)...); }
548 typedef _Res result_type;
550 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
554 operator()(const _Class& __object, _ArgTypes... __args) const
555 { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); }
559 operator()(const _Class* __object, _ArgTypes... __args) const
560 { return (__object->*__pmf)(std::forward<_ArgTypes>(__args)...); }
562 // Handle smart pointers, references and pointers to derived
563 template<typename _Tp>
564 _Res operator()(_Tp& __object, _ArgTypes... __args) const
566 return _M_call(__object, &__object,
567 std::forward<_ArgTypes>(__args)...);
574 /// Implementation of @c mem_fn for volatile member function pointers.
575 template<typename _Res, typename _Class, typename... _ArgTypes>
576 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) volatile>
577 : public _Maybe_unary_or_binary_function<_Res, volatile _Class*,
580 typedef _Res (_Class::*_Functor)(_ArgTypes...) volatile;
582 template<typename _Tp>
584 _M_call(_Tp& __object, const volatile _Class *,
585 _ArgTypes... __args) const
586 { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); }
588 template<typename _Tp>
590 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
591 { return ((*__ptr).*__pmf)(std::forward<_ArgTypes>(__args)...); }
594 typedef _Res result_type;
596 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
600 operator()(volatile _Class& __object, _ArgTypes... __args) const
601 { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); }
605 operator()(volatile _Class* __object, _ArgTypes... __args) const
606 { return (__object->*__pmf)(std::forward<_ArgTypes>(__args)...); }
608 // Handle smart pointers, references and pointers to derived
609 template<typename _Tp>
611 operator()(_Tp& __object, _ArgTypes... __args) const
613 return _M_call(__object, &__object,
614 std::forward<_ArgTypes>(__args)...);
621 /// Implementation of @c mem_fn for const volatile member function pointers.
622 template<typename _Res, typename _Class, typename... _ArgTypes>
623 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) const volatile>
624 : public _Maybe_unary_or_binary_function<_Res, const volatile _Class*,
627 typedef _Res (_Class::*_Functor)(_ArgTypes...) const volatile;
629 template<typename _Tp>
631 _M_call(_Tp& __object, const volatile _Class *,
632 _ArgTypes... __args) const
633 { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); }
635 template<typename _Tp>
637 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
638 { return ((*__ptr).*__pmf)(std::forward<_ArgTypes>(__args)...); }
641 typedef _Res result_type;
643 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
647 operator()(const volatile _Class& __object, _ArgTypes... __args) const
648 { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); }
652 operator()(const volatile _Class* __object, _ArgTypes... __args) const
653 { return (__object->*__pmf)(std::forward<_ArgTypes>(__args)...); }
655 // Handle smart pointers, references and pointers to derived
656 template<typename _Tp>
657 _Res operator()(_Tp& __object, _ArgTypes... __args) const
659 return _M_call(__object, &__object,
660 std::forward<_ArgTypes>(__args)...);
668 template<typename _Res, typename _Class>
669 class _Mem_fn<_Res _Class::*>
671 // This bit of genius is due to Peter Dimov, improved slightly by
673 template<typename _Tp>
675 _M_call(_Tp& __object, _Class *) const
676 { return __object.*__pm; }
678 template<typename _Tp, typename _Up>
680 _M_call(_Tp& __object, _Up * const *) const
681 { return (*__object).*__pm; }
683 template<typename _Tp, typename _Up>
685 _M_call(_Tp& __object, const _Up * const *) const
686 { return (*__object).*__pm; }
688 template<typename _Tp>
690 _M_call(_Tp& __object, const _Class *) const
691 { return __object.*__pm; }
693 template<typename _Tp>
695 _M_call(_Tp& __ptr, const volatile void*) const
696 { return (*__ptr).*__pm; }
698 template<typename _Tp> static _Tp& __get_ref();
700 template<typename _Tp>
701 static __sfinae_types::__one __check_const(_Tp&, _Class*);
702 template<typename _Tp, typename _Up>
703 static __sfinae_types::__one __check_const(_Tp&, _Up * const *);
704 template<typename _Tp, typename _Up>
705 static __sfinae_types::__two __check_const(_Tp&, const _Up * const *);
706 template<typename _Tp>
707 static __sfinae_types::__two __check_const(_Tp&, const _Class*);
708 template<typename _Tp>
709 static __sfinae_types::__two __check_const(_Tp&, const volatile void*);
712 template<typename _Tp>
714 : _Mem_fn_const_or_non<_Res,
715 (sizeof(__sfinae_types::__two)
716 == sizeof(__check_const<_Tp>(__get_ref<_Tp>(), (_Tp*)0)))>
719 template<typename _Signature>
722 template<typename _CVMem, typename _Tp>
723 struct result<_CVMem(_Tp)>
724 : public _Result_type<_Tp> { };
726 template<typename _CVMem, typename _Tp>
727 struct result<_CVMem(_Tp&)>
728 : public _Result_type<_Tp> { };
731 _Mem_fn(_Res _Class::*__pm) : __pm(__pm) { }
735 operator()(_Class& __object) const
736 { return __object.*__pm; }
739 operator()(const _Class& __object) const
740 { return __object.*__pm; }
744 operator()(_Class* __object) const
745 { return __object->*__pm; }
748 operator()(const _Class* __object) const
749 { return __object->*__pm; }
751 // Handle smart pointers and derived
752 template<typename _Tp>
753 typename _Result_type<_Tp>::type
754 operator()(_Tp& __unknown) const
755 { return _M_call(__unknown, &__unknown); }
762 * @brief Returns a function object that forwards to the member
766 template<typename _Tp, typename _Class>
767 inline _Mem_fn<_Tp _Class::*>
768 mem_fn(_Tp _Class::* __pm)
770 return _Mem_fn<_Tp _Class::*>(__pm);
774 * @brief Determines if the given type _Tp is a function object
775 * should be treated as a subexpression when evaluating calls to
776 * function objects returned by bind(). [TR1 3.6.1]
779 template<typename _Tp>
780 struct is_bind_expression
781 : public false_type { };
784 * @brief Determines if the given type _Tp is a placeholder in a
785 * bind() expression and, if so, which placeholder it is. [TR1 3.6.2]
788 template<typename _Tp>
789 struct is_placeholder
790 : public integral_constant<int, 0>
793 /// The type of placeholder objects defined by libstdc++.
794 template<int _Num> struct _Placeholder { };
796 /** @namespace std::placeholders
797 * @brief ISO C++ 0x entities sub namespace for functional.
800 * Define a large number of placeholders. There is no way to
801 * simplify this with variadic templates, because we're introducing
802 * unique names for each.
804 namespace placeholders
817 _Placeholder<10> _10;
818 _Placeholder<11> _11;
819 _Placeholder<12> _12;
820 _Placeholder<13> _13;
821 _Placeholder<14> _14;
822 _Placeholder<15> _15;
823 _Placeholder<16> _16;
824 _Placeholder<17> _17;
825 _Placeholder<18> _18;
826 _Placeholder<19> _19;
827 _Placeholder<20> _20;
828 _Placeholder<21> _21;
829 _Placeholder<22> _22;
830 _Placeholder<23> _23;
831 _Placeholder<24> _24;
832 _Placeholder<25> _25;
833 _Placeholder<26> _26;
834 _Placeholder<27> _27;
835 _Placeholder<28> _28;
836 _Placeholder<29> _29;
841 * Partial specialization of is_placeholder that provides the placeholder
842 * number for the placeholder objects defined by libstdc++.
846 struct is_placeholder<_Placeholder<_Num> >
847 : public integral_constant<int, _Num>
851 * Used by _Safe_tuple_element to indicate that there is no tuple
852 * element at this position.
854 struct _No_tuple_element;
857 * Implementation helper for _Safe_tuple_element. This primary
858 * template handles the case where it is safe to use @c
861 template<int __i, typename _Tuple, bool _IsSafe>
862 struct _Safe_tuple_element_impl
863 : tuple_element<__i, _Tuple> { };
866 * Implementation helper for _Safe_tuple_element. This partial
867 * specialization handles the case where it is not safe to use @c
868 * tuple_element. We just return @c _No_tuple_element.
870 template<int __i, typename _Tuple>
871 struct _Safe_tuple_element_impl<__i, _Tuple, false>
873 typedef _No_tuple_element type;
877 * Like tuple_element, but returns @c _No_tuple_element when
878 * tuple_element would return an error.
880 template<int __i, typename _Tuple>
881 struct _Safe_tuple_element
882 : _Safe_tuple_element_impl<__i, _Tuple,
883 (__i >= 0 && __i < tuple_size<_Tuple>::value)>
887 * Maps an argument to bind() into an actual argument to the bound
888 * function object [TR1 3.6.3/5]. Only the first parameter should
889 * be specified: the rest are used to determine among the various
890 * implementations. Note that, although this class is a function
891 * object, it isn't entirely normal because it takes only two
892 * parameters regardless of the number of parameters passed to the
893 * bind expression. The first parameter is the bound argument and
894 * the second parameter is a tuple containing references to the
895 * rest of the arguments.
897 template<typename _Arg,
898 bool _IsBindExp = is_bind_expression<_Arg>::value,
899 bool _IsPlaceholder = (is_placeholder<_Arg>::value > 0)>
903 * If the argument is reference_wrapper<_Tp>, returns the
904 * underlying reference. [TR1 3.6.3/5 bullet 1]
906 template<typename _Tp>
907 class _Mu<reference_wrapper<_Tp>, false, false>
910 typedef _Tp& result_type;
912 /* Note: This won't actually work for const volatile
913 * reference_wrappers, because reference_wrapper::get() is const
914 * but not volatile-qualified. This might be a defect in the TR.
916 template<typename _CVRef, typename _Tuple>
918 operator()(_CVRef& __arg, _Tuple&&) const volatile
919 { return __arg.get(); }
923 * If the argument is a bind expression, we invoke the underlying
924 * function object with the same cv-qualifiers as we are given and
925 * pass along all of our arguments (unwrapped). [TR1 3.6.3/5 bullet 2]
927 template<typename _Arg>
928 class _Mu<_Arg, true, false>
931 template<typename _Signature> class result;
933 // Determine the result type when we pass the arguments along. This
934 // involves passing along the cv-qualifiers placed on _Mu and
935 // unwrapping the argument bundle.
936 template<typename _CVMu, typename _CVArg, typename... _Args>
937 class result<_CVMu(_CVArg, tuple<_Args...>)>
938 : public result_of<_CVArg(_Args...)> { };
940 template<typename _CVArg, typename... _Args>
941 typename result_of<_CVArg(_Args...)>::type
942 operator()(_CVArg& __arg,
943 tuple<_Args...>&& __tuple) const volatile
945 // Construct an index tuple and forward to __call
946 typedef typename _Build_index_tuple<sizeof...(_Args)>::__type
948 return this->__call(__arg, std::move(__tuple), _Indexes());
952 // Invokes the underlying function object __arg by unpacking all
953 // of the arguments in the tuple.
954 template<typename _CVArg, typename... _Args, int... _Indexes>
955 typename result_of<_CVArg(_Args...)>::type
956 __call(_CVArg& __arg, tuple<_Args...>&& __tuple,
957 const _Index_tuple<_Indexes...>&) const volatile
959 return __arg(std::forward<_Args>(get<_Indexes>(__tuple))...);
964 * If the argument is a placeholder for the Nth argument, returns
965 * a reference to the Nth argument to the bind function object.
966 * [TR1 3.6.3/5 bullet 3]
968 template<typename _Arg>
969 class _Mu<_Arg, false, true>
972 template<typename _Signature> class result;
974 template<typename _CVMu, typename _CVArg, typename _Tuple>
975 class result<_CVMu(_CVArg, _Tuple)>
977 // Add a reference, if it hasn't already been done for us.
978 // This allows us to be a little bit sloppy in constructing
979 // the tuple that we pass to result_of<...>.
980 typedef typename _Safe_tuple_element<(is_placeholder<_Arg>::value
985 typedef typename add_rvalue_reference<__base_type>::type type;
988 template<typename _Tuple>
989 typename result<_Mu(_Arg, _Tuple)>::type
990 operator()(const volatile _Arg&, _Tuple&& __tuple) const volatile
992 return std::forward<typename result<_Mu(_Arg, _Tuple)>::type>(
993 ::std::get<(is_placeholder<_Arg>::value - 1)>(__tuple));
998 * If the argument is just a value, returns a reference to that
999 * value. The cv-qualifiers on the reference are the same as the
1000 * cv-qualifiers on the _Mu object. [TR1 3.6.3/5 bullet 4]
1002 template<typename _Arg>
1003 class _Mu<_Arg, false, false>
1006 template<typename _Signature> struct result;
1008 template<typename _CVMu, typename _CVArg, typename _Tuple>
1009 struct result<_CVMu(_CVArg, _Tuple)>
1011 typedef typename add_lvalue_reference<_CVArg>::type type;
1014 // Pick up the cv-qualifiers of the argument
1015 template<typename _CVArg, typename _Tuple>
1017 operator()(_CVArg&& __arg, _Tuple&&) const volatile
1018 { return std::forward<_CVArg>(__arg); }
1022 * Maps member pointers into instances of _Mem_fn but leaves all
1023 * other function objects untouched. Used by tr1::bind(). The
1024 * primary template handles the non--member-pointer case.
1026 template<typename _Tp>
1027 struct _Maybe_wrap_member_pointer
1032 __do_wrap(const _Tp& __x)
1037 * Maps member pointers into instances of _Mem_fn but leaves all
1038 * other function objects untouched. Used by tr1::bind(). This
1039 * partial specialization handles the member pointer case.
1041 template<typename _Tp, typename _Class>
1042 struct _Maybe_wrap_member_pointer<_Tp _Class::*>
1044 typedef _Mem_fn<_Tp _Class::*> type;
1047 __do_wrap(_Tp _Class::* __pm)
1048 { return type(__pm); }
1051 // Specialization needed to prevent "forming reference to void" errors when
1052 // bind<void>() is called, because argument deduction instantiates
1053 // _Maybe_wrap_member_pointer<void> outside the immediate context where
1056 struct _Maybe_wrap_member_pointer<void>
1061 /// Type of the function object returned from bind().
1062 template<typename _Signature>
1065 template<typename _Functor, typename... _Bound_args>
1066 class _Bind<_Functor(_Bound_args...)>
1067 : public _Weak_result_type<_Functor>
1069 typedef _Bind __self_type;
1070 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
1074 tuple<_Bound_args...> _M_bound_args;
1077 template<typename _Result, typename... _Args, int... _Indexes>
1079 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>)
1081 return _M_f(_Mu<_Bound_args>()
1082 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1086 template<typename _Result, typename... _Args, int... _Indexes>
1088 __call_c(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>) const
1090 return _M_f(_Mu<_Bound_args>()
1091 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1096 template<typename _Result, typename... _Args, int... _Indexes>
1098 __call_v(tuple<_Args...>&& __args,
1099 _Index_tuple<_Indexes...>) volatile
1101 return _M_f(_Mu<_Bound_args>()
1102 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1105 // Call as const volatile
1106 template<typename _Result, typename... _Args, int... _Indexes>
1108 __call_c_v(tuple<_Args...>&& __args,
1109 _Index_tuple<_Indexes...>) const volatile
1111 return _M_f(_Mu<_Bound_args>()
1112 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1117 explicit _Bind(_Functor __f, _Bound_args... __bound_args)
1118 : _M_f(std::forward<_Functor>(__f)),
1119 _M_bound_args(std::forward<_Bound_args>(__bound_args)...)
1123 template<typename... _Args, typename _Result
1124 = decltype( std::declval<_Functor>()(
1125 _Mu<_Bound_args>()( std::declval<_Bound_args&>(),
1126 std::declval<tuple<_Args...>&&>() )... ) )>
1128 operator()(_Args&&... __args)
1130 return this->__call<_Result>(tuple<_Args...>
1131 (std::forward<_Args>(__args)...),
1136 template<typename... _Args, typename _Result
1137 = decltype( std::declval<const _Functor>()(
1138 _Mu<_Bound_args>()( std::declval<const _Bound_args&>(),
1139 std::declval<tuple<_Args...>&&>() )... ) )>
1141 operator()(_Args&&... __args) const
1143 return this->__call_c<_Result>(tuple<_Args...>
1144 (std::forward<_Args>(__args)...),
1150 template<typename... _Args, typename _Result
1151 = decltype( std::declval<volatile _Functor>()(
1152 _Mu<_Bound_args>()( std::declval<volatile _Bound_args&>(),
1153 std::declval<tuple<_Args...>&&>() )... ) )>
1155 operator()(_Args&&... __args) volatile
1157 return this->__call_v<_Result>(tuple<_Args...>
1158 (std::forward<_Args>(__args)...),
1162 // Call as const volatile
1163 template<typename... _Args, typename _Result
1164 = decltype( std::declval<const volatile _Functor>()(
1165 _Mu<_Bound_args>()( std::declval<const volatile _Bound_args&>(),
1166 std::declval<tuple<_Args...>&&>() )... ) )>
1168 operator()(_Args&&... __args) const volatile
1170 return this->__call_c_v<_Result>(tuple<_Args...>
1171 (std::forward<_Args>(__args)...),
1177 /// Type of the function object returned from bind<R>().
1178 template<typename _Result, typename _Signature>
1179 struct _Bind_result;
1181 template<typename _Result, typename _Functor, typename... _Bound_args>
1182 class _Bind_result<_Result, _Functor(_Bound_args...)>
1184 typedef _Bind_result __self_type;
1185 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
1189 tuple<_Bound_args...> _M_bound_args;
1192 template<typename _Res>
1193 struct __enable_if_void : enable_if<is_void<_Res>::value, int> { };
1194 template<typename _Res>
1195 struct __disable_if_void : enable_if<!is_void<_Res>::value, int> { };
1198 template<typename _Res, typename... _Args, int... _Indexes>
1200 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1201 typename __disable_if_void<_Res>::type = 0)
1203 return _M_f(_Mu<_Bound_args>()
1204 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1207 // Call unqualified, return void
1208 template<typename _Res, typename... _Args, int... _Indexes>
1210 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1211 typename __enable_if_void<_Res>::type = 0)
1213 _M_f(_Mu<_Bound_args>()
1214 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1218 template<typename _Res, typename... _Args, int... _Indexes>
1220 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1221 typename __disable_if_void<_Res>::type = 0) const
1223 return _M_f(_Mu<_Bound_args>()
1224 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1227 // Call as const, return void
1228 template<typename _Res, typename... _Args, int... _Indexes>
1230 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1231 typename __enable_if_void<_Res>::type = 0) const
1233 _M_f(_Mu<_Bound_args>()
1234 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1238 template<typename _Res, typename... _Args, int... _Indexes>
1240 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1241 typename __disable_if_void<_Res>::type = 0) volatile
1243 return _M_f(_Mu<_Bound_args>()
1244 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1247 // Call as volatile, return void
1248 template<typename _Res, typename... _Args, int... _Indexes>
1250 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1251 typename __enable_if_void<_Res>::type = 0) volatile
1253 _M_f(_Mu<_Bound_args>()
1254 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1257 // Call as const volatile
1258 template<typename _Res, typename... _Args, int... _Indexes>
1260 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1261 typename __disable_if_void<_Res>::type = 0) const volatile
1263 return _M_f(_Mu<_Bound_args>()
1264 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1267 // Call as const volatile, return void
1268 template<typename _Res, typename... _Args, int... _Indexes>
1270 __call(tuple<_Args...>&& __args,
1271 _Index_tuple<_Indexes...>,
1272 typename __enable_if_void<_Res>::type = 0) const volatile
1274 _M_f(_Mu<_Bound_args>()
1275 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1279 typedef _Result result_type;
1282 _Bind_result(_Functor __f, _Bound_args... __bound_args)
1283 : _M_f(std::forward<_Functor>(__f)),
1284 _M_bound_args(std::forward<_Bound_args>(__bound_args)...)
1288 template<typename... _Args>
1290 operator()(_Args&&... __args)
1292 return this->__call<_Result>(
1293 tuple<_Args...>(std::forward<_Args...>(__args)...),
1298 template<typename... _Args>
1300 operator()(_Args&&... __args) const
1302 return this->__call<_Result>(
1303 tuple<_Args...>(std::forward<_Args...>(__args)...),
1308 template<typename... _Args>
1310 operator()(_Args&&... __args) volatile
1312 return this->__call<_Result>(
1313 tuple<_Args...>(std::forward<_Args...>(__args)...),
1317 // Call as const volatile
1318 template<typename... _Args>
1320 operator()(_Args&&... __args) const volatile
1322 return this->__call<_Result>(
1323 tuple<_Args...>(std::forward<_Args...>(__args)...),
1329 * @brief Class template _Bind is always a bind expression.
1332 template<typename _Signature>
1333 struct is_bind_expression<_Bind<_Signature> >
1334 : public true_type { };
1337 * @brief Class template _Bind is always a bind expression.
1340 template<typename _Result, typename _Signature>
1341 struct is_bind_expression<_Bind_result<_Result, _Signature> >
1342 : public true_type { };
1345 * @brief Function template for std::bind.
1348 template<typename _Functor, typename... _ArgTypes>
1350 _Bind<typename _Maybe_wrap_member_pointer<_Functor>::type(_ArgTypes...)>
1351 bind(_Functor __f, _ArgTypes... __args)
1353 typedef _Maybe_wrap_member_pointer<_Functor> __maybe_type;
1354 typedef typename __maybe_type::type __functor_type;
1355 typedef _Bind<__functor_type(_ArgTypes...)> __result_type;
1356 return __result_type(__maybe_type::__do_wrap(__f),
1357 std::forward<_ArgTypes>(__args)...);
1361 * @brief Function template for std::bind.
1364 template<typename _Result, typename _Functor, typename... _ArgTypes>
1366 _Bind_result<_Result,
1367 typename _Maybe_wrap_member_pointer<_Functor>::type
1369 bind(_Functor __f, _ArgTypes... __args)
1371 typedef _Maybe_wrap_member_pointer<_Functor> __maybe_type;
1372 typedef typename __maybe_type::type __functor_type;
1373 typedef _Bind_result<_Result, __functor_type(_ArgTypes...)>
1375 return __result_type(__maybe_type::__do_wrap(__f),
1376 std::forward<_ArgTypes>(__args)...);
1380 * @brief Exception class thrown when class template function's
1381 * operator() is called with an empty target.
1382 * @ingroup exceptions
1384 class bad_function_call : public std::exception { };
1387 * Trait identifying "location-invariant" types, meaning that the
1388 * address of the object (or any of its members) will not escape.
1389 * Also implies a trivial copy constructor and assignment operator.
1391 template<typename _Tp>
1392 struct __is_location_invariant
1393 : integral_constant<bool, (is_pointer<_Tp>::value
1394 || is_member_pointer<_Tp>::value)>
1397 class _Undefined_class;
1402 const void* _M_const_object;
1403 void (*_M_function_pointer)();
1404 void (_Undefined_class::*_M_member_pointer)();
1409 void* _M_access() { return &_M_pod_data[0]; }
1410 const void* _M_access() const { return &_M_pod_data[0]; }
1412 template<typename _Tp>
1415 { return *static_cast<_Tp*>(_M_access()); }
1417 template<typename _Tp>
1420 { return *static_cast<const _Tp*>(_M_access()); }
1422 _Nocopy_types _M_unused;
1423 char _M_pod_data[sizeof(_Nocopy_types)];
1426 enum _Manager_operation
1434 // Simple type wrapper that helps avoid annoying const problems
1435 // when casting between void pointers and pointers-to-pointers.
1436 template<typename _Tp>
1437 struct _Simple_type_wrapper
1439 _Simple_type_wrapper(_Tp __value) : __value(__value) { }
1444 template<typename _Tp>
1445 struct __is_location_invariant<_Simple_type_wrapper<_Tp> >
1446 : __is_location_invariant<_Tp>
1449 // Converts a reference to a function object into a callable
1451 template<typename _Functor>
1453 __callable_functor(_Functor& __f)
1456 template<typename _Member, typename _Class>
1457 inline _Mem_fn<_Member _Class::*>
1458 __callable_functor(_Member _Class::* &__p)
1459 { return mem_fn(__p); }
1461 template<typename _Member, typename _Class>
1462 inline _Mem_fn<_Member _Class::*>
1463 __callable_functor(_Member _Class::* const &__p)
1464 { return mem_fn(__p); }
1466 template<typename _Signature>
1469 /// Base class of all polymorphic function object wrappers.
1470 class _Function_base
1473 static const std::size_t _M_max_size = sizeof(_Nocopy_types);
1474 static const std::size_t _M_max_align = __alignof__(_Nocopy_types);
1476 template<typename _Functor>
1480 static const bool __stored_locally =
1481 (__is_location_invariant<_Functor>::value
1482 && sizeof(_Functor) <= _M_max_size
1483 && __alignof__(_Functor) <= _M_max_align
1484 && (_M_max_align % __alignof__(_Functor) == 0));
1486 typedef integral_constant<bool, __stored_locally> _Local_storage;
1488 // Retrieve a pointer to the function object
1490 _M_get_pointer(const _Any_data& __source)
1492 const _Functor* __ptr =
1493 __stored_locally? &__source._M_access<_Functor>()
1494 /* have stored a pointer */ : __source._M_access<_Functor*>();
1495 return const_cast<_Functor*>(__ptr);
1498 // Clone a location-invariant function object that fits within
1499 // an _Any_data structure.
1501 _M_clone(_Any_data& __dest, const _Any_data& __source, true_type)
1503 new (__dest._M_access()) _Functor(__source._M_access<_Functor>());
1506 // Clone a function object that is not location-invariant or
1507 // that cannot fit into an _Any_data structure.
1509 _M_clone(_Any_data& __dest, const _Any_data& __source, false_type)
1511 __dest._M_access<_Functor*>() =
1512 new _Functor(*__source._M_access<_Functor*>());
1515 // Destroying a location-invariant object may still require
1518 _M_destroy(_Any_data& __victim, true_type)
1520 __victim._M_access<_Functor>().~_Functor();
1523 // Destroying an object located on the heap.
1525 _M_destroy(_Any_data& __victim, false_type)
1527 delete __victim._M_access<_Functor*>();
1532 _M_manager(_Any_data& __dest, const _Any_data& __source,
1533 _Manager_operation __op)
1538 case __get_type_info:
1539 __dest._M_access<const type_info*>() = &typeid(_Functor);
1542 case __get_functor_ptr:
1543 __dest._M_access<_Functor*>() = _M_get_pointer(__source);
1546 case __clone_functor:
1547 _M_clone(__dest, __source, _Local_storage());
1550 case __destroy_functor:
1551 _M_destroy(__dest, _Local_storage());
1558 _M_init_functor(_Any_data& __functor, _Functor&& __f)
1559 { _M_init_functor(__functor, std::move(__f), _Local_storage()); }
1561 template<typename _Signature>
1563 _M_not_empty_function(const function<_Signature>& __f)
1564 { return static_cast<bool>(__f); }
1566 template<typename _Tp>
1568 _M_not_empty_function(const _Tp*& __fp)
1571 template<typename _Class, typename _Tp>
1573 _M_not_empty_function(_Tp _Class::* const& __mp)
1576 template<typename _Tp>
1578 _M_not_empty_function(const _Tp&)
1583 _M_init_functor(_Any_data& __functor, _Functor&& __f, true_type)
1584 { new (__functor._M_access()) _Functor(std::move(__f)); }
1587 _M_init_functor(_Any_data& __functor, _Functor&& __f, false_type)
1588 { __functor._M_access<_Functor*>() = new _Functor(std::move(__f)); }
1591 template<typename _Functor>
1592 class _Ref_manager : public _Base_manager<_Functor*>
1594 typedef _Function_base::_Base_manager<_Functor*> _Base;
1598 _M_manager(_Any_data& __dest, const _Any_data& __source,
1599 _Manager_operation __op)
1604 case __get_type_info:
1605 __dest._M_access<const type_info*>() = &typeid(_Functor);
1608 case __get_functor_ptr:
1609 __dest._M_access<_Functor*>() = *_Base::_M_get_pointer(__source);
1610 return is_const<_Functor>::value;
1614 _Base::_M_manager(__dest, __source, __op);
1620 _M_init_functor(_Any_data& __functor, reference_wrapper<_Functor> __f)
1622 // TBD: Use address_of function instead.
1623 _Base::_M_init_functor(__functor, &__f.get());
1627 _Function_base() : _M_manager(0) { }
1632 _M_manager(_M_functor, _M_functor, __destroy_functor);
1636 bool _M_empty() const { return !_M_manager; }
1638 typedef bool (*_Manager_type)(_Any_data&, const _Any_data&,
1639 _Manager_operation);
1641 _Any_data _M_functor;
1642 _Manager_type _M_manager;
1645 template<typename _Signature, typename _Functor>
1646 class _Function_handler;
1648 template<typename _Res, typename _Functor, typename... _ArgTypes>
1649 class _Function_handler<_Res(_ArgTypes...), _Functor>
1650 : public _Function_base::_Base_manager<_Functor>
1652 typedef _Function_base::_Base_manager<_Functor> _Base;
1656 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1658 return (*_Base::_M_get_pointer(__functor))(
1659 std::forward<_ArgTypes>(__args)...);
1663 template<typename _Functor, typename... _ArgTypes>
1664 class _Function_handler<void(_ArgTypes...), _Functor>
1665 : public _Function_base::_Base_manager<_Functor>
1667 typedef _Function_base::_Base_manager<_Functor> _Base;
1671 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1673 (*_Base::_M_get_pointer(__functor))(
1674 std::forward<_ArgTypes>(__args)...);
1678 template<typename _Res, typename _Functor, typename... _ArgTypes>
1679 class _Function_handler<_Res(_ArgTypes...), reference_wrapper<_Functor> >
1680 : public _Function_base::_Ref_manager<_Functor>
1682 typedef _Function_base::_Ref_manager<_Functor> _Base;
1686 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1688 return __callable_functor(**_Base::_M_get_pointer(__functor))(
1689 std::forward<_ArgTypes>(__args)...);
1693 template<typename _Functor, typename... _ArgTypes>
1694 class _Function_handler<void(_ArgTypes...), reference_wrapper<_Functor> >
1695 : public _Function_base::_Ref_manager<_Functor>
1697 typedef _Function_base::_Ref_manager<_Functor> _Base;
1701 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1703 __callable_functor(**_Base::_M_get_pointer(__functor))(
1704 std::forward<_ArgTypes>(__args)...);
1708 template<typename _Class, typename _Member, typename _Res,
1709 typename... _ArgTypes>
1710 class _Function_handler<_Res(_ArgTypes...), _Member _Class::*>
1711 : public _Function_handler<void(_ArgTypes...), _Member _Class::*>
1713 typedef _Function_handler<void(_ArgTypes...), _Member _Class::*>
1718 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1720 return mem_fn(_Base::_M_get_pointer(__functor)->__value)(
1721 std::forward<_ArgTypes>(__args)...);
1725 template<typename _Class, typename _Member, typename... _ArgTypes>
1726 class _Function_handler<void(_ArgTypes...), _Member _Class::*>
1727 : public _Function_base::_Base_manager<
1728 _Simple_type_wrapper< _Member _Class::* > >
1730 typedef _Member _Class::* _Functor;
1731 typedef _Simple_type_wrapper<_Functor> _Wrapper;
1732 typedef _Function_base::_Base_manager<_Wrapper> _Base;
1736 _M_manager(_Any_data& __dest, const _Any_data& __source,
1737 _Manager_operation __op)
1742 case __get_type_info:
1743 __dest._M_access<const type_info*>() = &typeid(_Functor);
1746 case __get_functor_ptr:
1747 __dest._M_access<_Functor*>() =
1748 &_Base::_M_get_pointer(__source)->__value;
1752 _Base::_M_manager(__dest, __source, __op);
1758 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1760 mem_fn(_Base::_M_get_pointer(__functor)->__value)(
1761 std::forward<_ArgTypes>(__args)...);
1766 * @brief Primary class template for std::function.
1769 * Polymorphic function wrapper.
1771 template<typename _Res, typename... _ArgTypes>
1772 class function<_Res(_ArgTypes...)>
1773 : public _Maybe_unary_or_binary_function<_Res, _ArgTypes...>,
1774 private _Function_base
1776 typedef _Res _Signature_type(_ArgTypes...);
1778 struct _Useless { };
1781 typedef _Res result_type;
1783 // [3.7.2.1] construct/copy/destroy
1786 * @brief Default construct creates an empty function call wrapper.
1787 * @post @c !(bool)*this
1790 function() : _Function_base() { }
1793 * @brief Creates an empty function call wrapper.
1794 * @post @c !(bool)*this
1796 function(nullptr_t) : _Function_base() { }
1799 * @brief %Function copy constructor.
1800 * @param x A %function object with identical call signature.
1801 * @post @c (bool)*this == (bool)x
1803 * The newly-created %function contains a copy of the target of @a
1804 * x (if it has one).
1806 function(const function& __x);
1809 * @brief %Function move constructor.
1810 * @param x A %function object rvalue with identical call signature.
1812 * The newly-created %function contains the target of @a x
1815 function(function&& __x) : _Function_base()
1820 // TODO: needs allocator_arg_t
1823 * @brief Builds a %function that targets a copy of the incoming
1825 * @param f A %function object that is callable with parameters of
1826 * type @c T1, @c T2, ..., @c TN and returns a value convertible
1829 * The newly-created %function object will target a copy of @a
1830 * f. If @a f is @c reference_wrapper<F>, then this function
1831 * object will contain a reference to the function object @c
1832 * f.get(). If @a f is a NULL function pointer or NULL
1833 * pointer-to-member, the newly-created object will be empty.
1835 * If @a f is a non-NULL function pointer or an object of type @c
1836 * reference_wrapper<F>, this function will not throw.
1838 template<typename _Functor>
1839 function(_Functor __f,
1841 !is_integral<_Functor>::value, _Useless>::type
1845 * @brief %Function assignment operator.
1846 * @param x A %function with identical call signature.
1847 * @post @c (bool)*this == (bool)x
1850 * The target of @a x is copied to @c *this. If @a x has no
1851 * target, then @c *this will be empty.
1853 * If @a x targets a function pointer or a reference to a function
1854 * object, then this operation will not throw an %exception.
1857 operator=(const function& __x)
1859 function(__x).swap(*this);
1864 * @brief %Function move-assignment operator.
1865 * @param x A %function rvalue with identical call signature.
1868 * The target of @a x is moved to @c *this. If @a x has no
1869 * target, then @c *this will be empty.
1871 * If @a x targets a function pointer or a reference to a function
1872 * object, then this operation will not throw an %exception.
1875 operator=(function&& __x)
1877 function(std::move(__x)).swap(*this);
1882 * @brief %Function assignment to zero.
1883 * @post @c !(bool)*this
1886 * The target of @c *this is deallocated, leaving it empty.
1889 operator=(nullptr_t)
1893 _M_manager(_M_functor, _M_functor, __destroy_functor);
1901 * @brief %Function assignment to a new target.
1902 * @param f A %function object that is callable with parameters of
1903 * type @c T1, @c T2, ..., @c TN and returns a value convertible
1907 * This %function object wrapper will target a copy of @a
1908 * f. If @a f is @c reference_wrapper<F>, then this function
1909 * object will contain a reference to the function object @c
1910 * f.get(). If @a f is a NULL function pointer or NULL
1911 * pointer-to-member, @c this object will be empty.
1913 * If @a f is a non-NULL function pointer or an object of type @c
1914 * reference_wrapper<F>, this function will not throw.
1916 template<typename _Functor>
1917 typename enable_if<!is_integral<_Functor>::value, function&>::type
1918 operator=(_Functor&& __f)
1920 function(std::forward<_Functor>(__f)).swap(*this);
1925 template<typename _Functor>
1926 typename enable_if<!is_integral<_Functor>::value, function&>::type
1927 operator=(reference_wrapper<_Functor> __f)
1929 function(__f).swap(*this);
1933 // [3.7.2.2] function modifiers
1936 * @brief Swap the targets of two %function objects.
1937 * @param f A %function with identical call signature.
1939 * Swap the targets of @c this function object and @a f. This
1940 * function will not throw an %exception.
1942 void swap(function& __x)
1944 _Any_data __old_functor = _M_functor;
1945 _M_functor = __x._M_functor;
1946 __x._M_functor = __old_functor;
1947 _Manager_type __old_manager = _M_manager;
1948 _M_manager = __x._M_manager;
1949 __x._M_manager = __old_manager;
1950 _Invoker_type __old_invoker = _M_invoker;
1951 _M_invoker = __x._M_invoker;
1952 __x._M_invoker = __old_invoker;
1955 // TODO: needs allocator_arg_t
1957 template<typename _Functor, typename _Alloc>
1959 assign(_Functor&& __f, const _Alloc& __a)
1961 function(allocator_arg, __a,
1962 std::forward<_Functor>(__f)).swap(*this);
1966 // [3.7.2.3] function capacity
1969 * @brief Determine if the %function wrapper has a target.
1971 * @return @c true when this %function object contains a target,
1972 * or @c false when it is empty.
1974 * This function will not throw an %exception.
1976 explicit operator bool() const
1977 { return !_M_empty(); }
1979 // [3.7.2.4] function invocation
1982 * @brief Invokes the function targeted by @c *this.
1983 * @returns the result of the target.
1984 * @throws bad_function_call when @c !(bool)*this
1986 * The function call operator invokes the target function object
1987 * stored by @c this.
1989 _Res operator()(_ArgTypes... __args) const;
1992 // [3.7.2.5] function target access
1994 * @brief Determine the type of the target of this function object
1997 * @returns the type identifier of the target function object, or
1998 * @c typeid(void) if @c !(bool)*this.
2000 * This function will not throw an %exception.
2002 const type_info& target_type() const;
2005 * @brief Access the stored target function object.
2007 * @return Returns a pointer to the stored target function object,
2008 * if @c typeid(Functor).equals(target_type()); otherwise, a NULL
2011 * This function will not throw an %exception.
2013 template<typename _Functor> _Functor* target();
2016 template<typename _Functor> const _Functor* target() const;
2019 // deleted overloads
2020 template<typename _Res2, typename... _ArgTypes2>
2021 void operator==(const function<_Res2(_ArgTypes2...)>&) const = delete;
2022 template<typename _Res2, typename... _ArgTypes2>
2023 void operator!=(const function<_Res2(_ArgTypes2...)>&) const = delete;
2026 typedef _Res (*_Invoker_type)(const _Any_data&, _ArgTypes...);
2027 _Invoker_type _M_invoker;
2030 // Out-of-line member definitions.
2031 template<typename _Res, typename... _ArgTypes>
2032 function<_Res(_ArgTypes...)>::
2033 function(const function& __x)
2036 if (static_cast<bool>(__x))
2038 _M_invoker = __x._M_invoker;
2039 _M_manager = __x._M_manager;
2040 __x._M_manager(_M_functor, __x._M_functor, __clone_functor);
2044 template<typename _Res, typename... _ArgTypes>
2045 template<typename _Functor>
2046 function<_Res(_ArgTypes...)>::
2047 function(_Functor __f,
2049 !is_integral<_Functor>::value, _Useless>::type)
2052 typedef _Function_handler<_Signature_type, _Functor> _My_handler;
2054 if (_My_handler::_M_not_empty_function(__f))
2056 _M_invoker = &_My_handler::_M_invoke;
2057 _M_manager = &_My_handler::_M_manager;
2058 _My_handler::_M_init_functor(_M_functor, std::move(__f));
2062 template<typename _Res, typename... _ArgTypes>
2064 function<_Res(_ArgTypes...)>::
2065 operator()(_ArgTypes... __args) const
2068 __throw_bad_function_call();
2069 return _M_invoker(_M_functor, std::forward<_ArgTypes>(__args)...);
2073 template<typename _Res, typename... _ArgTypes>
2075 function<_Res(_ArgTypes...)>::
2080 _Any_data __typeinfo_result;
2081 _M_manager(__typeinfo_result, _M_functor, __get_type_info);
2082 return *__typeinfo_result._M_access<const type_info*>();
2085 return typeid(void);
2088 template<typename _Res, typename... _ArgTypes>
2089 template<typename _Functor>
2091 function<_Res(_ArgTypes...)>::
2094 if (typeid(_Functor) == target_type() && _M_manager)
2097 if (_M_manager(__ptr, _M_functor, __get_functor_ptr)
2098 && !is_const<_Functor>::value)
2101 return __ptr._M_access<_Functor*>();
2107 template<typename _Res, typename... _ArgTypes>
2108 template<typename _Functor>
2110 function<_Res(_ArgTypes...)>::
2113 if (typeid(_Functor) == target_type() && _M_manager)
2116 _M_manager(__ptr, _M_functor, __get_functor_ptr);
2117 return __ptr._M_access<const _Functor*>();
2124 // [20.7.15.2.6] null pointer comparisons
2127 * @brief Compares a polymorphic function object wrapper against 0
2128 * (the NULL pointer).
2129 * @returns @c true if the wrapper has no target, @c false otherwise
2131 * This function will not throw an %exception.
2133 template<typename _Res, typename... _Args>
2135 operator==(const function<_Res(_Args...)>& __f, nullptr_t)
2136 { return !static_cast<bool>(__f); }
2139 template<typename _Res, typename... _Args>
2141 operator==(nullptr_t, const function<_Res(_Args...)>& __f)
2142 { return !static_cast<bool>(__f); }
2145 * @brief Compares a polymorphic function object wrapper against 0
2146 * (the NULL pointer).
2147 * @returns @c false if the wrapper has no target, @c true otherwise
2149 * This function will not throw an %exception.
2151 template<typename _Res, typename... _Args>
2153 operator!=(const function<_Res(_Args...)>& __f, nullptr_t)
2154 { return static_cast<bool>(__f); }
2157 template<typename _Res, typename... _Args>
2159 operator!=(nullptr_t, const function<_Res(_Args...)>& __f)
2160 { return static_cast<bool>(__f); }
2162 // [20.7.15.2.7] specialized algorithms
2165 * @brief Swap the targets of two polymorphic function object wrappers.
2167 * This function will not throw an %exception.
2169 template<typename _Res, typename... _Args>
2171 swap(function<_Res(_Args...)>& __x, function<_Res(_Args...)>& __y)
2175 #endif // __GXX_EXPERIMENTAL_CXX0X__
2177 #endif // _GLIBCXX_FUNCTIONAL