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 * The integral constant expression 0 can be converted into a
1388 * pointer to this type. It is used by the function template to
1389 * accept NULL pointers.
1391 struct _M_clear_type;
1394 * Trait identifying "location-invariant" types, meaning that the
1395 * address of the object (or any of its members) will not escape.
1396 * Also implies a trivial copy constructor and assignment operator.
1398 template<typename _Tp>
1399 struct __is_location_invariant
1400 : integral_constant<bool, (is_pointer<_Tp>::value
1401 || is_member_pointer<_Tp>::value)>
1404 class _Undefined_class;
1409 const void* _M_const_object;
1410 void (*_M_function_pointer)();
1411 void (_Undefined_class::*_M_member_pointer)();
1416 void* _M_access() { return &_M_pod_data[0]; }
1417 const void* _M_access() const { return &_M_pod_data[0]; }
1419 template<typename _Tp>
1422 { return *static_cast<_Tp*>(_M_access()); }
1424 template<typename _Tp>
1427 { return *static_cast<const _Tp*>(_M_access()); }
1429 _Nocopy_types _M_unused;
1430 char _M_pod_data[sizeof(_Nocopy_types)];
1433 enum _Manager_operation
1441 // Simple type wrapper that helps avoid annoying const problems
1442 // when casting between void pointers and pointers-to-pointers.
1443 template<typename _Tp>
1444 struct _Simple_type_wrapper
1446 _Simple_type_wrapper(_Tp __value) : __value(__value) { }
1451 template<typename _Tp>
1452 struct __is_location_invariant<_Simple_type_wrapper<_Tp> >
1453 : __is_location_invariant<_Tp>
1456 // Converts a reference to a function object into a callable
1458 template<typename _Functor>
1460 __callable_functor(_Functor& __f)
1463 template<typename _Member, typename _Class>
1464 inline _Mem_fn<_Member _Class::*>
1465 __callable_functor(_Member _Class::* &__p)
1466 { return mem_fn(__p); }
1468 template<typename _Member, typename _Class>
1469 inline _Mem_fn<_Member _Class::*>
1470 __callable_functor(_Member _Class::* const &__p)
1471 { return mem_fn(__p); }
1473 template<typename _Signature>
1476 /// Base class of all polymorphic function object wrappers.
1477 class _Function_base
1480 static const std::size_t _M_max_size = sizeof(_Nocopy_types);
1481 static const std::size_t _M_max_align = __alignof__(_Nocopy_types);
1483 template<typename _Functor>
1487 static const bool __stored_locally =
1488 (__is_location_invariant<_Functor>::value
1489 && sizeof(_Functor) <= _M_max_size
1490 && __alignof__(_Functor) <= _M_max_align
1491 && (_M_max_align % __alignof__(_Functor) == 0));
1493 typedef integral_constant<bool, __stored_locally> _Local_storage;
1495 // Retrieve a pointer to the function object
1497 _M_get_pointer(const _Any_data& __source)
1499 const _Functor* __ptr =
1500 __stored_locally? &__source._M_access<_Functor>()
1501 /* have stored a pointer */ : __source._M_access<_Functor*>();
1502 return const_cast<_Functor*>(__ptr);
1505 // Clone a location-invariant function object that fits within
1506 // an _Any_data structure.
1508 _M_clone(_Any_data& __dest, const _Any_data& __source, true_type)
1510 new (__dest._M_access()) _Functor(__source._M_access<_Functor>());
1513 // Clone a function object that is not location-invariant or
1514 // that cannot fit into an _Any_data structure.
1516 _M_clone(_Any_data& __dest, const _Any_data& __source, false_type)
1518 __dest._M_access<_Functor*>() =
1519 new _Functor(*__source._M_access<_Functor*>());
1522 // Destroying a location-invariant object may still require
1525 _M_destroy(_Any_data& __victim, true_type)
1527 __victim._M_access<_Functor>().~_Functor();
1530 // Destroying an object located on the heap.
1532 _M_destroy(_Any_data& __victim, false_type)
1534 delete __victim._M_access<_Functor*>();
1539 _M_manager(_Any_data& __dest, const _Any_data& __source,
1540 _Manager_operation __op)
1545 case __get_type_info:
1546 __dest._M_access<const type_info*>() = &typeid(_Functor);
1549 case __get_functor_ptr:
1550 __dest._M_access<_Functor*>() = _M_get_pointer(__source);
1553 case __clone_functor:
1554 _M_clone(__dest, __source, _Local_storage());
1557 case __destroy_functor:
1558 _M_destroy(__dest, _Local_storage());
1565 _M_init_functor(_Any_data& __functor, _Functor&& __f)
1566 { _M_init_functor(__functor, std::move(__f), _Local_storage()); }
1568 template<typename _Signature>
1570 _M_not_empty_function(const function<_Signature>& __f)
1571 { return static_cast<bool>(__f); }
1573 template<typename _Tp>
1575 _M_not_empty_function(const _Tp*& __fp)
1578 template<typename _Class, typename _Tp>
1580 _M_not_empty_function(_Tp _Class::* const& __mp)
1583 template<typename _Tp>
1585 _M_not_empty_function(const _Tp&)
1590 _M_init_functor(_Any_data& __functor, _Functor&& __f, true_type)
1591 { new (__functor._M_access()) _Functor(std::move(__f)); }
1594 _M_init_functor(_Any_data& __functor, _Functor&& __f, false_type)
1595 { __functor._M_access<_Functor*>() = new _Functor(std::move(__f)); }
1598 template<typename _Functor>
1599 class _Ref_manager : public _Base_manager<_Functor*>
1601 typedef _Function_base::_Base_manager<_Functor*> _Base;
1605 _M_manager(_Any_data& __dest, const _Any_data& __source,
1606 _Manager_operation __op)
1611 case __get_type_info:
1612 __dest._M_access<const type_info*>() = &typeid(_Functor);
1615 case __get_functor_ptr:
1616 __dest._M_access<_Functor*>() = *_Base::_M_get_pointer(__source);
1617 return is_const<_Functor>::value;
1621 _Base::_M_manager(__dest, __source, __op);
1627 _M_init_functor(_Any_data& __functor, reference_wrapper<_Functor> __f)
1629 // TBD: Use address_of function instead.
1630 _Base::_M_init_functor(__functor, &__f.get());
1634 _Function_base() : _M_manager(0) { }
1639 _M_manager(_M_functor, _M_functor, __destroy_functor);
1643 bool _M_empty() const { return !_M_manager; }
1645 typedef bool (*_Manager_type)(_Any_data&, const _Any_data&,
1646 _Manager_operation);
1648 _Any_data _M_functor;
1649 _Manager_type _M_manager;
1652 template<typename _Signature, typename _Functor>
1653 class _Function_handler;
1655 template<typename _Res, typename _Functor, typename... _ArgTypes>
1656 class _Function_handler<_Res(_ArgTypes...), _Functor>
1657 : public _Function_base::_Base_manager<_Functor>
1659 typedef _Function_base::_Base_manager<_Functor> _Base;
1663 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1665 return (*_Base::_M_get_pointer(__functor))(
1666 std::forward<_ArgTypes>(__args)...);
1670 template<typename _Functor, typename... _ArgTypes>
1671 class _Function_handler<void(_ArgTypes...), _Functor>
1672 : public _Function_base::_Base_manager<_Functor>
1674 typedef _Function_base::_Base_manager<_Functor> _Base;
1678 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1680 (*_Base::_M_get_pointer(__functor))(
1681 std::forward<_ArgTypes>(__args)...);
1685 template<typename _Res, typename _Functor, typename... _ArgTypes>
1686 class _Function_handler<_Res(_ArgTypes...), reference_wrapper<_Functor> >
1687 : public _Function_base::_Ref_manager<_Functor>
1689 typedef _Function_base::_Ref_manager<_Functor> _Base;
1693 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1695 return __callable_functor(**_Base::_M_get_pointer(__functor))(
1696 std::forward<_ArgTypes>(__args)...);
1700 template<typename _Functor, typename... _ArgTypes>
1701 class _Function_handler<void(_ArgTypes...), reference_wrapper<_Functor> >
1702 : public _Function_base::_Ref_manager<_Functor>
1704 typedef _Function_base::_Ref_manager<_Functor> _Base;
1708 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1710 __callable_functor(**_Base::_M_get_pointer(__functor))(
1711 std::forward<_ArgTypes>(__args)...);
1715 template<typename _Class, typename _Member, typename _Res,
1716 typename... _ArgTypes>
1717 class _Function_handler<_Res(_ArgTypes...), _Member _Class::*>
1718 : public _Function_handler<void(_ArgTypes...), _Member _Class::*>
1720 typedef _Function_handler<void(_ArgTypes...), _Member _Class::*>
1725 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1727 return mem_fn(_Base::_M_get_pointer(__functor)->__value)(
1728 std::forward<_ArgTypes>(__args)...);
1732 template<typename _Class, typename _Member, typename... _ArgTypes>
1733 class _Function_handler<void(_ArgTypes...), _Member _Class::*>
1734 : public _Function_base::_Base_manager<
1735 _Simple_type_wrapper< _Member _Class::* > >
1737 typedef _Member _Class::* _Functor;
1738 typedef _Simple_type_wrapper<_Functor> _Wrapper;
1739 typedef _Function_base::_Base_manager<_Wrapper> _Base;
1743 _M_manager(_Any_data& __dest, const _Any_data& __source,
1744 _Manager_operation __op)
1749 case __get_type_info:
1750 __dest._M_access<const type_info*>() = &typeid(_Functor);
1753 case __get_functor_ptr:
1754 __dest._M_access<_Functor*>() =
1755 &_Base::_M_get_pointer(__source)->__value;
1759 _Base::_M_manager(__dest, __source, __op);
1765 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1767 mem_fn(_Base::_M_get_pointer(__functor)->__value)(
1768 std::forward<_ArgTypes>(__args)...);
1773 * @brief Primary class template for std::function.
1776 * Polymorphic function wrapper.
1778 template<typename _Res, typename... _ArgTypes>
1779 class function<_Res(_ArgTypes...)>
1780 : public _Maybe_unary_or_binary_function<_Res, _ArgTypes...>,
1781 private _Function_base
1783 typedef _Res _Signature_type(_ArgTypes...);
1785 struct _Useless { };
1788 typedef _Res result_type;
1790 // [3.7.2.1] construct/copy/destroy
1793 * @brief Default construct creates an empty function call wrapper.
1794 * @post @c !(bool)*this
1797 function() : _Function_base() { }
1800 * @brief Default construct creates an empty function call wrapper.
1801 * @post @c !(bool)*this
1803 function(_M_clear_type*) : _Function_base() { }
1806 * @brief %Function copy constructor.
1807 * @param x A %function object with identical call signature.
1808 * @post @c (bool)*this == (bool)x
1810 * The newly-created %function contains a copy of the target of @a
1811 * x (if it has one).
1813 function(const function& __x);
1816 * @brief %Function move constructor.
1817 * @param x A %function object rvalue with identical call signature.
1819 * The newly-created %function contains the target of @a x
1822 function(function&& __x) : _Function_base()
1827 // TODO: needs allocator_arg_t
1830 * @brief Builds a %function that targets a copy of the incoming
1832 * @param f A %function object that is callable with parameters of
1833 * type @c T1, @c T2, ..., @c TN and returns a value convertible
1836 * The newly-created %function object will target a copy of @a
1837 * f. If @a f is @c reference_wrapper<F>, then this function
1838 * object will contain a reference to the function object @c
1839 * f.get(). If @a f is a NULL function pointer or NULL
1840 * pointer-to-member, the newly-created object will be empty.
1842 * If @a f is a non-NULL function pointer or an object of type @c
1843 * reference_wrapper<F>, this function will not throw.
1845 template<typename _Functor>
1846 function(_Functor __f,
1848 !is_integral<_Functor>::value, _Useless>::type
1852 * @brief %Function assignment operator.
1853 * @param x A %function with identical call signature.
1854 * @post @c (bool)*this == (bool)x
1857 * The target of @a x is copied to @c *this. If @a x has no
1858 * target, then @c *this will be empty.
1860 * If @a x targets a function pointer or a reference to a function
1861 * object, then this operation will not throw an %exception.
1864 operator=(const function& __x)
1866 function(__x).swap(*this);
1871 * @brief %Function move-assignment operator.
1872 * @param x A %function rvalue with identical call signature.
1875 * The target of @a x is moved to @c *this. If @a x has no
1876 * target, then @c *this will be empty.
1878 * If @a x targets a function pointer or a reference to a function
1879 * object, then this operation will not throw an %exception.
1882 operator=(function&& __x)
1884 function(std::move(__x)).swap(*this);
1889 * @brief %Function assignment to zero.
1890 * @post @c !(bool)*this
1893 * The target of @c *this is deallocated, leaving it empty.
1896 operator=(_M_clear_type*)
1900 _M_manager(_M_functor, _M_functor, __destroy_functor);
1908 * @brief %Function assignment to a new target.
1909 * @param f A %function object that is callable with parameters of
1910 * type @c T1, @c T2, ..., @c TN and returns a value convertible
1914 * This %function object wrapper will target a copy of @a
1915 * f. If @a f is @c reference_wrapper<F>, then this function
1916 * object will contain a reference to the function object @c
1917 * f.get(). If @a f is a NULL function pointer or NULL
1918 * pointer-to-member, @c this object will be empty.
1920 * If @a f is a non-NULL function pointer or an object of type @c
1921 * reference_wrapper<F>, this function will not throw.
1923 template<typename _Functor>
1924 typename enable_if<!is_integral<_Functor>::value, function&>::type
1925 operator=(_Functor&& __f)
1927 function(std::forward<_Functor>(__f)).swap(*this);
1932 template<typename _Functor>
1933 typename enable_if<!is_integral<_Functor>::value, function&>::type
1934 operator=(reference_wrapper<_Functor> __f)
1936 function(__f).swap(*this);
1940 // [3.7.2.2] function modifiers
1943 * @brief Swap the targets of two %function objects.
1944 * @param f A %function with identical call signature.
1946 * Swap the targets of @c this function object and @a f. This
1947 * function will not throw an %exception.
1949 void swap(function& __x)
1951 _Any_data __old_functor = _M_functor;
1952 _M_functor = __x._M_functor;
1953 __x._M_functor = __old_functor;
1954 _Manager_type __old_manager = _M_manager;
1955 _M_manager = __x._M_manager;
1956 __x._M_manager = __old_manager;
1957 _Invoker_type __old_invoker = _M_invoker;
1958 _M_invoker = __x._M_invoker;
1959 __x._M_invoker = __old_invoker;
1962 // TODO: needs allocator_arg_t
1964 template<typename _Functor, typename _Alloc>
1966 assign(_Functor&& __f, const _Alloc& __a)
1968 function(allocator_arg, __a,
1969 std::forward<_Functor>(__f)).swap(*this);
1973 // [3.7.2.3] function capacity
1976 * @brief Determine if the %function wrapper has a target.
1978 * @return @c true when this %function object contains a target,
1979 * or @c false when it is empty.
1981 * This function will not throw an %exception.
1983 explicit operator bool() const
1984 { return !_M_empty(); }
1986 // [3.7.2.4] function invocation
1989 * @brief Invokes the function targeted by @c *this.
1990 * @returns the result of the target.
1991 * @throws bad_function_call when @c !(bool)*this
1993 * The function call operator invokes the target function object
1994 * stored by @c this.
1996 _Res operator()(_ArgTypes... __args) const;
1999 // [3.7.2.5] function target access
2001 * @brief Determine the type of the target of this function object
2004 * @returns the type identifier of the target function object, or
2005 * @c typeid(void) if @c !(bool)*this.
2007 * This function will not throw an %exception.
2009 const type_info& target_type() const;
2012 * @brief Access the stored target function object.
2014 * @return Returns a pointer to the stored target function object,
2015 * if @c typeid(Functor).equals(target_type()); otherwise, a NULL
2018 * This function will not throw an %exception.
2020 template<typename _Functor> _Functor* target();
2023 template<typename _Functor> const _Functor* target() const;
2026 // deleted overloads
2027 template<typename _Res2, typename... _ArgTypes2>
2028 void operator==(const function<_Res2(_ArgTypes2...)>&) const = delete;
2029 template<typename _Res2, typename... _ArgTypes2>
2030 void operator!=(const function<_Res2(_ArgTypes2...)>&) const = delete;
2033 typedef _Res (*_Invoker_type)(const _Any_data&, _ArgTypes...);
2034 _Invoker_type _M_invoker;
2037 // Out-of-line member definitions.
2038 template<typename _Res, typename... _ArgTypes>
2039 function<_Res(_ArgTypes...)>::
2040 function(const function& __x)
2043 if (static_cast<bool>(__x))
2045 _M_invoker = __x._M_invoker;
2046 _M_manager = __x._M_manager;
2047 __x._M_manager(_M_functor, __x._M_functor, __clone_functor);
2051 template<typename _Res, typename... _ArgTypes>
2052 template<typename _Functor>
2053 function<_Res(_ArgTypes...)>::
2054 function(_Functor __f,
2056 !is_integral<_Functor>::value, _Useless>::type)
2059 typedef _Function_handler<_Signature_type, _Functor> _My_handler;
2061 if (_My_handler::_M_not_empty_function(__f))
2063 _M_invoker = &_My_handler::_M_invoke;
2064 _M_manager = &_My_handler::_M_manager;
2065 _My_handler::_M_init_functor(_M_functor, std::move(__f));
2069 template<typename _Res, typename... _ArgTypes>
2071 function<_Res(_ArgTypes...)>::
2072 operator()(_ArgTypes... __args) const
2075 __throw_bad_function_call();
2076 return _M_invoker(_M_functor, std::forward<_ArgTypes>(__args)...);
2080 template<typename _Res, typename... _ArgTypes>
2082 function<_Res(_ArgTypes...)>::
2087 _Any_data __typeinfo_result;
2088 _M_manager(__typeinfo_result, _M_functor, __get_type_info);
2089 return *__typeinfo_result._M_access<const type_info*>();
2092 return typeid(void);
2095 template<typename _Res, typename... _ArgTypes>
2096 template<typename _Functor>
2098 function<_Res(_ArgTypes...)>::
2101 if (typeid(_Functor) == target_type() && _M_manager)
2104 if (_M_manager(__ptr, _M_functor, __get_functor_ptr)
2105 && !is_const<_Functor>::value)
2108 return __ptr._M_access<_Functor*>();
2114 template<typename _Res, typename... _ArgTypes>
2115 template<typename _Functor>
2117 function<_Res(_ArgTypes...)>::
2120 if (typeid(_Functor) == target_type() && _M_manager)
2123 _M_manager(__ptr, _M_functor, __get_functor_ptr);
2124 return __ptr._M_access<const _Functor*>();
2131 // [20.7.15.2.6] null pointer comparisons
2134 * @brief Compares a polymorphic function object wrapper against 0
2135 * (the NULL pointer).
2136 * @returns @c true if the wrapper has no target, @c false otherwise
2138 * This function will not throw an %exception.
2140 template<typename _Res, typename... _Args>
2142 operator==(const function<_Res(_Args...)>& __f, _M_clear_type*)
2143 { return !static_cast<bool>(__f); }
2146 template<typename _Res, typename... _Args>
2148 operator==(_M_clear_type*, const function<_Res(_Args...)>& __f)
2149 { return !static_cast<bool>(__f); }
2152 * @brief Compares a polymorphic function object wrapper against 0
2153 * (the NULL pointer).
2154 * @returns @c false if the wrapper has no target, @c true otherwise
2156 * This function will not throw an %exception.
2158 template<typename _Res, typename... _Args>
2160 operator!=(const function<_Res(_Args...)>& __f, _M_clear_type*)
2161 { return static_cast<bool>(__f); }
2164 template<typename _Res, typename... _Args>
2166 operator!=(_M_clear_type*, const function<_Res(_Args...)>& __f)
2167 { return static_cast<bool>(__f); }
2169 // [20.7.15.2.7] specialized algorithms
2172 * @brief Swap the targets of two polymorphic function object wrappers.
2174 * This function will not throw an %exception.
2176 template<typename _Res, typename... _Args>
2178 swap(function<_Res(_Args...)>& __x, function<_Res(_Args...)>& __y)
2182 #endif // __GXX_EXPERIMENTAL_CXX0X__
2184 #endif // _GLIBCXX_FUNCTIONAL