1 // <functional> -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 // Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 // <http://www.gnu.org/licenses/>.
28 * Silicon Graphics Computer Systems, Inc.
30 * Permission to use, copy, modify, distribute and sell this software
31 * and its documentation for any purpose is hereby granted without fee,
32 * provided that the above copyright notice appear in all copies and
33 * that both that copyright notice and this permission notice appear
34 * in supporting documentation. Silicon Graphics makes no
35 * representations about the suitability of this software for any
36 * purpose. It is provided "as is" without express or implied warranty.
40 /** @file include/functional
41 * This is a Standard C++ Library header.
44 #ifndef _GLIBCXX_FUNCTIONAL
45 #define _GLIBCXX_FUNCTIONAL 1
47 #pragma GCC system_header
49 #include <bits/c++config.h>
50 #include <bits/stl_function.h>
52 #ifdef __GXX_EXPERIMENTAL_CXX0X__
57 #include <type_traits>
58 #include <bits/functexcept.h>
59 #include <bits/functional_hash.h>
63 template<typename _MemberPointer>
67 * Actual implementation of _Has_result_type, which uses SFINAE to
68 * determine if the type _Tp has a publicly-accessible member type
71 template<typename _Tp>
72 class _Has_result_type_helper : __sfinae_types
74 template<typename _Up>
78 template<typename _Up>
79 static __one __test(_Wrap_type<typename _Up::result_type>*);
81 template<typename _Up>
82 static __two __test(...);
85 static const bool value = sizeof(__test<_Tp>(0)) == 1;
88 template<typename _Tp>
89 struct _Has_result_type
90 : integral_constant<bool,
91 _Has_result_type_helper<typename remove_cv<_Tp>::type>::value>
94 /// If we have found a result_type, extract it.
95 template<bool _Has_result_type, typename _Functor>
96 struct _Maybe_get_result_type
99 template<typename _Functor>
100 struct _Maybe_get_result_type<true, _Functor>
102 typedef typename _Functor::result_type result_type;
106 * Base class for any function object that has a weak result type, as
107 * defined in 3.3/3 of TR1.
109 template<typename _Functor>
110 struct _Weak_result_type_impl
111 : _Maybe_get_result_type<_Has_result_type<_Functor>::value, _Functor>
114 /// Retrieve the result type for a function type.
115 template<typename _Res, typename... _ArgTypes>
116 struct _Weak_result_type_impl<_Res(_ArgTypes...)>
118 typedef _Res result_type;
121 /// Retrieve the result type for a function reference.
122 template<typename _Res, typename... _ArgTypes>
123 struct _Weak_result_type_impl<_Res(&)(_ArgTypes...)>
125 typedef _Res result_type;
128 /// Retrieve the result type for a function pointer.
129 template<typename _Res, typename... _ArgTypes>
130 struct _Weak_result_type_impl<_Res(*)(_ArgTypes...)>
132 typedef _Res result_type;
135 /// Retrieve result type for a member function pointer.
136 template<typename _Res, typename _Class, typename... _ArgTypes>
137 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...)>
139 typedef _Res result_type;
142 /// Retrieve result type for a const member function pointer.
143 template<typename _Res, typename _Class, typename... _ArgTypes>
144 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) const>
146 typedef _Res result_type;
149 /// Retrieve result type for a volatile member function pointer.
150 template<typename _Res, typename _Class, typename... _ArgTypes>
151 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) volatile>
153 typedef _Res result_type;
156 /// Retrieve result type for a const volatile member function pointer.
157 template<typename _Res, typename _Class, typename... _ArgTypes>
158 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...)const volatile>
160 typedef _Res result_type;
164 * Strip top-level cv-qualifiers from the function object and let
165 * _Weak_result_type_impl perform the real work.
167 template<typename _Functor>
168 struct _Weak_result_type
169 : _Weak_result_type_impl<typename remove_cv<_Functor>::type>
172 template<typename _Signature>
175 template<typename _Functor, typename... _ArgTypes>
176 struct result_of<_Functor(_ArgTypes...)>
179 decltype( std::declval<_Functor>()(std::declval<_ArgTypes>()...) )
183 /// Determines if the type _Tp derives from unary_function.
184 template<typename _Tp>
185 struct _Derives_from_unary_function : __sfinae_types
188 template<typename _T1, typename _Res>
189 static __one __test(const volatile unary_function<_T1, _Res>*);
191 // It's tempting to change "..." to const volatile void*, but
192 // that fails when _Tp is a function type.
193 static __two __test(...);
196 static const bool value = sizeof(__test((_Tp*)0)) == 1;
199 /// Determines if the type _Tp derives from binary_function.
200 template<typename _Tp>
201 struct _Derives_from_binary_function : __sfinae_types
204 template<typename _T1, typename _T2, typename _Res>
205 static __one __test(const volatile binary_function<_T1, _T2, _Res>*);
207 // It's tempting to change "..." to const volatile void*, but
208 // that fails when _Tp is a function type.
209 static __two __test(...);
212 static const bool value = sizeof(__test((_Tp*)0)) == 1;
215 /// Turns a function type into a function pointer type
216 template<typename _Tp, bool _IsFunctionType = is_function<_Tp>::value>
217 struct _Function_to_function_pointer
222 template<typename _Tp>
223 struct _Function_to_function_pointer<_Tp, true>
229 * Invoke a function object, which may be either a member pointer or a
230 * function object. The first parameter will tell which.
232 template<typename _Functor, typename... _Args>
235 (!is_member_pointer<_Functor>::value
236 && !is_function<_Functor>::value
237 && !is_function<typename remove_pointer<_Functor>::type>::value),
238 typename result_of<_Functor(_Args...)>::type
240 __invoke(_Functor& __f, _Args&&... __args)
242 return __f(std::forward<_Args>(__args)...);
245 // To pick up function references (that will become function pointers)
246 template<typename _Functor, typename... _Args>
249 (is_pointer<_Functor>::value
250 && is_function<typename remove_pointer<_Functor>::type>::value),
251 typename result_of<_Functor(_Args...)>::type
253 __invoke(_Functor __f, _Args&&... __args)
255 return __f(std::forward<_Args>(__args)...);
259 * Knowing which of unary_function and binary_function _Tp derives
260 * from, derives from the same and ensures that reference_wrapper
261 * will have a weak result type. See cases below.
263 template<bool _Unary, bool _Binary, typename _Tp>
264 struct _Reference_wrapper_base_impl;
266 // Not a unary_function or binary_function, so try a weak result type.
267 template<typename _Tp>
268 struct _Reference_wrapper_base_impl<false, false, _Tp>
269 : _Weak_result_type<_Tp>
272 // unary_function but not binary_function
273 template<typename _Tp>
274 struct _Reference_wrapper_base_impl<true, false, _Tp>
275 : unary_function<typename _Tp::argument_type,
276 typename _Tp::result_type>
279 // binary_function but not unary_function
280 template<typename _Tp>
281 struct _Reference_wrapper_base_impl<false, true, _Tp>
282 : binary_function<typename _Tp::first_argument_type,
283 typename _Tp::second_argument_type,
284 typename _Tp::result_type>
287 // Both unary_function and binary_function. Import result_type to
289 template<typename _Tp>
290 struct _Reference_wrapper_base_impl<true, true, _Tp>
291 : unary_function<typename _Tp::argument_type,
292 typename _Tp::result_type>,
293 binary_function<typename _Tp::first_argument_type,
294 typename _Tp::second_argument_type,
295 typename _Tp::result_type>
297 typedef typename _Tp::result_type result_type;
301 * Derives from unary_function or binary_function when it
302 * can. Specializations handle all of the easy cases. The primary
303 * template determines what to do with a class type, which may
304 * derive from both unary_function and binary_function.
306 template<typename _Tp>
307 struct _Reference_wrapper_base
308 : _Reference_wrapper_base_impl<
309 _Derives_from_unary_function<_Tp>::value,
310 _Derives_from_binary_function<_Tp>::value,
314 // - a function type (unary)
315 template<typename _Res, typename _T1>
316 struct _Reference_wrapper_base<_Res(_T1)>
317 : unary_function<_T1, _Res>
320 // - a function type (binary)
321 template<typename _Res, typename _T1, typename _T2>
322 struct _Reference_wrapper_base<_Res(_T1, _T2)>
323 : binary_function<_T1, _T2, _Res>
326 // - a function pointer type (unary)
327 template<typename _Res, typename _T1>
328 struct _Reference_wrapper_base<_Res(*)(_T1)>
329 : unary_function<_T1, _Res>
332 // - a function pointer type (binary)
333 template<typename _Res, typename _T1, typename _T2>
334 struct _Reference_wrapper_base<_Res(*)(_T1, _T2)>
335 : binary_function<_T1, _T2, _Res>
338 // - a pointer to member function type (unary, no qualifiers)
339 template<typename _Res, typename _T1>
340 struct _Reference_wrapper_base<_Res (_T1::*)()>
341 : unary_function<_T1*, _Res>
344 // - a pointer to member function type (binary, no qualifiers)
345 template<typename _Res, typename _T1, typename _T2>
346 struct _Reference_wrapper_base<_Res (_T1::*)(_T2)>
347 : binary_function<_T1*, _T2, _Res>
350 // - a pointer to member function type (unary, const)
351 template<typename _Res, typename _T1>
352 struct _Reference_wrapper_base<_Res (_T1::*)() const>
353 : unary_function<const _T1*, _Res>
356 // - a pointer to member function type (binary, const)
357 template<typename _Res, typename _T1, typename _T2>
358 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const>
359 : binary_function<const _T1*, _T2, _Res>
362 // - a pointer to member function type (unary, volatile)
363 template<typename _Res, typename _T1>
364 struct _Reference_wrapper_base<_Res (_T1::*)() volatile>
365 : unary_function<volatile _T1*, _Res>
368 // - a pointer to member function type (binary, volatile)
369 template<typename _Res, typename _T1, typename _T2>
370 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) volatile>
371 : binary_function<volatile _T1*, _T2, _Res>
374 // - a pointer to member function type (unary, const volatile)
375 template<typename _Res, typename _T1>
376 struct _Reference_wrapper_base<_Res (_T1::*)() const volatile>
377 : unary_function<const volatile _T1*, _Res>
380 // - a pointer to member function type (binary, const volatile)
381 template<typename _Res, typename _T1, typename _T2>
382 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const volatile>
383 : binary_function<const volatile _T1*, _T2, _Res>
387 * @brief Primary class template for reference_wrapper.
391 template<typename _Tp>
392 class reference_wrapper
393 : public _Reference_wrapper_base<typename remove_cv<_Tp>::type>
395 // If _Tp is a function type, we can't form result_of<_Tp(...)>,
396 // so turn it into a function pointer type.
397 typedef typename _Function_to_function_pointer<_Tp>::type
404 reference_wrapper(_Tp& __indata): _M_data(&__indata)
407 reference_wrapper(_Tp&&) = delete;
409 reference_wrapper(const reference_wrapper<_Tp>& __inref):
410 _M_data(__inref._M_data)
414 operator=(const reference_wrapper<_Tp>& __inref)
416 _M_data = __inref._M_data;
420 operator _Tp&() const
421 { return this->get(); }
427 template<typename... _Args>
428 typename result_of<_M_func_type(_Args...)>::type
429 operator()(_Args&&... __args) const
431 return __invoke(get(), std::forward<_Args>(__args)...);
436 /// Denotes a reference should be taken to a variable.
437 template<typename _Tp>
438 inline reference_wrapper<_Tp>
440 { return reference_wrapper<_Tp>(__t); }
442 /// Denotes a const reference should be taken to a variable.
443 template<typename _Tp>
444 inline reference_wrapper<const _Tp>
446 { return reference_wrapper<const _Tp>(__t); }
448 /// Partial specialization.
449 template<typename _Tp>
450 inline reference_wrapper<_Tp>
451 ref(reference_wrapper<_Tp> __t)
452 { return ref(__t.get()); }
454 /// Partial specialization.
455 template<typename _Tp>
456 inline reference_wrapper<const _Tp>
457 cref(reference_wrapper<_Tp> __t)
458 { return cref(__t.get()); }
462 template<typename _Tp, bool>
463 struct _Mem_fn_const_or_non
465 typedef const _Tp& type;
468 template<typename _Tp>
469 struct _Mem_fn_const_or_non<_Tp, false>
475 * Derives from @c unary_function or @c binary_function, or perhaps
476 * nothing, depending on the number of arguments provided. The
477 * primary template is the basis case, which derives nothing.
479 template<typename _Res, typename... _ArgTypes>
480 struct _Maybe_unary_or_binary_function { };
482 /// Derives from @c unary_function, as appropriate.
483 template<typename _Res, typename _T1>
484 struct _Maybe_unary_or_binary_function<_Res, _T1>
485 : std::unary_function<_T1, _Res> { };
487 /// Derives from @c binary_function, as appropriate.
488 template<typename _Res, typename _T1, typename _T2>
489 struct _Maybe_unary_or_binary_function<_Res, _T1, _T2>
490 : std::binary_function<_T1, _T2, _Res> { };
492 /// Implementation of @c mem_fn for member function pointers.
493 template<typename _Res, typename _Class, typename... _ArgTypes>
494 class _Mem_fn<_Res (_Class::*)(_ArgTypes...)>
495 : public _Maybe_unary_or_binary_function<_Res, _Class*, _ArgTypes...>
497 typedef _Res (_Class::*_Functor)(_ArgTypes...);
499 template<typename _Tp>
501 _M_call(_Tp& __object, const volatile _Class *,
502 _ArgTypes... __args) const
503 { return (__object.*__pmf)(__args...); }
505 template<typename _Tp>
507 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
508 { return ((*__ptr).*__pmf)(__args...); }
511 typedef _Res result_type;
513 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
517 operator()(_Class& __object, _ArgTypes... __args) const
518 { return (__object.*__pmf)(__args...); }
522 operator()(_Class* __object, _ArgTypes... __args) const
523 { return (__object->*__pmf)(__args...); }
525 // Handle smart pointers, references and pointers to derived
526 template<typename _Tp>
528 operator()(_Tp& __object, _ArgTypes... __args) const
529 { return _M_call(__object, &__object, __args...); }
535 /// Implementation of @c mem_fn for const member function pointers.
536 template<typename _Res, typename _Class, typename... _ArgTypes>
537 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) const>
538 : public _Maybe_unary_or_binary_function<_Res, const _Class*,
541 typedef _Res (_Class::*_Functor)(_ArgTypes...) const;
543 template<typename _Tp>
545 _M_call(_Tp& __object, const volatile _Class *,
546 _ArgTypes... __args) const
547 { return (__object.*__pmf)(__args...); }
549 template<typename _Tp>
551 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
552 { return ((*__ptr).*__pmf)(__args...); }
555 typedef _Res result_type;
557 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
561 operator()(const _Class& __object, _ArgTypes... __args) const
562 { return (__object.*__pmf)(__args...); }
566 operator()(const _Class* __object, _ArgTypes... __args) const
567 { return (__object->*__pmf)(__args...); }
569 // Handle smart pointers, references and pointers to derived
570 template<typename _Tp>
571 _Res operator()(_Tp& __object, _ArgTypes... __args) const
572 { return _M_call(__object, &__object, __args...); }
578 /// Implementation of @c mem_fn for volatile member function pointers.
579 template<typename _Res, typename _Class, typename... _ArgTypes>
580 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) volatile>
581 : public _Maybe_unary_or_binary_function<_Res, volatile _Class*,
584 typedef _Res (_Class::*_Functor)(_ArgTypes...) volatile;
586 template<typename _Tp>
588 _M_call(_Tp& __object, const volatile _Class *,
589 _ArgTypes... __args) const
590 { return (__object.*__pmf)(__args...); }
592 template<typename _Tp>
594 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
595 { return ((*__ptr).*__pmf)(__args...); }
598 typedef _Res result_type;
600 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
604 operator()(volatile _Class& __object, _ArgTypes... __args) const
605 { return (__object.*__pmf)(__args...); }
609 operator()(volatile _Class* __object, _ArgTypes... __args) const
610 { return (__object->*__pmf)(__args...); }
612 // Handle smart pointers, references and pointers to derived
613 template<typename _Tp>
615 operator()(_Tp& __object, _ArgTypes... __args) const
616 { return _M_call(__object, &__object, __args...); }
622 /// Implementation of @c mem_fn for const volatile member function pointers.
623 template<typename _Res, typename _Class, typename... _ArgTypes>
624 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) const volatile>
625 : public _Maybe_unary_or_binary_function<_Res, const volatile _Class*,
628 typedef _Res (_Class::*_Functor)(_ArgTypes...) const volatile;
630 template<typename _Tp>
632 _M_call(_Tp& __object, const volatile _Class *,
633 _ArgTypes... __args) const
634 { return (__object.*__pmf)(__args...); }
636 template<typename _Tp>
638 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
639 { return ((*__ptr).*__pmf)(__args...); }
642 typedef _Res result_type;
644 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
648 operator()(const volatile _Class& __object, _ArgTypes... __args) const
649 { return (__object.*__pmf)(__args...); }
653 operator()(const volatile _Class* __object, _ArgTypes... __args) const
654 { return (__object->*__pmf)(__args...); }
656 // Handle smart pointers, references and pointers to derived
657 template<typename _Tp>
658 _Res operator()(_Tp& __object, _ArgTypes... __args) const
659 { return _M_call(__object, &__object, __args...); }
666 template<typename _Res, typename _Class>
667 class _Mem_fn<_Res _Class::*>
669 // This bit of genius is due to Peter Dimov, improved slightly by
671 template<typename _Tp>
673 _M_call(_Tp& __object, _Class *) const
674 { return __object.*__pm; }
676 template<typename _Tp, typename _Up>
678 _M_call(_Tp& __object, _Up * const *) const
679 { return (*__object).*__pm; }
681 template<typename _Tp, typename _Up>
683 _M_call(_Tp& __object, const _Up * const *) const
684 { return (*__object).*__pm; }
686 template<typename _Tp>
688 _M_call(_Tp& __object, const _Class *) const
689 { return __object.*__pm; }
691 template<typename _Tp>
693 _M_call(_Tp& __ptr, const volatile void*) const
694 { return (*__ptr).*__pm; }
696 template<typename _Tp> static _Tp& __get_ref();
698 template<typename _Tp>
699 static __sfinae_types::__one __check_const(_Tp&, _Class*);
700 template<typename _Tp, typename _Up>
701 static __sfinae_types::__one __check_const(_Tp&, _Up * const *);
702 template<typename _Tp, typename _Up>
703 static __sfinae_types::__two __check_const(_Tp&, const _Up * const *);
704 template<typename _Tp>
705 static __sfinae_types::__two __check_const(_Tp&, const _Class*);
706 template<typename _Tp>
707 static __sfinae_types::__two __check_const(_Tp&, const volatile void*);
710 template<typename _Tp>
712 : _Mem_fn_const_or_non<_Res,
713 (sizeof(__sfinae_types::__two)
714 == sizeof(__check_const<_Tp>(__get_ref<_Tp>(), (_Tp*)0)))>
717 template<typename _Signature>
720 template<typename _CVMem, typename _Tp>
721 struct result<_CVMem(_Tp)>
722 : public _Result_type<_Tp> { };
724 template<typename _CVMem, typename _Tp>
725 struct result<_CVMem(_Tp&)>
726 : public _Result_type<_Tp> { };
729 _Mem_fn(_Res _Class::*__pm) : __pm(__pm) { }
733 operator()(_Class& __object) const
734 { return __object.*__pm; }
737 operator()(const _Class& __object) const
738 { return __object.*__pm; }
742 operator()(_Class* __object) const
743 { return __object->*__pm; }
746 operator()(const _Class* __object) const
747 { return __object->*__pm; }
749 // Handle smart pointers and derived
750 template<typename _Tp>
751 typename _Result_type<_Tp>::type
752 operator()(_Tp& __unknown) const
753 { return _M_call(__unknown, &__unknown); }
760 * @brief Returns a function object that forwards to the member
764 template<typename _Tp, typename _Class>
765 inline _Mem_fn<_Tp _Class::*>
766 mem_fn(_Tp _Class::* __pm)
768 return _Mem_fn<_Tp _Class::*>(__pm);
772 * @brief Determines if the given type _Tp is a function object
773 * should be treated as a subexpression when evaluating calls to
774 * function objects returned by bind(). [TR1 3.6.1]
777 template<typename _Tp>
778 struct is_bind_expression
779 : public false_type { };
782 * @brief Determines if the given type _Tp is a placeholder in a
783 * bind() expression and, if so, which placeholder it is. [TR1 3.6.2]
786 template<typename _Tp>
787 struct is_placeholder
788 : public integral_constant<int, 0>
791 /// The type of placeholder objects defined by libstdc++.
792 template<int _Num> struct _Placeholder { };
794 /** @namespace std::placeholders
795 * @brief ISO C++ 0x entities sub namespace for functional.
798 * Define a large number of placeholders. There is no way to
799 * simplify this with variadic templates, because we're introducing
800 * unique names for each.
802 namespace placeholders
815 _Placeholder<10> _10;
816 _Placeholder<11> _11;
817 _Placeholder<12> _12;
818 _Placeholder<13> _13;
819 _Placeholder<14> _14;
820 _Placeholder<15> _15;
821 _Placeholder<16> _16;
822 _Placeholder<17> _17;
823 _Placeholder<18> _18;
824 _Placeholder<19> _19;
825 _Placeholder<20> _20;
826 _Placeholder<21> _21;
827 _Placeholder<22> _22;
828 _Placeholder<23> _23;
829 _Placeholder<24> _24;
830 _Placeholder<25> _25;
831 _Placeholder<26> _26;
832 _Placeholder<27> _27;
833 _Placeholder<28> _28;
834 _Placeholder<29> _29;
839 * Partial specialization of is_placeholder that provides the placeholder
840 * number for the placeholder objects defined by libstdc++.
844 struct is_placeholder<_Placeholder<_Num> >
845 : public integral_constant<int, _Num>
849 * Stores a tuple of indices. Used by bind() to extract the elements
852 template<int... _Indexes>
855 typedef _Index_tuple<_Indexes..., sizeof...(_Indexes)> __next;
858 /// Builds an _Index_tuple<0, 1, 2, ..., _Num-1>.
859 template<std::size_t _Num>
860 struct _Build_index_tuple
862 typedef typename _Build_index_tuple<_Num-1>::__type::__next __type;
866 struct _Build_index_tuple<0>
868 typedef _Index_tuple<> __type;
872 * Used by _Safe_tuple_element to indicate that there is no tuple
873 * element at this position.
875 struct _No_tuple_element;
878 * Implementation helper for _Safe_tuple_element. This primary
879 * template handles the case where it is safe to use @c
882 template<int __i, typename _Tuple, bool _IsSafe>
883 struct _Safe_tuple_element_impl
884 : tuple_element<__i, _Tuple> { };
887 * Implementation helper for _Safe_tuple_element. This partial
888 * specialization handles the case where it is not safe to use @c
889 * tuple_element. We just return @c _No_tuple_element.
891 template<int __i, typename _Tuple>
892 struct _Safe_tuple_element_impl<__i, _Tuple, false>
894 typedef _No_tuple_element type;
898 * Like tuple_element, but returns @c _No_tuple_element when
899 * tuple_element would return an error.
901 template<int __i, typename _Tuple>
902 struct _Safe_tuple_element
903 : _Safe_tuple_element_impl<__i, _Tuple,
904 (__i >= 0 && __i < tuple_size<_Tuple>::value)>
908 * Maps an argument to bind() into an actual argument to the bound
909 * function object [TR1 3.6.3/5]. Only the first parameter should
910 * be specified: the rest are used to determine among the various
911 * implementations. Note that, although this class is a function
912 * object, it isn't entirely normal because it takes only two
913 * parameters regardless of the number of parameters passed to the
914 * bind expression. The first parameter is the bound argument and
915 * the second parameter is a tuple containing references to the
916 * rest of the arguments.
918 template<typename _Arg,
919 bool _IsBindExp = is_bind_expression<_Arg>::value,
920 bool _IsPlaceholder = (is_placeholder<_Arg>::value > 0)>
924 * If the argument is reference_wrapper<_Tp>, returns the
925 * underlying reference. [TR1 3.6.3/5 bullet 1]
927 template<typename _Tp>
928 class _Mu<reference_wrapper<_Tp>, false, false>
931 typedef _Tp& result_type;
933 /* Note: This won't actually work for const volatile
934 * reference_wrappers, because reference_wrapper::get() is const
935 * but not volatile-qualified. This might be a defect in the TR.
937 template<typename _CVRef, typename _Tuple>
939 operator()(_CVRef& __arg, _Tuple&&) const volatile
940 { return __arg.get(); }
944 * If the argument is a bind expression, we invoke the underlying
945 * function object with the same cv-qualifiers as we are given and
946 * pass along all of our arguments (unwrapped). [TR1 3.6.3/5 bullet 2]
948 template<typename _Arg>
949 class _Mu<_Arg, true, false>
952 template<typename _Signature> class result;
954 // Determine the result type when we pass the arguments along. This
955 // involves passing along the cv-qualifiers placed on _Mu and
956 // unwrapping the argument bundle.
957 template<typename _CVMu, typename _CVArg, typename... _Args>
958 class result<_CVMu(_CVArg, tuple<_Args...>)>
959 : public result_of<_CVArg(_Args...)> { };
961 template<typename _CVArg, typename... _Args>
962 typename result_of<_CVArg(_Args...)>::type
963 operator()(_CVArg& __arg,
964 tuple<_Args...>&& __tuple) const volatile
966 // Construct an index tuple and forward to __call
967 typedef typename _Build_index_tuple<sizeof...(_Args)>::__type
969 return this->__call(__arg, std::move(__tuple), _Indexes());
973 // Invokes the underlying function object __arg by unpacking all
974 // of the arguments in the tuple.
975 template<typename _CVArg, typename... _Args, int... _Indexes>
976 typename result_of<_CVArg(_Args...)>::type
977 __call(_CVArg& __arg, tuple<_Args...>&& __tuple,
978 const _Index_tuple<_Indexes...>&) const volatile
980 return __arg(std::forward<_Args>(get<_Indexes>(__tuple))...);
985 * If the argument is a placeholder for the Nth argument, returns
986 * a reference to the Nth argument to the bind function object.
987 * [TR1 3.6.3/5 bullet 3]
989 template<typename _Arg>
990 class _Mu<_Arg, false, true>
993 template<typename _Signature> class result;
995 template<typename _CVMu, typename _CVArg, typename _Tuple>
996 class result<_CVMu(_CVArg, _Tuple)>
998 // Add a reference, if it hasn't already been done for us.
999 // This allows us to be a little bit sloppy in constructing
1000 // the tuple that we pass to result_of<...>.
1001 typedef typename _Safe_tuple_element<(is_placeholder<_Arg>::value
1006 typedef typename add_rvalue_reference<__base_type>::type type;
1009 template<typename _Tuple>
1010 typename result<_Mu(_Arg, _Tuple)>::type
1011 operator()(const volatile _Arg&, _Tuple&& __tuple) const volatile
1013 return std::forward<typename result<_Mu(_Arg, _Tuple)>::type>(
1014 ::std::get<(is_placeholder<_Arg>::value - 1)>(__tuple));
1019 * If the argument is just a value, returns a reference to that
1020 * value. The cv-qualifiers on the reference are the same as the
1021 * cv-qualifiers on the _Mu object. [TR1 3.6.3/5 bullet 4]
1023 template<typename _Arg>
1024 class _Mu<_Arg, false, false>
1027 template<typename _Signature> struct result;
1029 template<typename _CVMu, typename _CVArg, typename _Tuple>
1030 struct result<_CVMu(_CVArg, _Tuple)>
1032 typedef typename add_lvalue_reference<_CVArg>::type type;
1035 // Pick up the cv-qualifiers of the argument
1036 template<typename _CVArg, typename _Tuple>
1038 operator()(_CVArg&& __arg, _Tuple&&) const volatile
1039 { return std::forward<_CVArg>(__arg); }
1043 * Maps member pointers into instances of _Mem_fn but leaves all
1044 * other function objects untouched. Used by tr1::bind(). The
1045 * primary template handles the non--member-pointer case.
1047 template<typename _Tp>
1048 struct _Maybe_wrap_member_pointer
1053 __do_wrap(const _Tp& __x)
1058 * Maps member pointers into instances of _Mem_fn but leaves all
1059 * other function objects untouched. Used by tr1::bind(). This
1060 * partial specialization handles the member pointer case.
1062 template<typename _Tp, typename _Class>
1063 struct _Maybe_wrap_member_pointer<_Tp _Class::*>
1065 typedef _Mem_fn<_Tp _Class::*> type;
1068 __do_wrap(_Tp _Class::* __pm)
1069 { return type(__pm); }
1072 // Specialization needed to prevent "forming reference to void" errors when
1073 // bind<void>() is called, because argument deduction instantiates
1074 // _Maybe_wrap_member_pointer<void> outside the immediate context where
1077 struct _Maybe_wrap_member_pointer<void>
1082 /// Type of the function object returned from bind().
1083 template<typename _Signature>
1086 template<typename _Functor, typename... _Bound_args>
1087 class _Bind<_Functor(_Bound_args...)>
1088 : public _Weak_result_type<_Functor>
1090 typedef _Bind __self_type;
1091 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
1095 tuple<_Bound_args...> _M_bound_args;
1098 template<typename _Result, typename... _Args, int... _Indexes>
1100 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>)
1102 return _M_f(_Mu<_Bound_args>()
1103 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1107 template<typename _Result, typename... _Args, int... _Indexes>
1109 __call_c(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>) const
1111 return _M_f(_Mu<_Bound_args>()
1112 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1117 template<typename _Result, typename... _Args, int... _Indexes>
1119 __call_v(tuple<_Args...>&& __args,
1120 _Index_tuple<_Indexes...>) volatile
1122 return _M_f(_Mu<_Bound_args>()
1123 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1126 // Call as const volatile
1127 template<typename _Result, typename... _Args, int... _Indexes>
1129 __call_c_v(tuple<_Args...>&& __args,
1130 _Index_tuple<_Indexes...>) const volatile
1132 return _M_f(_Mu<_Bound_args>()
1133 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1138 explicit _Bind(_Functor __f, _Bound_args... __bound_args)
1139 : _M_f(std::forward<_Functor>(__f)),
1140 _M_bound_args(std::forward<_Bound_args>(__bound_args)...)
1144 template<typename... _Args, typename _Result
1145 = decltype( std::declval<_Functor>()(
1146 _Mu<_Bound_args>()( std::declval<_Bound_args&>(),
1147 std::declval<tuple<_Args...>&&>() )... ) )>
1149 operator()(_Args&&... __args)
1151 return this->__call<_Result>(tuple<_Args...>
1152 (std::forward<_Args>(__args)...),
1157 template<typename... _Args, typename _Result
1158 = decltype( std::declval<const _Functor>()(
1159 _Mu<_Bound_args>()( std::declval<const _Bound_args&>(),
1160 std::declval<tuple<_Args...>&&>() )... ) )>
1162 operator()(_Args&&... __args) const
1164 return this->__call_c<_Result>(tuple<_Args...>
1165 (std::forward<_Args>(__args)...),
1171 template<typename... _Args, typename _Result
1172 = decltype( std::declval<volatile _Functor>()(
1173 _Mu<_Bound_args>()( std::declval<volatile _Bound_args&>(),
1174 std::declval<tuple<_Args...>&&>() )... ) )>
1176 operator()(_Args&&... __args) volatile
1178 return this->__call_v<_Result>(tuple<_Args...>
1179 (std::forward<_Args>(__args)...),
1183 // Call as const volatile
1184 template<typename... _Args, typename _Result
1185 = decltype( std::declval<const volatile _Functor>()(
1186 _Mu<_Bound_args>()( std::declval<const volatile _Bound_args&>(),
1187 std::declval<tuple<_Args...>&&>() )... ) )>
1189 operator()(_Args&&... __args) const volatile
1191 return this->__call_c_v<_Result>(tuple<_Args...>
1192 (std::forward<_Args>(__args)...),
1198 /// Type of the function object returned from bind<R>().
1199 template<typename _Result, typename _Signature>
1200 struct _Bind_result;
1202 template<typename _Result, typename _Functor, typename... _Bound_args>
1203 class _Bind_result<_Result, _Functor(_Bound_args...)>
1205 typedef _Bind_result __self_type;
1206 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
1210 tuple<_Bound_args...> _M_bound_args;
1213 template<typename _Res>
1214 struct __enable_if_void : enable_if<is_void<_Res>::value, int> { };
1215 template<typename _Res>
1216 struct __disable_if_void : enable_if<!is_void<_Res>::value, int> { };
1219 template<typename _Res, typename... _Args, int... _Indexes>
1221 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1222 typename __disable_if_void<_Res>::type = 0)
1224 return _M_f(_Mu<_Bound_args>()
1225 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1228 // Call unqualified, return void
1229 template<typename _Res, typename... _Args, int... _Indexes>
1231 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1232 typename __enable_if_void<_Res>::type = 0)
1234 _M_f(_Mu<_Bound_args>()
1235 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1239 template<typename _Res, typename... _Args, int... _Indexes>
1241 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1242 typename __disable_if_void<_Res>::type = 0) const
1244 return _M_f(_Mu<_Bound_args>()
1245 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1248 // Call as const, return void
1249 template<typename _Res, typename... _Args, int... _Indexes>
1251 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1252 typename __enable_if_void<_Res>::type = 0) const
1254 _M_f(_Mu<_Bound_args>()
1255 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1259 template<typename _Res, typename... _Args, int... _Indexes>
1261 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1262 typename __disable_if_void<_Res>::type = 0) volatile
1264 return _M_f(_Mu<_Bound_args>()
1265 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1268 // Call as volatile, return void
1269 template<typename _Res, typename... _Args, int... _Indexes>
1271 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1272 typename __enable_if_void<_Res>::type = 0) volatile
1274 _M_f(_Mu<_Bound_args>()
1275 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1278 // Call as const volatile
1279 template<typename _Res, typename... _Args, int... _Indexes>
1281 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1282 typename __disable_if_void<_Res>::type = 0) const volatile
1284 return _M_f(_Mu<_Bound_args>()
1285 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1288 // Call as const volatile, return void
1289 template<typename _Res, typename... _Args, int... _Indexes>
1291 __call(tuple<_Args...>&& __args,
1292 _Index_tuple<_Indexes...>,
1293 typename __enable_if_void<_Res>::type = 0) const volatile
1295 _M_f(_Mu<_Bound_args>()
1296 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1300 typedef _Result result_type;
1303 _Bind_result(_Functor __f, _Bound_args... __bound_args)
1304 : _M_f(std::forward<_Functor>(__f)),
1305 _M_bound_args(std::forward<_Bound_args>(__bound_args)...)
1309 template<typename... _Args>
1311 operator()(_Args&&... __args)
1313 return this->__call<_Result>(
1314 tuple<_Args...>(std::forward<_Args...>(__args)...),
1319 template<typename... _Args>
1321 operator()(_Args&&... __args) const
1323 return this->__call<_Result>(
1324 tuple<_Args...>(std::forward<_Args...>(__args)...),
1329 template<typename... _Args>
1331 operator()(_Args&&... __args) volatile
1333 return this->__call<_Result>(
1334 tuple<_Args...>(std::forward<_Args...>(__args)...),
1338 // Call as const volatile
1339 template<typename... _Args>
1341 operator()(_Args&&... __args) const volatile
1343 return this->__call<_Result>(
1344 tuple<_Args...>(std::forward<_Args...>(__args)...),
1350 * @brief Class template _Bind is always a bind expression.
1353 template<typename _Signature>
1354 struct is_bind_expression<_Bind<_Signature> >
1355 : public true_type { };
1358 * @brief Class template _Bind is always a bind expression.
1361 template<typename _Result, typename _Signature>
1362 struct is_bind_expression<_Bind_result<_Result, _Signature> >
1363 : public true_type { };
1366 * @brief Function template for std::bind.
1369 template<typename _Functor, typename... _ArgTypes>
1371 _Bind<typename _Maybe_wrap_member_pointer<_Functor>::type(_ArgTypes...)>
1372 bind(_Functor __f, _ArgTypes... __args)
1374 typedef _Maybe_wrap_member_pointer<_Functor> __maybe_type;
1375 typedef typename __maybe_type::type __functor_type;
1376 typedef _Bind<__functor_type(_ArgTypes...)> __result_type;
1377 return __result_type(__maybe_type::__do_wrap(__f),
1378 std::forward<_ArgTypes>(__args)...);
1382 * @brief Function template for std::bind.
1385 template<typename _Result, typename _Functor, typename... _ArgTypes>
1387 _Bind_result<_Result,
1388 typename _Maybe_wrap_member_pointer<_Functor>::type
1390 bind(_Functor __f, _ArgTypes... __args)
1392 typedef _Maybe_wrap_member_pointer<_Functor> __maybe_type;
1393 typedef typename __maybe_type::type __functor_type;
1394 typedef _Bind_result<_Result, __functor_type(_ArgTypes...)>
1396 return __result_type(__maybe_type::__do_wrap(__f),
1397 std::forward<_ArgTypes>(__args)...);
1401 * @brief Exception class thrown when class template function's
1402 * operator() is called with an empty target.
1403 * @ingroup exceptions
1405 class bad_function_call : public std::exception { };
1408 * The integral constant expression 0 can be converted into a
1409 * pointer to this type. It is used by the function template to
1410 * accept NULL pointers.
1412 struct _M_clear_type;
1415 * Trait identifying "location-invariant" types, meaning that the
1416 * address of the object (or any of its members) will not escape.
1417 * Also implies a trivial copy constructor and assignment operator.
1419 template<typename _Tp>
1420 struct __is_location_invariant
1421 : integral_constant<bool, (is_pointer<_Tp>::value
1422 || is_member_pointer<_Tp>::value)>
1425 class _Undefined_class;
1430 const void* _M_const_object;
1431 void (*_M_function_pointer)();
1432 void (_Undefined_class::*_M_member_pointer)();
1437 void* _M_access() { return &_M_pod_data[0]; }
1438 const void* _M_access() const { return &_M_pod_data[0]; }
1440 template<typename _Tp>
1443 { return *static_cast<_Tp*>(_M_access()); }
1445 template<typename _Tp>
1448 { return *static_cast<const _Tp*>(_M_access()); }
1450 _Nocopy_types _M_unused;
1451 char _M_pod_data[sizeof(_Nocopy_types)];
1454 enum _Manager_operation
1462 // Simple type wrapper that helps avoid annoying const problems
1463 // when casting between void pointers and pointers-to-pointers.
1464 template<typename _Tp>
1465 struct _Simple_type_wrapper
1467 _Simple_type_wrapper(_Tp __value) : __value(__value) { }
1472 template<typename _Tp>
1473 struct __is_location_invariant<_Simple_type_wrapper<_Tp> >
1474 : __is_location_invariant<_Tp>
1477 // Converts a reference to a function object into a callable
1479 template<typename _Functor>
1481 __callable_functor(_Functor& __f)
1484 template<typename _Member, typename _Class>
1485 inline _Mem_fn<_Member _Class::*>
1486 __callable_functor(_Member _Class::* &__p)
1487 { return mem_fn(__p); }
1489 template<typename _Member, typename _Class>
1490 inline _Mem_fn<_Member _Class::*>
1491 __callable_functor(_Member _Class::* const &__p)
1492 { return mem_fn(__p); }
1494 template<typename _Signature>
1497 /// Base class of all polymorphic function object wrappers.
1498 class _Function_base
1501 static const std::size_t _M_max_size = sizeof(_Nocopy_types);
1502 static const std::size_t _M_max_align = __alignof__(_Nocopy_types);
1504 template<typename _Functor>
1508 static const bool __stored_locally =
1509 (__is_location_invariant<_Functor>::value
1510 && sizeof(_Functor) <= _M_max_size
1511 && __alignof__(_Functor) <= _M_max_align
1512 && (_M_max_align % __alignof__(_Functor) == 0));
1514 typedef integral_constant<bool, __stored_locally> _Local_storage;
1516 // Retrieve a pointer to the function object
1518 _M_get_pointer(const _Any_data& __source)
1520 const _Functor* __ptr =
1521 __stored_locally? &__source._M_access<_Functor>()
1522 /* have stored a pointer */ : __source._M_access<_Functor*>();
1523 return const_cast<_Functor*>(__ptr);
1526 // Clone a location-invariant function object that fits within
1527 // an _Any_data structure.
1529 _M_clone(_Any_data& __dest, const _Any_data& __source, true_type)
1531 new (__dest._M_access()) _Functor(__source._M_access<_Functor>());
1534 // Clone a function object that is not location-invariant or
1535 // that cannot fit into an _Any_data structure.
1537 _M_clone(_Any_data& __dest, const _Any_data& __source, false_type)
1539 __dest._M_access<_Functor*>() =
1540 new _Functor(*__source._M_access<_Functor*>());
1543 // Destroying a location-invariant object may still require
1546 _M_destroy(_Any_data& __victim, true_type)
1548 __victim._M_access<_Functor>().~_Functor();
1551 // Destroying an object located on the heap.
1553 _M_destroy(_Any_data& __victim, false_type)
1555 delete __victim._M_access<_Functor*>();
1560 _M_manager(_Any_data& __dest, const _Any_data& __source,
1561 _Manager_operation __op)
1566 case __get_type_info:
1567 __dest._M_access<const type_info*>() = &typeid(_Functor);
1570 case __get_functor_ptr:
1571 __dest._M_access<_Functor*>() = _M_get_pointer(__source);
1574 case __clone_functor:
1575 _M_clone(__dest, __source, _Local_storage());
1578 case __destroy_functor:
1579 _M_destroy(__dest, _Local_storage());
1586 _M_init_functor(_Any_data& __functor, _Functor&& __f)
1587 { _M_init_functor(__functor, std::move(__f), _Local_storage()); }
1589 template<typename _Signature>
1591 _M_not_empty_function(const function<_Signature>& __f)
1592 { return static_cast<bool>(__f); }
1594 template<typename _Tp>
1596 _M_not_empty_function(const _Tp*& __fp)
1599 template<typename _Class, typename _Tp>
1601 _M_not_empty_function(_Tp _Class::* const& __mp)
1604 template<typename _Tp>
1606 _M_not_empty_function(const _Tp&)
1611 _M_init_functor(_Any_data& __functor, _Functor&& __f, true_type)
1612 { new (__functor._M_access()) _Functor(std::move(__f)); }
1615 _M_init_functor(_Any_data& __functor, _Functor&& __f, false_type)
1616 { __functor._M_access<_Functor*>() = new _Functor(std::move(__f)); }
1619 template<typename _Functor>
1620 class _Ref_manager : public _Base_manager<_Functor*>
1622 typedef _Function_base::_Base_manager<_Functor*> _Base;
1626 _M_manager(_Any_data& __dest, const _Any_data& __source,
1627 _Manager_operation __op)
1632 case __get_type_info:
1633 __dest._M_access<const type_info*>() = &typeid(_Functor);
1636 case __get_functor_ptr:
1637 __dest._M_access<_Functor*>() = *_Base::_M_get_pointer(__source);
1638 return is_const<_Functor>::value;
1642 _Base::_M_manager(__dest, __source, __op);
1648 _M_init_functor(_Any_data& __functor, reference_wrapper<_Functor> __f)
1650 // TBD: Use address_of function instead.
1651 _Base::_M_init_functor(__functor, &__f.get());
1655 _Function_base() : _M_manager(0) { }
1660 _M_manager(_M_functor, _M_functor, __destroy_functor);
1664 bool _M_empty() const { return !_M_manager; }
1666 typedef bool (*_Manager_type)(_Any_data&, const _Any_data&,
1667 _Manager_operation);
1669 _Any_data _M_functor;
1670 _Manager_type _M_manager;
1673 template<typename _Signature, typename _Functor>
1674 class _Function_handler;
1676 template<typename _Res, typename _Functor, typename... _ArgTypes>
1677 class _Function_handler<_Res(_ArgTypes...), _Functor>
1678 : public _Function_base::_Base_manager<_Functor>
1680 typedef _Function_base::_Base_manager<_Functor> _Base;
1684 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1686 return (*_Base::_M_get_pointer(__functor))(
1687 std::forward<_ArgTypes>(__args)...);
1691 template<typename _Functor, typename... _ArgTypes>
1692 class _Function_handler<void(_ArgTypes...), _Functor>
1693 : public _Function_base::_Base_manager<_Functor>
1695 typedef _Function_base::_Base_manager<_Functor> _Base;
1699 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1701 (*_Base::_M_get_pointer(__functor))(
1702 std::forward<_ArgTypes>(__args)...);
1706 template<typename _Res, typename _Functor, typename... _ArgTypes>
1707 class _Function_handler<_Res(_ArgTypes...), reference_wrapper<_Functor> >
1708 : public _Function_base::_Ref_manager<_Functor>
1710 typedef _Function_base::_Ref_manager<_Functor> _Base;
1714 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1716 return __callable_functor(**_Base::_M_get_pointer(__functor))(
1717 std::forward<_ArgTypes>(__args)...);
1721 template<typename _Functor, typename... _ArgTypes>
1722 class _Function_handler<void(_ArgTypes...), reference_wrapper<_Functor> >
1723 : public _Function_base::_Ref_manager<_Functor>
1725 typedef _Function_base::_Ref_manager<_Functor> _Base;
1729 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1731 __callable_functor(**_Base::_M_get_pointer(__functor))(
1732 std::forward<_ArgTypes>(__args)...);
1736 template<typename _Class, typename _Member, typename _Res,
1737 typename... _ArgTypes>
1738 class _Function_handler<_Res(_ArgTypes...), _Member _Class::*>
1739 : public _Function_handler<void(_ArgTypes...), _Member _Class::*>
1741 typedef _Function_handler<void(_ArgTypes...), _Member _Class::*>
1746 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1748 return mem_fn(_Base::_M_get_pointer(__functor)->__value)(
1749 std::forward<_ArgTypes>(__args)...);
1753 template<typename _Class, typename _Member, typename... _ArgTypes>
1754 class _Function_handler<void(_ArgTypes...), _Member _Class::*>
1755 : public _Function_base::_Base_manager<
1756 _Simple_type_wrapper< _Member _Class::* > >
1758 typedef _Member _Class::* _Functor;
1759 typedef _Simple_type_wrapper<_Functor> _Wrapper;
1760 typedef _Function_base::_Base_manager<_Wrapper> _Base;
1764 _M_manager(_Any_data& __dest, const _Any_data& __source,
1765 _Manager_operation __op)
1770 case __get_type_info:
1771 __dest._M_access<const type_info*>() = &typeid(_Functor);
1774 case __get_functor_ptr:
1775 __dest._M_access<_Functor*>() =
1776 &_Base::_M_get_pointer(__source)->__value;
1780 _Base::_M_manager(__dest, __source, __op);
1786 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1788 mem_fn(_Base::_M_get_pointer(__functor)->__value)(
1789 std::forward<_ArgTypes>(__args)...);
1794 * @brief Primary class template for std::function.
1797 * Polymorphic function wrapper.
1799 template<typename _Res, typename... _ArgTypes>
1800 class function<_Res(_ArgTypes...)>
1801 : public _Maybe_unary_or_binary_function<_Res, _ArgTypes...>,
1802 private _Function_base
1804 typedef _Res _Signature_type(_ArgTypes...);
1806 struct _Useless { };
1809 typedef _Res result_type;
1811 // [3.7.2.1] construct/copy/destroy
1814 * @brief Default construct creates an empty function call wrapper.
1815 * @post @c !(bool)*this
1818 function() : _Function_base() { }
1821 * @brief Default construct creates an empty function call wrapper.
1822 * @post @c !(bool)*this
1824 function(_M_clear_type*) : _Function_base() { }
1827 * @brief %Function copy constructor.
1828 * @param x A %function object with identical call signature.
1829 * @post @c (bool)*this == (bool)x
1831 * The newly-created %function contains a copy of the target of @a
1832 * x (if it has one).
1834 function(const function& __x);
1837 * @brief %Function move constructor.
1838 * @param x A %function object rvalue with identical call signature.
1840 * The newly-created %function contains the target of @a x
1843 function(function&& __x) : _Function_base()
1848 // TODO: needs allocator_arg_t
1851 * @brief Builds a %function that targets a copy of the incoming
1853 * @param f A %function object that is callable with parameters of
1854 * type @c T1, @c T2, ..., @c TN and returns a value convertible
1857 * The newly-created %function object will target a copy of @a
1858 * f. If @a f is @c reference_wrapper<F>, then this function
1859 * object will contain a reference to the function object @c
1860 * f.get(). If @a f is a NULL function pointer or NULL
1861 * pointer-to-member, the newly-created object will be empty.
1863 * If @a f is a non-NULL function pointer or an object of type @c
1864 * reference_wrapper<F>, this function will not throw.
1866 template<typename _Functor>
1867 function(_Functor __f,
1869 !is_integral<_Functor>::value, _Useless>::type
1873 * @brief %Function assignment operator.
1874 * @param x A %function with identical call signature.
1875 * @post @c (bool)*this == (bool)x
1878 * The target of @a x is copied to @c *this. If @a x has no
1879 * target, then @c *this will be empty.
1881 * If @a x targets a function pointer or a reference to a function
1882 * object, then this operation will not throw an %exception.
1885 operator=(const function& __x)
1887 function(__x).swap(*this);
1892 * @brief %Function move-assignment operator.
1893 * @param x A %function rvalue with identical call signature.
1896 * The target of @a x is moved to @c *this. If @a x has no
1897 * target, then @c *this will be empty.
1899 * If @a x targets a function pointer or a reference to a function
1900 * object, then this operation will not throw an %exception.
1903 operator=(function&& __x)
1905 function(std::move(__x)).swap(*this);
1910 * @brief %Function assignment to zero.
1911 * @post @c !(bool)*this
1914 * The target of @c *this is deallocated, leaving it empty.
1917 operator=(_M_clear_type*)
1921 _M_manager(_M_functor, _M_functor, __destroy_functor);
1929 * @brief %Function assignment to a new target.
1930 * @param f A %function object that is callable with parameters of
1931 * type @c T1, @c T2, ..., @c TN and returns a value convertible
1935 * This %function object wrapper will target a copy of @a
1936 * f. If @a f is @c reference_wrapper<F>, then this function
1937 * object will contain a reference to the function object @c
1938 * f.get(). If @a f is a NULL function pointer or NULL
1939 * pointer-to-member, @c this object will be empty.
1941 * If @a f is a non-NULL function pointer or an object of type @c
1942 * reference_wrapper<F>, this function will not throw.
1944 template<typename _Functor>
1945 typename enable_if<!is_integral<_Functor>::value, function&>::type
1946 operator=(_Functor&& __f)
1948 function(std::forward<_Functor>(__f)).swap(*this);
1953 template<typename _Functor>
1954 typename enable_if<!is_integral<_Functor>::value, function&>::type
1955 operator=(reference_wrapper<_Functor> __f)
1957 function(__f).swap(*this);
1961 // [3.7.2.2] function modifiers
1964 * @brief Swap the targets of two %function objects.
1965 * @param f A %function with identical call signature.
1967 * Swap the targets of @c this function object and @a f. This
1968 * function will not throw an %exception.
1970 void swap(function& __x)
1972 /* We cannot perform direct assignments of the _M_functor
1973 parts as they are of type _Any_data and have a different
1974 dynamic type. Doing so would violate type-based aliasing
1975 rules and lead to spurious miscompilations.
1976 Instead perform a bytewise exchange of the memory of
1978 ??? A wordwise exchange honoring alignment of _M_functor
1979 would be more efficient. See PR42845. */
1980 for (unsigned i = 0; i < sizeof (_M_functor._M_pod_data); ++i)
1981 std::swap (_M_functor._M_pod_data[i], __x._M_functor._M_pod_data[i]);
1982 _Manager_type __old_manager = _M_manager;
1983 _M_manager = __x._M_manager;
1984 __x._M_manager = __old_manager;
1985 _Invoker_type __old_invoker = _M_invoker;
1986 _M_invoker = __x._M_invoker;
1987 __x._M_invoker = __old_invoker;
1990 // TODO: needs allocator_arg_t
1992 template<typename _Functor, typename _Alloc>
1994 assign(_Functor&& __f, const _Alloc& __a)
1996 function(allocator_arg, __a,
1997 std::forward<_Functor>(__f)).swap(*this);
2001 // [3.7.2.3] function capacity
2004 * @brief Determine if the %function wrapper has a target.
2006 * @return @c true when this %function object contains a target,
2007 * or @c false when it is empty.
2009 * This function will not throw an %exception.
2011 explicit operator bool() const
2012 { return !_M_empty(); }
2014 // [3.7.2.4] function invocation
2017 * @brief Invokes the function targeted by @c *this.
2018 * @returns the result of the target.
2019 * @throws bad_function_call when @c !(bool)*this
2021 * The function call operator invokes the target function object
2022 * stored by @c this.
2024 _Res operator()(_ArgTypes... __args) const;
2027 // [3.7.2.5] function target access
2029 * @brief Determine the type of the target of this function object
2032 * @returns the type identifier of the target function object, or
2033 * @c typeid(void) if @c !(bool)*this.
2035 * This function will not throw an %exception.
2037 const type_info& target_type() const;
2040 * @brief Access the stored target function object.
2042 * @return Returns a pointer to the stored target function object,
2043 * if @c typeid(Functor).equals(target_type()); otherwise, a NULL
2046 * This function will not throw an %exception.
2048 template<typename _Functor> _Functor* target();
2051 template<typename _Functor> const _Functor* target() const;
2054 // deleted overloads
2055 template<typename _Res2, typename... _ArgTypes2>
2056 void operator==(const function<_Res2(_ArgTypes2...)>&) const = delete;
2057 template<typename _Res2, typename... _ArgTypes2>
2058 void operator!=(const function<_Res2(_ArgTypes2...)>&) const = delete;
2061 typedef _Res (*_Invoker_type)(const _Any_data&, _ArgTypes...);
2062 _Invoker_type _M_invoker;
2065 // Out-of-line member definitions.
2066 template<typename _Res, typename... _ArgTypes>
2067 function<_Res(_ArgTypes...)>::
2068 function(const function& __x)
2071 if (static_cast<bool>(__x))
2073 _M_invoker = __x._M_invoker;
2074 _M_manager = __x._M_manager;
2075 __x._M_manager(_M_functor, __x._M_functor, __clone_functor);
2079 template<typename _Res, typename... _ArgTypes>
2080 template<typename _Functor>
2081 function<_Res(_ArgTypes...)>::
2082 function(_Functor __f,
2084 !is_integral<_Functor>::value, _Useless>::type)
2087 typedef _Function_handler<_Signature_type, _Functor> _My_handler;
2089 if (_My_handler::_M_not_empty_function(__f))
2091 _M_invoker = &_My_handler::_M_invoke;
2092 _M_manager = &_My_handler::_M_manager;
2093 _My_handler::_M_init_functor(_M_functor, std::move(__f));
2097 template<typename _Res, typename... _ArgTypes>
2099 function<_Res(_ArgTypes...)>::
2100 operator()(_ArgTypes... __args) const
2103 __throw_bad_function_call();
2104 return _M_invoker(_M_functor, std::forward<_ArgTypes>(__args)...);
2108 template<typename _Res, typename... _ArgTypes>
2110 function<_Res(_ArgTypes...)>::
2115 _Any_data __typeinfo_result;
2116 _M_manager(__typeinfo_result, _M_functor, __get_type_info);
2117 return *__typeinfo_result._M_access<const type_info*>();
2120 return typeid(void);
2123 template<typename _Res, typename... _ArgTypes>
2124 template<typename _Functor>
2126 function<_Res(_ArgTypes...)>::
2129 if (typeid(_Functor) == target_type() && _M_manager)
2132 if (_M_manager(__ptr, _M_functor, __get_functor_ptr)
2133 && !is_const<_Functor>::value)
2136 return __ptr._M_access<_Functor*>();
2142 template<typename _Res, typename... _ArgTypes>
2143 template<typename _Functor>
2145 function<_Res(_ArgTypes...)>::
2148 if (typeid(_Functor) == target_type() && _M_manager)
2151 _M_manager(__ptr, _M_functor, __get_functor_ptr);
2152 return __ptr._M_access<const _Functor*>();
2159 // [20.7.15.2.6] null pointer comparisons
2162 * @brief Compares a polymorphic function object wrapper against 0
2163 * (the NULL pointer).
2164 * @returns @c true if the wrapper has no target, @c false otherwise
2166 * This function will not throw an %exception.
2168 template<typename _Res, typename... _Args>
2170 operator==(const function<_Res(_Args...)>& __f, _M_clear_type*)
2171 { return !static_cast<bool>(__f); }
2174 template<typename _Res, typename... _Args>
2176 operator==(_M_clear_type*, const function<_Res(_Args...)>& __f)
2177 { return !static_cast<bool>(__f); }
2180 * @brief Compares a polymorphic function object wrapper against 0
2181 * (the NULL pointer).
2182 * @returns @c false if the wrapper has no target, @c true otherwise
2184 * This function will not throw an %exception.
2186 template<typename _Res, typename... _Args>
2188 operator!=(const function<_Res(_Args...)>& __f, _M_clear_type*)
2189 { return static_cast<bool>(__f); }
2192 template<typename _Res, typename... _Args>
2194 operator!=(_M_clear_type*, const function<_Res(_Args...)>& __f)
2195 { return static_cast<bool>(__f); }
2197 // [20.7.15.2.7] specialized algorithms
2200 * @brief Swap the targets of two polymorphic function object wrappers.
2202 * This function will not throw an %exception.
2204 template<typename _Res, typename... _Args>
2206 swap(function<_Res(_Args...)>& __x, function<_Res(_Args...)>& __y)
2210 #endif // __GXX_EXPERIMENTAL_CXX0X__
2212 #endif // _GLIBCXX_FUNCTIONAL