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>
66 _GLIBCXX_HAS_NESTED_TYPE(result_type)
68 /// If we have found a result_type, extract it.
69 template<bool _Has_result_type, typename _Functor>
70 struct _Maybe_get_result_type
73 template<typename _Functor>
74 struct _Maybe_get_result_type<true, _Functor>
76 typedef typename _Functor::result_type result_type;
80 * Base class for any function object that has a weak result type, as
81 * defined in 3.3/3 of TR1.
83 template<typename _Functor>
84 struct _Weak_result_type_impl
85 : _Maybe_get_result_type<__has_result_type<_Functor>::value, _Functor>
88 /// Retrieve the result type for a function type.
89 template<typename _Res, typename... _ArgTypes>
90 struct _Weak_result_type_impl<_Res(_ArgTypes...)>
92 typedef _Res result_type;
95 /// Retrieve the result type for a function reference.
96 template<typename _Res, typename... _ArgTypes>
97 struct _Weak_result_type_impl<_Res(&)(_ArgTypes...)>
99 typedef _Res result_type;
102 /// Retrieve the result type for a function pointer.
103 template<typename _Res, typename... _ArgTypes>
104 struct _Weak_result_type_impl<_Res(*)(_ArgTypes...)>
106 typedef _Res result_type;
109 /// Retrieve result type for a member function pointer.
110 template<typename _Res, typename _Class, typename... _ArgTypes>
111 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...)>
113 typedef _Res result_type;
116 /// Retrieve result type for a const member function pointer.
117 template<typename _Res, typename _Class, typename... _ArgTypes>
118 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) const>
120 typedef _Res result_type;
123 /// Retrieve result type for a volatile member function pointer.
124 template<typename _Res, typename _Class, typename... _ArgTypes>
125 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) volatile>
127 typedef _Res result_type;
130 /// Retrieve result type for a const volatile member function pointer.
131 template<typename _Res, typename _Class, typename... _ArgTypes>
132 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...)const volatile>
134 typedef _Res result_type;
138 * Strip top-level cv-qualifiers from the function object and let
139 * _Weak_result_type_impl perform the real work.
141 template<typename _Functor>
142 struct _Weak_result_type
143 : _Weak_result_type_impl<typename remove_cv<_Functor>::type>
146 /// Determines if the type _Tp derives from unary_function.
147 template<typename _Tp>
148 struct _Derives_from_unary_function : __sfinae_types
151 template<typename _T1, typename _Res>
152 static __one __test(const volatile unary_function<_T1, _Res>*);
154 // It's tempting to change "..." to const volatile void*, but
155 // that fails when _Tp is a function type.
156 static __two __test(...);
159 static const bool value = sizeof(__test((_Tp*)0)) == 1;
162 /// Determines if the type _Tp derives from binary_function.
163 template<typename _Tp>
164 struct _Derives_from_binary_function : __sfinae_types
167 template<typename _T1, typename _T2, typename _Res>
168 static __one __test(const volatile binary_function<_T1, _T2, _Res>*);
170 // It's tempting to change "..." to const volatile void*, but
171 // that fails when _Tp is a function type.
172 static __two __test(...);
175 static const bool value = sizeof(__test((_Tp*)0)) == 1;
178 /// Turns a function type into a function pointer type
179 template<typename _Tp, bool _IsFunctionType = is_function<_Tp>::value>
180 struct _Function_to_function_pointer
185 template<typename _Tp>
186 struct _Function_to_function_pointer<_Tp, true>
192 * Invoke a function object, which may be either a member pointer or a
193 * function object. The first parameter will tell which.
195 template<typename _Functor, typename... _Args>
198 (!is_member_pointer<_Functor>::value
199 && !is_function<_Functor>::value
200 && !is_function<typename remove_pointer<_Functor>::type>::value),
201 typename result_of<_Functor(_Args...)>::type
203 __invoke(_Functor& __f, _Args&&... __args)
205 return __f(std::forward<_Args>(__args)...);
208 // To pick up function references (that will become function pointers)
209 template<typename _Functor, typename... _Args>
212 (is_pointer<_Functor>::value
213 && is_function<typename remove_pointer<_Functor>::type>::value),
214 typename result_of<_Functor(_Args...)>::type
216 __invoke(_Functor __f, _Args&&... __args)
218 return __f(std::forward<_Args>(__args)...);
222 * Knowing which of unary_function and binary_function _Tp derives
223 * from, derives from the same and ensures that reference_wrapper
224 * will have a weak result type. See cases below.
226 template<bool _Unary, bool _Binary, typename _Tp>
227 struct _Reference_wrapper_base_impl;
229 // Not a unary_function or binary_function, so try a weak result type.
230 template<typename _Tp>
231 struct _Reference_wrapper_base_impl<false, false, _Tp>
232 : _Weak_result_type<_Tp>
235 // unary_function but not binary_function
236 template<typename _Tp>
237 struct _Reference_wrapper_base_impl<true, false, _Tp>
238 : unary_function<typename _Tp::argument_type,
239 typename _Tp::result_type>
242 // binary_function but not unary_function
243 template<typename _Tp>
244 struct _Reference_wrapper_base_impl<false, true, _Tp>
245 : binary_function<typename _Tp::first_argument_type,
246 typename _Tp::second_argument_type,
247 typename _Tp::result_type>
250 // Both unary_function and binary_function. Import result_type to
252 template<typename _Tp>
253 struct _Reference_wrapper_base_impl<true, true, _Tp>
254 : unary_function<typename _Tp::argument_type,
255 typename _Tp::result_type>,
256 binary_function<typename _Tp::first_argument_type,
257 typename _Tp::second_argument_type,
258 typename _Tp::result_type>
260 typedef typename _Tp::result_type result_type;
264 * Derives from unary_function or binary_function when it
265 * can. Specializations handle all of the easy cases. The primary
266 * template determines what to do with a class type, which may
267 * derive from both unary_function and binary_function.
269 template<typename _Tp>
270 struct _Reference_wrapper_base
271 : _Reference_wrapper_base_impl<
272 _Derives_from_unary_function<_Tp>::value,
273 _Derives_from_binary_function<_Tp>::value,
277 // - a function type (unary)
278 template<typename _Res, typename _T1>
279 struct _Reference_wrapper_base<_Res(_T1)>
280 : unary_function<_T1, _Res>
283 // - a function type (binary)
284 template<typename _Res, typename _T1, typename _T2>
285 struct _Reference_wrapper_base<_Res(_T1, _T2)>
286 : binary_function<_T1, _T2, _Res>
289 // - a function pointer type (unary)
290 template<typename _Res, typename _T1>
291 struct _Reference_wrapper_base<_Res(*)(_T1)>
292 : unary_function<_T1, _Res>
295 // - a function pointer type (binary)
296 template<typename _Res, typename _T1, typename _T2>
297 struct _Reference_wrapper_base<_Res(*)(_T1, _T2)>
298 : binary_function<_T1, _T2, _Res>
301 // - a pointer to member function type (unary, no qualifiers)
302 template<typename _Res, typename _T1>
303 struct _Reference_wrapper_base<_Res (_T1::*)()>
304 : unary_function<_T1*, _Res>
307 // - a pointer to member function type (binary, no qualifiers)
308 template<typename _Res, typename _T1, typename _T2>
309 struct _Reference_wrapper_base<_Res (_T1::*)(_T2)>
310 : binary_function<_T1*, _T2, _Res>
313 // - a pointer to member function type (unary, const)
314 template<typename _Res, typename _T1>
315 struct _Reference_wrapper_base<_Res (_T1::*)() const>
316 : unary_function<const _T1*, _Res>
319 // - a pointer to member function type (binary, const)
320 template<typename _Res, typename _T1, typename _T2>
321 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const>
322 : binary_function<const _T1*, _T2, _Res>
325 // - a pointer to member function type (unary, volatile)
326 template<typename _Res, typename _T1>
327 struct _Reference_wrapper_base<_Res (_T1::*)() volatile>
328 : unary_function<volatile _T1*, _Res>
331 // - a pointer to member function type (binary, volatile)
332 template<typename _Res, typename _T1, typename _T2>
333 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) volatile>
334 : binary_function<volatile _T1*, _T2, _Res>
337 // - a pointer to member function type (unary, const volatile)
338 template<typename _Res, typename _T1>
339 struct _Reference_wrapper_base<_Res (_T1::*)() const volatile>
340 : unary_function<const volatile _T1*, _Res>
343 // - a pointer to member function type (binary, const volatile)
344 template<typename _Res, typename _T1, typename _T2>
345 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const volatile>
346 : binary_function<const volatile _T1*, _T2, _Res>
350 * @brief Primary class template for reference_wrapper.
354 template<typename _Tp>
355 class reference_wrapper
356 : public _Reference_wrapper_base<typename remove_cv<_Tp>::type>
358 // If _Tp is a function type, we can't form result_of<_Tp(...)>,
359 // so turn it into a function pointer type.
360 typedef typename _Function_to_function_pointer<_Tp>::type
367 reference_wrapper(_Tp& __indata)
368 : _M_data(std::__addressof(__indata))
371 reference_wrapper(_Tp&&) = delete;
373 reference_wrapper(const reference_wrapper<_Tp>& __inref):
374 _M_data(__inref._M_data)
378 operator=(const reference_wrapper<_Tp>& __inref)
380 _M_data = __inref._M_data;
384 operator _Tp&() const
385 { return this->get(); }
391 template<typename... _Args>
392 typename result_of<_M_func_type(_Args...)>::type
393 operator()(_Args&&... __args) const
395 return __invoke(get(), std::forward<_Args>(__args)...);
400 /// Denotes a reference should be taken to a variable.
401 template<typename _Tp>
402 inline reference_wrapper<_Tp>
404 { return reference_wrapper<_Tp>(__t); }
406 /// Denotes a const reference should be taken to a variable.
407 template<typename _Tp>
408 inline reference_wrapper<const _Tp>
410 { return reference_wrapper<const _Tp>(__t); }
412 /// Partial specialization.
413 template<typename _Tp>
414 inline reference_wrapper<_Tp>
415 ref(reference_wrapper<_Tp> __t)
416 { return ref(__t.get()); }
418 /// Partial specialization.
419 template<typename _Tp>
420 inline reference_wrapper<const _Tp>
421 cref(reference_wrapper<_Tp> __t)
422 { return cref(__t.get()); }
426 template<typename _Tp, bool>
427 struct _Mem_fn_const_or_non
429 typedef const _Tp& type;
432 template<typename _Tp>
433 struct _Mem_fn_const_or_non<_Tp, false>
439 * Derives from @c unary_function or @c binary_function, or perhaps
440 * nothing, depending on the number of arguments provided. The
441 * primary template is the basis case, which derives nothing.
443 template<typename _Res, typename... _ArgTypes>
444 struct _Maybe_unary_or_binary_function { };
446 /// Derives from @c unary_function, as appropriate.
447 template<typename _Res, typename _T1>
448 struct _Maybe_unary_or_binary_function<_Res, _T1>
449 : std::unary_function<_T1, _Res> { };
451 /// Derives from @c binary_function, as appropriate.
452 template<typename _Res, typename _T1, typename _T2>
453 struct _Maybe_unary_or_binary_function<_Res, _T1, _T2>
454 : std::binary_function<_T1, _T2, _Res> { };
456 /// Implementation of @c mem_fn for member function pointers.
457 template<typename _Res, typename _Class, typename... _ArgTypes>
458 class _Mem_fn<_Res (_Class::*)(_ArgTypes...)>
459 : public _Maybe_unary_or_binary_function<_Res, _Class*, _ArgTypes...>
461 typedef _Res (_Class::*_Functor)(_ArgTypes...);
463 template<typename _Tp>
465 _M_call(_Tp& __object, const volatile _Class *,
466 _ArgTypes... __args) const
467 { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); }
469 template<typename _Tp>
471 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
472 { return ((*__ptr).*__pmf)(std::forward<_ArgTypes>(__args)...); }
475 typedef _Res result_type;
477 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
481 operator()(_Class& __object, _ArgTypes... __args) const
482 { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); }
486 operator()(_Class* __object, _ArgTypes... __args) const
487 { return (__object->*__pmf)(std::forward<_ArgTypes>(__args)...); }
489 // Handle smart pointers, references and pointers to derived
490 template<typename _Tp>
492 operator()(_Tp& __object, _ArgTypes... __args) const
494 return _M_call(__object, &__object,
495 std::forward<_ArgTypes>(__args)...);
502 /// Implementation of @c mem_fn for const member function pointers.
503 template<typename _Res, typename _Class, typename... _ArgTypes>
504 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) const>
505 : public _Maybe_unary_or_binary_function<_Res, const _Class*,
508 typedef _Res (_Class::*_Functor)(_ArgTypes...) const;
510 template<typename _Tp>
512 _M_call(_Tp& __object, const volatile _Class *,
513 _ArgTypes... __args) const
514 { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); }
516 template<typename _Tp>
518 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
519 { return ((*__ptr).*__pmf)(std::forward<_ArgTypes>(__args)...); }
522 typedef _Res result_type;
524 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
528 operator()(const _Class& __object, _ArgTypes... __args) const
529 { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); }
533 operator()(const _Class* __object, _ArgTypes... __args) const
534 { return (__object->*__pmf)(std::forward<_ArgTypes>(__args)...); }
536 // Handle smart pointers, references and pointers to derived
537 template<typename _Tp>
538 _Res operator()(_Tp& __object, _ArgTypes... __args) const
540 return _M_call(__object, &__object,
541 std::forward<_ArgTypes>(__args)...);
548 /// Implementation of @c mem_fn for volatile member function pointers.
549 template<typename _Res, typename _Class, typename... _ArgTypes>
550 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) volatile>
551 : public _Maybe_unary_or_binary_function<_Res, volatile _Class*,
554 typedef _Res (_Class::*_Functor)(_ArgTypes...) volatile;
556 template<typename _Tp>
558 _M_call(_Tp& __object, const volatile _Class *,
559 _ArgTypes... __args) const
560 { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); }
562 template<typename _Tp>
564 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
565 { return ((*__ptr).*__pmf)(std::forward<_ArgTypes>(__args)...); }
568 typedef _Res result_type;
570 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
574 operator()(volatile _Class& __object, _ArgTypes... __args) const
575 { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); }
579 operator()(volatile _Class* __object, _ArgTypes... __args) const
580 { return (__object->*__pmf)(std::forward<_ArgTypes>(__args)...); }
582 // Handle smart pointers, references and pointers to derived
583 template<typename _Tp>
585 operator()(_Tp& __object, _ArgTypes... __args) const
587 return _M_call(__object, &__object,
588 std::forward<_ArgTypes>(__args)...);
595 /// Implementation of @c mem_fn for const volatile member function pointers.
596 template<typename _Res, typename _Class, typename... _ArgTypes>
597 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) const volatile>
598 : public _Maybe_unary_or_binary_function<_Res, const volatile _Class*,
601 typedef _Res (_Class::*_Functor)(_ArgTypes...) const volatile;
603 template<typename _Tp>
605 _M_call(_Tp& __object, const volatile _Class *,
606 _ArgTypes... __args) const
607 { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); }
609 template<typename _Tp>
611 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
612 { return ((*__ptr).*__pmf)(std::forward<_ArgTypes>(__args)...); }
615 typedef _Res result_type;
617 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
621 operator()(const volatile _Class& __object, _ArgTypes... __args) const
622 { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); }
626 operator()(const volatile _Class* __object, _ArgTypes... __args) const
627 { return (__object->*__pmf)(std::forward<_ArgTypes>(__args)...); }
629 // Handle smart pointers, references and pointers to derived
630 template<typename _Tp>
631 _Res operator()(_Tp& __object, _ArgTypes... __args) const
633 return _M_call(__object, &__object,
634 std::forward<_ArgTypes>(__args)...);
642 template<typename _Res, typename _Class>
643 class _Mem_fn<_Res _Class::*>
645 // This bit of genius is due to Peter Dimov, improved slightly by
647 template<typename _Tp>
649 _M_call(_Tp& __object, _Class *) const
650 { return __object.*__pm; }
652 template<typename _Tp, typename _Up>
654 _M_call(_Tp& __object, _Up * const *) const
655 { return (*__object).*__pm; }
657 template<typename _Tp, typename _Up>
659 _M_call(_Tp& __object, const _Up * const *) const
660 { return (*__object).*__pm; }
662 template<typename _Tp>
664 _M_call(_Tp& __object, const _Class *) const
665 { return __object.*__pm; }
667 template<typename _Tp>
669 _M_call(_Tp& __ptr, const volatile void*) const
670 { return (*__ptr).*__pm; }
672 template<typename _Tp> static _Tp& __get_ref();
674 template<typename _Tp>
675 static __sfinae_types::__one __check_const(_Tp&, _Class*);
676 template<typename _Tp, typename _Up>
677 static __sfinae_types::__one __check_const(_Tp&, _Up * const *);
678 template<typename _Tp, typename _Up>
679 static __sfinae_types::__two __check_const(_Tp&, const _Up * const *);
680 template<typename _Tp>
681 static __sfinae_types::__two __check_const(_Tp&, const _Class*);
682 template<typename _Tp>
683 static __sfinae_types::__two __check_const(_Tp&, const volatile void*);
686 template<typename _Tp>
688 : _Mem_fn_const_or_non<_Res,
689 (sizeof(__sfinae_types::__two)
690 == sizeof(__check_const<_Tp>(__get_ref<_Tp>(), (_Tp*)0)))>
693 template<typename _Signature>
696 template<typename _CVMem, typename _Tp>
697 struct result<_CVMem(_Tp)>
698 : public _Result_type<_Tp> { };
700 template<typename _CVMem, typename _Tp>
701 struct result<_CVMem(_Tp&)>
702 : public _Result_type<_Tp> { };
705 _Mem_fn(_Res _Class::*__pm) : __pm(__pm) { }
709 operator()(_Class& __object) const
710 { return __object.*__pm; }
713 operator()(const _Class& __object) const
714 { return __object.*__pm; }
718 operator()(_Class* __object) const
719 { return __object->*__pm; }
722 operator()(const _Class* __object) const
723 { return __object->*__pm; }
725 // Handle smart pointers and derived
726 template<typename _Tp>
727 typename _Result_type<_Tp>::type
728 operator()(_Tp& __unknown) const
729 { return _M_call(__unknown, &__unknown); }
736 * @brief Returns a function object that forwards to the member
740 template<typename _Tp, typename _Class>
741 inline _Mem_fn<_Tp _Class::*>
742 mem_fn(_Tp _Class::* __pm)
744 return _Mem_fn<_Tp _Class::*>(__pm);
748 * @brief Determines if the given type _Tp is a function object
749 * should be treated as a subexpression when evaluating calls to
750 * function objects returned by bind(). [TR1 3.6.1]
753 template<typename _Tp>
754 struct is_bind_expression
755 : public false_type { };
758 * @brief Determines if the given type _Tp is a placeholder in a
759 * bind() expression and, if so, which placeholder it is. [TR1 3.6.2]
762 template<typename _Tp>
763 struct is_placeholder
764 : public integral_constant<int, 0>
767 /// The type of placeholder objects defined by libstdc++.
768 template<int _Num> struct _Placeholder { };
770 /** @namespace std::placeholders
771 * @brief ISO C++ 0x entities sub namespace for functional.
774 * Define a large number of placeholders. There is no way to
775 * simplify this with variadic templates, because we're introducing
776 * unique names for each.
778 namespace placeholders
791 _Placeholder<10> _10;
792 _Placeholder<11> _11;
793 _Placeholder<12> _12;
794 _Placeholder<13> _13;
795 _Placeholder<14> _14;
796 _Placeholder<15> _15;
797 _Placeholder<16> _16;
798 _Placeholder<17> _17;
799 _Placeholder<18> _18;
800 _Placeholder<19> _19;
801 _Placeholder<20> _20;
802 _Placeholder<21> _21;
803 _Placeholder<22> _22;
804 _Placeholder<23> _23;
805 _Placeholder<24> _24;
806 _Placeholder<25> _25;
807 _Placeholder<26> _26;
808 _Placeholder<27> _27;
809 _Placeholder<28> _28;
810 _Placeholder<29> _29;
815 * Partial specialization of is_placeholder that provides the placeholder
816 * number for the placeholder objects defined by libstdc++.
820 struct is_placeholder<_Placeholder<_Num> >
821 : public integral_constant<int, _Num>
825 * Used by _Safe_tuple_element to indicate that there is no tuple
826 * element at this position.
828 struct _No_tuple_element;
831 * Implementation helper for _Safe_tuple_element. This primary
832 * template handles the case where it is safe to use @c
835 template<int __i, typename _Tuple, bool _IsSafe>
836 struct _Safe_tuple_element_impl
837 : tuple_element<__i, _Tuple> { };
840 * Implementation helper for _Safe_tuple_element. This partial
841 * specialization handles the case where it is not safe to use @c
842 * tuple_element. We just return @c _No_tuple_element.
844 template<int __i, typename _Tuple>
845 struct _Safe_tuple_element_impl<__i, _Tuple, false>
847 typedef _No_tuple_element type;
851 * Like tuple_element, but returns @c _No_tuple_element when
852 * tuple_element would return an error.
854 template<int __i, typename _Tuple>
855 struct _Safe_tuple_element
856 : _Safe_tuple_element_impl<__i, _Tuple,
857 (__i >= 0 && __i < tuple_size<_Tuple>::value)>
861 * Maps an argument to bind() into an actual argument to the bound
862 * function object [TR1 3.6.3/5]. Only the first parameter should
863 * be specified: the rest are used to determine among the various
864 * implementations. Note that, although this class is a function
865 * object, it isn't entirely normal because it takes only two
866 * parameters regardless of the number of parameters passed to the
867 * bind expression. The first parameter is the bound argument and
868 * the second parameter is a tuple containing references to the
869 * rest of the arguments.
871 template<typename _Arg,
872 bool _IsBindExp = is_bind_expression<_Arg>::value,
873 bool _IsPlaceholder = (is_placeholder<_Arg>::value > 0)>
877 * If the argument is reference_wrapper<_Tp>, returns the
878 * underlying reference. [TR1 3.6.3/5 bullet 1]
880 template<typename _Tp>
881 class _Mu<reference_wrapper<_Tp>, false, false>
884 typedef _Tp& result_type;
886 /* Note: This won't actually work for const volatile
887 * reference_wrappers, because reference_wrapper::get() is const
888 * but not volatile-qualified. This might be a defect in the TR.
890 template<typename _CVRef, typename _Tuple>
892 operator()(_CVRef& __arg, _Tuple&&) const volatile
893 { return __arg.get(); }
897 * If the argument is a bind expression, we invoke the underlying
898 * function object with the same cv-qualifiers as we are given and
899 * pass along all of our arguments (unwrapped). [TR1 3.6.3/5 bullet 2]
901 template<typename _Arg>
902 class _Mu<_Arg, true, false>
905 template<typename _Signature> class result;
907 // Determine the result type when we pass the arguments along. This
908 // involves passing along the cv-qualifiers placed on _Mu and
909 // unwrapping the argument bundle.
910 template<typename _CVMu, typename _CVArg, typename... _Args>
911 class result<_CVMu(_CVArg, tuple<_Args...>)>
912 : public result_of<_CVArg(_Args...)> { };
914 template<typename _CVArg, typename... _Args>
915 typename result_of<_CVArg(_Args...)>::type
916 operator()(_CVArg& __arg,
917 tuple<_Args...>&& __tuple) const volatile
919 // Construct an index tuple and forward to __call
920 typedef typename _Build_index_tuple<sizeof...(_Args)>::__type
922 return this->__call(__arg, std::move(__tuple), _Indexes());
926 // Invokes the underlying function object __arg by unpacking all
927 // of the arguments in the tuple.
928 template<typename _CVArg, typename... _Args, int... _Indexes>
929 typename result_of<_CVArg(_Args...)>::type
930 __call(_CVArg& __arg, tuple<_Args...>&& __tuple,
931 const _Index_tuple<_Indexes...>&) const volatile
933 return __arg(std::forward<_Args>(get<_Indexes>(__tuple))...);
938 * If the argument is a placeholder for the Nth argument, returns
939 * a reference to the Nth argument to the bind function object.
940 * [TR1 3.6.3/5 bullet 3]
942 template<typename _Arg>
943 class _Mu<_Arg, false, true>
946 template<typename _Signature> class result;
948 template<typename _CVMu, typename _CVArg, typename _Tuple>
949 class result<_CVMu(_CVArg, _Tuple)>
951 // Add a reference, if it hasn't already been done for us.
952 // This allows us to be a little bit sloppy in constructing
953 // the tuple that we pass to result_of<...>.
954 typedef typename _Safe_tuple_element<(is_placeholder<_Arg>::value
959 typedef typename add_rvalue_reference<__base_type>::type type;
962 template<typename _Tuple>
963 typename result<_Mu(_Arg, _Tuple)>::type
964 operator()(const volatile _Arg&, _Tuple&& __tuple) const volatile
966 return std::forward<typename result<_Mu(_Arg, _Tuple)>::type>(
967 ::std::get<(is_placeholder<_Arg>::value - 1)>(__tuple));
972 * If the argument is just a value, returns a reference to that
973 * value. The cv-qualifiers on the reference are the same as the
974 * cv-qualifiers on the _Mu object. [TR1 3.6.3/5 bullet 4]
976 template<typename _Arg>
977 class _Mu<_Arg, false, false>
980 template<typename _Signature> struct result;
982 template<typename _CVMu, typename _CVArg, typename _Tuple>
983 struct result<_CVMu(_CVArg, _Tuple)>
985 typedef typename add_lvalue_reference<_CVArg>::type type;
988 // Pick up the cv-qualifiers of the argument
989 template<typename _CVArg, typename _Tuple>
991 operator()(_CVArg&& __arg, _Tuple&&) const volatile
992 { return std::forward<_CVArg>(__arg); }
996 * Maps member pointers into instances of _Mem_fn but leaves all
997 * other function objects untouched. Used by tr1::bind(). The
998 * primary template handles the non--member-pointer case.
1000 template<typename _Tp>
1001 struct _Maybe_wrap_member_pointer
1006 __do_wrap(const _Tp& __x)
1011 * Maps member pointers into instances of _Mem_fn but leaves all
1012 * other function objects untouched. Used by tr1::bind(). This
1013 * partial specialization handles the member pointer case.
1015 template<typename _Tp, typename _Class>
1016 struct _Maybe_wrap_member_pointer<_Tp _Class::*>
1018 typedef _Mem_fn<_Tp _Class::*> type;
1021 __do_wrap(_Tp _Class::* __pm)
1022 { return type(__pm); }
1025 // Specialization needed to prevent "forming reference to void" errors when
1026 // bind<void>() is called, because argument deduction instantiates
1027 // _Maybe_wrap_member_pointer<void> outside the immediate context where
1030 struct _Maybe_wrap_member_pointer<void>
1035 /// Type of the function object returned from bind().
1036 template<typename _Signature>
1039 template<typename _Functor, typename... _Bound_args>
1040 class _Bind<_Functor(_Bound_args...)>
1041 : public _Weak_result_type<_Functor>
1043 typedef _Bind __self_type;
1044 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
1048 tuple<_Bound_args...> _M_bound_args;
1051 template<typename _Result, typename... _Args, int... _Indexes>
1053 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>)
1055 return _M_f(_Mu<_Bound_args>()
1056 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1060 template<typename _Result, typename... _Args, int... _Indexes>
1062 __call_c(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>) const
1064 return _M_f(_Mu<_Bound_args>()
1065 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1070 template<typename _Result, typename... _Args, int... _Indexes>
1072 __call_v(tuple<_Args...>&& __args,
1073 _Index_tuple<_Indexes...>) volatile
1075 return _M_f(_Mu<_Bound_args>()
1076 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1079 // Call as const volatile
1080 template<typename _Result, typename... _Args, int... _Indexes>
1082 __call_c_v(tuple<_Args...>&& __args,
1083 _Index_tuple<_Indexes...>) const volatile
1085 return _M_f(_Mu<_Bound_args>()
1086 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1091 explicit _Bind(_Functor __f, _Bound_args... __bound_args)
1092 : _M_f(std::forward<_Functor>(__f)),
1093 _M_bound_args(std::forward<_Bound_args>(__bound_args)...)
1097 template<typename... _Args, typename _Result
1098 = decltype( std::declval<_Functor>()(
1099 _Mu<_Bound_args>()( std::declval<_Bound_args&>(),
1100 std::declval<tuple<_Args...>&&>() )... ) )>
1102 operator()(_Args&&... __args)
1104 return this->__call<_Result>(tuple<_Args...>
1105 (std::forward<_Args>(__args)...),
1110 template<typename... _Args, typename _Result
1111 = decltype( std::declval<const _Functor>()(
1112 _Mu<_Bound_args>()( std::declval<const _Bound_args&>(),
1113 std::declval<tuple<_Args...>&&>() )... ) )>
1115 operator()(_Args&&... __args) const
1117 return this->__call_c<_Result>(tuple<_Args...>
1118 (std::forward<_Args>(__args)...),
1124 template<typename... _Args, typename _Result
1125 = decltype( std::declval<volatile _Functor>()(
1126 _Mu<_Bound_args>()( std::declval<volatile _Bound_args&>(),
1127 std::declval<tuple<_Args...>&&>() )... ) )>
1129 operator()(_Args&&... __args) volatile
1131 return this->__call_v<_Result>(tuple<_Args...>
1132 (std::forward<_Args>(__args)...),
1136 // Call as const volatile
1137 template<typename... _Args, typename _Result
1138 = decltype( std::declval<const volatile _Functor>()(
1139 _Mu<_Bound_args>()( std::declval<const volatile _Bound_args&>(),
1140 std::declval<tuple<_Args...>&&>() )... ) )>
1142 operator()(_Args&&... __args) const volatile
1144 return this->__call_c_v<_Result>(tuple<_Args...>
1145 (std::forward<_Args>(__args)...),
1151 /// Type of the function object returned from bind<R>().
1152 template<typename _Result, typename _Signature>
1153 struct _Bind_result;
1155 template<typename _Result, typename _Functor, typename... _Bound_args>
1156 class _Bind_result<_Result, _Functor(_Bound_args...)>
1158 typedef _Bind_result __self_type;
1159 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
1163 tuple<_Bound_args...> _M_bound_args;
1166 template<typename _Res>
1167 struct __enable_if_void : enable_if<is_void<_Res>::value, int> { };
1168 template<typename _Res>
1169 struct __disable_if_void : enable_if<!is_void<_Res>::value, int> { };
1172 template<typename _Res, typename... _Args, int... _Indexes>
1174 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1175 typename __disable_if_void<_Res>::type = 0)
1177 return _M_f(_Mu<_Bound_args>()
1178 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1181 // Call unqualified, return void
1182 template<typename _Res, typename... _Args, int... _Indexes>
1184 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1185 typename __enable_if_void<_Res>::type = 0)
1187 _M_f(_Mu<_Bound_args>()
1188 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1192 template<typename _Res, typename... _Args, int... _Indexes>
1194 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1195 typename __disable_if_void<_Res>::type = 0) const
1197 return _M_f(_Mu<_Bound_args>()
1198 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1201 // Call as const, return void
1202 template<typename _Res, typename... _Args, int... _Indexes>
1204 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1205 typename __enable_if_void<_Res>::type = 0) const
1207 _M_f(_Mu<_Bound_args>()
1208 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1212 template<typename _Res, typename... _Args, int... _Indexes>
1214 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1215 typename __disable_if_void<_Res>::type = 0) volatile
1217 return _M_f(_Mu<_Bound_args>()
1218 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1221 // Call as volatile, return void
1222 template<typename _Res, typename... _Args, int... _Indexes>
1224 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1225 typename __enable_if_void<_Res>::type = 0) volatile
1227 _M_f(_Mu<_Bound_args>()
1228 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1231 // Call as const volatile
1232 template<typename _Res, typename... _Args, int... _Indexes>
1234 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1235 typename __disable_if_void<_Res>::type = 0) const volatile
1237 return _M_f(_Mu<_Bound_args>()
1238 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1241 // Call as const volatile, return void
1242 template<typename _Res, typename... _Args, int... _Indexes>
1244 __call(tuple<_Args...>&& __args,
1245 _Index_tuple<_Indexes...>,
1246 typename __enable_if_void<_Res>::type = 0) const volatile
1248 _M_f(_Mu<_Bound_args>()
1249 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1253 typedef _Result result_type;
1256 _Bind_result(_Functor __f, _Bound_args... __bound_args)
1257 : _M_f(std::forward<_Functor>(__f)),
1258 _M_bound_args(std::forward<_Bound_args>(__bound_args)...)
1262 template<typename... _Args>
1264 operator()(_Args&&... __args)
1266 return this->__call<_Result>(
1267 tuple<_Args...>(std::forward<_Args...>(__args)...),
1272 template<typename... _Args>
1274 operator()(_Args&&... __args) const
1276 return this->__call<_Result>(
1277 tuple<_Args...>(std::forward<_Args...>(__args)...),
1282 template<typename... _Args>
1284 operator()(_Args&&... __args) volatile
1286 return this->__call<_Result>(
1287 tuple<_Args...>(std::forward<_Args...>(__args)...),
1291 // Call as const volatile
1292 template<typename... _Args>
1294 operator()(_Args&&... __args) const volatile
1296 return this->__call<_Result>(
1297 tuple<_Args...>(std::forward<_Args...>(__args)...),
1303 * @brief Class template _Bind is always a bind expression.
1306 template<typename _Signature>
1307 struct is_bind_expression<_Bind<_Signature> >
1308 : public true_type { };
1311 * @brief Class template _Bind is always a bind expression.
1314 template<typename _Result, typename _Signature>
1315 struct is_bind_expression<_Bind_result<_Result, _Signature> >
1316 : public true_type { };
1319 * @brief Function template for std::bind.
1322 template<typename _Functor, typename... _ArgTypes>
1324 _Bind<typename _Maybe_wrap_member_pointer<_Functor>::type(_ArgTypes...)>
1325 bind(_Functor __f, _ArgTypes... __args)
1327 typedef _Maybe_wrap_member_pointer<_Functor> __maybe_type;
1328 typedef typename __maybe_type::type __functor_type;
1329 typedef _Bind<__functor_type(_ArgTypes...)> __result_type;
1330 return __result_type(__maybe_type::__do_wrap(__f),
1331 std::forward<_ArgTypes>(__args)...);
1335 * @brief Function template for std::bind.
1338 template<typename _Result, typename _Functor, typename... _ArgTypes>
1340 _Bind_result<_Result,
1341 typename _Maybe_wrap_member_pointer<_Functor>::type
1343 bind(_Functor __f, _ArgTypes... __args)
1345 typedef _Maybe_wrap_member_pointer<_Functor> __maybe_type;
1346 typedef typename __maybe_type::type __functor_type;
1347 typedef _Bind_result<_Result, __functor_type(_ArgTypes...)>
1349 return __result_type(__maybe_type::__do_wrap(__f),
1350 std::forward<_ArgTypes>(__args)...);
1354 * @brief Exception class thrown when class template function's
1355 * operator() is called with an empty target.
1356 * @ingroup exceptions
1358 class bad_function_call : public std::exception { };
1361 * Trait identifying "location-invariant" types, meaning that the
1362 * address of the object (or any of its members) will not escape.
1363 * Also implies a trivial copy constructor and assignment operator.
1365 template<typename _Tp>
1366 struct __is_location_invariant
1367 : integral_constant<bool, (is_pointer<_Tp>::value
1368 || is_member_pointer<_Tp>::value)>
1371 class _Undefined_class;
1376 const void* _M_const_object;
1377 void (*_M_function_pointer)();
1378 void (_Undefined_class::*_M_member_pointer)();
1383 void* _M_access() { return &_M_pod_data[0]; }
1384 const void* _M_access() const { return &_M_pod_data[0]; }
1386 template<typename _Tp>
1389 { return *static_cast<_Tp*>(_M_access()); }
1391 template<typename _Tp>
1394 { return *static_cast<const _Tp*>(_M_access()); }
1396 _Nocopy_types _M_unused;
1397 char _M_pod_data[sizeof(_Nocopy_types)];
1400 enum _Manager_operation
1408 // Simple type wrapper that helps avoid annoying const problems
1409 // when casting between void pointers and pointers-to-pointers.
1410 template<typename _Tp>
1411 struct _Simple_type_wrapper
1413 _Simple_type_wrapper(_Tp __value) : __value(__value) { }
1418 template<typename _Tp>
1419 struct __is_location_invariant<_Simple_type_wrapper<_Tp> >
1420 : __is_location_invariant<_Tp>
1423 // Converts a reference to a function object into a callable
1425 template<typename _Functor>
1427 __callable_functor(_Functor& __f)
1430 template<typename _Member, typename _Class>
1431 inline _Mem_fn<_Member _Class::*>
1432 __callable_functor(_Member _Class::* &__p)
1433 { return mem_fn(__p); }
1435 template<typename _Member, typename _Class>
1436 inline _Mem_fn<_Member _Class::*>
1437 __callable_functor(_Member _Class::* const &__p)
1438 { return mem_fn(__p); }
1440 template<typename _Signature>
1443 /// Base class of all polymorphic function object wrappers.
1444 class _Function_base
1447 static const std::size_t _M_max_size = sizeof(_Nocopy_types);
1448 static const std::size_t _M_max_align = __alignof__(_Nocopy_types);
1450 template<typename _Functor>
1454 static const bool __stored_locally =
1455 (__is_location_invariant<_Functor>::value
1456 && sizeof(_Functor) <= _M_max_size
1457 && __alignof__(_Functor) <= _M_max_align
1458 && (_M_max_align % __alignof__(_Functor) == 0));
1460 typedef integral_constant<bool, __stored_locally> _Local_storage;
1462 // Retrieve a pointer to the function object
1464 _M_get_pointer(const _Any_data& __source)
1466 const _Functor* __ptr =
1467 __stored_locally? &__source._M_access<_Functor>()
1468 /* have stored a pointer */ : __source._M_access<_Functor*>();
1469 return const_cast<_Functor*>(__ptr);
1472 // Clone a location-invariant function object that fits within
1473 // an _Any_data structure.
1475 _M_clone(_Any_data& __dest, const _Any_data& __source, true_type)
1477 new (__dest._M_access()) _Functor(__source._M_access<_Functor>());
1480 // Clone a function object that is not location-invariant or
1481 // that cannot fit into an _Any_data structure.
1483 _M_clone(_Any_data& __dest, const _Any_data& __source, false_type)
1485 __dest._M_access<_Functor*>() =
1486 new _Functor(*__source._M_access<_Functor*>());
1489 // Destroying a location-invariant object may still require
1492 _M_destroy(_Any_data& __victim, true_type)
1494 __victim._M_access<_Functor>().~_Functor();
1497 // Destroying an object located on the heap.
1499 _M_destroy(_Any_data& __victim, false_type)
1501 delete __victim._M_access<_Functor*>();
1506 _M_manager(_Any_data& __dest, const _Any_data& __source,
1507 _Manager_operation __op)
1512 case __get_type_info:
1513 __dest._M_access<const type_info*>() = &typeid(_Functor);
1516 case __get_functor_ptr:
1517 __dest._M_access<_Functor*>() = _M_get_pointer(__source);
1520 case __clone_functor:
1521 _M_clone(__dest, __source, _Local_storage());
1524 case __destroy_functor:
1525 _M_destroy(__dest, _Local_storage());
1532 _M_init_functor(_Any_data& __functor, _Functor&& __f)
1533 { _M_init_functor(__functor, std::move(__f), _Local_storage()); }
1535 template<typename _Signature>
1537 _M_not_empty_function(const function<_Signature>& __f)
1538 { return static_cast<bool>(__f); }
1540 template<typename _Tp>
1542 _M_not_empty_function(const _Tp*& __fp)
1545 template<typename _Class, typename _Tp>
1547 _M_not_empty_function(_Tp _Class::* const& __mp)
1550 template<typename _Tp>
1552 _M_not_empty_function(const _Tp&)
1557 _M_init_functor(_Any_data& __functor, _Functor&& __f, true_type)
1558 { new (__functor._M_access()) _Functor(std::move(__f)); }
1561 _M_init_functor(_Any_data& __functor, _Functor&& __f, false_type)
1562 { __functor._M_access<_Functor*>() = new _Functor(std::move(__f)); }
1565 template<typename _Functor>
1566 class _Ref_manager : public _Base_manager<_Functor*>
1568 typedef _Function_base::_Base_manager<_Functor*> _Base;
1572 _M_manager(_Any_data& __dest, const _Any_data& __source,
1573 _Manager_operation __op)
1578 case __get_type_info:
1579 __dest._M_access<const type_info*>() = &typeid(_Functor);
1582 case __get_functor_ptr:
1583 __dest._M_access<_Functor*>() = *_Base::_M_get_pointer(__source);
1584 return is_const<_Functor>::value;
1588 _Base::_M_manager(__dest, __source, __op);
1594 _M_init_functor(_Any_data& __functor, reference_wrapper<_Functor> __f)
1596 // TBD: Use address_of function instead.
1597 _Base::_M_init_functor(__functor, &__f.get());
1601 _Function_base() : _M_manager(0) { }
1606 _M_manager(_M_functor, _M_functor, __destroy_functor);
1610 bool _M_empty() const { return !_M_manager; }
1612 typedef bool (*_Manager_type)(_Any_data&, const _Any_data&,
1613 _Manager_operation);
1615 _Any_data _M_functor;
1616 _Manager_type _M_manager;
1619 template<typename _Signature, typename _Functor>
1620 class _Function_handler;
1622 template<typename _Res, typename _Functor, typename... _ArgTypes>
1623 class _Function_handler<_Res(_ArgTypes...), _Functor>
1624 : public _Function_base::_Base_manager<_Functor>
1626 typedef _Function_base::_Base_manager<_Functor> _Base;
1630 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1632 return (*_Base::_M_get_pointer(__functor))(
1633 std::forward<_ArgTypes>(__args)...);
1637 template<typename _Functor, typename... _ArgTypes>
1638 class _Function_handler<void(_ArgTypes...), _Functor>
1639 : public _Function_base::_Base_manager<_Functor>
1641 typedef _Function_base::_Base_manager<_Functor> _Base;
1645 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1647 (*_Base::_M_get_pointer(__functor))(
1648 std::forward<_ArgTypes>(__args)...);
1652 template<typename _Res, typename _Functor, typename... _ArgTypes>
1653 class _Function_handler<_Res(_ArgTypes...), reference_wrapper<_Functor> >
1654 : public _Function_base::_Ref_manager<_Functor>
1656 typedef _Function_base::_Ref_manager<_Functor> _Base;
1660 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1662 return __callable_functor(**_Base::_M_get_pointer(__functor))(
1663 std::forward<_ArgTypes>(__args)...);
1667 template<typename _Functor, typename... _ArgTypes>
1668 class _Function_handler<void(_ArgTypes...), reference_wrapper<_Functor> >
1669 : public _Function_base::_Ref_manager<_Functor>
1671 typedef _Function_base::_Ref_manager<_Functor> _Base;
1675 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1677 __callable_functor(**_Base::_M_get_pointer(__functor))(
1678 std::forward<_ArgTypes>(__args)...);
1682 template<typename _Class, typename _Member, typename _Res,
1683 typename... _ArgTypes>
1684 class _Function_handler<_Res(_ArgTypes...), _Member _Class::*>
1685 : public _Function_handler<void(_ArgTypes...), _Member _Class::*>
1687 typedef _Function_handler<void(_ArgTypes...), _Member _Class::*>
1692 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1694 return mem_fn(_Base::_M_get_pointer(__functor)->__value)(
1695 std::forward<_ArgTypes>(__args)...);
1699 template<typename _Class, typename _Member, typename... _ArgTypes>
1700 class _Function_handler<void(_ArgTypes...), _Member _Class::*>
1701 : public _Function_base::_Base_manager<
1702 _Simple_type_wrapper< _Member _Class::* > >
1704 typedef _Member _Class::* _Functor;
1705 typedef _Simple_type_wrapper<_Functor> _Wrapper;
1706 typedef _Function_base::_Base_manager<_Wrapper> _Base;
1710 _M_manager(_Any_data& __dest, const _Any_data& __source,
1711 _Manager_operation __op)
1716 case __get_type_info:
1717 __dest._M_access<const type_info*>() = &typeid(_Functor);
1720 case __get_functor_ptr:
1721 __dest._M_access<_Functor*>() =
1722 &_Base::_M_get_pointer(__source)->__value;
1726 _Base::_M_manager(__dest, __source, __op);
1732 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1734 mem_fn(_Base::_M_get_pointer(__functor)->__value)(
1735 std::forward<_ArgTypes>(__args)...);
1740 * @brief Primary class template for std::function.
1743 * Polymorphic function wrapper.
1745 template<typename _Res, typename... _ArgTypes>
1746 class function<_Res(_ArgTypes...)>
1747 : public _Maybe_unary_or_binary_function<_Res, _ArgTypes...>,
1748 private _Function_base
1750 typedef _Res _Signature_type(_ArgTypes...);
1752 struct _Useless { };
1755 typedef _Res result_type;
1757 // [3.7.2.1] construct/copy/destroy
1760 * @brief Default construct creates an empty function call wrapper.
1761 * @post @c !(bool)*this
1763 function() : _Function_base() { }
1766 * @brief Creates an empty function call wrapper.
1767 * @post @c !(bool)*this
1769 function(nullptr_t) : _Function_base() { }
1772 * @brief %Function copy constructor.
1773 * @param x A %function object with identical call signature.
1774 * @post @c (bool)*this == (bool)x
1776 * The newly-created %function contains a copy of the target of @a
1777 * x (if it has one).
1779 function(const function& __x);
1782 * @brief %Function move constructor.
1783 * @param x A %function object rvalue with identical call signature.
1785 * The newly-created %function contains the target of @a x
1788 function(function&& __x) : _Function_base()
1793 // TODO: needs allocator_arg_t
1796 * @brief Builds a %function that targets a copy of the incoming
1798 * @param f A %function object that is callable with parameters of
1799 * type @c T1, @c T2, ..., @c TN and returns a value convertible
1802 * The newly-created %function object will target a copy of @a
1803 * f. If @a f is @c reference_wrapper<F>, then this function
1804 * object will contain a reference to the function object @c
1805 * f.get(). If @a f is a NULL function pointer or NULL
1806 * pointer-to-member, the newly-created object will be empty.
1808 * If @a f is a non-NULL function pointer or an object of type @c
1809 * reference_wrapper<F>, this function will not throw.
1811 template<typename _Functor>
1812 function(_Functor __f,
1814 !is_integral<_Functor>::value, _Useless>::type
1818 * @brief %Function assignment operator.
1819 * @param x A %function with identical call signature.
1820 * @post @c (bool)*this == (bool)x
1823 * The target of @a x is copied to @c *this. If @a x has no
1824 * target, then @c *this will be empty.
1826 * If @a x targets a function pointer or a reference to a function
1827 * object, then this operation will not throw an %exception.
1830 operator=(const function& __x)
1832 function(__x).swap(*this);
1837 * @brief %Function move-assignment operator.
1838 * @param x A %function rvalue with identical call signature.
1841 * The target of @a x is moved to @c *this. If @a x has no
1842 * target, then @c *this will be empty.
1844 * If @a x targets a function pointer or a reference to a function
1845 * object, then this operation will not throw an %exception.
1848 operator=(function&& __x)
1850 function(std::move(__x)).swap(*this);
1855 * @brief %Function assignment to zero.
1856 * @post @c !(bool)*this
1859 * The target of @c *this is deallocated, leaving it empty.
1862 operator=(nullptr_t)
1866 _M_manager(_M_functor, _M_functor, __destroy_functor);
1874 * @brief %Function assignment to a new target.
1875 * @param f A %function object that is callable with parameters of
1876 * type @c T1, @c T2, ..., @c TN and returns a value convertible
1880 * This %function object wrapper will target a copy of @a
1881 * f. If @a f is @c reference_wrapper<F>, then this function
1882 * object will contain a reference to the function object @c
1883 * f.get(). If @a f is a NULL function pointer or NULL
1884 * pointer-to-member, @c this object will be empty.
1886 * If @a f is a non-NULL function pointer or an object of type @c
1887 * reference_wrapper<F>, this function will not throw.
1889 template<typename _Functor>
1890 typename enable_if<!is_integral<_Functor>::value, function&>::type
1891 operator=(_Functor&& __f)
1893 function(std::forward<_Functor>(__f)).swap(*this);
1898 template<typename _Functor>
1899 typename enable_if<!is_integral<_Functor>::value, function&>::type
1900 operator=(reference_wrapper<_Functor> __f)
1902 function(__f).swap(*this);
1906 // [3.7.2.2] function modifiers
1909 * @brief Swap the targets of two %function objects.
1910 * @param f A %function with identical call signature.
1912 * Swap the targets of @c this function object and @a f. This
1913 * function will not throw an %exception.
1915 void swap(function& __x)
1917 std::swap(_M_functor, __x._M_functor);
1918 std::swap(_M_manager, __x._M_manager);
1919 std::swap(_M_invoker, __x._M_invoker);
1922 // TODO: needs allocator_arg_t
1924 template<typename _Functor, typename _Alloc>
1926 assign(_Functor&& __f, const _Alloc& __a)
1928 function(allocator_arg, __a,
1929 std::forward<_Functor>(__f)).swap(*this);
1933 // [3.7.2.3] function capacity
1936 * @brief Determine if the %function wrapper has a target.
1938 * @return @c true when this %function object contains a target,
1939 * or @c false when it is empty.
1941 * This function will not throw an %exception.
1943 explicit operator bool() const
1944 { return !_M_empty(); }
1946 // [3.7.2.4] function invocation
1949 * @brief Invokes the function targeted by @c *this.
1950 * @returns the result of the target.
1951 * @throws bad_function_call when @c !(bool)*this
1953 * The function call operator invokes the target function object
1954 * stored by @c this.
1956 _Res operator()(_ArgTypes... __args) const;
1959 // [3.7.2.5] function target access
1961 * @brief Determine the type of the target of this function object
1964 * @returns the type identifier of the target function object, or
1965 * @c typeid(void) if @c !(bool)*this.
1967 * This function will not throw an %exception.
1969 const type_info& target_type() const;
1972 * @brief Access the stored target function object.
1974 * @return Returns a pointer to the stored target function object,
1975 * if @c typeid(Functor).equals(target_type()); otherwise, a NULL
1978 * This function will not throw an %exception.
1980 template<typename _Functor> _Functor* target();
1983 template<typename _Functor> const _Functor* target() const;
1986 // deleted overloads
1987 template<typename _Res2, typename... _ArgTypes2>
1988 void operator==(const function<_Res2(_ArgTypes2...)>&) const = delete;
1989 template<typename _Res2, typename... _ArgTypes2>
1990 void operator!=(const function<_Res2(_ArgTypes2...)>&) const = delete;
1993 typedef _Res (*_Invoker_type)(const _Any_data&, _ArgTypes...);
1994 _Invoker_type _M_invoker;
1997 // Out-of-line member definitions.
1998 template<typename _Res, typename... _ArgTypes>
1999 function<_Res(_ArgTypes...)>::
2000 function(const function& __x)
2003 if (static_cast<bool>(__x))
2005 _M_invoker = __x._M_invoker;
2006 _M_manager = __x._M_manager;
2007 __x._M_manager(_M_functor, __x._M_functor, __clone_functor);
2011 template<typename _Res, typename... _ArgTypes>
2012 template<typename _Functor>
2013 function<_Res(_ArgTypes...)>::
2014 function(_Functor __f,
2016 !is_integral<_Functor>::value, _Useless>::type)
2019 typedef _Function_handler<_Signature_type, _Functor> _My_handler;
2021 if (_My_handler::_M_not_empty_function(__f))
2023 _M_invoker = &_My_handler::_M_invoke;
2024 _M_manager = &_My_handler::_M_manager;
2025 _My_handler::_M_init_functor(_M_functor, std::move(__f));
2029 template<typename _Res, typename... _ArgTypes>
2031 function<_Res(_ArgTypes...)>::
2032 operator()(_ArgTypes... __args) const
2035 __throw_bad_function_call();
2036 return _M_invoker(_M_functor, std::forward<_ArgTypes>(__args)...);
2040 template<typename _Res, typename... _ArgTypes>
2042 function<_Res(_ArgTypes...)>::
2047 _Any_data __typeinfo_result;
2048 _M_manager(__typeinfo_result, _M_functor, __get_type_info);
2049 return *__typeinfo_result._M_access<const type_info*>();
2052 return typeid(void);
2055 template<typename _Res, typename... _ArgTypes>
2056 template<typename _Functor>
2058 function<_Res(_ArgTypes...)>::
2061 if (typeid(_Functor) == target_type() && _M_manager)
2064 if (_M_manager(__ptr, _M_functor, __get_functor_ptr)
2065 && !is_const<_Functor>::value)
2068 return __ptr._M_access<_Functor*>();
2074 template<typename _Res, typename... _ArgTypes>
2075 template<typename _Functor>
2077 function<_Res(_ArgTypes...)>::
2080 if (typeid(_Functor) == target_type() && _M_manager)
2083 _M_manager(__ptr, _M_functor, __get_functor_ptr);
2084 return __ptr._M_access<const _Functor*>();
2091 // [20.7.15.2.6] null pointer comparisons
2094 * @brief Compares a polymorphic function object wrapper against 0
2095 * (the NULL pointer).
2096 * @returns @c true if the wrapper has no target, @c false otherwise
2098 * This function will not throw an %exception.
2100 template<typename _Res, typename... _Args>
2102 operator==(const function<_Res(_Args...)>& __f, nullptr_t)
2103 { return !static_cast<bool>(__f); }
2106 template<typename _Res, typename... _Args>
2108 operator==(nullptr_t, const function<_Res(_Args...)>& __f)
2109 { return !static_cast<bool>(__f); }
2112 * @brief Compares a polymorphic function object wrapper against 0
2113 * (the NULL pointer).
2114 * @returns @c false if the wrapper has no target, @c true otherwise
2116 * This function will not throw an %exception.
2118 template<typename _Res, typename... _Args>
2120 operator!=(const function<_Res(_Args...)>& __f, nullptr_t)
2121 { return static_cast<bool>(__f); }
2124 template<typename _Res, typename... _Args>
2126 operator!=(nullptr_t, const function<_Res(_Args...)>& __f)
2127 { return static_cast<bool>(__f); }
2129 // [20.7.15.2.7] specialized algorithms
2132 * @brief Swap the targets of two polymorphic function object wrappers.
2134 * This function will not throw an %exception.
2136 template<typename _Res, typename... _Args>
2138 swap(function<_Res(_Args...)>& __x, function<_Res(_Args...)>& __y)
2142 #endif // __GXX_EXPERIMENTAL_CXX0X__
2144 #endif // _GLIBCXX_FUNCTIONAL