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/functional_hash.h>
62 template<typename _MemberPointer>
66 * Actual implementation of _Has_result_type, which uses SFINAE to
67 * determine if the type _Tp has a publicly-accessible member type
70 template<typename _Tp>
71 class _Has_result_type_helper : __sfinae_types
73 template<typename _Up>
77 template<typename _Up>
78 static __one __test(_Wrap_type<typename _Up::result_type>*);
80 template<typename _Up>
81 static __two __test(...);
84 static const bool value = sizeof(__test<_Tp>(0)) == 1;
87 template<typename _Tp>
88 struct _Has_result_type
89 : integral_constant<bool,
90 _Has_result_type_helper<typename remove_cv<_Tp>::type>::value>
93 /// If we have found a result_type, extract it.
94 template<bool _Has_result_type, typename _Functor>
95 struct _Maybe_get_result_type
98 template<typename _Functor>
99 struct _Maybe_get_result_type<true, _Functor>
101 typedef typename _Functor::result_type result_type;
105 * Base class for any function object that has a weak result type, as
106 * defined in 3.3/3 of TR1.
108 template<typename _Functor>
109 struct _Weak_result_type_impl
110 : _Maybe_get_result_type<_Has_result_type<_Functor>::value, _Functor>
113 /// Retrieve the result type for a function type.
114 template<typename _Res, typename... _ArgTypes>
115 struct _Weak_result_type_impl<_Res(_ArgTypes...)>
117 typedef _Res result_type;
120 /// Retrieve the result type for a function reference.
121 template<typename _Res, typename... _ArgTypes>
122 struct _Weak_result_type_impl<_Res(&)(_ArgTypes...)>
124 typedef _Res result_type;
127 /// Retrieve the result type for a function pointer.
128 template<typename _Res, typename... _ArgTypes>
129 struct _Weak_result_type_impl<_Res(*)(_ArgTypes...)>
131 typedef _Res result_type;
134 /// Retrieve result type for a member function pointer.
135 template<typename _Res, typename _Class, typename... _ArgTypes>
136 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...)>
138 typedef _Res result_type;
141 /// Retrieve result type for a const member function pointer.
142 template<typename _Res, typename _Class, typename... _ArgTypes>
143 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) const>
145 typedef _Res result_type;
148 /// Retrieve result type for a volatile member function pointer.
149 template<typename _Res, typename _Class, typename... _ArgTypes>
150 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) volatile>
152 typedef _Res result_type;
155 /// Retrieve result type for a const volatile member function pointer.
156 template<typename _Res, typename _Class, typename... _ArgTypes>
157 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...)const volatile>
159 typedef _Res result_type;
163 * Strip top-level cv-qualifiers from the function object and let
164 * _Weak_result_type_impl perform the real work.
166 template<typename _Functor>
167 struct _Weak_result_type
168 : _Weak_result_type_impl<typename remove_cv<_Functor>::type>
171 template<typename _Signature>
174 template<typename _Functor, typename... _ArgTypes>
175 struct result_of<_Functor(_ArgTypes...)>
178 decltype( std::declval<_Functor>()(std::declval<_ArgTypes>()...) )
182 /// Determines if the type _Tp derives from unary_function.
183 template<typename _Tp>
184 struct _Derives_from_unary_function : __sfinae_types
187 template<typename _T1, typename _Res>
188 static __one __test(const volatile unary_function<_T1, _Res>*);
190 // It's tempting to change "..." to const volatile void*, but
191 // that fails when _Tp is a function type.
192 static __two __test(...);
195 static const bool value = sizeof(__test((_Tp*)0)) == 1;
198 /// Determines if the type _Tp derives from binary_function.
199 template<typename _Tp>
200 struct _Derives_from_binary_function : __sfinae_types
203 template<typename _T1, typename _T2, typename _Res>
204 static __one __test(const volatile binary_function<_T1, _T2, _Res>*);
206 // It's tempting to change "..." to const volatile void*, but
207 // that fails when _Tp is a function type.
208 static __two __test(...);
211 static const bool value = sizeof(__test((_Tp*)0)) == 1;
214 /// Turns a function type into a function pointer type
215 template<typename _Tp, bool _IsFunctionType = is_function<_Tp>::value>
216 struct _Function_to_function_pointer
221 template<typename _Tp>
222 struct _Function_to_function_pointer<_Tp, true>
228 * Invoke a function object, which may be either a member pointer or a
229 * function object. The first parameter will tell which.
231 template<typename _Functor, typename... _Args>
234 (!is_member_pointer<_Functor>::value
235 && !is_function<_Functor>::value
236 && !is_function<typename remove_pointer<_Functor>::type>::value),
237 typename result_of<_Functor(_Args...)>::type
239 __invoke(_Functor& __f, _Args&&... __args)
241 return __f(std::forward<_Args>(__args)...);
244 // To pick up function references (that will become function pointers)
245 template<typename _Functor, typename... _Args>
248 (is_pointer<_Functor>::value
249 && is_function<typename remove_pointer<_Functor>::type>::value),
250 typename result_of<_Functor(_Args...)>::type
252 __invoke(_Functor __f, _Args&&... __args)
254 return __f(std::forward<_Args>(__args)...);
258 * Knowing which of unary_function and binary_function _Tp derives
259 * from, derives from the same and ensures that reference_wrapper
260 * will have a weak result type. See cases below.
262 template<bool _Unary, bool _Binary, typename _Tp>
263 struct _Reference_wrapper_base_impl;
265 // Not a unary_function or binary_function, so try a weak result type.
266 template<typename _Tp>
267 struct _Reference_wrapper_base_impl<false, false, _Tp>
268 : _Weak_result_type<_Tp>
271 // unary_function but not binary_function
272 template<typename _Tp>
273 struct _Reference_wrapper_base_impl<true, false, _Tp>
274 : unary_function<typename _Tp::argument_type,
275 typename _Tp::result_type>
278 // binary_function but not unary_function
279 template<typename _Tp>
280 struct _Reference_wrapper_base_impl<false, true, _Tp>
281 : binary_function<typename _Tp::first_argument_type,
282 typename _Tp::second_argument_type,
283 typename _Tp::result_type>
286 // Both unary_function and binary_function. Import result_type to
288 template<typename _Tp>
289 struct _Reference_wrapper_base_impl<true, true, _Tp>
290 : unary_function<typename _Tp::argument_type,
291 typename _Tp::result_type>,
292 binary_function<typename _Tp::first_argument_type,
293 typename _Tp::second_argument_type,
294 typename _Tp::result_type>
296 typedef typename _Tp::result_type result_type;
300 * Derives from unary_function or binary_function when it
301 * can. Specializations handle all of the easy cases. The primary
302 * template determines what to do with a class type, which may
303 * derive from both unary_function and binary_function.
305 template<typename _Tp>
306 struct _Reference_wrapper_base
307 : _Reference_wrapper_base_impl<
308 _Derives_from_unary_function<_Tp>::value,
309 _Derives_from_binary_function<_Tp>::value,
313 // - a function type (unary)
314 template<typename _Res, typename _T1>
315 struct _Reference_wrapper_base<_Res(_T1)>
316 : unary_function<_T1, _Res>
319 // - a function type (binary)
320 template<typename _Res, typename _T1, typename _T2>
321 struct _Reference_wrapper_base<_Res(_T1, _T2)>
322 : binary_function<_T1, _T2, _Res>
325 // - a function pointer type (unary)
326 template<typename _Res, typename _T1>
327 struct _Reference_wrapper_base<_Res(*)(_T1)>
328 : unary_function<_T1, _Res>
331 // - a function pointer type (binary)
332 template<typename _Res, typename _T1, typename _T2>
333 struct _Reference_wrapper_base<_Res(*)(_T1, _T2)>
334 : binary_function<_T1, _T2, _Res>
337 // - a pointer to member function type (unary, no qualifiers)
338 template<typename _Res, typename _T1>
339 struct _Reference_wrapper_base<_Res (_T1::*)()>
340 : unary_function<_T1*, _Res>
343 // - a pointer to member function type (binary, no qualifiers)
344 template<typename _Res, typename _T1, typename _T2>
345 struct _Reference_wrapper_base<_Res (_T1::*)(_T2)>
346 : binary_function<_T1*, _T2, _Res>
349 // - a pointer to member function type (unary, const)
350 template<typename _Res, typename _T1>
351 struct _Reference_wrapper_base<_Res (_T1::*)() const>
352 : unary_function<const _T1*, _Res>
355 // - a pointer to member function type (binary, const)
356 template<typename _Res, typename _T1, typename _T2>
357 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const>
358 : binary_function<const _T1*, _T2, _Res>
361 // - a pointer to member function type (unary, volatile)
362 template<typename _Res, typename _T1>
363 struct _Reference_wrapper_base<_Res (_T1::*)() volatile>
364 : unary_function<volatile _T1*, _Res>
367 // - a pointer to member function type (binary, volatile)
368 template<typename _Res, typename _T1, typename _T2>
369 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) volatile>
370 : binary_function<volatile _T1*, _T2, _Res>
373 // - a pointer to member function type (unary, const volatile)
374 template<typename _Res, typename _T1>
375 struct _Reference_wrapper_base<_Res (_T1::*)() const volatile>
376 : unary_function<const volatile _T1*, _Res>
379 // - a pointer to member function type (binary, const volatile)
380 template<typename _Res, typename _T1, typename _T2>
381 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const volatile>
382 : binary_function<const volatile _T1*, _T2, _Res>
386 * @brief Primary class template for reference_wrapper.
390 template<typename _Tp>
391 class reference_wrapper
392 : public _Reference_wrapper_base<typename remove_cv<_Tp>::type>
394 // If _Tp is a function type, we can't form result_of<_Tp(...)>,
395 // so turn it into a function pointer type.
396 typedef typename _Function_to_function_pointer<_Tp>::type
403 reference_wrapper(_Tp& __indata): _M_data(&__indata)
406 reference_wrapper(_Tp&&) = delete;
408 reference_wrapper(const reference_wrapper<_Tp>& __inref):
409 _M_data(__inref._M_data)
413 operator=(const reference_wrapper<_Tp>& __inref)
415 _M_data = __inref._M_data;
419 operator _Tp&() const
420 { return this->get(); }
426 template<typename... _Args>
427 typename result_of<_M_func_type(_Args...)>::type
428 operator()(_Args&&... __args) const
430 return __invoke(get(), std::forward<_Args>(__args)...);
435 /// Denotes a reference should be taken to a variable.
436 template<typename _Tp>
437 inline reference_wrapper<_Tp>
439 { return reference_wrapper<_Tp>(__t); }
441 /// Denotes a const reference should be taken to a variable.
442 template<typename _Tp>
443 inline reference_wrapper<const _Tp>
445 { return reference_wrapper<const _Tp>(__t); }
447 /// Partial specialization.
448 template<typename _Tp>
449 inline reference_wrapper<_Tp>
450 ref(reference_wrapper<_Tp> __t)
451 { return ref(__t.get()); }
453 /// Partial specialization.
454 template<typename _Tp>
455 inline reference_wrapper<const _Tp>
456 cref(reference_wrapper<_Tp> __t)
457 { return cref(__t.get()); }
461 template<typename _Tp, bool>
462 struct _Mem_fn_const_or_non
464 typedef const _Tp& type;
467 template<typename _Tp>
468 struct _Mem_fn_const_or_non<_Tp, false>
474 * Derives from @c unary_function or @c binary_function, or perhaps
475 * nothing, depending on the number of arguments provided. The
476 * primary template is the basis case, which derives nothing.
478 template<typename _Res, typename... _ArgTypes>
479 struct _Maybe_unary_or_binary_function { };
481 /// Derives from @c unary_function, as appropriate.
482 template<typename _Res, typename _T1>
483 struct _Maybe_unary_or_binary_function<_Res, _T1>
484 : std::unary_function<_T1, _Res> { };
486 /// Derives from @c binary_function, as appropriate.
487 template<typename _Res, typename _T1, typename _T2>
488 struct _Maybe_unary_or_binary_function<_Res, _T1, _T2>
489 : std::binary_function<_T1, _T2, _Res> { };
491 /// Implementation of @c mem_fn for member function pointers.
492 template<typename _Res, typename _Class, typename... _ArgTypes>
493 class _Mem_fn<_Res (_Class::*)(_ArgTypes...)>
494 : public _Maybe_unary_or_binary_function<_Res, _Class*, _ArgTypes...>
496 typedef _Res (_Class::*_Functor)(_ArgTypes...);
498 template<typename _Tp>
500 _M_call(_Tp& __object, const volatile _Class *,
501 _ArgTypes... __args) const
502 { return (__object.*__pmf)(__args...); }
504 template<typename _Tp>
506 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
507 { return ((*__ptr).*__pmf)(__args...); }
510 typedef _Res result_type;
512 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
516 operator()(_Class& __object, _ArgTypes... __args) const
517 { return (__object.*__pmf)(__args...); }
521 operator()(_Class* __object, _ArgTypes... __args) const
522 { return (__object->*__pmf)(__args...); }
524 // Handle smart pointers, references and pointers to derived
525 template<typename _Tp>
527 operator()(_Tp& __object, _ArgTypes... __args) const
528 { return _M_call(__object, &__object, __args...); }
534 /// Implementation of @c mem_fn for const member function pointers.
535 template<typename _Res, typename _Class, typename... _ArgTypes>
536 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) const>
537 : public _Maybe_unary_or_binary_function<_Res, const _Class*,
540 typedef _Res (_Class::*_Functor)(_ArgTypes...) const;
542 template<typename _Tp>
544 _M_call(_Tp& __object, const volatile _Class *,
545 _ArgTypes... __args) const
546 { return (__object.*__pmf)(__args...); }
548 template<typename _Tp>
550 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
551 { return ((*__ptr).*__pmf)(__args...); }
554 typedef _Res result_type;
556 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
560 operator()(const _Class& __object, _ArgTypes... __args) const
561 { return (__object.*__pmf)(__args...); }
565 operator()(const _Class* __object, _ArgTypes... __args) const
566 { return (__object->*__pmf)(__args...); }
568 // Handle smart pointers, references and pointers to derived
569 template<typename _Tp>
570 _Res operator()(_Tp& __object, _ArgTypes... __args) const
571 { return _M_call(__object, &__object, __args...); }
577 /// Implementation of @c mem_fn for volatile member function pointers.
578 template<typename _Res, typename _Class, typename... _ArgTypes>
579 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) volatile>
580 : public _Maybe_unary_or_binary_function<_Res, volatile _Class*,
583 typedef _Res (_Class::*_Functor)(_ArgTypes...) volatile;
585 template<typename _Tp>
587 _M_call(_Tp& __object, const volatile _Class *,
588 _ArgTypes... __args) const
589 { return (__object.*__pmf)(__args...); }
591 template<typename _Tp>
593 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
594 { return ((*__ptr).*__pmf)(__args...); }
597 typedef _Res result_type;
599 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
603 operator()(volatile _Class& __object, _ArgTypes... __args) const
604 { return (__object.*__pmf)(__args...); }
608 operator()(volatile _Class* __object, _ArgTypes... __args) const
609 { return (__object->*__pmf)(__args...); }
611 // Handle smart pointers, references and pointers to derived
612 template<typename _Tp>
614 operator()(_Tp& __object, _ArgTypes... __args) const
615 { return _M_call(__object, &__object, __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)(__args...); }
635 template<typename _Tp>
637 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
638 { return ((*__ptr).*__pmf)(__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)(__args...); }
652 operator()(const volatile _Class* __object, _ArgTypes... __args) const
653 { return (__object->*__pmf)(__args...); }
655 // Handle smart pointers, references and pointers to derived
656 template<typename _Tp>
657 _Res operator()(_Tp& __object, _ArgTypes... __args) const
658 { return _M_call(__object, &__object, __args...); }
665 template<typename _Res, typename _Class>
666 class _Mem_fn<_Res _Class::*>
668 // This bit of genius is due to Peter Dimov, improved slightly by
670 template<typename _Tp>
672 _M_call(_Tp& __object, _Class *) const
673 { return __object.*__pm; }
675 template<typename _Tp, typename _Up>
677 _M_call(_Tp& __object, _Up * const *) const
678 { return (*__object).*__pm; }
680 template<typename _Tp, typename _Up>
682 _M_call(_Tp& __object, const _Up * const *) const
683 { return (*__object).*__pm; }
685 template<typename _Tp>
687 _M_call(_Tp& __object, const _Class *) const
688 { return __object.*__pm; }
690 template<typename _Tp>
692 _M_call(_Tp& __ptr, const volatile void*) const
693 { return (*__ptr).*__pm; }
695 template<typename _Tp> static _Tp& __get_ref();
697 template<typename _Tp>
698 static __sfinae_types::__one __check_const(_Tp&, _Class*);
699 template<typename _Tp, typename _Up>
700 static __sfinae_types::__one __check_const(_Tp&, _Up * const *);
701 template<typename _Tp, typename _Up>
702 static __sfinae_types::__two __check_const(_Tp&, const _Up * const *);
703 template<typename _Tp>
704 static __sfinae_types::__two __check_const(_Tp&, const _Class*);
705 template<typename _Tp>
706 static __sfinae_types::__two __check_const(_Tp&, const volatile void*);
709 template<typename _Tp>
711 : _Mem_fn_const_or_non<_Res,
712 (sizeof(__sfinae_types::__two)
713 == sizeof(__check_const<_Tp>(__get_ref<_Tp>(), (_Tp*)0)))>
716 template<typename _Signature>
719 template<typename _CVMem, typename _Tp>
720 struct result<_CVMem(_Tp)>
721 : public _Result_type<_Tp> { };
723 template<typename _CVMem, typename _Tp>
724 struct result<_CVMem(_Tp&)>
725 : public _Result_type<_Tp> { };
728 _Mem_fn(_Res _Class::*__pm) : __pm(__pm) { }
732 operator()(_Class& __object) const
733 { return __object.*__pm; }
736 operator()(const _Class& __object) const
737 { return __object.*__pm; }
741 operator()(_Class* __object) const
742 { return __object->*__pm; }
745 operator()(const _Class* __object) const
746 { return __object->*__pm; }
748 // Handle smart pointers and derived
749 template<typename _Tp>
750 typename _Result_type<_Tp>::type
751 operator()(_Tp& __unknown) const
752 { return _M_call(__unknown, &__unknown); }
759 * @brief Returns a function object that forwards to the member
763 template<typename _Tp, typename _Class>
764 inline _Mem_fn<_Tp _Class::*>
765 mem_fn(_Tp _Class::* __pm)
767 return _Mem_fn<_Tp _Class::*>(__pm);
771 * @brief Determines if the given type _Tp is a function object
772 * should be treated as a subexpression when evaluating calls to
773 * function objects returned by bind(). [TR1 3.6.1]
776 template<typename _Tp>
777 struct is_bind_expression
778 : public false_type { };
781 * @brief Determines if the given type _Tp is a placeholder in a
782 * bind() expression and, if so, which placeholder it is. [TR1 3.6.2]
785 template<typename _Tp>
786 struct is_placeholder
787 : public integral_constant<int, 0>
790 /// The type of placeholder objects defined by libstdc++.
791 template<int _Num> struct _Placeholder { };
793 /** @namespace std::placeholders
794 * @brief ISO C++ 0x entities sub namespace for functional.
797 * Define a large number of placeholders. There is no way to
798 * simplify this with variadic templates, because we're introducing
799 * unique names for each.
801 namespace placeholders
814 _Placeholder<10> _10;
815 _Placeholder<11> _11;
816 _Placeholder<12> _12;
817 _Placeholder<13> _13;
818 _Placeholder<14> _14;
819 _Placeholder<15> _15;
820 _Placeholder<16> _16;
821 _Placeholder<17> _17;
822 _Placeholder<18> _18;
823 _Placeholder<19> _19;
824 _Placeholder<20> _20;
825 _Placeholder<21> _21;
826 _Placeholder<22> _22;
827 _Placeholder<23> _23;
828 _Placeholder<24> _24;
829 _Placeholder<25> _25;
830 _Placeholder<26> _26;
831 _Placeholder<27> _27;
832 _Placeholder<28> _28;
833 _Placeholder<29> _29;
838 * Partial specialization of is_placeholder that provides the placeholder
839 * number for the placeholder objects defined by libstdc++.
843 struct is_placeholder<_Placeholder<_Num> >
844 : public integral_constant<int, _Num>
848 * Stores a tuple of indices. Used by bind() to extract the elements
851 template<int... _Indexes>
854 typedef _Index_tuple<_Indexes..., sizeof...(_Indexes)> __next;
857 /// Builds an _Index_tuple<0, 1, 2, ..., _Num-1>.
858 template<std::size_t _Num>
859 struct _Build_index_tuple
861 typedef typename _Build_index_tuple<_Num-1>::__type::__next __type;
865 struct _Build_index_tuple<0>
867 typedef _Index_tuple<> __type;
871 * Used by _Safe_tuple_element to indicate that there is no tuple
872 * element at this position.
874 struct _No_tuple_element;
877 * Implementation helper for _Safe_tuple_element. This primary
878 * template handles the case where it is safe to use @c
881 template<int __i, typename _Tuple, bool _IsSafe>
882 struct _Safe_tuple_element_impl
883 : tuple_element<__i, _Tuple> { };
886 * Implementation helper for _Safe_tuple_element. This partial
887 * specialization handles the case where it is not safe to use @c
888 * tuple_element. We just return @c _No_tuple_element.
890 template<int __i, typename _Tuple>
891 struct _Safe_tuple_element_impl<__i, _Tuple, false>
893 typedef _No_tuple_element type;
897 * Like tuple_element, but returns @c _No_tuple_element when
898 * tuple_element would return an error.
900 template<int __i, typename _Tuple>
901 struct _Safe_tuple_element
902 : _Safe_tuple_element_impl<__i, _Tuple,
903 (__i >= 0 && __i < tuple_size<_Tuple>::value)>
907 * Maps an argument to bind() into an actual argument to the bound
908 * function object [TR1 3.6.3/5]. Only the first parameter should
909 * be specified: the rest are used to determine among the various
910 * implementations. Note that, although this class is a function
911 * object, it isn't entirely normal because it takes only two
912 * parameters regardless of the number of parameters passed to the
913 * bind expression. The first parameter is the bound argument and
914 * the second parameter is a tuple containing references to the
915 * rest of the arguments.
917 template<typename _Arg,
918 bool _IsBindExp = is_bind_expression<_Arg>::value,
919 bool _IsPlaceholder = (is_placeholder<_Arg>::value > 0)>
923 * If the argument is reference_wrapper<_Tp>, returns the
924 * underlying reference. [TR1 3.6.3/5 bullet 1]
926 template<typename _Tp>
927 class _Mu<reference_wrapper<_Tp>, false, false>
930 typedef _Tp& result_type;
932 /* Note: This won't actually work for const volatile
933 * reference_wrappers, because reference_wrapper::get() is const
934 * but not volatile-qualified. This might be a defect in the TR.
936 template<typename _CVRef, typename _Tuple>
938 operator()(_CVRef& __arg, _Tuple&&) const volatile
939 { return __arg.get(); }
943 * If the argument is a bind expression, we invoke the underlying
944 * function object with the same cv-qualifiers as we are given and
945 * pass along all of our arguments (unwrapped). [TR1 3.6.3/5 bullet 2]
947 template<typename _Arg>
948 class _Mu<_Arg, true, false>
951 template<typename _Signature> class result;
953 // Determine the result type when we pass the arguments along. This
954 // involves passing along the cv-qualifiers placed on _Mu and
955 // unwrapping the argument bundle.
956 template<typename _CVMu, typename _CVArg, typename... _Args>
957 class result<_CVMu(_CVArg, tuple<_Args...>)>
958 : public result_of<_CVArg(_Args...)> { };
960 template<typename _CVArg, typename... _Args>
961 typename result_of<_CVArg(_Args...)>::type
962 operator()(_CVArg& __arg,
963 tuple<_Args...>&& __tuple) const volatile
965 // Construct an index tuple and forward to __call
966 typedef typename _Build_index_tuple<sizeof...(_Args)>::__type
968 return this->__call(__arg, std::move(__tuple), _Indexes());
972 // Invokes the underlying function object __arg by unpacking all
973 // of the arguments in the tuple.
974 template<typename _CVArg, typename... _Args, int... _Indexes>
975 typename result_of<_CVArg(_Args...)>::type
976 __call(_CVArg& __arg, tuple<_Args...>&& __tuple,
977 const _Index_tuple<_Indexes...>&) const volatile
979 return __arg(std::forward<_Args>(get<_Indexes>(__tuple))...);
984 * If the argument is a placeholder for the Nth argument, returns
985 * a reference to the Nth argument to the bind function object.
986 * [TR1 3.6.3/5 bullet 3]
988 template<typename _Arg>
989 class _Mu<_Arg, false, true>
992 template<typename _Signature> class result;
994 template<typename _CVMu, typename _CVArg, typename _Tuple>
995 class result<_CVMu(_CVArg, _Tuple)>
997 // Add a reference, if it hasn't already been done for us.
998 // This allows us to be a little bit sloppy in constructing
999 // the tuple that we pass to result_of<...>.
1000 typedef typename _Safe_tuple_element<(is_placeholder<_Arg>::value
1005 typedef typename add_rvalue_reference<__base_type>::type type;
1008 template<typename _Tuple>
1009 typename result<_Mu(_Arg, _Tuple)>::type
1010 operator()(const volatile _Arg&, _Tuple&& __tuple) const volatile
1012 return std::forward<typename result<_Mu(_Arg, _Tuple)>::type>(
1013 ::std::get<(is_placeholder<_Arg>::value - 1)>(__tuple));
1018 * If the argument is just a value, returns a reference to that
1019 * value. The cv-qualifiers on the reference are the same as the
1020 * cv-qualifiers on the _Mu object. [TR1 3.6.3/5 bullet 4]
1022 template<typename _Arg>
1023 class _Mu<_Arg, false, false>
1026 template<typename _Signature> struct result;
1028 template<typename _CVMu, typename _CVArg, typename _Tuple>
1029 struct result<_CVMu(_CVArg, _Tuple)>
1031 typedef typename add_lvalue_reference<_CVArg>::type type;
1034 // Pick up the cv-qualifiers of the argument
1035 template<typename _CVArg, typename _Tuple>
1037 operator()(_CVArg&& __arg, _Tuple&&) const volatile
1038 { return std::forward<_CVArg>(__arg); }
1042 * Maps member pointers into instances of _Mem_fn but leaves all
1043 * other function objects untouched. Used by tr1::bind(). The
1044 * primary template handles the non--member-pointer case.
1046 template<typename _Tp>
1047 struct _Maybe_wrap_member_pointer
1052 __do_wrap(const _Tp& __x)
1057 * Maps member pointers into instances of _Mem_fn but leaves all
1058 * other function objects untouched. Used by tr1::bind(). This
1059 * partial specialization handles the member pointer case.
1061 template<typename _Tp, typename _Class>
1062 struct _Maybe_wrap_member_pointer<_Tp _Class::*>
1064 typedef _Mem_fn<_Tp _Class::*> type;
1067 __do_wrap(_Tp _Class::* __pm)
1068 { return type(__pm); }
1071 // Specialization needed to prevent "forming reference to void" errors when
1072 // bind<void>() is called, because argument deduction instantiates
1073 // _Maybe_wrap_member_pointer<void> outside the immediate context where
1076 struct _Maybe_wrap_member_pointer<void>
1081 /// Type of the function object returned from bind().
1082 template<typename _Signature>
1085 template<typename _Functor, typename... _Bound_args>
1086 class _Bind<_Functor(_Bound_args...)>
1087 : public _Weak_result_type<_Functor>
1089 typedef _Bind __self_type;
1090 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
1094 tuple<_Bound_args...> _M_bound_args;
1097 template<typename _Result, typename... _Args, int... _Indexes>
1099 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>)
1101 return _M_f(_Mu<_Bound_args>()
1102 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1106 template<typename _Result, typename... _Args, int... _Indexes>
1108 __call_c(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>) const
1110 return _M_f(_Mu<_Bound_args>()
1111 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1116 template<typename _Result, typename... _Args, int... _Indexes>
1118 __call_v(tuple<_Args...>&& __args,
1119 _Index_tuple<_Indexes...>) volatile
1121 return _M_f(_Mu<_Bound_args>()
1122 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1125 // Call as const volatile
1126 template<typename _Result, typename... _Args, int... _Indexes>
1128 __call_c_v(tuple<_Args...>&& __args,
1129 _Index_tuple<_Indexes...>) const volatile
1131 return _M_f(_Mu<_Bound_args>()
1132 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1137 explicit _Bind(_Functor __f, _Bound_args... __bound_args)
1138 : _M_f(std::forward<_Functor>(__f)),
1139 _M_bound_args(std::forward<_Bound_args>(__bound_args)...)
1143 template<typename... _Args, typename _Result
1144 = decltype( std::declval<_Functor>()(
1145 _Mu<_Bound_args>()( std::declval<_Bound_args&>(),
1146 std::declval<tuple<_Args...>&&>() )... ) )>
1148 operator()(_Args&&... __args)
1150 return this->__call<_Result>(tuple<_Args...>
1151 (std::forward<_Args>(__args)...),
1156 template<typename... _Args, typename _Result
1157 = decltype( std::declval<const _Functor>()(
1158 _Mu<_Bound_args>()( std::declval<const _Bound_args&>(),
1159 std::declval<tuple<_Args...>&&>() )... ) )>
1161 operator()(_Args&&... __args) const
1163 return this->__call_c<_Result>(tuple<_Args...>
1164 (std::forward<_Args>(__args)...),
1170 template<typename... _Args, typename _Result
1171 = decltype( std::declval<volatile _Functor>()(
1172 _Mu<_Bound_args>()( std::declval<volatile _Bound_args&>(),
1173 std::declval<tuple<_Args...>&&>() )... ) )>
1175 operator()(_Args&&... __args) volatile
1177 return this->__call_v<_Result>(tuple<_Args...>
1178 (std::forward<_Args>(__args)...),
1182 // Call as const volatile
1183 template<typename... _Args, typename _Result
1184 = decltype( std::declval<const volatile _Functor>()(
1185 _Mu<_Bound_args>()( std::declval<const volatile _Bound_args&>(),
1186 std::declval<tuple<_Args...>&&>() )... ) )>
1188 operator()(_Args&&... __args) const volatile
1190 return this->__call_c_v<_Result>(tuple<_Args...>
1191 (std::forward<_Args>(__args)...),
1197 /// Type of the function object returned from bind<R>().
1198 template<typename _Result, typename _Signature>
1199 struct _Bind_result;
1201 template<typename _Result, typename _Functor, typename... _Bound_args>
1202 class _Bind_result<_Result, _Functor(_Bound_args...)>
1204 typedef _Bind_result __self_type;
1205 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
1209 tuple<_Bound_args...> _M_bound_args;
1212 template<typename _Res>
1213 struct __enable_if_void : enable_if<is_void<_Res>::value, int> { };
1214 template<typename _Res>
1215 struct __disable_if_void : enable_if<!is_void<_Res>::value, int> { };
1218 template<typename _Res, typename... _Args, int... _Indexes>
1220 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1221 typename __disable_if_void<_Res>::type = 0)
1223 return _M_f(_Mu<_Bound_args>()
1224 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1227 // Call unqualified, 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)
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) const
1243 return _M_f(_Mu<_Bound_args>()
1244 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1247 // Call as const, 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) const
1253 _M_f(_Mu<_Bound_args>()
1254 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1258 template<typename _Res, typename... _Args, int... _Indexes>
1260 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1261 typename __disable_if_void<_Res>::type = 0) volatile
1263 return _M_f(_Mu<_Bound_args>()
1264 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1267 // Call as volatile, return void
1268 template<typename _Res, typename... _Args, int... _Indexes>
1270 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1271 typename __enable_if_void<_Res>::type = 0) volatile
1273 _M_f(_Mu<_Bound_args>()
1274 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1277 // Call as const volatile
1278 template<typename _Res, typename... _Args, int... _Indexes>
1280 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1281 typename __disable_if_void<_Res>::type = 0) const volatile
1283 return _M_f(_Mu<_Bound_args>()
1284 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1287 // Call as const volatile, return void
1288 template<typename _Res, typename... _Args, int... _Indexes>
1290 __call(tuple<_Args...>&& __args,
1291 _Index_tuple<_Indexes...>,
1292 typename __enable_if_void<_Res>::type = 0) const volatile
1294 _M_f(_Mu<_Bound_args>()
1295 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1299 typedef _Result result_type;
1302 _Bind_result(_Functor __f, _Bound_args... __bound_args)
1303 : _M_f(std::forward<_Functor>(__f)),
1304 _M_bound_args(std::forward<_Bound_args>(__bound_args)...)
1308 template<typename... _Args>
1310 operator()(_Args&&... __args)
1312 return this->__call<_Result>(
1313 tuple<_Args...>(std::forward<_Args...>(__args)...),
1318 template<typename... _Args>
1320 operator()(_Args&&... __args) const
1322 return this->__call<_Result>(
1323 tuple<_Args...>(std::forward<_Args...>(__args)...),
1328 template<typename... _Args>
1330 operator()(_Args&&... __args) volatile
1332 return this->__call<_Result>(
1333 tuple<_Args...>(std::forward<_Args...>(__args)...),
1337 // Call as const volatile
1338 template<typename... _Args>
1340 operator()(_Args&&... __args) const volatile
1342 return this->__call<_Result>(
1343 tuple<_Args...>(std::forward<_Args...>(__args)...),
1349 * @brief Class template _Bind is always a bind expression.
1352 template<typename _Signature>
1353 struct is_bind_expression<_Bind<_Signature> >
1354 : public true_type { };
1357 * @brief Class template _Bind is always a bind expression.
1360 template<typename _Result, typename _Signature>
1361 struct is_bind_expression<_Bind_result<_Result, _Signature> >
1362 : public true_type { };
1365 * @brief Function template for std::bind.
1368 template<typename _Functor, typename... _ArgTypes>
1370 _Bind<typename _Maybe_wrap_member_pointer<_Functor>::type(_ArgTypes...)>
1371 bind(_Functor __f, _ArgTypes... __args)
1373 typedef _Maybe_wrap_member_pointer<_Functor> __maybe_type;
1374 typedef typename __maybe_type::type __functor_type;
1375 typedef _Bind<__functor_type(_ArgTypes...)> __result_type;
1376 return __result_type(__maybe_type::__do_wrap(__f),
1377 std::forward<_ArgTypes>(__args)...);
1381 * @brief Function template for std::bind.
1384 template<typename _Result, typename _Functor, typename... _ArgTypes>
1386 _Bind_result<_Result,
1387 typename _Maybe_wrap_member_pointer<_Functor>::type
1389 bind(_Functor __f, _ArgTypes... __args)
1391 typedef _Maybe_wrap_member_pointer<_Functor> __maybe_type;
1392 typedef typename __maybe_type::type __functor_type;
1393 typedef _Bind_result<_Result, __functor_type(_ArgTypes...)>
1395 return __result_type(__maybe_type::__do_wrap(__f),
1396 std::forward<_ArgTypes>(__args)...);
1400 * @brief Exception class thrown when class template function's
1401 * operator() is called with an empty target.
1402 * @ingroup exceptions
1404 class bad_function_call : public std::exception { };
1407 * The integral constant expression 0 can be converted into a
1408 * pointer to this type. It is used by the function template to
1409 * accept NULL pointers.
1411 struct _M_clear_type;
1414 * Trait identifying "location-invariant" types, meaning that the
1415 * address of the object (or any of its members) will not escape.
1416 * Also implies a trivial copy constructor and assignment operator.
1418 template<typename _Tp>
1419 struct __is_location_invariant
1420 : integral_constant<bool, (is_pointer<_Tp>::value
1421 || is_member_pointer<_Tp>::value)>
1424 class _Undefined_class;
1429 const void* _M_const_object;
1430 void (*_M_function_pointer)();
1431 void (_Undefined_class::*_M_member_pointer)();
1436 void* _M_access() { return &_M_pod_data[0]; }
1437 const void* _M_access() const { return &_M_pod_data[0]; }
1439 template<typename _Tp>
1442 { return *static_cast<_Tp*>(_M_access()); }
1444 template<typename _Tp>
1447 { return *static_cast<const _Tp*>(_M_access()); }
1449 _Nocopy_types _M_unused;
1450 char _M_pod_data[sizeof(_Nocopy_types)];
1453 enum _Manager_operation
1461 // Simple type wrapper that helps avoid annoying const problems
1462 // when casting between void pointers and pointers-to-pointers.
1463 template<typename _Tp>
1464 struct _Simple_type_wrapper
1466 _Simple_type_wrapper(_Tp __value) : __value(__value) { }
1471 template<typename _Tp>
1472 struct __is_location_invariant<_Simple_type_wrapper<_Tp> >
1473 : __is_location_invariant<_Tp>
1476 // Converts a reference to a function object into a callable
1478 template<typename _Functor>
1480 __callable_functor(_Functor& __f)
1483 template<typename _Member, typename _Class>
1484 inline _Mem_fn<_Member _Class::*>
1485 __callable_functor(_Member _Class::* &__p)
1486 { return mem_fn(__p); }
1488 template<typename _Member, typename _Class>
1489 inline _Mem_fn<_Member _Class::*>
1490 __callable_functor(_Member _Class::* const &__p)
1491 { return mem_fn(__p); }
1493 template<typename _Signature>
1496 /// Base class of all polymorphic function object wrappers.
1497 class _Function_base
1500 static const std::size_t _M_max_size = sizeof(_Nocopy_types);
1501 static const std::size_t _M_max_align = __alignof__(_Nocopy_types);
1503 template<typename _Functor>
1507 static const bool __stored_locally =
1508 (__is_location_invariant<_Functor>::value
1509 && sizeof(_Functor) <= _M_max_size
1510 && __alignof__(_Functor) <= _M_max_align
1511 && (_M_max_align % __alignof__(_Functor) == 0));
1513 typedef integral_constant<bool, __stored_locally> _Local_storage;
1515 // Retrieve a pointer to the function object
1517 _M_get_pointer(const _Any_data& __source)
1519 const _Functor* __ptr =
1520 __stored_locally? &__source._M_access<_Functor>()
1521 /* have stored a pointer */ : __source._M_access<_Functor*>();
1522 return const_cast<_Functor*>(__ptr);
1525 // Clone a location-invariant function object that fits within
1526 // an _Any_data structure.
1528 _M_clone(_Any_data& __dest, const _Any_data& __source, true_type)
1530 new (__dest._M_access()) _Functor(__source._M_access<_Functor>());
1533 // Clone a function object that is not location-invariant or
1534 // that cannot fit into an _Any_data structure.
1536 _M_clone(_Any_data& __dest, const _Any_data& __source, false_type)
1538 __dest._M_access<_Functor*>() =
1539 new _Functor(*__source._M_access<_Functor*>());
1542 // Destroying a location-invariant object may still require
1545 _M_destroy(_Any_data& __victim, true_type)
1547 __victim._M_access<_Functor>().~_Functor();
1550 // Destroying an object located on the heap.
1552 _M_destroy(_Any_data& __victim, false_type)
1554 delete __victim._M_access<_Functor*>();
1559 _M_manager(_Any_data& __dest, const _Any_data& __source,
1560 _Manager_operation __op)
1565 case __get_type_info:
1566 __dest._M_access<const type_info*>() = &typeid(_Functor);
1569 case __get_functor_ptr:
1570 __dest._M_access<_Functor*>() = _M_get_pointer(__source);
1573 case __clone_functor:
1574 _M_clone(__dest, __source, _Local_storage());
1577 case __destroy_functor:
1578 _M_destroy(__dest, _Local_storage());
1585 _M_init_functor(_Any_data& __functor, _Functor&& __f)
1586 { _M_init_functor(__functor, std::move(__f), _Local_storage()); }
1588 template<typename _Signature>
1590 _M_not_empty_function(const function<_Signature>& __f)
1591 { return static_cast<bool>(__f); }
1593 template<typename _Tp>
1595 _M_not_empty_function(const _Tp*& __fp)
1598 template<typename _Class, typename _Tp>
1600 _M_not_empty_function(_Tp _Class::* const& __mp)
1603 template<typename _Tp>
1605 _M_not_empty_function(const _Tp&)
1610 _M_init_functor(_Any_data& __functor, _Functor&& __f, true_type)
1611 { new (__functor._M_access()) _Functor(std::move(__f)); }
1614 _M_init_functor(_Any_data& __functor, _Functor&& __f, false_type)
1615 { __functor._M_access<_Functor*>() = new _Functor(std::move(__f)); }
1618 template<typename _Functor>
1619 class _Ref_manager : public _Base_manager<_Functor*>
1621 typedef _Function_base::_Base_manager<_Functor*> _Base;
1625 _M_manager(_Any_data& __dest, const _Any_data& __source,
1626 _Manager_operation __op)
1631 case __get_type_info:
1632 __dest._M_access<const type_info*>() = &typeid(_Functor);
1635 case __get_functor_ptr:
1636 __dest._M_access<_Functor*>() = *_Base::_M_get_pointer(__source);
1637 return is_const<_Functor>::value;
1641 _Base::_M_manager(__dest, __source, __op);
1647 _M_init_functor(_Any_data& __functor, reference_wrapper<_Functor> __f)
1649 // TBD: Use address_of function instead.
1650 _Base::_M_init_functor(__functor, &__f.get());
1654 _Function_base() : _M_manager(0) { }
1659 _M_manager(_M_functor, _M_functor, __destroy_functor);
1663 bool _M_empty() const { return !_M_manager; }
1665 typedef bool (*_Manager_type)(_Any_data&, const _Any_data&,
1666 _Manager_operation);
1668 _Any_data _M_functor;
1669 _Manager_type _M_manager;
1672 template<typename _Signature, typename _Functor>
1673 class _Function_handler;
1675 template<typename _Res, typename _Functor, typename... _ArgTypes>
1676 class _Function_handler<_Res(_ArgTypes...), _Functor>
1677 : public _Function_base::_Base_manager<_Functor>
1679 typedef _Function_base::_Base_manager<_Functor> _Base;
1683 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1685 return (*_Base::_M_get_pointer(__functor))(
1686 std::forward<_ArgTypes>(__args)...);
1690 template<typename _Functor, typename... _ArgTypes>
1691 class _Function_handler<void(_ArgTypes...), _Functor>
1692 : public _Function_base::_Base_manager<_Functor>
1694 typedef _Function_base::_Base_manager<_Functor> _Base;
1698 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1700 (*_Base::_M_get_pointer(__functor))(
1701 std::forward<_ArgTypes>(__args)...);
1705 template<typename _Res, typename _Functor, typename... _ArgTypes>
1706 class _Function_handler<_Res(_ArgTypes...), reference_wrapper<_Functor> >
1707 : public _Function_base::_Ref_manager<_Functor>
1709 typedef _Function_base::_Ref_manager<_Functor> _Base;
1713 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1715 return __callable_functor(**_Base::_M_get_pointer(__functor))(
1716 std::forward<_ArgTypes>(__args)...);
1720 template<typename _Functor, typename... _ArgTypes>
1721 class _Function_handler<void(_ArgTypes...), reference_wrapper<_Functor> >
1722 : public _Function_base::_Ref_manager<_Functor>
1724 typedef _Function_base::_Ref_manager<_Functor> _Base;
1728 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1730 __callable_functor(**_Base::_M_get_pointer(__functor))(
1731 std::forward<_ArgTypes>(__args)...);
1735 template<typename _Class, typename _Member, typename _Res,
1736 typename... _ArgTypes>
1737 class _Function_handler<_Res(_ArgTypes...), _Member _Class::*>
1738 : public _Function_handler<void(_ArgTypes...), _Member _Class::*>
1740 typedef _Function_handler<void(_ArgTypes...), _Member _Class::*>
1745 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1747 return mem_fn(_Base::_M_get_pointer(__functor)->__value)(
1748 std::forward<_ArgTypes>(__args)...);
1752 template<typename _Class, typename _Member, typename... _ArgTypes>
1753 class _Function_handler<void(_ArgTypes...), _Member _Class::*>
1754 : public _Function_base::_Base_manager<
1755 _Simple_type_wrapper< _Member _Class::* > >
1757 typedef _Member _Class::* _Functor;
1758 typedef _Simple_type_wrapper<_Functor> _Wrapper;
1759 typedef _Function_base::_Base_manager<_Wrapper> _Base;
1763 _M_manager(_Any_data& __dest, const _Any_data& __source,
1764 _Manager_operation __op)
1769 case __get_type_info:
1770 __dest._M_access<const type_info*>() = &typeid(_Functor);
1773 case __get_functor_ptr:
1774 __dest._M_access<_Functor*>() =
1775 &_Base::_M_get_pointer(__source)->__value;
1779 _Base::_M_manager(__dest, __source, __op);
1785 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1787 mem_fn(_Base::_M_get_pointer(__functor)->__value)(
1788 std::forward<_ArgTypes>(__args)...);
1793 * @brief Primary class template for std::function.
1796 * Polymorphic function wrapper.
1798 template<typename _Res, typename... _ArgTypes>
1799 class function<_Res(_ArgTypes...)>
1800 : public _Maybe_unary_or_binary_function<_Res, _ArgTypes...>,
1801 private _Function_base
1803 typedef _Res _Signature_type(_ArgTypes...);
1805 struct _Useless { };
1808 typedef _Res result_type;
1810 // [3.7.2.1] construct/copy/destroy
1813 * @brief Default construct creates an empty function call wrapper.
1814 * @post @c !(bool)*this
1817 function() : _Function_base() { }
1820 * @brief Default construct creates an empty function call wrapper.
1821 * @post @c !(bool)*this
1823 function(_M_clear_type*) : _Function_base() { }
1826 * @brief %Function copy constructor.
1827 * @param x A %function object with identical call signature.
1828 * @post @c (bool)*this == (bool)x
1830 * The newly-created %function contains a copy of the target of @a
1831 * x (if it has one).
1833 function(const function& __x);
1836 * @brief %Function move constructor.
1837 * @param x A %function object rvalue with identical call signature.
1839 * The newly-created %function contains the target of @a x
1842 function(function&& __x) : _Function_base()
1847 // TODO: needs allocator_arg_t
1850 * @brief Builds a %function that targets a copy of the incoming
1852 * @param f A %function object that is callable with parameters of
1853 * type @c T1, @c T2, ..., @c TN and returns a value convertible
1856 * The newly-created %function object will target a copy of @a
1857 * f. If @a f is @c reference_wrapper<F>, then this function
1858 * object will contain a reference to the function object @c
1859 * f.get(). If @a f is a NULL function pointer or NULL
1860 * pointer-to-member, the newly-created object will be empty.
1862 * If @a f is a non-NULL function pointer or an object of type @c
1863 * reference_wrapper<F>, this function will not throw.
1865 template<typename _Functor>
1866 function(_Functor __f,
1868 !is_integral<_Functor>::value, _Useless>::type
1872 * @brief %Function assignment operator.
1873 * @param x A %function with identical call signature.
1874 * @post @c (bool)*this == (bool)x
1877 * The target of @a x is copied to @c *this. If @a x has no
1878 * target, then @c *this will be empty.
1880 * If @a x targets a function pointer or a reference to a function
1881 * object, then this operation will not throw an %exception.
1884 operator=(const function& __x)
1886 function(__x).swap(*this);
1891 * @brief %Function move-assignment operator.
1892 * @param x A %function rvalue with identical call signature.
1895 * The target of @a x is moved to @c *this. If @a x has no
1896 * target, then @c *this will be empty.
1898 * If @a x targets a function pointer or a reference to a function
1899 * object, then this operation will not throw an %exception.
1902 operator=(function&& __x)
1904 function(std::move(__x)).swap(*this);
1909 * @brief %Function assignment to zero.
1910 * @post @c !(bool)*this
1913 * The target of @c *this is deallocated, leaving it empty.
1916 operator=(_M_clear_type*)
1920 _M_manager(_M_functor, _M_functor, __destroy_functor);
1928 * @brief %Function assignment to a new target.
1929 * @param f A %function object that is callable with parameters of
1930 * type @c T1, @c T2, ..., @c TN and returns a value convertible
1934 * This %function object wrapper will target a copy of @a
1935 * f. If @a f is @c reference_wrapper<F>, then this function
1936 * object will contain a reference to the function object @c
1937 * f.get(). If @a f is a NULL function pointer or NULL
1938 * pointer-to-member, @c this object will be empty.
1940 * If @a f is a non-NULL function pointer or an object of type @c
1941 * reference_wrapper<F>, this function will not throw.
1943 template<typename _Functor>
1944 typename enable_if<!is_integral<_Functor>::value, function&>::type
1945 operator=(_Functor&& __f)
1947 function(std::forward<_Functor>(__f)).swap(*this);
1952 template<typename _Functor>
1953 typename enable_if<!is_integral<_Functor>::value, function&>::type
1954 operator=(reference_wrapper<_Functor> __f)
1956 function(__f).swap(*this);
1960 // [3.7.2.2] function modifiers
1963 * @brief Swap the targets of two %function objects.
1964 * @param f A %function with identical call signature.
1966 * Swap the targets of @c this function object and @a f. This
1967 * function will not throw an %exception.
1969 void swap(function& __x)
1971 /* We cannot perform direct assignments of the _M_functor
1972 parts as they are of type _Any_data and have a different
1973 dynamic type. Doing so would violate type-based aliasing
1974 rules and lead to spurious miscompilations.
1975 Instead perform a bytewise exchange of the memory of
1977 ??? A wordwise exchange honoring alignment of _M_functor
1978 would be more efficient. See PR42845. */
1979 for (unsigned i = 0; i < sizeof (_M_functor._M_pod_data); ++i)
1980 std::swap (_M_functor._M_pod_data[i], __x._M_functor._M_pod_data[i]);
1981 _Manager_type __old_manager = _M_manager;
1982 _M_manager = __x._M_manager;
1983 __x._M_manager = __old_manager;
1984 _Invoker_type __old_invoker = _M_invoker;
1985 _M_invoker = __x._M_invoker;
1986 __x._M_invoker = __old_invoker;
1989 // TODO: needs allocator_arg_t
1991 template<typename _Functor, typename _Alloc>
1993 assign(_Functor&& __f, const _Alloc& __a)
1995 function(allocator_arg, __a,
1996 std::forward<_Functor>(__f)).swap(*this);
2000 // [3.7.2.3] function capacity
2003 * @brief Determine if the %function wrapper has a target.
2005 * @return @c true when this %function object contains a target,
2006 * or @c false when it is empty.
2008 * This function will not throw an %exception.
2010 explicit operator bool() const
2011 { return !_M_empty(); }
2013 // [3.7.2.4] function invocation
2016 * @brief Invokes the function targeted by @c *this.
2017 * @returns the result of the target.
2018 * @throws bad_function_call when @c !(bool)*this
2020 * The function call operator invokes the target function object
2021 * stored by @c this.
2023 _Res operator()(_ArgTypes... __args) const;
2026 // [3.7.2.5] function target access
2028 * @brief Determine the type of the target of this function object
2031 * @returns the type identifier of the target function object, or
2032 * @c typeid(void) if @c !(bool)*this.
2034 * This function will not throw an %exception.
2036 const type_info& target_type() const;
2039 * @brief Access the stored target function object.
2041 * @return Returns a pointer to the stored target function object,
2042 * if @c typeid(Functor).equals(target_type()); otherwise, a NULL
2045 * This function will not throw an %exception.
2047 template<typename _Functor> _Functor* target();
2050 template<typename _Functor> const _Functor* target() const;
2053 // deleted overloads
2054 template<typename _Res2, typename... _ArgTypes2>
2055 void operator==(const function<_Res2(_ArgTypes2...)>&) const = delete;
2056 template<typename _Res2, typename... _ArgTypes2>
2057 void operator!=(const function<_Res2(_ArgTypes2...)>&) const = delete;
2060 typedef _Res (*_Invoker_type)(const _Any_data&, _ArgTypes...);
2061 _Invoker_type _M_invoker;
2064 // Out-of-line member definitions.
2065 template<typename _Res, typename... _ArgTypes>
2066 function<_Res(_ArgTypes...)>::
2067 function(const function& __x)
2070 if (static_cast<bool>(__x))
2072 _M_invoker = __x._M_invoker;
2073 _M_manager = __x._M_manager;
2074 __x._M_manager(_M_functor, __x._M_functor, __clone_functor);
2078 template<typename _Res, typename... _ArgTypes>
2079 template<typename _Functor>
2080 function<_Res(_ArgTypes...)>::
2081 function(_Functor __f,
2083 !is_integral<_Functor>::value, _Useless>::type)
2086 typedef _Function_handler<_Signature_type, _Functor> _My_handler;
2088 if (_My_handler::_M_not_empty_function(__f))
2090 _M_invoker = &_My_handler::_M_invoke;
2091 _M_manager = &_My_handler::_M_manager;
2092 _My_handler::_M_init_functor(_M_functor, std::move(__f));
2096 template<typename _Res, typename... _ArgTypes>
2098 function<_Res(_ArgTypes...)>::
2099 operator()(_ArgTypes... __args) const
2102 __throw_bad_function_call();
2103 return _M_invoker(_M_functor, std::forward<_ArgTypes>(__args)...);
2107 template<typename _Res, typename... _ArgTypes>
2109 function<_Res(_ArgTypes...)>::
2114 _Any_data __typeinfo_result;
2115 _M_manager(__typeinfo_result, _M_functor, __get_type_info);
2116 return *__typeinfo_result._M_access<const type_info*>();
2119 return typeid(void);
2122 template<typename _Res, typename... _ArgTypes>
2123 template<typename _Functor>
2125 function<_Res(_ArgTypes...)>::
2128 if (typeid(_Functor) == target_type() && _M_manager)
2131 if (_M_manager(__ptr, _M_functor, __get_functor_ptr)
2132 && !is_const<_Functor>::value)
2135 return __ptr._M_access<_Functor*>();
2141 template<typename _Res, typename... _ArgTypes>
2142 template<typename _Functor>
2144 function<_Res(_ArgTypes...)>::
2147 if (typeid(_Functor) == target_type() && _M_manager)
2150 _M_manager(__ptr, _M_functor, __get_functor_ptr);
2151 return __ptr._M_access<const _Functor*>();
2158 // [20.7.15.2.6] null pointer comparisons
2161 * @brief Compares a polymorphic function object wrapper against 0
2162 * (the NULL pointer).
2163 * @returns @c true if the wrapper has no target, @c false otherwise
2165 * This function will not throw an %exception.
2167 template<typename _Res, typename... _Args>
2169 operator==(const function<_Res(_Args...)>& __f, _M_clear_type*)
2170 { return !static_cast<bool>(__f); }
2173 template<typename _Res, typename... _Args>
2175 operator==(_M_clear_type*, const function<_Res(_Args...)>& __f)
2176 { return !static_cast<bool>(__f); }
2179 * @brief Compares a polymorphic function object wrapper against 0
2180 * (the NULL pointer).
2181 * @returns @c false if the wrapper has no target, @c true otherwise
2183 * This function will not throw an %exception.
2185 template<typename _Res, typename... _Args>
2187 operator!=(const function<_Res(_Args...)>& __f, _M_clear_type*)
2188 { return static_cast<bool>(__f); }
2191 template<typename _Res, typename... _Args>
2193 operator!=(_M_clear_type*, const function<_Res(_Args...)>& __f)
2194 { return static_cast<bool>(__f); }
2196 // [20.7.15.2.7] specialized algorithms
2199 * @brief Swap the targets of two polymorphic function object wrappers.
2201 * This function will not throw an %exception.
2203 template<typename _Res, typename... _Args>
2205 swap(function<_Res(_Args...)>& __x, function<_Res(_Args...)>& __y)
2209 #endif // __GXX_EXPERIMENTAL_CXX0X__
2211 #endif // _GLIBCXX_FUNCTIONAL