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>
59 #include <ext/type_traits.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>
97 /// If we have found a result_type, extract it.
98 template<bool _Has_result_type, typename _Functor>
99 struct _Maybe_get_result_type
102 template<typename _Functor>
103 struct _Maybe_get_result_type<true, _Functor>
105 typedef typename _Functor::result_type result_type;
109 * Base class for any function object that has a weak result type, as
110 * defined in 3.3/3 of TR1.
112 template<typename _Functor>
113 struct _Weak_result_type_impl
114 : _Maybe_get_result_type<_Has_result_type<_Functor>::value, _Functor>
117 /// Retrieve the result type for a function type.
118 template<typename _Res, typename... _ArgTypes>
119 struct _Weak_result_type_impl<_Res(_ArgTypes...)>
121 typedef _Res result_type;
124 /// Retrieve the result type for a function reference.
125 template<typename _Res, typename... _ArgTypes>
126 struct _Weak_result_type_impl<_Res(&)(_ArgTypes...)>
128 typedef _Res result_type;
131 /// Retrieve the result type for a function pointer.
132 template<typename _Res, typename... _ArgTypes>
133 struct _Weak_result_type_impl<_Res(*)(_ArgTypes...)>
135 typedef _Res result_type;
138 /// Retrieve result type for a member function pointer.
139 template<typename _Res, typename _Class, typename... _ArgTypes>
140 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...)>
142 typedef _Res result_type;
145 /// Retrieve result type for a const member function pointer.
146 template<typename _Res, typename _Class, typename... _ArgTypes>
147 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) const>
149 typedef _Res result_type;
152 /// Retrieve result type for a volatile member function pointer.
153 template<typename _Res, typename _Class, typename... _ArgTypes>
154 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) volatile>
156 typedef _Res result_type;
159 /// Retrieve result type for a const volatile member function pointer.
160 template<typename _Res, typename _Class, typename... _ArgTypes>
161 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...)const volatile>
163 typedef _Res result_type;
167 * Strip top-level cv-qualifiers from the function object and let
168 * _Weak_result_type_impl perform the real work.
170 template<typename _Functor>
171 struct _Weak_result_type
172 : _Weak_result_type_impl<typename remove_cv<_Functor>::type>
175 template<typename _Signature>
178 template<typename _Functor, typename... _ArgTypes>
179 struct result_of<_Functor(_ArgTypes...)>
182 decltype( std::declval<_Functor>()(std::declval<_ArgTypes>()...) )
186 /// Determines if the type _Tp derives from unary_function.
187 template<typename _Tp>
188 struct _Derives_from_unary_function : __sfinae_types
191 template<typename _T1, typename _Res>
192 static __one __test(const volatile unary_function<_T1, _Res>*);
194 // It's tempting to change "..." to const volatile void*, but
195 // that fails when _Tp is a function type.
196 static __two __test(...);
199 static const bool value = sizeof(__test((_Tp*)0)) == 1;
202 /// Determines if the type _Tp derives from binary_function.
203 template<typename _Tp>
204 struct _Derives_from_binary_function : __sfinae_types
207 template<typename _T1, typename _T2, typename _Res>
208 static __one __test(const volatile binary_function<_T1, _T2, _Res>*);
210 // It's tempting to change "..." to const volatile void*, but
211 // that fails when _Tp is a function type.
212 static __two __test(...);
215 static const bool value = sizeof(__test((_Tp*)0)) == 1;
218 /// Turns a function type into a function pointer type
219 template<typename _Tp, bool _IsFunctionType = is_function<_Tp>::value>
220 struct _Function_to_function_pointer
225 template<typename _Tp>
226 struct _Function_to_function_pointer<_Tp, true>
232 * Invoke a function object, which may be either a member pointer or a
233 * function object. The first parameter will tell which.
235 template<typename _Functor, typename... _Args>
237 typename __gnu_cxx::__enable_if<
238 (!is_member_pointer<_Functor>::value
239 && !is_function<_Functor>::value
240 && !is_function<typename remove_pointer<_Functor>::type>::value),
241 typename result_of<_Functor(_Args...)>::type
243 __invoke(_Functor& __f, _Args&&... __args)
245 return __f(std::forward<_Args>(__args)...);
248 // To pick up function references (that will become function pointers)
249 template<typename _Functor, typename... _Args>
251 typename __gnu_cxx::__enable_if<
252 (is_pointer<_Functor>::value
253 && is_function<typename remove_pointer<_Functor>::type>::value),
254 typename result_of<_Functor(_Args...)>::type
256 __invoke(_Functor __f, _Args&&... __args)
258 return __f(std::forward<_Args>(__args)...);
262 * Knowing which of unary_function and binary_function _Tp derives
263 * from, derives from the same and ensures that reference_wrapper
264 * will have a weak result type. See cases below.
266 template<bool _Unary, bool _Binary, typename _Tp>
267 struct _Reference_wrapper_base_impl;
269 // Not a unary_function or binary_function, so try a weak result type.
270 template<typename _Tp>
271 struct _Reference_wrapper_base_impl<false, false, _Tp>
272 : _Weak_result_type<_Tp>
275 // unary_function but not binary_function
276 template<typename _Tp>
277 struct _Reference_wrapper_base_impl<true, false, _Tp>
278 : unary_function<typename _Tp::argument_type,
279 typename _Tp::result_type>
282 // binary_function but not unary_function
283 template<typename _Tp>
284 struct _Reference_wrapper_base_impl<false, true, _Tp>
285 : binary_function<typename _Tp::first_argument_type,
286 typename _Tp::second_argument_type,
287 typename _Tp::result_type>
290 // Both unary_function and binary_function. Import result_type to
292 template<typename _Tp>
293 struct _Reference_wrapper_base_impl<true, true, _Tp>
294 : unary_function<typename _Tp::argument_type,
295 typename _Tp::result_type>,
296 binary_function<typename _Tp::first_argument_type,
297 typename _Tp::second_argument_type,
298 typename _Tp::result_type>
300 typedef typename _Tp::result_type result_type;
304 * Derives from unary_function or binary_function when it
305 * can. Specializations handle all of the easy cases. The primary
306 * template determines what to do with a class type, which may
307 * derive from both unary_function and binary_function.
309 template<typename _Tp>
310 struct _Reference_wrapper_base
311 : _Reference_wrapper_base_impl<
312 _Derives_from_unary_function<_Tp>::value,
313 _Derives_from_binary_function<_Tp>::value,
317 // - a function type (unary)
318 template<typename _Res, typename _T1>
319 struct _Reference_wrapper_base<_Res(_T1)>
320 : unary_function<_T1, _Res>
323 // - a function type (binary)
324 template<typename _Res, typename _T1, typename _T2>
325 struct _Reference_wrapper_base<_Res(_T1, _T2)>
326 : binary_function<_T1, _T2, _Res>
329 // - a function pointer type (unary)
330 template<typename _Res, typename _T1>
331 struct _Reference_wrapper_base<_Res(*)(_T1)>
332 : unary_function<_T1, _Res>
335 // - a function pointer type (binary)
336 template<typename _Res, typename _T1, typename _T2>
337 struct _Reference_wrapper_base<_Res(*)(_T1, _T2)>
338 : binary_function<_T1, _T2, _Res>
341 // - a pointer to member function type (unary, no qualifiers)
342 template<typename _Res, typename _T1>
343 struct _Reference_wrapper_base<_Res (_T1::*)()>
344 : unary_function<_T1*, _Res>
347 // - a pointer to member function type (binary, no qualifiers)
348 template<typename _Res, typename _T1, typename _T2>
349 struct _Reference_wrapper_base<_Res (_T1::*)(_T2)>
350 : binary_function<_T1*, _T2, _Res>
353 // - a pointer to member function type (unary, const)
354 template<typename _Res, typename _T1>
355 struct _Reference_wrapper_base<_Res (_T1::*)() const>
356 : unary_function<const _T1*, _Res>
359 // - a pointer to member function type (binary, const)
360 template<typename _Res, typename _T1, typename _T2>
361 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const>
362 : binary_function<const _T1*, _T2, _Res>
365 // - a pointer to member function type (unary, volatile)
366 template<typename _Res, typename _T1>
367 struct _Reference_wrapper_base<_Res (_T1::*)() volatile>
368 : unary_function<volatile _T1*, _Res>
371 // - a pointer to member function type (binary, volatile)
372 template<typename _Res, typename _T1, typename _T2>
373 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) volatile>
374 : binary_function<volatile _T1*, _T2, _Res>
377 // - a pointer to member function type (unary, const volatile)
378 template<typename _Res, typename _T1>
379 struct _Reference_wrapper_base<_Res (_T1::*)() const volatile>
380 : unary_function<const volatile _T1*, _Res>
383 // - a pointer to member function type (binary, const volatile)
384 template<typename _Res, typename _T1, typename _T2>
385 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const volatile>
386 : binary_function<const volatile _T1*, _T2, _Res>
389 /// 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 template<typename _Tp>
448 inline reference_wrapper<_Tp>
449 ref(reference_wrapper<_Tp> __t)
450 { return ref(__t.get()); }
452 template<typename _Tp>
453 inline reference_wrapper<const _Tp>
454 cref(reference_wrapper<_Tp> __t)
455 { return cref(__t.get()); }
457 template<typename _Tp, bool>
458 struct _Mem_fn_const_or_non
460 typedef const _Tp& type;
463 template<typename _Tp>
464 struct _Mem_fn_const_or_non<_Tp, false>
470 * Derives from @c unary_function or @c binary_function, or perhaps
471 * nothing, depending on the number of arguments provided. The
472 * primary template is the basis case, which derives nothing.
474 template<typename _Res, typename... _ArgTypes>
475 struct _Maybe_unary_or_binary_function { };
477 /// Derives from @c unary_function, as appropriate.
478 template<typename _Res, typename _T1>
479 struct _Maybe_unary_or_binary_function<_Res, _T1>
480 : std::unary_function<_T1, _Res> { };
482 /// Derives from @c binary_function, as appropriate.
483 template<typename _Res, typename _T1, typename _T2>
484 struct _Maybe_unary_or_binary_function<_Res, _T1, _T2>
485 : std::binary_function<_T1, _T2, _Res> { };
487 /// Implementation of @c mem_fn for member function pointers.
488 template<typename _Res, typename _Class, typename... _ArgTypes>
489 class _Mem_fn<_Res (_Class::*)(_ArgTypes...)>
490 : public _Maybe_unary_or_binary_function<_Res, _Class*, _ArgTypes...>
492 typedef _Res (_Class::*_Functor)(_ArgTypes...);
494 template<typename _Tp>
496 _M_call(_Tp& __object, const volatile _Class *,
497 _ArgTypes... __args) const
498 { return (__object.*__pmf)(__args...); }
500 template<typename _Tp>
502 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
503 { return ((*__ptr).*__pmf)(__args...); }
506 typedef _Res result_type;
508 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
512 operator()(_Class& __object, _ArgTypes... __args) const
513 { return (__object.*__pmf)(__args...); }
517 operator()(_Class* __object, _ArgTypes... __args) const
518 { return (__object->*__pmf)(__args...); }
520 // Handle smart pointers, references and pointers to derived
521 template<typename _Tp>
523 operator()(_Tp& __object, _ArgTypes... __args) const
524 { return _M_call(__object, &__object, __args...); }
530 /// Implementation of @c mem_fn for const member function pointers.
531 template<typename _Res, typename _Class, typename... _ArgTypes>
532 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) const>
533 : public _Maybe_unary_or_binary_function<_Res, const _Class*,
536 typedef _Res (_Class::*_Functor)(_ArgTypes...) const;
538 template<typename _Tp>
540 _M_call(_Tp& __object, const volatile _Class *,
541 _ArgTypes... __args) const
542 { return (__object.*__pmf)(__args...); }
544 template<typename _Tp>
546 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
547 { return ((*__ptr).*__pmf)(__args...); }
550 typedef _Res result_type;
552 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
556 operator()(const _Class& __object, _ArgTypes... __args) const
557 { return (__object.*__pmf)(__args...); }
561 operator()(const _Class* __object, _ArgTypes... __args) const
562 { return (__object->*__pmf)(__args...); }
564 // Handle smart pointers, references and pointers to derived
565 template<typename _Tp>
566 _Res operator()(_Tp& __object, _ArgTypes... __args) const
567 { return _M_call(__object, &__object, __args...); }
573 /// Implementation of @c mem_fn for volatile member function pointers.
574 template<typename _Res, typename _Class, typename... _ArgTypes>
575 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) volatile>
576 : public _Maybe_unary_or_binary_function<_Res, volatile _Class*,
579 typedef _Res (_Class::*_Functor)(_ArgTypes...) volatile;
581 template<typename _Tp>
583 _M_call(_Tp& __object, const volatile _Class *,
584 _ArgTypes... __args) const
585 { return (__object.*__pmf)(__args...); }
587 template<typename _Tp>
589 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
590 { return ((*__ptr).*__pmf)(__args...); }
593 typedef _Res result_type;
595 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
599 operator()(volatile _Class& __object, _ArgTypes... __args) const
600 { return (__object.*__pmf)(__args...); }
604 operator()(volatile _Class* __object, _ArgTypes... __args) const
605 { return (__object->*__pmf)(__args...); }
607 // Handle smart pointers, references and pointers to derived
608 template<typename _Tp>
610 operator()(_Tp& __object, _ArgTypes... __args) const
611 { return _M_call(__object, &__object, __args...); }
617 /// Implementation of @c mem_fn for const volatile member function pointers.
618 template<typename _Res, typename _Class, typename... _ArgTypes>
619 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) const volatile>
620 : public _Maybe_unary_or_binary_function<_Res, const volatile _Class*,
623 typedef _Res (_Class::*_Functor)(_ArgTypes...) const volatile;
625 template<typename _Tp>
627 _M_call(_Tp& __object, const volatile _Class *,
628 _ArgTypes... __args) const
629 { return (__object.*__pmf)(__args...); }
631 template<typename _Tp>
633 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
634 { return ((*__ptr).*__pmf)(__args...); }
637 typedef _Res result_type;
639 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
643 operator()(const volatile _Class& __object, _ArgTypes... __args) const
644 { return (__object.*__pmf)(__args...); }
648 operator()(const volatile _Class* __object, _ArgTypes... __args) const
649 { return (__object->*__pmf)(__args...); }
651 // Handle smart pointers, references and pointers to derived
652 template<typename _Tp>
653 _Res operator()(_Tp& __object, _ArgTypes... __args) const
654 { return _M_call(__object, &__object, __args...); }
661 template<typename _Res, typename _Class>
662 class _Mem_fn<_Res _Class::*>
664 // This bit of genius is due to Peter Dimov, improved slightly by
666 template<typename _Tp>
668 _M_call(_Tp& __object, _Class *) const
669 { return __object.*__pm; }
671 template<typename _Tp, typename _Up>
673 _M_call(_Tp& __object, _Up * const *) const
674 { return (*__object).*__pm; }
676 template<typename _Tp, typename _Up>
678 _M_call(_Tp& __object, const _Up * const *) const
679 { return (*__object).*__pm; }
681 template<typename _Tp>
683 _M_call(_Tp& __object, const _Class *) const
684 { return __object.*__pm; }
686 template<typename _Tp>
688 _M_call(_Tp& __ptr, const volatile void*) const
689 { return (*__ptr).*__pm; }
691 template<typename _Tp> static _Tp& __get_ref();
693 template<typename _Tp>
694 static __sfinae_types::__one __check_const(_Tp&, _Class*);
695 template<typename _Tp, typename _Up>
696 static __sfinae_types::__one __check_const(_Tp&, _Up * const *);
697 template<typename _Tp, typename _Up>
698 static __sfinae_types::__two __check_const(_Tp&, const _Up * const *);
699 template<typename _Tp>
700 static __sfinae_types::__two __check_const(_Tp&, const _Class*);
701 template<typename _Tp>
702 static __sfinae_types::__two __check_const(_Tp&, const volatile void*);
705 template<typename _Tp>
707 : _Mem_fn_const_or_non<_Res,
708 (sizeof(__sfinae_types::__two)
709 == sizeof(__check_const<_Tp>(__get_ref<_Tp>(), (_Tp*)0)))>
712 template<typename _Signature>
715 template<typename _CVMem, typename _Tp>
716 struct result<_CVMem(_Tp)>
717 : public _Result_type<_Tp> { };
719 template<typename _CVMem, typename _Tp>
720 struct result<_CVMem(_Tp&)>
721 : public _Result_type<_Tp> { };
724 _Mem_fn(_Res _Class::*__pm) : __pm(__pm) { }
728 operator()(_Class& __object) const
729 { return __object.*__pm; }
732 operator()(const _Class& __object) const
733 { return __object.*__pm; }
737 operator()(_Class* __object) const
738 { return __object->*__pm; }
741 operator()(const _Class* __object) const
742 { return __object->*__pm; }
744 // Handle smart pointers and derived
745 template<typename _Tp>
746 typename _Result_type<_Tp>::type
747 operator()(_Tp& __unknown) const
748 { return _M_call(__unknown, &__unknown); }
755 * @brief Returns a function object that forwards to the member
758 template<typename _Tp, typename _Class>
759 inline _Mem_fn<_Tp _Class::*>
760 mem_fn(_Tp _Class::* __pm)
762 return _Mem_fn<_Tp _Class::*>(__pm);
766 * @brief Determines if the given type _Tp is a function object
767 * should be treated as a subexpression when evaluating calls to
768 * function objects returned by bind(). [TR1 3.6.1]
770 template<typename _Tp>
771 struct is_bind_expression
772 { static const bool value = false; };
774 template<typename _Tp>
775 const bool is_bind_expression<_Tp>::value;
778 * @brief Determines if the given type _Tp is a placeholder in a
779 * bind() expression and, if so, which placeholder it is. [TR1 3.6.2]
781 template<typename _Tp>
782 struct is_placeholder
783 { static const int value = 0; };
785 template<typename _Tp>
786 const int is_placeholder<_Tp>::value;
788 /// The type of placeholder objects defined by libstdc++.
789 template<int _Num> struct _Placeholder { };
791 /** @namespace std::placeholders
792 * @brief ISO C++ 0x entities sub namespace for functional.
794 * Define a large number of placeholders. There is no way to
795 * simplify this with variadic templates, because we're introducing
796 * unique names for each.
798 namespace placeholders
811 _Placeholder<10> _10;
812 _Placeholder<11> _11;
813 _Placeholder<12> _12;
814 _Placeholder<13> _13;
815 _Placeholder<14> _14;
816 _Placeholder<15> _15;
817 _Placeholder<16> _16;
818 _Placeholder<17> _17;
819 _Placeholder<18> _18;
820 _Placeholder<19> _19;
821 _Placeholder<20> _20;
822 _Placeholder<21> _21;
823 _Placeholder<22> _22;
824 _Placeholder<23> _23;
825 _Placeholder<24> _24;
826 _Placeholder<25> _25;
827 _Placeholder<26> _26;
828 _Placeholder<27> _27;
829 _Placeholder<28> _28;
830 _Placeholder<29> _29;
835 * Partial specialization of is_placeholder that provides the placeholder
836 * number for the placeholder objects defined by libstdc++.
839 struct is_placeholder<_Placeholder<_Num> >
840 { static const int value = _Num; };
843 const int is_placeholder<_Placeholder<_Num> >::value;
846 * Stores a tuple of indices. Used by bind() to extract the elements
849 template<int... _Indexes>
850 struct _Index_tuple { };
852 /// Builds an _Index_tuple<0, 1, 2, ..., _Num-1>.
853 template<std::size_t _Num, typename _Tuple = _Index_tuple<> >
854 struct _Build_index_tuple;
856 template<std::size_t _Num, int... _Indexes>
857 struct _Build_index_tuple<_Num, _Index_tuple<_Indexes...> >
858 : _Build_index_tuple<_Num - 1,
859 _Index_tuple<_Indexes..., sizeof...(_Indexes)> >
862 template<int... _Indexes>
863 struct _Build_index_tuple<0, _Index_tuple<_Indexes...> >
865 typedef _Index_tuple<_Indexes...> __type;
869 * Used by _Safe_tuple_element to indicate that there is no tuple
870 * element at this position.
872 struct _No_tuple_element;
875 * Implementation helper for _Safe_tuple_element. This primary
876 * template handles the case where it is safe to use @c
879 template<int __i, typename _Tuple, bool _IsSafe>
880 struct _Safe_tuple_element_impl
881 : tuple_element<__i, _Tuple> { };
884 * Implementation helper for _Safe_tuple_element. This partial
885 * specialization handles the case where it is not safe to use @c
886 * tuple_element. We just return @c _No_tuple_element.
888 template<int __i, typename _Tuple>
889 struct _Safe_tuple_element_impl<__i, _Tuple, false>
891 typedef _No_tuple_element type;
895 * Like tuple_element, but returns @c _No_tuple_element when
896 * tuple_element would return an error.
898 template<int __i, typename _Tuple>
899 struct _Safe_tuple_element
900 : _Safe_tuple_element_impl<__i, _Tuple,
901 (__i >= 0 && __i < tuple_size<_Tuple>::value)>
905 * Maps an argument to bind() into an actual argument to the bound
906 * function object [TR1 3.6.3/5]. Only the first parameter should
907 * be specified: the rest are used to determine among the various
908 * implementations. Note that, although this class is a function
909 * object, it isn't entirely normal because it takes only two
910 * parameters regardless of the number of parameters passed to the
911 * bind expression. The first parameter is the bound argument and
912 * the second parameter is a tuple containing references to the
913 * rest of the arguments.
915 template<typename _Arg,
916 bool _IsBindExp = is_bind_expression<_Arg>::value,
917 bool _IsPlaceholder = (is_placeholder<_Arg>::value > 0)>
921 * If the argument is reference_wrapper<_Tp>, returns the
922 * underlying reference. [TR1 3.6.3/5 bullet 1]
924 template<typename _Tp>
925 class _Mu<reference_wrapper<_Tp>, false, false>
928 typedef _Tp& result_type;
930 /* Note: This won't actually work for const volatile
931 * reference_wrappers, because reference_wrapper::get() is const
932 * but not volatile-qualified. This might be a defect in the TR.
934 template<typename _CVRef, typename _Tuple>
936 operator()(_CVRef& __arg, _Tuple&&) const volatile
937 { return __arg.get(); }
941 * If the argument is a bind expression, we invoke the underlying
942 * function object with the same cv-qualifiers as we are given and
943 * pass along all of our arguments (unwrapped). [TR1 3.6.3/5 bullet 2]
945 template<typename _Arg>
946 class _Mu<_Arg, true, false>
949 template<typename _Signature> class result;
951 // Determine the result type when we pass the arguments along. This
952 // involves passing along the cv-qualifiers placed on _Mu and
953 // unwrapping the argument bundle.
954 template<typename _CVMu, typename _CVArg, typename... _Args>
955 class result<_CVMu(_CVArg, tuple<_Args...>)>
956 : public result_of<_CVArg(_Args...)> { };
958 template<typename _CVArg, typename... _Args>
959 typename result_of<_CVArg(_Args...)>::type
960 operator()(_CVArg& __arg,
961 tuple<_Args...>&& __tuple) const volatile
963 // Construct an index tuple and forward to __call
964 typedef typename _Build_index_tuple<sizeof...(_Args)>::__type
966 return this->__call(__arg, std::move(__tuple), _Indexes());
970 // Invokes the underlying function object __arg by unpacking all
971 // of the arguments in the tuple.
972 template<typename _CVArg, typename... _Args, int... _Indexes>
973 typename result_of<_CVArg(_Args...)>::type
974 __call(_CVArg& __arg, tuple<_Args...>&& __tuple,
975 const _Index_tuple<_Indexes...>&) const volatile
977 return __arg(std::forward<_Args>(get<_Indexes>(__tuple))...);
982 * If the argument is a placeholder for the Nth argument, returns
983 * a reference to the Nth argument to the bind function object.
984 * [TR1 3.6.3/5 bullet 3]
986 template<typename _Arg>
987 class _Mu<_Arg, false, true>
990 template<typename _Signature> class result;
992 template<typename _CVMu, typename _CVArg, typename _Tuple>
993 class result<_CVMu(_CVArg, _Tuple)>
995 // Add a reference, if it hasn't already been done for us.
996 // This allows us to be a little bit sloppy in constructing
997 // the tuple that we pass to result_of<...>.
998 typedef typename _Safe_tuple_element<(is_placeholder<_Arg>::value
1003 typedef typename add_rvalue_reference<__base_type>::type type;
1006 template<typename _Tuple>
1007 typename result<_Mu(_Arg, _Tuple)>::type
1008 operator()(const volatile _Arg&, _Tuple&& __tuple) const volatile
1010 return std::forward<typename result<_Mu(_Arg, _Tuple)>::type>(
1011 ::std::get<(is_placeholder<_Arg>::value - 1)>(__tuple));
1016 * If the argument is just a value, returns a reference to that
1017 * value. The cv-qualifiers on the reference are the same as the
1018 * cv-qualifiers on the _Mu object. [TR1 3.6.3/5 bullet 4]
1020 template<typename _Arg>
1021 class _Mu<_Arg, false, false>
1024 template<typename _Signature> struct result;
1026 template<typename _CVMu, typename _CVArg, typename _Tuple>
1027 struct result<_CVMu(_CVArg, _Tuple)>
1029 typedef typename add_lvalue_reference<_CVArg>::type type;
1032 // Pick up the cv-qualifiers of the argument
1033 template<typename _CVArg, typename _Tuple>
1035 operator()(_CVArg&& __arg, _Tuple&&) const volatile
1036 { return std::forward<_CVArg>(__arg); }
1040 * Maps member pointers into instances of _Mem_fn but leaves all
1041 * other function objects untouched. Used by tr1::bind(). The
1042 * primary template handles the non--member-pointer case.
1044 template<typename _Tp>
1045 struct _Maybe_wrap_member_pointer
1050 __do_wrap(const _Tp& __x)
1055 * Maps member pointers into instances of _Mem_fn but leaves all
1056 * other function objects untouched. Used by tr1::bind(). This
1057 * partial specialization handles the member pointer case.
1059 template<typename _Tp, typename _Class>
1060 struct _Maybe_wrap_member_pointer<_Tp _Class::*>
1062 typedef _Mem_fn<_Tp _Class::*> type;
1065 __do_wrap(_Tp _Class::* __pm)
1066 { return type(__pm); }
1069 // Specialization needed to prevent "forming reference to void" errors when
1070 // bind<void>() is called, because argument deduction instantiates
1071 // _Maybe_wrap_member_pointer<void> outside the immediate context where
1074 struct _Maybe_wrap_member_pointer<void>
1079 /// Type of the function object returned from bind().
1080 template<typename _Signature>
1083 template<typename _Functor, typename... _Bound_args>
1084 class _Bind<_Functor(_Bound_args...)>
1085 : public _Weak_result_type<_Functor>
1087 typedef _Bind __self_type;
1088 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
1092 tuple<_Bound_args...> _M_bound_args;
1095 template<typename _Result, typename... _Args, int... _Indexes>
1097 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>)
1099 return _M_f(_Mu<_Bound_args>()
1100 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1104 template<typename _Result, typename... _Args, int... _Indexes>
1106 __call_c(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>) const
1108 return _M_f(_Mu<_Bound_args>()
1109 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1114 template<typename _Result, typename... _Args, int... _Indexes>
1116 __call_v(tuple<_Args...>&& __args,
1117 _Index_tuple<_Indexes...>) volatile
1119 return _M_f(_Mu<_Bound_args>()
1120 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1123 // Call as const volatile
1124 template<typename _Result, typename... _Args, int... _Indexes>
1126 __call_c_v(tuple<_Args...>&& __args,
1127 _Index_tuple<_Indexes...>) const volatile
1129 return _M_f(_Mu<_Bound_args>()
1130 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1135 explicit _Bind(_Functor __f, _Bound_args... __bound_args)
1136 : _M_f(std::forward<_Functor>(__f)),
1137 _M_bound_args(std::forward<_Bound_args>(__bound_args)...)
1141 template<typename... _Args, typename _Result
1142 = decltype( std::declval<_Functor>()(
1143 _Mu<_Bound_args>()( std::declval<_Bound_args&>(),
1144 std::declval<tuple<_Args...>&&>() )... ) )>
1146 operator()(_Args&&... __args)
1148 return this->__call<_Result>(tuple<_Args...>
1149 (std::forward<_Args>(__args)...),
1154 template<typename... _Args, typename _Result
1155 = decltype( std::declval<const _Functor>()(
1156 _Mu<_Bound_args>()( std::declval<const _Bound_args&>(),
1157 std::declval<tuple<_Args...>&&>() )... ) )>
1159 operator()(_Args&&... __args) const
1161 return this->__call_c<_Result>(tuple<_Args...>
1162 (std::forward<_Args>(__args)...),
1168 template<typename... _Args, typename _Result
1169 = decltype( std::declval<volatile _Functor>()(
1170 _Mu<_Bound_args>()( std::declval<volatile _Bound_args&>(),
1171 std::declval<tuple<_Args...>&&>() )... ) )>
1173 operator()(_Args&&... __args) volatile
1175 return this->__call_v<_Result>(tuple<_Args...>
1176 (std::forward<_Args>(__args)...),
1180 // Call as const volatile
1181 template<typename... _Args, typename _Result
1182 = decltype( std::declval<const volatile _Functor>()(
1183 _Mu<_Bound_args>()( std::declval<const volatile _Bound_args&>(),
1184 std::declval<tuple<_Args...>&&>() )... ) )>
1186 operator()(_Args&&... __args) const volatile
1188 return this->__call_c_v<_Result>(tuple<_Args...>
1189 (std::forward<_Args>(__args)...),
1195 /// Type of the function object returned from bind<R>().
1196 template<typename _Result, typename _Signature>
1197 struct _Bind_result;
1199 template<typename _Result, typename _Functor, typename... _Bound_args>
1200 class _Bind_result<_Result, _Functor(_Bound_args...)>
1202 typedef _Bind_result __self_type;
1203 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
1207 tuple<_Bound_args...> _M_bound_args;
1210 template<typename _Res>
1211 struct __enable_if_void : enable_if<is_void<_Res>::value, int> { };
1212 template<typename _Res>
1213 struct __disable_if_void : enable_if<!is_void<_Res>::value, int> { };
1216 template<typename _Res, typename... _Args, int... _Indexes>
1218 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1219 typename __disable_if_void<_Res>::type = 0)
1221 return _M_f(_Mu<_Bound_args>()
1222 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1225 // Call unqualified, return void
1226 template<typename _Res, typename... _Args, int... _Indexes>
1228 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1229 typename __enable_if_void<_Res>::type = 0)
1231 _M_f(_Mu<_Bound_args>()
1232 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1236 template<typename _Res, typename... _Args, int... _Indexes>
1238 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1239 typename __disable_if_void<_Res>::type = 0) const
1241 return _M_f(_Mu<_Bound_args>()
1242 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1245 // Call as const, return void
1246 template<typename _Res, typename... _Args, int... _Indexes>
1248 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1249 typename __enable_if_void<_Res>::type = 0) const
1251 _M_f(_Mu<_Bound_args>()
1252 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1256 template<typename _Res, typename... _Args, int... _Indexes>
1258 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1259 typename __disable_if_void<_Res>::type = 0) volatile
1261 return _M_f(_Mu<_Bound_args>()
1262 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1265 // Call as volatile, return void
1266 template<typename _Res, typename... _Args, int... _Indexes>
1268 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1269 typename __enable_if_void<_Res>::type = 0) volatile
1271 _M_f(_Mu<_Bound_args>()
1272 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1275 // Call as const volatile
1276 template<typename _Res, typename... _Args, int... _Indexes>
1278 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1279 typename __disable_if_void<_Res>::type = 0) const volatile
1281 return _M_f(_Mu<_Bound_args>()
1282 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1285 // Call as const volatile, return void
1286 template<typename _Res, typename... _Args, int... _Indexes>
1288 __call(tuple<_Args...>&& __args,
1289 _Index_tuple<_Indexes...>,
1290 typename __enable_if_void<_Res>::type = 0) const volatile
1292 _M_f(_Mu<_Bound_args>()
1293 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1297 typedef _Result result_type;
1300 _Bind_result(_Functor __f, _Bound_args... __bound_args)
1301 : _M_f(std::forward<_Functor>(__f)),
1302 _M_bound_args(std::forward<_Bound_args>(__bound_args)...)
1306 template<typename... _Args>
1308 operator()(_Args&&... __args)
1310 return this->__call<_Result>(
1311 tuple<_Args...>(std::forward<_Args...>(__args)...),
1316 template<typename... _Args>
1318 operator()(_Args&&... __args) const
1320 return this->__call<_Result>(
1321 tuple<_Args...>(std::forward<_Args...>(__args)...),
1326 template<typename... _Args>
1328 operator()(_Args&&... __args) volatile
1330 return this->__call<_Result>(
1331 tuple<_Args...>(std::forward<_Args...>(__args)...),
1335 // Call as const volatile
1336 template<typename... _Args>
1338 operator()(_Args&&... __args) const volatile
1340 return this->__call<_Result>(
1341 tuple<_Args...>(std::forward<_Args...>(__args)...),
1346 /// Class template _Bind is always a bind expression.
1347 template<typename _Signature>
1348 struct is_bind_expression<_Bind<_Signature> >
1349 { static const bool value = true; };
1351 template<typename _Signature>
1352 const bool is_bind_expression<_Bind<_Signature> >::value;
1354 /// Class template _Bind_result is always a bind expression.
1355 template<typename _Result, typename _Signature>
1356 struct is_bind_expression<_Bind_result<_Result, _Signature> >
1357 { static const bool value = true; };
1359 template<typename _Result, typename _Signature>
1360 const bool is_bind_expression<_Bind_result<_Result, _Signature> >::value;
1363 template<typename _Functor, typename... _ArgTypes>
1365 _Bind<typename _Maybe_wrap_member_pointer<_Functor>::type(_ArgTypes...)>
1366 bind(_Functor __f, _ArgTypes... __args)
1368 typedef _Maybe_wrap_member_pointer<_Functor> __maybe_type;
1369 typedef typename __maybe_type::type __functor_type;
1370 typedef _Bind<__functor_type(_ArgTypes...)> __result_type;
1371 return __result_type(__maybe_type::__do_wrap(__f),
1372 std::forward<_ArgTypes>(__args)...);
1375 template<typename _Result, typename _Functor, typename... _ArgTypes>
1377 _Bind_result<_Result,
1378 typename _Maybe_wrap_member_pointer<_Functor>::type
1380 bind(_Functor __f, _ArgTypes... __args)
1382 typedef _Maybe_wrap_member_pointer<_Functor> __maybe_type;
1383 typedef typename __maybe_type::type __functor_type;
1384 typedef _Bind_result<_Result, __functor_type(_ArgTypes...)>
1386 return __result_type(__maybe_type::__do_wrap(__f),
1387 std::forward<_ArgTypes>(__args)...);
1391 * @brief Exception class thrown when class template function's
1392 * operator() is called with an empty target.
1393 * @ingroup exceptions
1395 class bad_function_call : public std::exception { };
1398 * The integral constant expression 0 can be converted into a
1399 * pointer to this type. It is used by the function template to
1400 * accept NULL pointers.
1402 struct _M_clear_type;
1405 * Trait identifying "location-invariant" types, meaning that the
1406 * address of the object (or any of its members) will not escape.
1407 * Also implies a trivial copy constructor and assignment operator.
1409 template<typename _Tp>
1410 struct __is_location_invariant
1411 : integral_constant<bool, (is_pointer<_Tp>::value
1412 || is_member_pointer<_Tp>::value)>
1415 class _Undefined_class;
1420 const void* _M_const_object;
1421 void (*_M_function_pointer)();
1422 void (_Undefined_class::*_M_member_pointer)();
1427 void* _M_access() { return &_M_pod_data[0]; }
1428 const void* _M_access() const { return &_M_pod_data[0]; }
1430 template<typename _Tp>
1433 { return *static_cast<_Tp*>(_M_access()); }
1435 template<typename _Tp>
1438 { return *static_cast<const _Tp*>(_M_access()); }
1440 _Nocopy_types _M_unused;
1441 char _M_pod_data[sizeof(_Nocopy_types)];
1444 enum _Manager_operation
1452 // Simple type wrapper that helps avoid annoying const problems
1453 // when casting between void pointers and pointers-to-pointers.
1454 template<typename _Tp>
1455 struct _Simple_type_wrapper
1457 _Simple_type_wrapper(_Tp __value) : __value(__value) { }
1462 template<typename _Tp>
1463 struct __is_location_invariant<_Simple_type_wrapper<_Tp> >
1464 : __is_location_invariant<_Tp>
1467 // Converts a reference to a function object into a callable
1469 template<typename _Functor>
1471 __callable_functor(_Functor& __f)
1474 template<typename _Member, typename _Class>
1475 inline _Mem_fn<_Member _Class::*>
1476 __callable_functor(_Member _Class::* &__p)
1477 { return mem_fn(__p); }
1479 template<typename _Member, typename _Class>
1480 inline _Mem_fn<_Member _Class::*>
1481 __callable_functor(_Member _Class::* const &__p)
1482 { return mem_fn(__p); }
1484 template<typename _Signature>
1487 /// Base class of all polymorphic function object wrappers.
1488 class _Function_base
1491 static const std::size_t _M_max_size = sizeof(_Nocopy_types);
1492 static const std::size_t _M_max_align = __alignof__(_Nocopy_types);
1494 template<typename _Functor>
1498 static const bool __stored_locally =
1499 (__is_location_invariant<_Functor>::value
1500 && sizeof(_Functor) <= _M_max_size
1501 && __alignof__(_Functor) <= _M_max_align
1502 && (_M_max_align % __alignof__(_Functor) == 0));
1504 typedef integral_constant<bool, __stored_locally> _Local_storage;
1506 // Retrieve a pointer to the function object
1508 _M_get_pointer(const _Any_data& __source)
1510 const _Functor* __ptr =
1511 __stored_locally? &__source._M_access<_Functor>()
1512 /* have stored a pointer */ : __source._M_access<_Functor*>();
1513 return const_cast<_Functor*>(__ptr);
1516 // Clone a location-invariant function object that fits within
1517 // an _Any_data structure.
1519 _M_clone(_Any_data& __dest, const _Any_data& __source, true_type)
1521 new (__dest._M_access()) _Functor(__source._M_access<_Functor>());
1524 // Clone a function object that is not location-invariant or
1525 // that cannot fit into an _Any_data structure.
1527 _M_clone(_Any_data& __dest, const _Any_data& __source, false_type)
1529 __dest._M_access<_Functor*>() =
1530 new _Functor(*__source._M_access<_Functor*>());
1533 // Destroying a location-invariant object may still require
1536 _M_destroy(_Any_data& __victim, true_type)
1538 __victim._M_access<_Functor>().~_Functor();
1541 // Destroying an object located on the heap.
1543 _M_destroy(_Any_data& __victim, false_type)
1545 delete __victim._M_access<_Functor*>();
1550 _M_manager(_Any_data& __dest, const _Any_data& __source,
1551 _Manager_operation __op)
1556 case __get_type_info:
1557 __dest._M_access<const type_info*>() = &typeid(_Functor);
1560 case __get_functor_ptr:
1561 __dest._M_access<_Functor*>() = _M_get_pointer(__source);
1564 case __clone_functor:
1565 _M_clone(__dest, __source, _Local_storage());
1568 case __destroy_functor:
1569 _M_destroy(__dest, _Local_storage());
1576 _M_init_functor(_Any_data& __functor, _Functor&& __f)
1577 { _M_init_functor(__functor, std::move(__f), _Local_storage()); }
1579 template<typename _Signature>
1581 _M_not_empty_function(const function<_Signature>& __f)
1582 { return static_cast<bool>(__f); }
1584 template<typename _Tp>
1586 _M_not_empty_function(const _Tp*& __fp)
1589 template<typename _Class, typename _Tp>
1591 _M_not_empty_function(_Tp _Class::* const& __mp)
1594 template<typename _Tp>
1596 _M_not_empty_function(const _Tp&)
1601 _M_init_functor(_Any_data& __functor, _Functor&& __f, true_type)
1602 { new (__functor._M_access()) _Functor(std::move(__f)); }
1605 _M_init_functor(_Any_data& __functor, _Functor&& __f, false_type)
1606 { __functor._M_access<_Functor*>() = new _Functor(std::move(__f)); }
1609 template<typename _Functor>
1610 class _Ref_manager : public _Base_manager<_Functor*>
1612 typedef _Function_base::_Base_manager<_Functor*> _Base;
1616 _M_manager(_Any_data& __dest, const _Any_data& __source,
1617 _Manager_operation __op)
1622 case __get_type_info:
1623 __dest._M_access<const type_info*>() = &typeid(_Functor);
1626 case __get_functor_ptr:
1627 __dest._M_access<_Functor*>() = *_Base::_M_get_pointer(__source);
1628 return is_const<_Functor>::value;
1632 _Base::_M_manager(__dest, __source, __op);
1638 _M_init_functor(_Any_data& __functor, reference_wrapper<_Functor> __f)
1640 // TBD: Use address_of function instead.
1641 _Base::_M_init_functor(__functor, &__f.get());
1645 _Function_base() : _M_manager(0) { }
1650 _M_manager(_M_functor, _M_functor, __destroy_functor);
1654 bool _M_empty() const { return !_M_manager; }
1656 typedef bool (*_Manager_type)(_Any_data&, const _Any_data&,
1657 _Manager_operation);
1659 _Any_data _M_functor;
1660 _Manager_type _M_manager;
1663 template<typename _Signature, typename _Functor>
1664 class _Function_handler;
1666 template<typename _Res, typename _Functor, typename... _ArgTypes>
1667 class _Function_handler<_Res(_ArgTypes...), _Functor>
1668 : public _Function_base::_Base_manager<_Functor>
1670 typedef _Function_base::_Base_manager<_Functor> _Base;
1674 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1676 return (*_Base::_M_get_pointer(__functor))(
1677 std::forward<_ArgTypes>(__args)...);
1681 template<typename _Functor, typename... _ArgTypes>
1682 class _Function_handler<void(_ArgTypes...), _Functor>
1683 : public _Function_base::_Base_manager<_Functor>
1685 typedef _Function_base::_Base_manager<_Functor> _Base;
1689 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1691 (*_Base::_M_get_pointer(__functor))(
1692 std::forward<_ArgTypes>(__args)...);
1696 template<typename _Res, typename _Functor, typename... _ArgTypes>
1697 class _Function_handler<_Res(_ArgTypes...), reference_wrapper<_Functor> >
1698 : public _Function_base::_Ref_manager<_Functor>
1700 typedef _Function_base::_Ref_manager<_Functor> _Base;
1704 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1706 return __callable_functor(**_Base::_M_get_pointer(__functor))(
1707 std::forward<_ArgTypes>(__args)...);
1711 template<typename _Functor, typename... _ArgTypes>
1712 class _Function_handler<void(_ArgTypes...), reference_wrapper<_Functor> >
1713 : public _Function_base::_Ref_manager<_Functor>
1715 typedef _Function_base::_Ref_manager<_Functor> _Base;
1719 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1721 __callable_functor(**_Base::_M_get_pointer(__functor))(
1722 std::forward<_ArgTypes>(__args)...);
1726 template<typename _Class, typename _Member, typename _Res,
1727 typename... _ArgTypes>
1728 class _Function_handler<_Res(_ArgTypes...), _Member _Class::*>
1729 : public _Function_handler<void(_ArgTypes...), _Member _Class::*>
1731 typedef _Function_handler<void(_ArgTypes...), _Member _Class::*>
1736 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1738 return mem_fn(_Base::_M_get_pointer(__functor)->__value)(
1739 std::forward<_ArgTypes>(__args)...);
1743 template<typename _Class, typename _Member, typename... _ArgTypes>
1744 class _Function_handler<void(_ArgTypes...), _Member _Class::*>
1745 : public _Function_base::_Base_manager<
1746 _Simple_type_wrapper< _Member _Class::* > >
1748 typedef _Member _Class::* _Functor;
1749 typedef _Simple_type_wrapper<_Functor> _Wrapper;
1750 typedef _Function_base::_Base_manager<_Wrapper> _Base;
1754 _M_manager(_Any_data& __dest, const _Any_data& __source,
1755 _Manager_operation __op)
1760 case __get_type_info:
1761 __dest._M_access<const type_info*>() = &typeid(_Functor);
1764 case __get_functor_ptr:
1765 __dest._M_access<_Functor*>() =
1766 &_Base::_M_get_pointer(__source)->__value;
1770 _Base::_M_manager(__dest, __source, __op);
1776 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1778 mem_fn(_Base::_M_get_pointer(__functor)->__value)(
1779 std::forward<_ArgTypes>(__args)...);
1784 template<typename _Res, typename... _ArgTypes>
1785 class function<_Res(_ArgTypes...)>
1786 : public _Maybe_unary_or_binary_function<_Res, _ArgTypes...>,
1787 private _Function_base
1789 typedef _Res _Signature_type(_ArgTypes...);
1791 struct _Useless { };
1794 typedef _Res result_type;
1796 // [3.7.2.1] construct/copy/destroy
1799 * @brief Default construct creates an empty function call wrapper.
1800 * @post @c !(bool)*this
1803 function() : _Function_base() { }
1806 * @brief Default construct creates an empty function call wrapper.
1807 * @post @c !(bool)*this
1809 function(_M_clear_type*) : _Function_base() { }
1812 * @brief %Function copy constructor.
1813 * @param x A %function object with identical call signature.
1814 * @post @c (bool)*this == (bool)x
1816 * The newly-created %function contains a copy of the target of @a
1817 * x (if it has one).
1819 function(const function& __x);
1822 * @brief %Function move constructor.
1823 * @param x A %function object rvalue with identical call signature.
1825 * The newly-created %function contains the target of @a x
1828 function(function&& __x) : _Function_base()
1833 // TODO: needs allocator_arg_t
1836 * @brief Builds a %function that targets a copy of the incoming
1838 * @param f A %function object that is callable with parameters of
1839 * type @c T1, @c T2, ..., @c TN and returns a value convertible
1842 * The newly-created %function object will target a copy of @a
1843 * f. If @a f is @c reference_wrapper<F>, then this function
1844 * object will contain a reference to the function object @c
1845 * f.get(). If @a f is a NULL function pointer or NULL
1846 * pointer-to-member, the newly-created object will be empty.
1848 * If @a f is a non-NULL function pointer or an object of type @c
1849 * reference_wrapper<F>, this function will not throw.
1851 template<typename _Functor>
1852 function(_Functor __f,
1853 typename __gnu_cxx::__enable_if<
1854 !is_integral<_Functor>::value, _Useless>::__type
1858 * @brief %Function assignment operator.
1859 * @param x A %function with identical call signature.
1860 * @post @c (bool)*this == (bool)x
1863 * The target of @a x is copied to @c *this. If @a x has no
1864 * target, then @c *this will be empty.
1866 * If @a x targets a function pointer or a reference to a function
1867 * object, then this operation will not throw an %exception.
1870 operator=(const function& __x)
1872 function(__x).swap(*this);
1877 * @brief %Function move-assignment operator.
1878 * @param x A %function rvalue with identical call signature.
1881 * The target of @a x is moved to @c *this. If @a x has no
1882 * target, then @c *this will be empty.
1884 * If @a x targets a function pointer or a reference to a function
1885 * object, then this operation will not throw an %exception.
1888 operator=(function&& __x)
1890 function(std::move(__x)).swap(*this);
1895 * @brief %Function assignment to zero.
1896 * @post @c !(bool)*this
1899 * The target of @c *this is deallocated, leaving it empty.
1902 operator=(_M_clear_type*)
1906 _M_manager(_M_functor, _M_functor, __destroy_functor);
1914 * @brief %Function assignment to a new target.
1915 * @param f A %function object that is callable with parameters of
1916 * type @c T1, @c T2, ..., @c TN and returns a value convertible
1920 * This %function object wrapper will target a copy of @a
1921 * f. If @a f is @c reference_wrapper<F>, then this function
1922 * object will contain a reference to the function object @c
1923 * f.get(). If @a f is a NULL function pointer or NULL
1924 * pointer-to-member, @c this object will be empty.
1926 * If @a f is a non-NULL function pointer or an object of type @c
1927 * reference_wrapper<F>, this function will not throw.
1929 template<typename _Functor>
1930 typename __gnu_cxx::__enable_if<!is_integral<_Functor>::value,
1932 operator=(_Functor&& __f)
1934 function(std::forward<_Functor>(__f)).swap(*this);
1939 template<typename _Functor>
1940 typename __gnu_cxx::__enable_if<!is_integral<_Functor>::value,
1942 operator=(reference_wrapper<_Functor> __f)
1944 function(__f).swap(*this);
1948 // [3.7.2.2] function modifiers
1951 * @brief Swap the targets of two %function objects.
1952 * @param f A %function with identical call signature.
1954 * Swap the targets of @c this function object and @a f. This
1955 * function will not throw an %exception.
1957 void swap(function& __x)
1959 _Any_data __old_functor = _M_functor;
1960 _M_functor = __x._M_functor;
1961 __x._M_functor = __old_functor;
1962 _Manager_type __old_manager = _M_manager;
1963 _M_manager = __x._M_manager;
1964 __x._M_manager = __old_manager;
1965 _Invoker_type __old_invoker = _M_invoker;
1966 _M_invoker = __x._M_invoker;
1967 __x._M_invoker = __old_invoker;
1970 // TODO: needs allocator_arg_t
1972 template<typename _Functor, typename _Alloc>
1974 assign(_Functor&& __f, const _Alloc& __a)
1976 function(allocator_arg, __a,
1977 std::forward<_Functor>(__f)).swap(*this);
1981 // [3.7.2.3] function capacity
1984 * @brief Determine if the %function wrapper has a target.
1986 * @return @c true when this %function object contains a target,
1987 * or @c false when it is empty.
1989 * This function will not throw an %exception.
1991 explicit operator bool() const
1992 { return !_M_empty(); }
1994 // [3.7.2.4] function invocation
1997 * @brief Invokes the function targeted by @c *this.
1998 * @returns the result of the target.
1999 * @throws bad_function_call when @c !(bool)*this
2001 * The function call operator invokes the target function object
2002 * stored by @c this.
2004 _Res operator()(_ArgTypes... __args) const;
2007 // [3.7.2.5] function target access
2009 * @brief Determine the type of the target of this function object
2012 * @returns the type identifier of the target function object, or
2013 * @c typeid(void) if @c !(bool)*this.
2015 * This function will not throw an %exception.
2017 const type_info& target_type() const;
2020 * @brief Access the stored target function object.
2022 * @return Returns a pointer to the stored target function object,
2023 * if @c typeid(Functor).equals(target_type()); otherwise, a NULL
2026 * This function will not throw an %exception.
2028 template<typename _Functor> _Functor* target();
2031 template<typename _Functor> const _Functor* target() const;
2034 // deleted overloads
2035 template<typename _Res2, typename... _ArgTypes2>
2036 void operator==(const function<_Res2(_ArgTypes2...)>&) const = delete;
2037 template<typename _Res2, typename... _ArgTypes2>
2038 void operator!=(const function<_Res2(_ArgTypes2...)>&) const = delete;
2041 typedef _Res (*_Invoker_type)(const _Any_data&, _ArgTypes...);
2042 _Invoker_type _M_invoker;
2045 template<typename _Res, typename... _ArgTypes>
2046 function<_Res(_ArgTypes...)>::
2047 function(const function& __x)
2050 if (static_cast<bool>(__x))
2052 _M_invoker = __x._M_invoker;
2053 _M_manager = __x._M_manager;
2054 __x._M_manager(_M_functor, __x._M_functor, __clone_functor);
2058 template<typename _Res, typename... _ArgTypes>
2059 template<typename _Functor>
2060 function<_Res(_ArgTypes...)>::
2061 function(_Functor __f,
2062 typename __gnu_cxx::__enable_if<
2063 !is_integral<_Functor>::value, _Useless>::__type)
2066 typedef _Function_handler<_Signature_type, _Functor> _My_handler;
2068 if (_My_handler::_M_not_empty_function(__f))
2070 _M_invoker = &_My_handler::_M_invoke;
2071 _M_manager = &_My_handler::_M_manager;
2072 _My_handler::_M_init_functor(_M_functor, std::move(__f));
2076 template<typename _Res, typename... _ArgTypes>
2078 function<_Res(_ArgTypes...)>::
2079 operator()(_ArgTypes... __args) const
2082 __throw_bad_function_call();
2083 return _M_invoker(_M_functor, std::forward<_ArgTypes>(__args)...);
2087 template<typename _Res, typename... _ArgTypes>
2089 function<_Res(_ArgTypes...)>::
2094 _Any_data __typeinfo_result;
2095 _M_manager(__typeinfo_result, _M_functor, __get_type_info);
2096 return *__typeinfo_result._M_access<const type_info*>();
2099 return typeid(void);
2102 template<typename _Res, typename... _ArgTypes>
2103 template<typename _Functor>
2105 function<_Res(_ArgTypes...)>::
2108 if (typeid(_Functor) == target_type() && _M_manager)
2111 if (_M_manager(__ptr, _M_functor, __get_functor_ptr)
2112 && !is_const<_Functor>::value)
2115 return __ptr._M_access<_Functor*>();
2121 template<typename _Res, typename... _ArgTypes>
2122 template<typename _Functor>
2124 function<_Res(_ArgTypes...)>::
2127 if (typeid(_Functor) == target_type() && _M_manager)
2130 _M_manager(__ptr, _M_functor, __get_functor_ptr);
2131 return __ptr._M_access<const _Functor*>();
2138 // [20.7.15.2.6] null pointer comparisons
2141 * @brief Compares a polymorphic function object wrapper against 0
2142 * (the NULL pointer).
2143 * @returns @c true if the wrapper has no target, @c false otherwise
2145 * This function will not throw an %exception.
2147 template<typename _Res, typename... _Args>
2149 operator==(const function<_Res(_Args...)>& __f, _M_clear_type*)
2150 { return !static_cast<bool>(__f); }
2153 template<typename _Res, typename... _Args>
2155 operator==(_M_clear_type*, const function<_Res(_Args...)>& __f)
2156 { return !static_cast<bool>(__f); }
2159 * @brief Compares a polymorphic function object wrapper against 0
2160 * (the NULL pointer).
2161 * @returns @c false if the wrapper has no target, @c true otherwise
2163 * This function will not throw an %exception.
2165 template<typename _Res, typename... _Args>
2167 operator!=(const function<_Res(_Args...)>& __f, _M_clear_type*)
2168 { return static_cast<bool>(__f); }
2171 template<typename _Res, typename... _Args>
2173 operator!=(_M_clear_type*, const function<_Res(_Args...)>& __f)
2174 { return static_cast<bool>(__f); }
2176 // [20.7.15.2.7] specialized algorithms
2179 * @brief Swap the targets of two polymorphic function object wrappers.
2181 * This function will not throw an %exception.
2183 template<typename _Res, typename... _Args>
2185 swap(function<_Res(_Args...)>& __x, function<_Res(_Args...)>& __y)
2189 #endif // __GXX_EXPERIMENTAL_CXX0X__
2191 #endif // _GLIBCXX_FUNCTIONAL