1 // <functional> -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 // Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 // <http://www.gnu.org/licenses/>.
28 * Silicon Graphics Computer Systems, Inc.
30 * Permission to use, copy, modify, distribute and sell this software
31 * and its documentation for any purpose is hereby granted without fee,
32 * provided that the above copyright notice appear in all copies and
33 * that both that copyright notice and this permission notice appear
34 * in supporting documentation. Silicon Graphics makes no
35 * representations about the suitability of this software for any
36 * purpose. It is provided "as is" without express or implied warranty.
40 /** @file include/functional
41 * This is a Standard C++ Library header.
44 #ifndef _GLIBCXX_FUNCTIONAL
45 #define _GLIBCXX_FUNCTIONAL 1
47 #pragma GCC system_header
49 #include <bits/c++config.h>
50 #include <bits/stl_function.h>
52 #ifdef __GXX_EXPERIMENTAL_CXX0X__
57 #include <type_traits>
58 #include <bits/functional_hash.h>
62 template<typename _MemberPointer>
66 * Actual implementation of _Has_result_type, which uses SFINAE to
67 * determine if the type _Tp has a publicly-accessible member type
70 template<typename _Tp>
71 class _Has_result_type_helper : __sfinae_types
73 template<typename _Up>
77 template<typename _Up>
78 static __one __test(_Wrap_type<typename _Up::result_type>*);
80 template<typename _Up>
81 static __two __test(...);
84 static const bool value = sizeof(__test<_Tp>(0)) == 1;
87 template<typename _Tp>
88 struct _Has_result_type
89 : integral_constant<bool,
90 _Has_result_type_helper<typename remove_cv<_Tp>::type>::value>
96 /// If we have found a result_type, extract it.
97 template<bool _Has_result_type, typename _Functor>
98 struct _Maybe_get_result_type
101 template<typename _Functor>
102 struct _Maybe_get_result_type<true, _Functor>
104 typedef typename _Functor::result_type result_type;
108 * Base class for any function object that has a weak result type, as
109 * defined in 3.3/3 of TR1.
111 template<typename _Functor>
112 struct _Weak_result_type_impl
113 : _Maybe_get_result_type<_Has_result_type<_Functor>::value, _Functor>
116 /// Retrieve the result type for a function type.
117 template<typename _Res, typename... _ArgTypes>
118 struct _Weak_result_type_impl<_Res(_ArgTypes...)>
120 typedef _Res result_type;
123 /// Retrieve the result type for a function reference.
124 template<typename _Res, typename... _ArgTypes>
125 struct _Weak_result_type_impl<_Res(&)(_ArgTypes...)>
127 typedef _Res result_type;
130 /// Retrieve the result type for a function pointer.
131 template<typename _Res, typename... _ArgTypes>
132 struct _Weak_result_type_impl<_Res(*)(_ArgTypes...)>
134 typedef _Res result_type;
137 /// Retrieve result type for a member function pointer.
138 template<typename _Res, typename _Class, typename... _ArgTypes>
139 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...)>
141 typedef _Res result_type;
144 /// Retrieve result type for a const member function pointer.
145 template<typename _Res, typename _Class, typename... _ArgTypes>
146 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) const>
148 typedef _Res result_type;
151 /// Retrieve result type for a volatile member function pointer.
152 template<typename _Res, typename _Class, typename... _ArgTypes>
153 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) volatile>
155 typedef _Res result_type;
158 /// Retrieve result type for a const volatile member function pointer.
159 template<typename _Res, typename _Class, typename... _ArgTypes>
160 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...)const volatile>
162 typedef _Res result_type;
166 * Strip top-level cv-qualifiers from the function object and let
167 * _Weak_result_type_impl perform the real work.
169 template<typename _Functor>
170 struct _Weak_result_type
171 : _Weak_result_type_impl<typename remove_cv<_Functor>::type>
174 template<typename _Signature>
177 template<typename _Functor, typename... _ArgTypes>
178 struct result_of<_Functor(_ArgTypes...)>
181 decltype( std::declval<_Functor>()(std::declval<_ArgTypes>()...) )
185 /// Determines if the type _Tp derives from unary_function.
186 template<typename _Tp>
187 struct _Derives_from_unary_function : __sfinae_types
190 template<typename _T1, typename _Res>
191 static __one __test(const volatile unary_function<_T1, _Res>*);
193 // It's tempting to change "..." to const volatile void*, but
194 // that fails when _Tp is a function type.
195 static __two __test(...);
198 static const bool value = sizeof(__test((_Tp*)0)) == 1;
201 /// Determines if the type _Tp derives from binary_function.
202 template<typename _Tp>
203 struct _Derives_from_binary_function : __sfinae_types
206 template<typename _T1, typename _T2, typename _Res>
207 static __one __test(const volatile binary_function<_T1, _T2, _Res>*);
209 // It's tempting to change "..." to const volatile void*, but
210 // that fails when _Tp is a function type.
211 static __two __test(...);
214 static const bool value = sizeof(__test((_Tp*)0)) == 1;
217 /// Turns a function type into a function pointer type
218 template<typename _Tp, bool _IsFunctionType = is_function<_Tp>::value>
219 struct _Function_to_function_pointer
224 template<typename _Tp>
225 struct _Function_to_function_pointer<_Tp, true>
231 * Invoke a function object, which may be either a member pointer or a
232 * function object. The first parameter will tell which.
234 template<typename _Functor, typename... _Args>
237 (!is_member_pointer<_Functor>::value
238 && !is_function<_Functor>::value
239 && !is_function<typename remove_pointer<_Functor>::type>::value),
240 typename result_of<_Functor(_Args...)>::type
242 __invoke(_Functor& __f, _Args&&... __args)
244 return __f(std::forward<_Args>(__args)...);
247 // To pick up function references (that will become function pointers)
248 template<typename _Functor, typename... _Args>
251 (is_pointer<_Functor>::value
252 && is_function<typename remove_pointer<_Functor>::type>::value),
253 typename result_of<_Functor(_Args...)>::type
255 __invoke(_Functor __f, _Args&&... __args)
257 return __f(std::forward<_Args>(__args)...);
261 * Knowing which of unary_function and binary_function _Tp derives
262 * from, derives from the same and ensures that reference_wrapper
263 * will have a weak result type. See cases below.
265 template<bool _Unary, bool _Binary, typename _Tp>
266 struct _Reference_wrapper_base_impl;
268 // Not a unary_function or binary_function, so try a weak result type.
269 template<typename _Tp>
270 struct _Reference_wrapper_base_impl<false, false, _Tp>
271 : _Weak_result_type<_Tp>
274 // unary_function but not binary_function
275 template<typename _Tp>
276 struct _Reference_wrapper_base_impl<true, false, _Tp>
277 : unary_function<typename _Tp::argument_type,
278 typename _Tp::result_type>
281 // binary_function but not unary_function
282 template<typename _Tp>
283 struct _Reference_wrapper_base_impl<false, true, _Tp>
284 : binary_function<typename _Tp::first_argument_type,
285 typename _Tp::second_argument_type,
286 typename _Tp::result_type>
289 // Both unary_function and binary_function. Import result_type to
291 template<typename _Tp>
292 struct _Reference_wrapper_base_impl<true, true, _Tp>
293 : unary_function<typename _Tp::argument_type,
294 typename _Tp::result_type>,
295 binary_function<typename _Tp::first_argument_type,
296 typename _Tp::second_argument_type,
297 typename _Tp::result_type>
299 typedef typename _Tp::result_type result_type;
303 * Derives from unary_function or binary_function when it
304 * can. Specializations handle all of the easy cases. The primary
305 * template determines what to do with a class type, which may
306 * derive from both unary_function and binary_function.
308 template<typename _Tp>
309 struct _Reference_wrapper_base
310 : _Reference_wrapper_base_impl<
311 _Derives_from_unary_function<_Tp>::value,
312 _Derives_from_binary_function<_Tp>::value,
316 // - a function type (unary)
317 template<typename _Res, typename _T1>
318 struct _Reference_wrapper_base<_Res(_T1)>
319 : unary_function<_T1, _Res>
322 // - a function type (binary)
323 template<typename _Res, typename _T1, typename _T2>
324 struct _Reference_wrapper_base<_Res(_T1, _T2)>
325 : binary_function<_T1, _T2, _Res>
328 // - a function pointer type (unary)
329 template<typename _Res, typename _T1>
330 struct _Reference_wrapper_base<_Res(*)(_T1)>
331 : unary_function<_T1, _Res>
334 // - a function pointer type (binary)
335 template<typename _Res, typename _T1, typename _T2>
336 struct _Reference_wrapper_base<_Res(*)(_T1, _T2)>
337 : binary_function<_T1, _T2, _Res>
340 // - a pointer to member function type (unary, no qualifiers)
341 template<typename _Res, typename _T1>
342 struct _Reference_wrapper_base<_Res (_T1::*)()>
343 : unary_function<_T1*, _Res>
346 // - a pointer to member function type (binary, no qualifiers)
347 template<typename _Res, typename _T1, typename _T2>
348 struct _Reference_wrapper_base<_Res (_T1::*)(_T2)>
349 : binary_function<_T1*, _T2, _Res>
352 // - a pointer to member function type (unary, const)
353 template<typename _Res, typename _T1>
354 struct _Reference_wrapper_base<_Res (_T1::*)() const>
355 : unary_function<const _T1*, _Res>
358 // - a pointer to member function type (binary, const)
359 template<typename _Res, typename _T1, typename _T2>
360 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const>
361 : binary_function<const _T1*, _T2, _Res>
364 // - a pointer to member function type (unary, volatile)
365 template<typename _Res, typename _T1>
366 struct _Reference_wrapper_base<_Res (_T1::*)() volatile>
367 : unary_function<volatile _T1*, _Res>
370 // - a pointer to member function type (binary, volatile)
371 template<typename _Res, typename _T1, typename _T2>
372 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) volatile>
373 : binary_function<volatile _T1*, _T2, _Res>
376 // - a pointer to member function type (unary, const volatile)
377 template<typename _Res, typename _T1>
378 struct _Reference_wrapper_base<_Res (_T1::*)() const volatile>
379 : unary_function<const volatile _T1*, _Res>
382 // - a pointer to member function type (binary, const volatile)
383 template<typename _Res, typename _T1, typename _T2>
384 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const volatile>
385 : binary_function<const volatile _T1*, _T2, _Res>
388 /// reference_wrapper
389 template<typename _Tp>
390 class reference_wrapper
391 : public _Reference_wrapper_base<typename remove_cv<_Tp>::type>
393 // If _Tp is a function type, we can't form result_of<_Tp(...)>,
394 // so turn it into a function pointer type.
395 typedef typename _Function_to_function_pointer<_Tp>::type
402 reference_wrapper(_Tp& __indata): _M_data(&__indata)
405 reference_wrapper(_Tp&&) = delete;
407 reference_wrapper(const reference_wrapper<_Tp>& __inref):
408 _M_data(__inref._M_data)
412 operator=(const reference_wrapper<_Tp>& __inref)
414 _M_data = __inref._M_data;
418 operator _Tp&() const
419 { return this->get(); }
425 template<typename... _Args>
426 typename result_of<_M_func_type(_Args...)>::type
427 operator()(_Args&&... __args) const
429 return __invoke(get(), std::forward<_Args>(__args)...);
434 // Denotes a reference should be taken to a variable.
435 template<typename _Tp>
436 inline reference_wrapper<_Tp>
438 { return reference_wrapper<_Tp>(__t); }
440 // Denotes a const reference should be taken to a variable.
441 template<typename _Tp>
442 inline reference_wrapper<const _Tp>
444 { return reference_wrapper<const _Tp>(__t); }
446 template<typename _Tp>
447 inline reference_wrapper<_Tp>
448 ref(reference_wrapper<_Tp> __t)
449 { return ref(__t.get()); }
451 template<typename _Tp>
452 inline reference_wrapper<const _Tp>
453 cref(reference_wrapper<_Tp> __t)
454 { return cref(__t.get()); }
456 template<typename _Tp, bool>
457 struct _Mem_fn_const_or_non
459 typedef const _Tp& type;
462 template<typename _Tp>
463 struct _Mem_fn_const_or_non<_Tp, false>
469 * Derives from @c unary_function or @c binary_function, or perhaps
470 * nothing, depending on the number of arguments provided. The
471 * primary template is the basis case, which derives nothing.
473 template<typename _Res, typename... _ArgTypes>
474 struct _Maybe_unary_or_binary_function { };
476 /// Derives from @c unary_function, as appropriate.
477 template<typename _Res, typename _T1>
478 struct _Maybe_unary_or_binary_function<_Res, _T1>
479 : std::unary_function<_T1, _Res> { };
481 /// Derives from @c binary_function, as appropriate.
482 template<typename _Res, typename _T1, typename _T2>
483 struct _Maybe_unary_or_binary_function<_Res, _T1, _T2>
484 : std::binary_function<_T1, _T2, _Res> { };
486 /// Implementation of @c mem_fn for member function pointers.
487 template<typename _Res, typename _Class, typename... _ArgTypes>
488 class _Mem_fn<_Res (_Class::*)(_ArgTypes...)>
489 : public _Maybe_unary_or_binary_function<_Res, _Class*, _ArgTypes...>
491 typedef _Res (_Class::*_Functor)(_ArgTypes...);
493 template<typename _Tp>
495 _M_call(_Tp& __object, const volatile _Class *,
496 _ArgTypes... __args) const
497 { return (__object.*__pmf)(__args...); }
499 template<typename _Tp>
501 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
502 { return ((*__ptr).*__pmf)(__args...); }
505 typedef _Res result_type;
507 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
511 operator()(_Class& __object, _ArgTypes... __args) const
512 { return (__object.*__pmf)(__args...); }
516 operator()(_Class* __object, _ArgTypes... __args) const
517 { return (__object->*__pmf)(__args...); }
519 // Handle smart pointers, references and pointers to derived
520 template<typename _Tp>
522 operator()(_Tp& __object, _ArgTypes... __args) const
523 { return _M_call(__object, &__object, __args...); }
529 /// Implementation of @c mem_fn for const member function pointers.
530 template<typename _Res, typename _Class, typename... _ArgTypes>
531 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) const>
532 : public _Maybe_unary_or_binary_function<_Res, const _Class*,
535 typedef _Res (_Class::*_Functor)(_ArgTypes...) const;
537 template<typename _Tp>
539 _M_call(_Tp& __object, const volatile _Class *,
540 _ArgTypes... __args) const
541 { return (__object.*__pmf)(__args...); }
543 template<typename _Tp>
545 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
546 { return ((*__ptr).*__pmf)(__args...); }
549 typedef _Res result_type;
551 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
555 operator()(const _Class& __object, _ArgTypes... __args) const
556 { return (__object.*__pmf)(__args...); }
560 operator()(const _Class* __object, _ArgTypes... __args) const
561 { return (__object->*__pmf)(__args...); }
563 // Handle smart pointers, references and pointers to derived
564 template<typename _Tp>
565 _Res operator()(_Tp& __object, _ArgTypes... __args) const
566 { return _M_call(__object, &__object, __args...); }
572 /// Implementation of @c mem_fn for volatile member function pointers.
573 template<typename _Res, typename _Class, typename... _ArgTypes>
574 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) volatile>
575 : public _Maybe_unary_or_binary_function<_Res, volatile _Class*,
578 typedef _Res (_Class::*_Functor)(_ArgTypes...) volatile;
580 template<typename _Tp>
582 _M_call(_Tp& __object, const volatile _Class *,
583 _ArgTypes... __args) const
584 { return (__object.*__pmf)(__args...); }
586 template<typename _Tp>
588 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
589 { return ((*__ptr).*__pmf)(__args...); }
592 typedef _Res result_type;
594 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
598 operator()(volatile _Class& __object, _ArgTypes... __args) const
599 { return (__object.*__pmf)(__args...); }
603 operator()(volatile _Class* __object, _ArgTypes... __args) const
604 { return (__object->*__pmf)(__args...); }
606 // Handle smart pointers, references and pointers to derived
607 template<typename _Tp>
609 operator()(_Tp& __object, _ArgTypes... __args) const
610 { return _M_call(__object, &__object, __args...); }
616 /// Implementation of @c mem_fn for const volatile member function pointers.
617 template<typename _Res, typename _Class, typename... _ArgTypes>
618 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) const volatile>
619 : public _Maybe_unary_or_binary_function<_Res, const volatile _Class*,
622 typedef _Res (_Class::*_Functor)(_ArgTypes...) const volatile;
624 template<typename _Tp>
626 _M_call(_Tp& __object, const volatile _Class *,
627 _ArgTypes... __args) const
628 { return (__object.*__pmf)(__args...); }
630 template<typename _Tp>
632 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
633 { return ((*__ptr).*__pmf)(__args...); }
636 typedef _Res result_type;
638 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
642 operator()(const volatile _Class& __object, _ArgTypes... __args) const
643 { return (__object.*__pmf)(__args...); }
647 operator()(const volatile _Class* __object, _ArgTypes... __args) const
648 { return (__object->*__pmf)(__args...); }
650 // Handle smart pointers, references and pointers to derived
651 template<typename _Tp>
652 _Res operator()(_Tp& __object, _ArgTypes... __args) const
653 { return _M_call(__object, &__object, __args...); }
660 template<typename _Res, typename _Class>
661 class _Mem_fn<_Res _Class::*>
663 // This bit of genius is due to Peter Dimov, improved slightly by
665 template<typename _Tp>
667 _M_call(_Tp& __object, _Class *) const
668 { return __object.*__pm; }
670 template<typename _Tp, typename _Up>
672 _M_call(_Tp& __object, _Up * const *) const
673 { return (*__object).*__pm; }
675 template<typename _Tp, typename _Up>
677 _M_call(_Tp& __object, const _Up * const *) const
678 { return (*__object).*__pm; }
680 template<typename _Tp>
682 _M_call(_Tp& __object, const _Class *) const
683 { return __object.*__pm; }
685 template<typename _Tp>
687 _M_call(_Tp& __ptr, const volatile void*) const
688 { return (*__ptr).*__pm; }
690 template<typename _Tp> static _Tp& __get_ref();
692 template<typename _Tp>
693 static __sfinae_types::__one __check_const(_Tp&, _Class*);
694 template<typename _Tp, typename _Up>
695 static __sfinae_types::__one __check_const(_Tp&, _Up * const *);
696 template<typename _Tp, typename _Up>
697 static __sfinae_types::__two __check_const(_Tp&, const _Up * const *);
698 template<typename _Tp>
699 static __sfinae_types::__two __check_const(_Tp&, const _Class*);
700 template<typename _Tp>
701 static __sfinae_types::__two __check_const(_Tp&, const volatile void*);
704 template<typename _Tp>
706 : _Mem_fn_const_or_non<_Res,
707 (sizeof(__sfinae_types::__two)
708 == sizeof(__check_const<_Tp>(__get_ref<_Tp>(), (_Tp*)0)))>
711 template<typename _Signature>
714 template<typename _CVMem, typename _Tp>
715 struct result<_CVMem(_Tp)>
716 : public _Result_type<_Tp> { };
718 template<typename _CVMem, typename _Tp>
719 struct result<_CVMem(_Tp&)>
720 : public _Result_type<_Tp> { };
723 _Mem_fn(_Res _Class::*__pm) : __pm(__pm) { }
727 operator()(_Class& __object) const
728 { return __object.*__pm; }
731 operator()(const _Class& __object) const
732 { return __object.*__pm; }
736 operator()(_Class* __object) const
737 { return __object->*__pm; }
740 operator()(const _Class* __object) const
741 { return __object->*__pm; }
743 // Handle smart pointers and derived
744 template<typename _Tp>
745 typename _Result_type<_Tp>::type
746 operator()(_Tp& __unknown) const
747 { return _M_call(__unknown, &__unknown); }
754 * @brief Returns a function object that forwards to the member
757 template<typename _Tp, typename _Class>
758 inline _Mem_fn<_Tp _Class::*>
759 mem_fn(_Tp _Class::* __pm)
761 return _Mem_fn<_Tp _Class::*>(__pm);
765 * @brief Determines if the given type _Tp is a function object
766 * should be treated as a subexpression when evaluating calls to
767 * function objects returned by bind(). [TR1 3.6.1]
769 template<typename _Tp>
770 struct is_bind_expression
771 : public false_type { };
774 * @brief Determines if the given type _Tp is a placeholder in a
775 * bind() expression and, if so, which placeholder it is. [TR1 3.6.2]
777 template<typename _Tp>
778 struct is_placeholder
779 : public integral_constant<int, 0>
782 /// The type of placeholder objects defined by libstdc++.
783 template<int _Num> struct _Placeholder { };
785 /** @namespace std::placeholders
786 * @brief ISO C++ 0x entities sub namespace for functional.
788 * Define a large number of placeholders. There is no way to
789 * simplify this with variadic templates, because we're introducing
790 * unique names for each.
792 namespace placeholders
805 _Placeholder<10> _10;
806 _Placeholder<11> _11;
807 _Placeholder<12> _12;
808 _Placeholder<13> _13;
809 _Placeholder<14> _14;
810 _Placeholder<15> _15;
811 _Placeholder<16> _16;
812 _Placeholder<17> _17;
813 _Placeholder<18> _18;
814 _Placeholder<19> _19;
815 _Placeholder<20> _20;
816 _Placeholder<21> _21;
817 _Placeholder<22> _22;
818 _Placeholder<23> _23;
819 _Placeholder<24> _24;
820 _Placeholder<25> _25;
821 _Placeholder<26> _26;
822 _Placeholder<27> _27;
823 _Placeholder<28> _28;
824 _Placeholder<29> _29;
829 * Partial specialization of is_placeholder that provides the placeholder
830 * number for the placeholder objects defined by libstdc++.
833 struct is_placeholder<_Placeholder<_Num> >
834 : public integral_constant<int, _Num>
838 * Stores a tuple of indices. Used by bind() to extract the elements
841 template<int... _Indexes>
844 typedef _Index_tuple<_Indexes..., sizeof...(_Indexes)> __next;
847 /// Builds an _Index_tuple<0, 1, 2, ..., _Num-1>.
848 template<std::size_t _Num>
849 struct _Build_index_tuple
851 typedef typename _Build_index_tuple<_Num-1>::__type::__next __type;
855 struct _Build_index_tuple<0>
857 typedef _Index_tuple<> __type;
861 * Used by _Safe_tuple_element to indicate that there is no tuple
862 * element at this position.
864 struct _No_tuple_element;
867 * Implementation helper for _Safe_tuple_element. This primary
868 * template handles the case where it is safe to use @c
871 template<int __i, typename _Tuple, bool _IsSafe>
872 struct _Safe_tuple_element_impl
873 : tuple_element<__i, _Tuple> { };
876 * Implementation helper for _Safe_tuple_element. This partial
877 * specialization handles the case where it is not safe to use @c
878 * tuple_element. We just return @c _No_tuple_element.
880 template<int __i, typename _Tuple>
881 struct _Safe_tuple_element_impl<__i, _Tuple, false>
883 typedef _No_tuple_element type;
887 * Like tuple_element, but returns @c _No_tuple_element when
888 * tuple_element would return an error.
890 template<int __i, typename _Tuple>
891 struct _Safe_tuple_element
892 : _Safe_tuple_element_impl<__i, _Tuple,
893 (__i >= 0 && __i < tuple_size<_Tuple>::value)>
897 * Maps an argument to bind() into an actual argument to the bound
898 * function object [TR1 3.6.3/5]. Only the first parameter should
899 * be specified: the rest are used to determine among the various
900 * implementations. Note that, although this class is a function
901 * object, it isn't entirely normal because it takes only two
902 * parameters regardless of the number of parameters passed to the
903 * bind expression. The first parameter is the bound argument and
904 * the second parameter is a tuple containing references to the
905 * rest of the arguments.
907 template<typename _Arg,
908 bool _IsBindExp = is_bind_expression<_Arg>::value,
909 bool _IsPlaceholder = (is_placeholder<_Arg>::value > 0)>
913 * If the argument is reference_wrapper<_Tp>, returns the
914 * underlying reference. [TR1 3.6.3/5 bullet 1]
916 template<typename _Tp>
917 class _Mu<reference_wrapper<_Tp>, false, false>
920 typedef _Tp& result_type;
922 /* Note: This won't actually work for const volatile
923 * reference_wrappers, because reference_wrapper::get() is const
924 * but not volatile-qualified. This might be a defect in the TR.
926 template<typename _CVRef, typename _Tuple>
928 operator()(_CVRef& __arg, _Tuple&&) const volatile
929 { return __arg.get(); }
933 * If the argument is a bind expression, we invoke the underlying
934 * function object with the same cv-qualifiers as we are given and
935 * pass along all of our arguments (unwrapped). [TR1 3.6.3/5 bullet 2]
937 template<typename _Arg>
938 class _Mu<_Arg, true, false>
941 template<typename _Signature> class result;
943 // Determine the result type when we pass the arguments along. This
944 // involves passing along the cv-qualifiers placed on _Mu and
945 // unwrapping the argument bundle.
946 template<typename _CVMu, typename _CVArg, typename... _Args>
947 class result<_CVMu(_CVArg, tuple<_Args...>)>
948 : public result_of<_CVArg(_Args...)> { };
950 template<typename _CVArg, typename... _Args>
951 typename result_of<_CVArg(_Args...)>::type
952 operator()(_CVArg& __arg,
953 tuple<_Args...>&& __tuple) const volatile
955 // Construct an index tuple and forward to __call
956 typedef typename _Build_index_tuple<sizeof...(_Args)>::__type
958 return this->__call(__arg, std::move(__tuple), _Indexes());
962 // Invokes the underlying function object __arg by unpacking all
963 // of the arguments in the tuple.
964 template<typename _CVArg, typename... _Args, int... _Indexes>
965 typename result_of<_CVArg(_Args...)>::type
966 __call(_CVArg& __arg, tuple<_Args...>&& __tuple,
967 const _Index_tuple<_Indexes...>&) const volatile
969 return __arg(std::forward<_Args>(get<_Indexes>(__tuple))...);
974 * If the argument is a placeholder for the Nth argument, returns
975 * a reference to the Nth argument to the bind function object.
976 * [TR1 3.6.3/5 bullet 3]
978 template<typename _Arg>
979 class _Mu<_Arg, false, true>
982 template<typename _Signature> class result;
984 template<typename _CVMu, typename _CVArg, typename _Tuple>
985 class result<_CVMu(_CVArg, _Tuple)>
987 // Add a reference, if it hasn't already been done for us.
988 // This allows us to be a little bit sloppy in constructing
989 // the tuple that we pass to result_of<...>.
990 typedef typename _Safe_tuple_element<(is_placeholder<_Arg>::value
995 typedef typename add_rvalue_reference<__base_type>::type type;
998 template<typename _Tuple>
999 typename result<_Mu(_Arg, _Tuple)>::type
1000 operator()(const volatile _Arg&, _Tuple&& __tuple) const volatile
1002 return std::forward<typename result<_Mu(_Arg, _Tuple)>::type>(
1003 ::std::get<(is_placeholder<_Arg>::value - 1)>(__tuple));
1008 * If the argument is just a value, returns a reference to that
1009 * value. The cv-qualifiers on the reference are the same as the
1010 * cv-qualifiers on the _Mu object. [TR1 3.6.3/5 bullet 4]
1012 template<typename _Arg>
1013 class _Mu<_Arg, false, false>
1016 template<typename _Signature> struct result;
1018 template<typename _CVMu, typename _CVArg, typename _Tuple>
1019 struct result<_CVMu(_CVArg, _Tuple)>
1021 typedef typename add_lvalue_reference<_CVArg>::type type;
1024 // Pick up the cv-qualifiers of the argument
1025 template<typename _CVArg, typename _Tuple>
1027 operator()(_CVArg&& __arg, _Tuple&&) const volatile
1028 { return std::forward<_CVArg>(__arg); }
1032 * Maps member pointers into instances of _Mem_fn but leaves all
1033 * other function objects untouched. Used by tr1::bind(). The
1034 * primary template handles the non--member-pointer case.
1036 template<typename _Tp>
1037 struct _Maybe_wrap_member_pointer
1042 __do_wrap(const _Tp& __x)
1047 * Maps member pointers into instances of _Mem_fn but leaves all
1048 * other function objects untouched. Used by tr1::bind(). This
1049 * partial specialization handles the member pointer case.
1051 template<typename _Tp, typename _Class>
1052 struct _Maybe_wrap_member_pointer<_Tp _Class::*>
1054 typedef _Mem_fn<_Tp _Class::*> type;
1057 __do_wrap(_Tp _Class::* __pm)
1058 { return type(__pm); }
1061 // Specialization needed to prevent "forming reference to void" errors when
1062 // bind<void>() is called, because argument deduction instantiates
1063 // _Maybe_wrap_member_pointer<void> outside the immediate context where
1066 struct _Maybe_wrap_member_pointer<void>
1071 /// Type of the function object returned from bind().
1072 template<typename _Signature>
1075 template<typename _Functor, typename... _Bound_args>
1076 class _Bind<_Functor(_Bound_args...)>
1077 : public _Weak_result_type<_Functor>
1079 typedef _Bind __self_type;
1080 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
1084 tuple<_Bound_args...> _M_bound_args;
1087 template<typename _Result, typename... _Args, int... _Indexes>
1089 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>)
1091 return _M_f(_Mu<_Bound_args>()
1092 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1096 template<typename _Result, typename... _Args, int... _Indexes>
1098 __call_c(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>) const
1100 return _M_f(_Mu<_Bound_args>()
1101 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1106 template<typename _Result, typename... _Args, int... _Indexes>
1108 __call_v(tuple<_Args...>&& __args,
1109 _Index_tuple<_Indexes...>) volatile
1111 return _M_f(_Mu<_Bound_args>()
1112 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1115 // Call as const volatile
1116 template<typename _Result, typename... _Args, int... _Indexes>
1118 __call_c_v(tuple<_Args...>&& __args,
1119 _Index_tuple<_Indexes...>) const volatile
1121 return _M_f(_Mu<_Bound_args>()
1122 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1127 explicit _Bind(_Functor __f, _Bound_args... __bound_args)
1128 : _M_f(std::forward<_Functor>(__f)),
1129 _M_bound_args(std::forward<_Bound_args>(__bound_args)...)
1133 template<typename... _Args, typename _Result
1134 = decltype( std::declval<_Functor>()(
1135 _Mu<_Bound_args>()( std::declval<_Bound_args&>(),
1136 std::declval<tuple<_Args...>&&>() )... ) )>
1138 operator()(_Args&&... __args)
1140 return this->__call<_Result>(tuple<_Args...>
1141 (std::forward<_Args>(__args)...),
1146 template<typename... _Args, typename _Result
1147 = decltype( std::declval<const _Functor>()(
1148 _Mu<_Bound_args>()( std::declval<const _Bound_args&>(),
1149 std::declval<tuple<_Args...>&&>() )... ) )>
1151 operator()(_Args&&... __args) const
1153 return this->__call_c<_Result>(tuple<_Args...>
1154 (std::forward<_Args>(__args)...),
1160 template<typename... _Args, typename _Result
1161 = decltype( std::declval<volatile _Functor>()(
1162 _Mu<_Bound_args>()( std::declval<volatile _Bound_args&>(),
1163 std::declval<tuple<_Args...>&&>() )... ) )>
1165 operator()(_Args&&... __args) volatile
1167 return this->__call_v<_Result>(tuple<_Args...>
1168 (std::forward<_Args>(__args)...),
1172 // Call as const volatile
1173 template<typename... _Args, typename _Result
1174 = decltype( std::declval<const volatile _Functor>()(
1175 _Mu<_Bound_args>()( std::declval<const volatile _Bound_args&>(),
1176 std::declval<tuple<_Args...>&&>() )... ) )>
1178 operator()(_Args&&... __args) const volatile
1180 return this->__call_c_v<_Result>(tuple<_Args...>
1181 (std::forward<_Args>(__args)...),
1187 /// Type of the function object returned from bind<R>().
1188 template<typename _Result, typename _Signature>
1189 struct _Bind_result;
1191 template<typename _Result, typename _Functor, typename... _Bound_args>
1192 class _Bind_result<_Result, _Functor(_Bound_args...)>
1194 typedef _Bind_result __self_type;
1195 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
1199 tuple<_Bound_args...> _M_bound_args;
1202 template<typename _Res>
1203 struct __enable_if_void : enable_if<is_void<_Res>::value, int> { };
1204 template<typename _Res>
1205 struct __disable_if_void : enable_if<!is_void<_Res>::value, int> { };
1208 template<typename _Res, typename... _Args, int... _Indexes>
1210 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1211 typename __disable_if_void<_Res>::type = 0)
1213 return _M_f(_Mu<_Bound_args>()
1214 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1217 // Call unqualified, return void
1218 template<typename _Res, typename... _Args, int... _Indexes>
1220 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1221 typename __enable_if_void<_Res>::type = 0)
1223 _M_f(_Mu<_Bound_args>()
1224 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1228 template<typename _Res, typename... _Args, int... _Indexes>
1230 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1231 typename __disable_if_void<_Res>::type = 0) const
1233 return _M_f(_Mu<_Bound_args>()
1234 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1237 // Call as const, return void
1238 template<typename _Res, typename... _Args, int... _Indexes>
1240 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1241 typename __enable_if_void<_Res>::type = 0) const
1243 _M_f(_Mu<_Bound_args>()
1244 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1248 template<typename _Res, typename... _Args, int... _Indexes>
1250 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1251 typename __disable_if_void<_Res>::type = 0) volatile
1253 return _M_f(_Mu<_Bound_args>()
1254 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1257 // Call as volatile, return void
1258 template<typename _Res, typename... _Args, int... _Indexes>
1260 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1261 typename __enable_if_void<_Res>::type = 0) volatile
1263 _M_f(_Mu<_Bound_args>()
1264 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1267 // Call as const volatile
1268 template<typename _Res, typename... _Args, int... _Indexes>
1270 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1271 typename __disable_if_void<_Res>::type = 0) const volatile
1273 return _M_f(_Mu<_Bound_args>()
1274 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1277 // Call as const volatile, return void
1278 template<typename _Res, typename... _Args, int... _Indexes>
1280 __call(tuple<_Args...>&& __args,
1281 _Index_tuple<_Indexes...>,
1282 typename __enable_if_void<_Res>::type = 0) const volatile
1284 _M_f(_Mu<_Bound_args>()
1285 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1289 typedef _Result result_type;
1292 _Bind_result(_Functor __f, _Bound_args... __bound_args)
1293 : _M_f(std::forward<_Functor>(__f)),
1294 _M_bound_args(std::forward<_Bound_args>(__bound_args)...)
1298 template<typename... _Args>
1300 operator()(_Args&&... __args)
1302 return this->__call<_Result>(
1303 tuple<_Args...>(std::forward<_Args...>(__args)...),
1308 template<typename... _Args>
1310 operator()(_Args&&... __args) const
1312 return this->__call<_Result>(
1313 tuple<_Args...>(std::forward<_Args...>(__args)...),
1318 template<typename... _Args>
1320 operator()(_Args&&... __args) volatile
1322 return this->__call<_Result>(
1323 tuple<_Args...>(std::forward<_Args...>(__args)...),
1327 // Call as const volatile
1328 template<typename... _Args>
1330 operator()(_Args&&... __args) const volatile
1332 return this->__call<_Result>(
1333 tuple<_Args...>(std::forward<_Args...>(__args)...),
1338 /// Class template _Bind is always a bind expression.
1339 template<typename _Signature>
1340 struct is_bind_expression<_Bind<_Signature> >
1341 : public true_type { };
1343 /// Class template _Bind_result is always a bind expression.
1344 template<typename _Result, typename _Signature>
1345 struct is_bind_expression<_Bind_result<_Result, _Signature> >
1346 : public true_type { };
1349 template<typename _Functor, typename... _ArgTypes>
1351 _Bind<typename _Maybe_wrap_member_pointer<_Functor>::type(_ArgTypes...)>
1352 bind(_Functor __f, _ArgTypes... __args)
1354 typedef _Maybe_wrap_member_pointer<_Functor> __maybe_type;
1355 typedef typename __maybe_type::type __functor_type;
1356 typedef _Bind<__functor_type(_ArgTypes...)> __result_type;
1357 return __result_type(__maybe_type::__do_wrap(__f),
1358 std::forward<_ArgTypes>(__args)...);
1361 template<typename _Result, typename _Functor, typename... _ArgTypes>
1363 _Bind_result<_Result,
1364 typename _Maybe_wrap_member_pointer<_Functor>::type
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_result<_Result, __functor_type(_ArgTypes...)>
1372 return __result_type(__maybe_type::__do_wrap(__f),
1373 std::forward<_ArgTypes>(__args)...);
1377 * @brief Exception class thrown when class template function's
1378 * operator() is called with an empty target.
1379 * @ingroup exceptions
1381 class bad_function_call : public std::exception { };
1384 * The integral constant expression 0 can be converted into a
1385 * pointer to this type. It is used by the function template to
1386 * accept NULL pointers.
1388 struct _M_clear_type;
1391 * Trait identifying "location-invariant" types, meaning that the
1392 * address of the object (or any of its members) will not escape.
1393 * Also implies a trivial copy constructor and assignment operator.
1395 template<typename _Tp>
1396 struct __is_location_invariant
1397 : integral_constant<bool, (is_pointer<_Tp>::value
1398 || is_member_pointer<_Tp>::value)>
1401 class _Undefined_class;
1406 const void* _M_const_object;
1407 void (*_M_function_pointer)();
1408 void (_Undefined_class::*_M_member_pointer)();
1413 void* _M_access() { return &_M_pod_data[0]; }
1414 const void* _M_access() const { return &_M_pod_data[0]; }
1416 template<typename _Tp>
1419 { return *static_cast<_Tp*>(_M_access()); }
1421 template<typename _Tp>
1424 { return *static_cast<const _Tp*>(_M_access()); }
1426 _Nocopy_types _M_unused;
1427 char _M_pod_data[sizeof(_Nocopy_types)];
1430 enum _Manager_operation
1438 // Simple type wrapper that helps avoid annoying const problems
1439 // when casting between void pointers and pointers-to-pointers.
1440 template<typename _Tp>
1441 struct _Simple_type_wrapper
1443 _Simple_type_wrapper(_Tp __value) : __value(__value) { }
1448 template<typename _Tp>
1449 struct __is_location_invariant<_Simple_type_wrapper<_Tp> >
1450 : __is_location_invariant<_Tp>
1453 // Converts a reference to a function object into a callable
1455 template<typename _Functor>
1457 __callable_functor(_Functor& __f)
1460 template<typename _Member, typename _Class>
1461 inline _Mem_fn<_Member _Class::*>
1462 __callable_functor(_Member _Class::* &__p)
1463 { return mem_fn(__p); }
1465 template<typename _Member, typename _Class>
1466 inline _Mem_fn<_Member _Class::*>
1467 __callable_functor(_Member _Class::* const &__p)
1468 { return mem_fn(__p); }
1470 template<typename _Signature>
1473 /// Base class of all polymorphic function object wrappers.
1474 class _Function_base
1477 static const std::size_t _M_max_size = sizeof(_Nocopy_types);
1478 static const std::size_t _M_max_align = __alignof__(_Nocopy_types);
1480 template<typename _Functor>
1484 static const bool __stored_locally =
1485 (__is_location_invariant<_Functor>::value
1486 && sizeof(_Functor) <= _M_max_size
1487 && __alignof__(_Functor) <= _M_max_align
1488 && (_M_max_align % __alignof__(_Functor) == 0));
1490 typedef integral_constant<bool, __stored_locally> _Local_storage;
1492 // Retrieve a pointer to the function object
1494 _M_get_pointer(const _Any_data& __source)
1496 const _Functor* __ptr =
1497 __stored_locally? &__source._M_access<_Functor>()
1498 /* have stored a pointer */ : __source._M_access<_Functor*>();
1499 return const_cast<_Functor*>(__ptr);
1502 // Clone a location-invariant function object that fits within
1503 // an _Any_data structure.
1505 _M_clone(_Any_data& __dest, const _Any_data& __source, true_type)
1507 new (__dest._M_access()) _Functor(__source._M_access<_Functor>());
1510 // Clone a function object that is not location-invariant or
1511 // that cannot fit into an _Any_data structure.
1513 _M_clone(_Any_data& __dest, const _Any_data& __source, false_type)
1515 __dest._M_access<_Functor*>() =
1516 new _Functor(*__source._M_access<_Functor*>());
1519 // Destroying a location-invariant object may still require
1522 _M_destroy(_Any_data& __victim, true_type)
1524 __victim._M_access<_Functor>().~_Functor();
1527 // Destroying an object located on the heap.
1529 _M_destroy(_Any_data& __victim, false_type)
1531 delete __victim._M_access<_Functor*>();
1536 _M_manager(_Any_data& __dest, const _Any_data& __source,
1537 _Manager_operation __op)
1542 case __get_type_info:
1543 __dest._M_access<const type_info*>() = &typeid(_Functor);
1546 case __get_functor_ptr:
1547 __dest._M_access<_Functor*>() = _M_get_pointer(__source);
1550 case __clone_functor:
1551 _M_clone(__dest, __source, _Local_storage());
1554 case __destroy_functor:
1555 _M_destroy(__dest, _Local_storage());
1562 _M_init_functor(_Any_data& __functor, _Functor&& __f)
1563 { _M_init_functor(__functor, std::move(__f), _Local_storage()); }
1565 template<typename _Signature>
1567 _M_not_empty_function(const function<_Signature>& __f)
1568 { return static_cast<bool>(__f); }
1570 template<typename _Tp>
1572 _M_not_empty_function(const _Tp*& __fp)
1575 template<typename _Class, typename _Tp>
1577 _M_not_empty_function(_Tp _Class::* const& __mp)
1580 template<typename _Tp>
1582 _M_not_empty_function(const _Tp&)
1587 _M_init_functor(_Any_data& __functor, _Functor&& __f, true_type)
1588 { new (__functor._M_access()) _Functor(std::move(__f)); }
1591 _M_init_functor(_Any_data& __functor, _Functor&& __f, false_type)
1592 { __functor._M_access<_Functor*>() = new _Functor(std::move(__f)); }
1595 template<typename _Functor>
1596 class _Ref_manager : public _Base_manager<_Functor*>
1598 typedef _Function_base::_Base_manager<_Functor*> _Base;
1602 _M_manager(_Any_data& __dest, const _Any_data& __source,
1603 _Manager_operation __op)
1608 case __get_type_info:
1609 __dest._M_access<const type_info*>() = &typeid(_Functor);
1612 case __get_functor_ptr:
1613 __dest._M_access<_Functor*>() = *_Base::_M_get_pointer(__source);
1614 return is_const<_Functor>::value;
1618 _Base::_M_manager(__dest, __source, __op);
1624 _M_init_functor(_Any_data& __functor, reference_wrapper<_Functor> __f)
1626 // TBD: Use address_of function instead.
1627 _Base::_M_init_functor(__functor, &__f.get());
1631 _Function_base() : _M_manager(0) { }
1636 _M_manager(_M_functor, _M_functor, __destroy_functor);
1640 bool _M_empty() const { return !_M_manager; }
1642 typedef bool (*_Manager_type)(_Any_data&, const _Any_data&,
1643 _Manager_operation);
1645 _Any_data _M_functor;
1646 _Manager_type _M_manager;
1649 template<typename _Signature, typename _Functor>
1650 class _Function_handler;
1652 template<typename _Res, typename _Functor, typename... _ArgTypes>
1653 class _Function_handler<_Res(_ArgTypes...), _Functor>
1654 : public _Function_base::_Base_manager<_Functor>
1656 typedef _Function_base::_Base_manager<_Functor> _Base;
1660 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1662 return (*_Base::_M_get_pointer(__functor))(
1663 std::forward<_ArgTypes>(__args)...);
1667 template<typename _Functor, typename... _ArgTypes>
1668 class _Function_handler<void(_ArgTypes...), _Functor>
1669 : public _Function_base::_Base_manager<_Functor>
1671 typedef _Function_base::_Base_manager<_Functor> _Base;
1675 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1677 (*_Base::_M_get_pointer(__functor))(
1678 std::forward<_ArgTypes>(__args)...);
1682 template<typename _Res, typename _Functor, typename... _ArgTypes>
1683 class _Function_handler<_Res(_ArgTypes...), reference_wrapper<_Functor> >
1684 : public _Function_base::_Ref_manager<_Functor>
1686 typedef _Function_base::_Ref_manager<_Functor> _Base;
1690 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1692 return __callable_functor(**_Base::_M_get_pointer(__functor))(
1693 std::forward<_ArgTypes>(__args)...);
1697 template<typename _Functor, typename... _ArgTypes>
1698 class _Function_handler<void(_ArgTypes...), reference_wrapper<_Functor> >
1699 : public _Function_base::_Ref_manager<_Functor>
1701 typedef _Function_base::_Ref_manager<_Functor> _Base;
1705 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1707 __callable_functor(**_Base::_M_get_pointer(__functor))(
1708 std::forward<_ArgTypes>(__args)...);
1712 template<typename _Class, typename _Member, typename _Res,
1713 typename... _ArgTypes>
1714 class _Function_handler<_Res(_ArgTypes...), _Member _Class::*>
1715 : public _Function_handler<void(_ArgTypes...), _Member _Class::*>
1717 typedef _Function_handler<void(_ArgTypes...), _Member _Class::*>
1722 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1724 return mem_fn(_Base::_M_get_pointer(__functor)->__value)(
1725 std::forward<_ArgTypes>(__args)...);
1729 template<typename _Class, typename _Member, typename... _ArgTypes>
1730 class _Function_handler<void(_ArgTypes...), _Member _Class::*>
1731 : public _Function_base::_Base_manager<
1732 _Simple_type_wrapper< _Member _Class::* > >
1734 typedef _Member _Class::* _Functor;
1735 typedef _Simple_type_wrapper<_Functor> _Wrapper;
1736 typedef _Function_base::_Base_manager<_Wrapper> _Base;
1740 _M_manager(_Any_data& __dest, const _Any_data& __source,
1741 _Manager_operation __op)
1746 case __get_type_info:
1747 __dest._M_access<const type_info*>() = &typeid(_Functor);
1750 case __get_functor_ptr:
1751 __dest._M_access<_Functor*>() =
1752 &_Base::_M_get_pointer(__source)->__value;
1756 _Base::_M_manager(__dest, __source, __op);
1762 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1764 mem_fn(_Base::_M_get_pointer(__functor)->__value)(
1765 std::forward<_ArgTypes>(__args)...);
1770 template<typename _Res, typename... _ArgTypes>
1771 class function<_Res(_ArgTypes...)>
1772 : public _Maybe_unary_or_binary_function<_Res, _ArgTypes...>,
1773 private _Function_base
1775 typedef _Res _Signature_type(_ArgTypes...);
1777 struct _Useless { };
1780 typedef _Res result_type;
1782 // [3.7.2.1] construct/copy/destroy
1785 * @brief Default construct creates an empty function call wrapper.
1786 * @post @c !(bool)*this
1789 function() : _Function_base() { }
1792 * @brief Default construct creates an empty function call wrapper.
1793 * @post @c !(bool)*this
1795 function(_M_clear_type*) : _Function_base() { }
1798 * @brief %Function copy constructor.
1799 * @param x A %function object with identical call signature.
1800 * @post @c (bool)*this == (bool)x
1802 * The newly-created %function contains a copy of the target of @a
1803 * x (if it has one).
1805 function(const function& __x);
1808 * @brief %Function move constructor.
1809 * @param x A %function object rvalue with identical call signature.
1811 * The newly-created %function contains the target of @a x
1814 function(function&& __x) : _Function_base()
1819 // TODO: needs allocator_arg_t
1822 * @brief Builds a %function that targets a copy of the incoming
1824 * @param f A %function object that is callable with parameters of
1825 * type @c T1, @c T2, ..., @c TN and returns a value convertible
1828 * The newly-created %function object will target a copy of @a
1829 * f. If @a f is @c reference_wrapper<F>, then this function
1830 * object will contain a reference to the function object @c
1831 * f.get(). If @a f is a NULL function pointer or NULL
1832 * pointer-to-member, the newly-created object will be empty.
1834 * If @a f is a non-NULL function pointer or an object of type @c
1835 * reference_wrapper<F>, this function will not throw.
1837 template<typename _Functor>
1838 function(_Functor __f,
1840 !is_integral<_Functor>::value, _Useless>::type
1844 * @brief %Function assignment operator.
1845 * @param x A %function with identical call signature.
1846 * @post @c (bool)*this == (bool)x
1849 * The target of @a x is copied to @c *this. If @a x has no
1850 * target, then @c *this will be empty.
1852 * If @a x targets a function pointer or a reference to a function
1853 * object, then this operation will not throw an %exception.
1856 operator=(const function& __x)
1858 function(__x).swap(*this);
1863 * @brief %Function move-assignment operator.
1864 * @param x A %function rvalue with identical call signature.
1867 * The target of @a x is moved to @c *this. If @a x has no
1868 * target, then @c *this will be empty.
1870 * If @a x targets a function pointer or a reference to a function
1871 * object, then this operation will not throw an %exception.
1874 operator=(function&& __x)
1876 function(std::move(__x)).swap(*this);
1881 * @brief %Function assignment to zero.
1882 * @post @c !(bool)*this
1885 * The target of @c *this is deallocated, leaving it empty.
1888 operator=(_M_clear_type*)
1892 _M_manager(_M_functor, _M_functor, __destroy_functor);
1900 * @brief %Function assignment to a new target.
1901 * @param f A %function object that is callable with parameters of
1902 * type @c T1, @c T2, ..., @c TN and returns a value convertible
1906 * This %function object wrapper will target a copy of @a
1907 * f. If @a f is @c reference_wrapper<F>, then this function
1908 * object will contain a reference to the function object @c
1909 * f.get(). If @a f is a NULL function pointer or NULL
1910 * pointer-to-member, @c this object will be empty.
1912 * If @a f is a non-NULL function pointer or an object of type @c
1913 * reference_wrapper<F>, this function will not throw.
1915 template<typename _Functor>
1916 typename enable_if<!is_integral<_Functor>::value, function&>::type
1917 operator=(_Functor&& __f)
1919 function(std::forward<_Functor>(__f)).swap(*this);
1924 template<typename _Functor>
1925 typename enable_if<!is_integral<_Functor>::value, function&>::type
1926 operator=(reference_wrapper<_Functor> __f)
1928 function(__f).swap(*this);
1932 // [3.7.2.2] function modifiers
1935 * @brief Swap the targets of two %function objects.
1936 * @param f A %function with identical call signature.
1938 * Swap the targets of @c this function object and @a f. This
1939 * function will not throw an %exception.
1941 void swap(function& __x)
1943 /* We cannot perform direct assignments of the _M_functor
1944 parts as they are of type _Any_data and have a different
1945 dynamic type. Doing so would violate type-based aliasing
1946 rules and lead to spurious miscompilations.
1947 Instead perform a bytewise exchange of the memory of
1949 ??? A wordwise exchange honoring alignment of _M_functor
1950 would be more efficient. See PR42845. */
1951 for (unsigned i = 0; i < sizeof (_M_functor._M_pod_data); ++i)
1952 std::swap (_M_functor._M_pod_data[i], __x._M_functor._M_pod_data[i]);
1953 _Manager_type __old_manager = _M_manager;
1954 _M_manager = __x._M_manager;
1955 __x._M_manager = __old_manager;
1956 _Invoker_type __old_invoker = _M_invoker;
1957 _M_invoker = __x._M_invoker;
1958 __x._M_invoker = __old_invoker;
1961 // TODO: needs allocator_arg_t
1963 template<typename _Functor, typename _Alloc>
1965 assign(_Functor&& __f, const _Alloc& __a)
1967 function(allocator_arg, __a,
1968 std::forward<_Functor>(__f)).swap(*this);
1972 // [3.7.2.3] function capacity
1975 * @brief Determine if the %function wrapper has a target.
1977 * @return @c true when this %function object contains a target,
1978 * or @c false when it is empty.
1980 * This function will not throw an %exception.
1982 explicit operator bool() const
1983 { return !_M_empty(); }
1985 // [3.7.2.4] function invocation
1988 * @brief Invokes the function targeted by @c *this.
1989 * @returns the result of the target.
1990 * @throws bad_function_call when @c !(bool)*this
1992 * The function call operator invokes the target function object
1993 * stored by @c this.
1995 _Res operator()(_ArgTypes... __args) const;
1998 // [3.7.2.5] function target access
2000 * @brief Determine the type of the target of this function object
2003 * @returns the type identifier of the target function object, or
2004 * @c typeid(void) if @c !(bool)*this.
2006 * This function will not throw an %exception.
2008 const type_info& target_type() const;
2011 * @brief Access the stored target function object.
2013 * @return Returns a pointer to the stored target function object,
2014 * if @c typeid(Functor).equals(target_type()); otherwise, a NULL
2017 * This function will not throw an %exception.
2019 template<typename _Functor> _Functor* target();
2022 template<typename _Functor> const _Functor* target() const;
2025 // deleted overloads
2026 template<typename _Res2, typename... _ArgTypes2>
2027 void operator==(const function<_Res2(_ArgTypes2...)>&) const = delete;
2028 template<typename _Res2, typename... _ArgTypes2>
2029 void operator!=(const function<_Res2(_ArgTypes2...)>&) const = delete;
2032 typedef _Res (*_Invoker_type)(const _Any_data&, _ArgTypes...);
2033 _Invoker_type _M_invoker;
2036 template<typename _Res, typename... _ArgTypes>
2037 function<_Res(_ArgTypes...)>::
2038 function(const function& __x)
2041 if (static_cast<bool>(__x))
2043 _M_invoker = __x._M_invoker;
2044 _M_manager = __x._M_manager;
2045 __x._M_manager(_M_functor, __x._M_functor, __clone_functor);
2049 template<typename _Res, typename... _ArgTypes>
2050 template<typename _Functor>
2051 function<_Res(_ArgTypes...)>::
2052 function(_Functor __f,
2054 !is_integral<_Functor>::value, _Useless>::type)
2057 typedef _Function_handler<_Signature_type, _Functor> _My_handler;
2059 if (_My_handler::_M_not_empty_function(__f))
2061 _M_invoker = &_My_handler::_M_invoke;
2062 _M_manager = &_My_handler::_M_manager;
2063 _My_handler::_M_init_functor(_M_functor, std::move(__f));
2067 template<typename _Res, typename... _ArgTypes>
2069 function<_Res(_ArgTypes...)>::
2070 operator()(_ArgTypes... __args) const
2073 __throw_bad_function_call();
2074 return _M_invoker(_M_functor, std::forward<_ArgTypes>(__args)...);
2078 template<typename _Res, typename... _ArgTypes>
2080 function<_Res(_ArgTypes...)>::
2085 _Any_data __typeinfo_result;
2086 _M_manager(__typeinfo_result, _M_functor, __get_type_info);
2087 return *__typeinfo_result._M_access<const type_info*>();
2090 return typeid(void);
2093 template<typename _Res, typename... _ArgTypes>
2094 template<typename _Functor>
2096 function<_Res(_ArgTypes...)>::
2099 if (typeid(_Functor) == target_type() && _M_manager)
2102 if (_M_manager(__ptr, _M_functor, __get_functor_ptr)
2103 && !is_const<_Functor>::value)
2106 return __ptr._M_access<_Functor*>();
2112 template<typename _Res, typename... _ArgTypes>
2113 template<typename _Functor>
2115 function<_Res(_ArgTypes...)>::
2118 if (typeid(_Functor) == target_type() && _M_manager)
2121 _M_manager(__ptr, _M_functor, __get_functor_ptr);
2122 return __ptr._M_access<const _Functor*>();
2129 // [20.7.15.2.6] null pointer comparisons
2132 * @brief Compares a polymorphic function object wrapper against 0
2133 * (the NULL pointer).
2134 * @returns @c true if the wrapper has no target, @c false otherwise
2136 * This function will not throw an %exception.
2138 template<typename _Res, typename... _Args>
2140 operator==(const function<_Res(_Args...)>& __f, _M_clear_type*)
2141 { return !static_cast<bool>(__f); }
2144 template<typename _Res, typename... _Args>
2146 operator==(_M_clear_type*, const function<_Res(_Args...)>& __f)
2147 { return !static_cast<bool>(__f); }
2150 * @brief Compares a polymorphic function object wrapper against 0
2151 * (the NULL pointer).
2152 * @returns @c false if the wrapper has no target, @c true otherwise
2154 * This function will not throw an %exception.
2156 template<typename _Res, typename... _Args>
2158 operator!=(const function<_Res(_Args...)>& __f, _M_clear_type*)
2159 { return static_cast<bool>(__f); }
2162 template<typename _Res, typename... _Args>
2164 operator!=(_M_clear_type*, const function<_Res(_Args...)>& __f)
2165 { return static_cast<bool>(__f); }
2167 // [20.7.15.2.7] specialized algorithms
2170 * @brief Swap the targets of two polymorphic function object wrappers.
2172 * This function will not throw an %exception.
2174 template<typename _Res, typename... _Args>
2176 swap(function<_Res(_Args...)>& __x, function<_Res(_Args...)>& __y)
2180 #endif // __GXX_EXPERIMENTAL_CXX0X__
2182 #endif // _GLIBCXX_FUNCTIONAL