1 // <functional> -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 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>
118 /// Retrieve the result type for a function type.
119 template<typename _Res, typename... _ArgTypes>
120 struct _Weak_result_type_impl<_Res(_ArgTypes...)>
122 typedef _Res result_type;
125 /// Retrieve the result type for a function reference.
126 template<typename _Res, typename... _ArgTypes>
127 struct _Weak_result_type_impl<_Res(&)(_ArgTypes...)>
129 typedef _Res result_type;
132 /// Retrieve the result type for a function pointer.
133 template<typename _Res, typename... _ArgTypes>
134 struct _Weak_result_type_impl<_Res(*)(_ArgTypes...)>
136 typedef _Res result_type;
139 /// Retrieve result type for a member function pointer.
140 template<typename _Res, typename _Class, typename... _ArgTypes>
141 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...)>
143 typedef _Res result_type;
146 /// Retrieve result type for a const member function pointer.
147 template<typename _Res, typename _Class, typename... _ArgTypes>
148 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) const>
150 typedef _Res result_type;
153 /// Retrieve result type for a volatile member function pointer.
154 template<typename _Res, typename _Class, typename... _ArgTypes>
155 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) volatile>
157 typedef _Res result_type;
160 /// Retrieve result type for a const volatile member function pointer.
161 template<typename _Res, typename _Class, typename... _ArgTypes>
162 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...)const volatile>
164 typedef _Res result_type;
168 * Strip top-level cv-qualifiers from the function object and let
169 * _Weak_result_type_impl perform the real work.
171 template<typename _Functor>
172 struct _Weak_result_type
173 : _Weak_result_type_impl<typename remove_cv<_Functor>::type>
177 template<typename _Signature>
180 template<typename _Functor, typename... _ArgTypes>
181 struct result_of<_Functor(_ArgTypes...)>
184 decltype( std::declval<_Functor>()(std::declval<_ArgTypes>()...) )
188 /// Determines if the type _Tp derives from unary_function.
189 template<typename _Tp>
190 struct _Derives_from_unary_function : __sfinae_types
193 template<typename _T1, typename _Res>
194 static __one __test(const volatile unary_function<_T1, _Res>*);
196 // It's tempting to change "..." to const volatile void*, but
197 // that fails when _Tp is a function type.
198 static __two __test(...);
201 static const bool value = sizeof(__test((_Tp*)0)) == 1;
204 /// Determines if the type _Tp derives from binary_function.
205 template<typename _Tp>
206 struct _Derives_from_binary_function : __sfinae_types
209 template<typename _T1, typename _T2, typename _Res>
210 static __one __test(const volatile binary_function<_T1, _T2, _Res>*);
212 // It's tempting to change "..." to const volatile void*, but
213 // that fails when _Tp is a function type.
214 static __two __test(...);
217 static const bool value = sizeof(__test((_Tp*)0)) == 1;
220 /// Turns a function type into a function pointer type
221 template<typename _Tp, bool _IsFunctionType = is_function<_Tp>::value>
222 struct _Function_to_function_pointer
227 template<typename _Tp>
228 struct _Function_to_function_pointer<_Tp, true>
234 * Invoke a function object, which may be either a member pointer or a
235 * function object. The first parameter will tell which.
237 template<typename _Functor, typename... _Args>
239 typename __gnu_cxx::__enable_if<
240 (!is_member_pointer<_Functor>::value
241 && !is_function<_Functor>::value
242 && !is_function<typename remove_pointer<_Functor>::type>::value),
243 typename result_of<_Functor(_Args...)>::type
245 __invoke(_Functor& __f, _Args&&... __args)
247 return __f(std::forward<_Args>(__args)...);
250 // To pick up function references (that will become function pointers)
251 template<typename _Functor, typename... _Args>
253 typename __gnu_cxx::__enable_if<
254 (is_pointer<_Functor>::value
255 && is_function<typename remove_pointer<_Functor>::type>::value),
256 typename result_of<_Functor(_Args...)>::type
258 __invoke(_Functor __f, _Args&&... __args)
260 return __f(std::forward<_Args>(__args)...);
264 * Knowing which of unary_function and binary_function _Tp derives
265 * from, derives from the same and ensures that reference_wrapper
266 * will have a weak result type. See cases below.
268 template<bool _Unary, bool _Binary, typename _Tp>
269 struct _Reference_wrapper_base_impl;
271 // Not a unary_function or binary_function, so try a weak result type.
272 template<typename _Tp>
273 struct _Reference_wrapper_base_impl<false, false, _Tp>
274 : _Weak_result_type<_Tp>
277 // unary_function but not binary_function
278 template<typename _Tp>
279 struct _Reference_wrapper_base_impl<true, false, _Tp>
280 : unary_function<typename _Tp::argument_type,
281 typename _Tp::result_type>
284 // binary_function but not unary_function
285 template<typename _Tp>
286 struct _Reference_wrapper_base_impl<false, true, _Tp>
287 : binary_function<typename _Tp::first_argument_type,
288 typename _Tp::second_argument_type,
289 typename _Tp::result_type>
292 // Both unary_function and binary_function. Import result_type to
294 template<typename _Tp>
295 struct _Reference_wrapper_base_impl<true, true, _Tp>
296 : unary_function<typename _Tp::argument_type,
297 typename _Tp::result_type>,
298 binary_function<typename _Tp::first_argument_type,
299 typename _Tp::second_argument_type,
300 typename _Tp::result_type>
302 typedef typename _Tp::result_type result_type;
306 * Derives from unary_function or binary_function when it
307 * can. Specializations handle all of the easy cases. The primary
308 * template determines what to do with a class type, which may
309 * derive from both unary_function and binary_function.
311 template<typename _Tp>
312 struct _Reference_wrapper_base
313 : _Reference_wrapper_base_impl<
314 _Derives_from_unary_function<_Tp>::value,
315 _Derives_from_binary_function<_Tp>::value,
319 // - a function type (unary)
320 template<typename _Res, typename _T1>
321 struct _Reference_wrapper_base<_Res(_T1)>
322 : unary_function<_T1, _Res>
325 // - a function type (binary)
326 template<typename _Res, typename _T1, typename _T2>
327 struct _Reference_wrapper_base<_Res(_T1, _T2)>
328 : binary_function<_T1, _T2, _Res>
331 // - a function pointer type (unary)
332 template<typename _Res, typename _T1>
333 struct _Reference_wrapper_base<_Res(*)(_T1)>
334 : unary_function<_T1, _Res>
337 // - a function pointer type (binary)
338 template<typename _Res, typename _T1, typename _T2>
339 struct _Reference_wrapper_base<_Res(*)(_T1, _T2)>
340 : binary_function<_T1, _T2, _Res>
343 // - a pointer to member function type (unary, no qualifiers)
344 template<typename _Res, typename _T1>
345 struct _Reference_wrapper_base<_Res (_T1::*)()>
346 : unary_function<_T1*, _Res>
349 // - a pointer to member function type (binary, no qualifiers)
350 template<typename _Res, typename _T1, typename _T2>
351 struct _Reference_wrapper_base<_Res (_T1::*)(_T2)>
352 : binary_function<_T1*, _T2, _Res>
355 // - a pointer to member function type (unary, const)
356 template<typename _Res, typename _T1>
357 struct _Reference_wrapper_base<_Res (_T1::*)() const>
358 : unary_function<const _T1*, _Res>
361 // - a pointer to member function type (binary, const)
362 template<typename _Res, typename _T1, typename _T2>
363 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const>
364 : binary_function<const _T1*, _T2, _Res>
367 // - a pointer to member function type (unary, volatile)
368 template<typename _Res, typename _T1>
369 struct _Reference_wrapper_base<_Res (_T1::*)() volatile>
370 : unary_function<volatile _T1*, _Res>
373 // - a pointer to member function type (binary, volatile)
374 template<typename _Res, typename _T1, typename _T2>
375 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) volatile>
376 : binary_function<volatile _T1*, _T2, _Res>
379 // - a pointer to member function type (unary, const volatile)
380 template<typename _Res, typename _T1>
381 struct _Reference_wrapper_base<_Res (_T1::*)() const volatile>
382 : unary_function<const volatile _T1*, _Res>
385 // - a pointer to member function type (binary, const volatile)
386 template<typename _Res, typename _T1, typename _T2>
387 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const volatile>
388 : binary_function<const volatile _T1*, _T2, _Res>
391 /// reference_wrapper
392 template<typename _Tp>
393 class reference_wrapper
394 : public _Reference_wrapper_base<typename remove_cv<_Tp>::type>
396 // If _Tp is a function type, we can't form result_of<_Tp(...)>,
397 // so turn it into a function pointer type.
398 typedef typename _Function_to_function_pointer<_Tp>::type
405 reference_wrapper(_Tp& __indata): _M_data(&__indata)
408 reference_wrapper(_Tp&&) = delete;
410 reference_wrapper(const reference_wrapper<_Tp>& __inref):
411 _M_data(__inref._M_data)
415 operator=(const reference_wrapper<_Tp>& __inref)
417 _M_data = __inref._M_data;
421 operator _Tp&() const
422 { return this->get(); }
428 template<typename... _Args>
429 typename result_of<_M_func_type(_Args...)>::type
430 operator()(_Args&&... __args) const
432 return __invoke(get(), std::forward<_Args>(__args)...);
437 // Denotes a reference should be taken to a variable.
438 template<typename _Tp>
439 inline reference_wrapper<_Tp>
441 { return reference_wrapper<_Tp>(__t); }
443 // Denotes a const reference should be taken to a variable.
444 template<typename _Tp>
445 inline reference_wrapper<const _Tp>
447 { return reference_wrapper<const _Tp>(__t); }
449 template<typename _Tp>
450 inline reference_wrapper<_Tp>
451 ref(reference_wrapper<_Tp> __t)
452 { return ref(__t.get()); }
454 template<typename _Tp>
455 inline reference_wrapper<const _Tp>
456 cref(reference_wrapper<_Tp> __t)
457 { return cref(__t.get()); }
459 template<typename _Tp, bool>
460 struct _Mem_fn_const_or_non
462 typedef const _Tp& type;
465 template<typename _Tp>
466 struct _Mem_fn_const_or_non<_Tp, false>
472 * Derives from @c unary_function or @c binary_function, or perhaps
473 * nothing, depending on the number of arguments provided. The
474 * primary template is the basis case, which derives nothing.
476 template<typename _Res, typename... _ArgTypes>
477 struct _Maybe_unary_or_binary_function { };
479 /// Derives from @c unary_function, as appropriate.
480 template<typename _Res, typename _T1>
481 struct _Maybe_unary_or_binary_function<_Res, _T1>
482 : std::unary_function<_T1, _Res> { };
484 /// Derives from @c binary_function, as appropriate.
485 template<typename _Res, typename _T1, typename _T2>
486 struct _Maybe_unary_or_binary_function<_Res, _T1, _T2>
487 : std::binary_function<_T1, _T2, _Res> { };
489 /// Implementation of @c mem_fn for member function pointers.
490 template<typename _Res, typename _Class, typename... _ArgTypes>
491 class _Mem_fn<_Res (_Class::*)(_ArgTypes...)>
492 : public _Maybe_unary_or_binary_function<_Res, _Class*, _ArgTypes...>
494 typedef _Res (_Class::*_Functor)(_ArgTypes...);
496 template<typename _Tp>
498 _M_call(_Tp& __object, const volatile _Class *,
499 _ArgTypes... __args) const
500 { return (__object.*__pmf)(__args...); }
502 template<typename _Tp>
504 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
505 { return ((*__ptr).*__pmf)(__args...); }
508 typedef _Res result_type;
510 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
514 operator()(_Class& __object, _ArgTypes... __args) const
515 { return (__object.*__pmf)(__args...); }
519 operator()(_Class* __object, _ArgTypes... __args) const
520 { return (__object->*__pmf)(__args...); }
522 // Handle smart pointers, references and pointers to derived
523 template<typename _Tp>
525 operator()(_Tp& __object, _ArgTypes... __args) const
526 { return _M_call(__object, &__object, __args...); }
532 /// Implementation of @c mem_fn for const member function pointers.
533 template<typename _Res, typename _Class, typename... _ArgTypes>
534 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) const>
535 : public _Maybe_unary_or_binary_function<_Res, const _Class*,
538 typedef _Res (_Class::*_Functor)(_ArgTypes...) const;
540 template<typename _Tp>
542 _M_call(_Tp& __object, const volatile _Class *,
543 _ArgTypes... __args) const
544 { return (__object.*__pmf)(__args...); }
546 template<typename _Tp>
548 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
549 { return ((*__ptr).*__pmf)(__args...); }
552 typedef _Res result_type;
554 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
558 operator()(const _Class& __object, _ArgTypes... __args) const
559 { return (__object.*__pmf)(__args...); }
563 operator()(const _Class* __object, _ArgTypes... __args) const
564 { return (__object->*__pmf)(__args...); }
566 // Handle smart pointers, references and pointers to derived
567 template<typename _Tp>
568 _Res operator()(_Tp& __object, _ArgTypes... __args) const
569 { return _M_call(__object, &__object, __args...); }
575 /// Implementation of @c mem_fn for volatile member function pointers.
576 template<typename _Res, typename _Class, typename... _ArgTypes>
577 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) volatile>
578 : public _Maybe_unary_or_binary_function<_Res, volatile _Class*,
581 typedef _Res (_Class::*_Functor)(_ArgTypes...) volatile;
583 template<typename _Tp>
585 _M_call(_Tp& __object, const volatile _Class *,
586 _ArgTypes... __args) const
587 { return (__object.*__pmf)(__args...); }
589 template<typename _Tp>
591 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
592 { return ((*__ptr).*__pmf)(__args...); }
595 typedef _Res result_type;
597 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
601 operator()(volatile _Class& __object, _ArgTypes... __args) const
602 { return (__object.*__pmf)(__args...); }
606 operator()(volatile _Class* __object, _ArgTypes... __args) const
607 { return (__object->*__pmf)(__args...); }
609 // Handle smart pointers, references and pointers to derived
610 template<typename _Tp>
612 operator()(_Tp& __object, _ArgTypes... __args) const
613 { return _M_call(__object, &__object, __args...); }
619 /// Implementation of @c mem_fn for const volatile member function pointers.
620 template<typename _Res, typename _Class, typename... _ArgTypes>
621 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) const volatile>
622 : public _Maybe_unary_or_binary_function<_Res, const volatile _Class*,
625 typedef _Res (_Class::*_Functor)(_ArgTypes...) const volatile;
627 template<typename _Tp>
629 _M_call(_Tp& __object, const volatile _Class *,
630 _ArgTypes... __args) const
631 { return (__object.*__pmf)(__args...); }
633 template<typename _Tp>
635 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
636 { return ((*__ptr).*__pmf)(__args...); }
639 typedef _Res result_type;
641 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
645 operator()(const volatile _Class& __object, _ArgTypes... __args) const
646 { return (__object.*__pmf)(__args...); }
650 operator()(const volatile _Class* __object, _ArgTypes... __args) const
651 { return (__object->*__pmf)(__args...); }
653 // Handle smart pointers, references and pointers to derived
654 template<typename _Tp>
655 _Res operator()(_Tp& __object, _ArgTypes... __args) const
656 { return _M_call(__object, &__object, __args...); }
663 template<typename _Res, typename _Class>
664 class _Mem_fn<_Res _Class::*>
666 // This bit of genius is due to Peter Dimov, improved slightly by
668 template<typename _Tp>
670 _M_call(_Tp& __object, _Class *) const
671 { return __object.*__pm; }
673 template<typename _Tp, typename _Up>
675 _M_call(_Tp& __object, _Up * const *) const
676 { return (*__object).*__pm; }
678 template<typename _Tp, typename _Up>
680 _M_call(_Tp& __object, const _Up * const *) const
681 { return (*__object).*__pm; }
683 template<typename _Tp>
685 _M_call(_Tp& __object, const _Class *) const
686 { return __object.*__pm; }
688 template<typename _Tp>
690 _M_call(_Tp& __ptr, const volatile void*) const
691 { return (*__ptr).*__pm; }
693 template<typename _Tp> static _Tp& __get_ref();
695 template<typename _Tp>
696 static __sfinae_types::__one __check_const(_Tp&, _Class*);
697 template<typename _Tp, typename _Up>
698 static __sfinae_types::__one __check_const(_Tp&, _Up * const *);
699 template<typename _Tp, typename _Up>
700 static __sfinae_types::__two __check_const(_Tp&, const _Up * const *);
701 template<typename _Tp>
702 static __sfinae_types::__two __check_const(_Tp&, const _Class*);
703 template<typename _Tp>
704 static __sfinae_types::__two __check_const(_Tp&, const volatile void*);
707 template<typename _Tp>
709 : _Mem_fn_const_or_non<_Res,
710 (sizeof(__sfinae_types::__two)
711 == sizeof(__check_const<_Tp>(__get_ref<_Tp>(), (_Tp*)0)))>
714 template<typename _Signature>
717 template<typename _CVMem, typename _Tp>
718 struct result<_CVMem(_Tp)>
719 : public _Result_type<_Tp> { };
721 template<typename _CVMem, typename _Tp>
722 struct result<_CVMem(_Tp&)>
723 : public _Result_type<_Tp> { };
726 _Mem_fn(_Res _Class::*__pm) : __pm(__pm) { }
730 operator()(_Class& __object) const
731 { return __object.*__pm; }
734 operator()(const _Class& __object) const
735 { return __object.*__pm; }
739 operator()(_Class* __object) const
740 { return __object->*__pm; }
743 operator()(const _Class* __object) const
744 { return __object->*__pm; }
746 // Handle smart pointers and derived
747 template<typename _Tp>
748 typename _Result_type<_Tp>::type
749 operator()(_Tp& __unknown) const
750 { return _M_call(__unknown, &__unknown); }
757 * @brief Returns a function object that forwards to the member
760 template<typename _Tp, typename _Class>
761 inline _Mem_fn<_Tp _Class::*>
762 mem_fn(_Tp _Class::* __pm)
764 return _Mem_fn<_Tp _Class::*>(__pm);
768 * @brief Determines if the given type _Tp is a function object
769 * should be treated as a subexpression when evaluating calls to
770 * function objects returned by bind(). [TR1 3.6.1]
772 template<typename _Tp>
773 struct is_bind_expression
774 { static const bool value = false; };
776 template<typename _Tp>
777 const bool is_bind_expression<_Tp>::value;
780 * @brief Determines if the given type _Tp is a placeholder in a
781 * bind() expression and, if so, which placeholder it is. [TR1 3.6.2]
783 template<typename _Tp>
784 struct is_placeholder
785 { static const int value = 0; };
787 template<typename _Tp>
788 const int is_placeholder<_Tp>::value;
790 /// The type of placeholder objects defined by libstdc++.
791 template<int _Num> struct _Placeholder { };
793 /** @namespace std::placeholders
794 * @brief ISO C++ 0x entities sub namespace for functional.
796 * Define a large number of placeholders. There is no way to
797 * simplify this with variadic templates, because we're introducing
798 * unique names for each.
800 namespace placeholders
813 _Placeholder<10> _10;
814 _Placeholder<11> _11;
815 _Placeholder<12> _12;
816 _Placeholder<13> _13;
817 _Placeholder<14> _14;
818 _Placeholder<15> _15;
819 _Placeholder<16> _16;
820 _Placeholder<17> _17;
821 _Placeholder<18> _18;
822 _Placeholder<19> _19;
823 _Placeholder<20> _20;
824 _Placeholder<21> _21;
825 _Placeholder<22> _22;
826 _Placeholder<23> _23;
827 _Placeholder<24> _24;
828 _Placeholder<25> _25;
829 _Placeholder<26> _26;
830 _Placeholder<27> _27;
831 _Placeholder<28> _28;
832 _Placeholder<29> _29;
837 * Partial specialization of is_placeholder that provides the placeholder
838 * number for the placeholder objects defined by libstdc++.
841 struct is_placeholder<_Placeholder<_Num> >
842 { static const int value = _Num; };
845 const int is_placeholder<_Placeholder<_Num> >::value;
848 * Stores a tuple of indices. Used by bind() to extract the elements
851 template<int... _Indexes>
852 struct _Index_tuple { };
854 /// Builds an _Index_tuple<0, 1, 2, ..., _Num-1>.
855 template<std::size_t _Num, typename _Tuple = _Index_tuple<> >
856 struct _Build_index_tuple;
858 template<std::size_t _Num, int... _Indexes>
859 struct _Build_index_tuple<_Num, _Index_tuple<_Indexes...> >
860 : _Build_index_tuple<_Num - 1,
861 _Index_tuple<_Indexes..., sizeof...(_Indexes)> >
865 template<int... _Indexes>
866 struct _Build_index_tuple<0, _Index_tuple<_Indexes...> >
868 typedef _Index_tuple<_Indexes...> __type;
872 * Used by _Safe_tuple_element to indicate that there is no tuple
873 * element at this position.
875 struct _No_tuple_element;
878 * Implementation helper for _Safe_tuple_element. This primary
879 * template handles the case where it is safe to use @c
882 template<int __i, typename _Tuple, bool _IsSafe>
883 struct _Safe_tuple_element_impl
884 : tuple_element<__i, _Tuple> { };
887 * Implementation helper for _Safe_tuple_element. This partial
888 * specialization handles the case where it is not safe to use @c
889 * tuple_element. We just return @c _No_tuple_element.
891 template<int __i, typename _Tuple>
892 struct _Safe_tuple_element_impl<__i, _Tuple, false>
894 typedef _No_tuple_element type;
898 * Like tuple_element, but returns @c _No_tuple_element when
899 * tuple_element would return an error.
901 template<int __i, typename _Tuple>
902 struct _Safe_tuple_element
903 : _Safe_tuple_element_impl<__i, _Tuple,
904 (__i >= 0 && __i < tuple_size<_Tuple>::value)>
909 * Maps an argument to bind() into an actual argument to the bound
910 * function object [TR1 3.6.3/5]. Only the first parameter should
911 * be specified: the rest are used to determine among the various
912 * implementations. Note that, although this class is a function
913 * object, it isn't entirely normal because it takes only two
914 * parameters regardless of the number of parameters passed to the
915 * bind expression. The first parameter is the bound argument and
916 * the second parameter is a tuple containing references to the
917 * rest of the arguments.
919 template<typename _Arg,
920 bool _IsBindExp = is_bind_expression<_Arg>::value,
921 bool _IsPlaceholder = (is_placeholder<_Arg>::value > 0)>
925 * If the argument is reference_wrapper<_Tp>, returns the
926 * underlying reference. [TR1 3.6.3/5 bullet 1]
928 template<typename _Tp>
929 class _Mu<reference_wrapper<_Tp>, false, false>
932 typedef _Tp& result_type;
934 /* Note: This won't actually work for const volatile
935 * reference_wrappers, because reference_wrapper::get() is const
936 * but not volatile-qualified. This might be a defect in the TR.
938 template<typename _CVRef, typename _Tuple>
940 operator()(_CVRef& __arg, const _Tuple&) const volatile
941 { return __arg.get(); }
945 * If the argument is a bind expression, we invoke the underlying
946 * function object with the same cv-qualifiers as we are given and
947 * pass along all of our arguments (unwrapped). [TR1 3.6.3/5 bullet 2]
949 template<typename _Arg>
950 class _Mu<_Arg, true, false>
953 template<typename _Signature> class result;
955 // Determine the result type when we pass the arguments along. This
956 // involves passing along the cv-qualifiers placed on _Mu and
957 // unwrapping the argument bundle.
958 template<typename _CVMu, typename _CVArg, typename... _Args>
959 class result<_CVMu(_CVArg, tuple<_Args...>)>
960 : public result_of<_CVArg(_Args...)> { };
962 template<typename _CVArg, typename... _Args>
963 typename result_of<_CVArg(_Args...)>::type
964 operator()(_CVArg& __arg,
965 tuple<_Args...>& __tuple) const volatile
967 // Construct an index tuple and forward to __call
968 typedef typename _Build_index_tuple<sizeof...(_Args)>::__type
970 return this->__call(__arg, __tuple, _Indexes());
974 // Invokes the underlying function object __arg by unpacking all
975 // of the arguments in the tuple.
976 template<typename _CVArg, typename... _Args, int... _Indexes>
977 typename result_of<_CVArg(_Args...)>::type
978 __call(_CVArg& __arg, tuple<_Args...>& __tuple,
979 const _Index_tuple<_Indexes...>&) const volatile
981 return __arg(std::forward<_Args>(get<_Indexes>(__tuple))...);
986 * If the argument is a placeholder for the Nth argument, returns
987 * a reference to the Nth argument to the bind function object.
988 * [TR1 3.6.3/5 bullet 3]
990 template<typename _Arg>
991 class _Mu<_Arg, false, true>
994 template<typename _Signature> class result;
996 template<typename _CVMu, typename _CVArg, typename _Tuple>
997 class result<_CVMu(_CVArg, _Tuple)>
999 // Add a reference, if it hasn't already been done for us.
1000 // This allows us to be a little bit sloppy in constructing
1001 // the tuple that we pass to result_of<...>.
1002 typedef typename _Safe_tuple_element<(is_placeholder<_Arg>::value
1007 typedef typename add_rvalue_reference<__base_type>::type type;
1010 template<typename _Tuple>
1011 typename result<_Mu(_Arg, _Tuple)>::type
1012 operator()(const volatile _Arg&, _Tuple& __tuple) const volatile
1014 return std::forward<typename result<_Mu(_Arg, _Tuple)>::type>(
1015 ::std::get<(is_placeholder<_Arg>::value - 1)>(__tuple));
1020 * If the argument is just a value, returns a reference to that
1021 * value. The cv-qualifiers on the reference are the same as the
1022 * cv-qualifiers on the _Mu object. [TR1 3.6.3/5 bullet 4]
1024 template<typename _Arg>
1025 class _Mu<_Arg, false, false>
1028 template<typename _Signature> struct result;
1030 template<typename _CVMu, typename _CVArg, typename _Tuple>
1031 struct result<_CVMu(_CVArg, _Tuple)>
1033 typedef typename add_lvalue_reference<_CVArg>::type type;
1036 // Pick up the cv-qualifiers of the argument
1037 template<typename _CVArg, typename _Tuple>
1039 operator()(_CVArg&& __arg, const _Tuple&) const volatile
1040 { return std::forward<_CVArg>(__arg); }
1044 * Maps member pointers into instances of _Mem_fn but leaves all
1045 * other function objects untouched. Used by tr1::bind(). The
1046 * primary template handles the non--member-pointer case.
1048 template<typename _Tp>
1049 struct _Maybe_wrap_member_pointer
1054 __do_wrap(const _Tp& __x)
1059 * Maps member pointers into instances of _Mem_fn but leaves all
1060 * other function objects untouched. Used by tr1::bind(). This
1061 * partial specialization handles the member pointer case.
1063 template<typename _Tp, typename _Class>
1064 struct _Maybe_wrap_member_pointer<_Tp _Class::*>
1066 typedef _Mem_fn<_Tp _Class::*> type;
1069 __do_wrap(_Tp _Class::* __pm)
1070 { return type(__pm); }
1073 // Specialization needed to prevent "forming reference to void" errors when
1074 // bind<void>() is called, because argument deduction instantiates
1075 // _Maybe_wrap_member_pointer<void> outside the immediate context where
1078 struct _Maybe_wrap_member_pointer<void>
1083 /// Type of the function object returned from bind().
1084 template<typename _Signature>
1087 template<typename _Functor, typename... _Bound_args>
1088 class _Bind<_Functor(_Bound_args...)>
1089 : public _Weak_result_type<_Functor>
1091 typedef _Bind __self_type;
1092 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
1096 tuple<_Bound_args...> _M_bound_args;
1099 template<typename... _Args, int... _Indexes, typename _Sfinae
1100 = decltype( std::declval<_Functor>()(
1101 _Mu<_Bound_args>()( std::declval<_Bound_args&>(),
1102 std::declval<tuple<_Args...>&>() )... ) )>
1104 _Functor(typename result_of<_Mu<_Bound_args>
1105 (_Bound_args&, tuple<_Args...>&)>::type...)
1107 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>)
1109 return _M_f(_Mu<_Bound_args>()
1110 (get<_Indexes>(_M_bound_args), __args)...);
1114 template<typename... _Args, int... _Indexes, typename _Sfinae
1115 = decltype( std::declval<const _Functor>()(
1116 _Mu<_Bound_args>()( std::declval<const _Bound_args&>(),
1117 std::declval<tuple<_Args...>&>() )... ) )>
1119 const _Functor(typename result_of<_Mu<_Bound_args>
1120 (const _Bound_args&, tuple<_Args...>&)
1122 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>) const
1124 return _M_f(_Mu<_Bound_args>()
1125 (get<_Indexes>(_M_bound_args), __args)...);
1130 template<typename... _Args, int... _Indexes, typename _Sfinae
1131 = decltype( std::declval<volatile _Functor>()(
1132 _Mu<_Bound_args>()( std::declval<volatile _Bound_args&>(),
1133 std::declval<tuple<_Args...>&>() )... ) )>
1135 volatile _Functor(typename result_of<_Mu<_Bound_args>
1136 (volatile _Bound_args&, tuple<_Args...>&)
1138 __call(tuple<_Args...>&& __args,
1139 _Index_tuple<_Indexes...>) volatile
1141 return _M_f(_Mu<_Bound_args>()
1142 (get<_Indexes>(_M_bound_args), __args)...);
1145 // Call as const volatile
1146 template<typename... _Args, int... _Indexes, typename _Sfinae
1147 = decltype( std::declval<const volatile _Functor>()(
1148 _Mu<_Bound_args>()( std::declval<const volatile _Bound_args&>(),
1149 std::declval<tuple<_Args...>&>() )... ) )>
1151 const volatile _Functor(typename result_of<_Mu<_Bound_args>
1152 (const volatile _Bound_args&,
1155 __call(tuple<_Args...>&& __args,
1156 _Index_tuple<_Indexes...>) const volatile
1158 return _M_f(_Mu<_Bound_args>()
1159 (get<_Indexes>(_M_bound_args), __args)...);
1164 explicit _Bind(_Functor __f, _Bound_args... __bound_args)
1165 : _M_f(std::forward<_Functor>(__f)),
1166 _M_bound_args(std::forward<_Bound_args>(__bound_args)...)
1170 template<typename... _Args, typename _Sfinae
1171 = decltype( std::declval<_Functor>()(
1172 _Mu<_Bound_args>()( std::declval<_Bound_args&>(),
1173 std::declval<tuple<_Args...>&>() )... ) )>
1175 _Functor(typename result_of<_Mu<_Bound_args>
1176 (_Bound_args&, tuple<_Args...>&)>::type...)
1178 operator()(_Args&&... __args)
1180 return this->__call(tuple<_Args...>(std::forward<_Args>(__args)...),
1185 template<typename... _Args, typename _Sfinae
1186 = decltype( std::declval<const _Functor>()(
1187 _Mu<_Bound_args>()( std::declval<const _Bound_args&>(),
1188 std::declval<tuple<_Args...>&>() )... ) )>
1190 const _Functor(typename result_of<_Mu<_Bound_args>
1191 (const _Bound_args&, tuple<_Args...>&)>::type...)
1193 operator()(_Args&&... __args) const
1195 return this->__call(tuple<_Args...>(std::forward<_Args>(__args)...),
1201 template<typename... _Args, typename _Sfinae
1202 = decltype( std::declval<volatile _Functor>()(
1203 _Mu<_Bound_args>()( std::declval<volatile _Bound_args&>(),
1204 std::declval<tuple<_Args...>&>() )... ) )>
1206 volatile _Functor(typename result_of<_Mu<_Bound_args>
1207 (volatile _Bound_args&, tuple<_Args...>&)>::type...)
1209 operator()(_Args&&... __args) volatile
1211 return this->__call(tuple<_Args...>(std::forward<_Args>(__args)...),
1216 // Call as const volatile
1217 template<typename... _Args, typename _Sfinae
1218 = decltype( std::declval<const volatile _Functor>()(
1219 _Mu<_Bound_args>()( std::declval<const volatile _Bound_args&>(),
1220 std::declval<tuple<_Args...>&>() )... ) )>
1222 const volatile _Functor(typename result_of<_Mu<_Bound_args>
1223 (const volatile _Bound_args&,
1224 tuple<_Args...>&)>::type...)
1226 operator()(_Args&&... __args) const volatile
1228 return this->__call(tuple<_Args...>(std::forward<_Args>(__args)...),
1234 /// Type of the function object returned from bind<R>().
1235 template<typename _Result, typename _Signature>
1236 struct _Bind_result;
1238 template<typename _Result, typename _Functor, typename... _Bound_args>
1239 class _Bind_result<_Result, _Functor(_Bound_args...)>
1241 typedef _Bind_result __self_type;
1242 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
1246 tuple<_Bound_args...> _M_bound_args;
1249 template<typename _Res>
1250 struct __enable_if_void : enable_if<is_void<_Res>::value, int> { };
1251 template<typename _Res>
1252 struct __disable_if_void : enable_if<!is_void<_Res>::value, int> { };
1255 template<typename _Res, typename... _Args, int... _Indexes>
1257 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1258 typename __disable_if_void<_Res>::type = 0)
1260 return _M_f(_Mu<_Bound_args>()
1261 (get<_Indexes>(_M_bound_args), __args)...);
1264 // Call unqualified, return void
1265 template<typename _Res, typename... _Args, int... _Indexes>
1267 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1268 typename __enable_if_void<_Res>::type = 0)
1270 _M_f(_Mu<_Bound_args>()
1271 (get<_Indexes>(_M_bound_args), __args)...);
1275 template<typename _Res, typename... _Args, int... _Indexes>
1277 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1278 typename __disable_if_void<_Res>::type = 0) const
1280 return _M_f(_Mu<_Bound_args>()
1281 (get<_Indexes>(_M_bound_args), __args)...);
1284 // Call as const, return void
1285 template<typename _Res, typename... _Args, int... _Indexes>
1287 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1288 typename __enable_if_void<_Res>::type = 0) const
1290 _M_f(_Mu<_Bound_args>()
1291 (get<_Indexes>(_M_bound_args), __args)...);
1295 template<typename _Res, typename... _Args, int... _Indexes>
1297 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1298 typename __disable_if_void<_Res>::type = 0) volatile
1300 return _M_f(_Mu<_Bound_args>()
1301 (get<_Indexes>(_M_bound_args), __args)...);
1304 // Call as volatile, return void
1305 template<typename _Res, typename... _Args, int... _Indexes>
1307 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1308 typename __enable_if_void<_Res>::type = 0) volatile
1310 _M_f(_Mu<_Bound_args>()
1311 (get<_Indexes>(_M_bound_args), __args)...);
1314 // Call as const volatile
1315 template<typename _Res, typename... _Args, int... _Indexes>
1317 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1318 typename __disable_if_void<_Res>::type = 0) const volatile
1320 return _M_f(_Mu<_Bound_args>()
1321 (get<_Indexes>(_M_bound_args), __args)...);
1324 // Call as const volatile, return void
1325 template<typename _Res, typename... _Args, int... _Indexes>
1327 __call(tuple<_Args...>&& __args,
1328 _Index_tuple<_Indexes...>,
1329 typename __enable_if_void<_Res>::type = 0) const volatile
1331 _M_f(_Mu<_Bound_args>()
1332 (get<_Indexes>(_M_bound_args), __args)...);
1336 typedef _Result result_type;
1339 _Bind_result(_Functor __f, _Bound_args... __bound_args)
1340 : _M_f(std::forward<_Functor>(__f)),
1341 _M_bound_args(std::forward<_Bound_args>(__bound_args)...)
1345 template<typename... _Args>
1347 operator()(_Args&&... __args)
1349 return this->__call<_Result>(
1350 tuple<_Args...>(std::forward<_Args...>(__args)...),
1355 template<typename... _Args>
1357 operator()(_Args&&... __args) const
1359 return this->__call<_Result>(
1360 tuple<_Args...>(std::forward<_Args...>(__args)...),
1365 template<typename... _Args>
1367 operator()(_Args&&... __args) volatile
1369 return this->__call<_Result>(
1370 tuple<_Args...>(std::forward<_Args...>(__args)...),
1374 // Call as const volatile
1375 template<typename... _Args>
1377 operator()(_Args&&... __args) const volatile
1379 return this->__call<_Result>(
1380 tuple<_Args...>(std::forward<_Args...>(__args)...),
1385 /// Class template _Bind is always a bind expression.
1386 template<typename _Signature>
1387 struct is_bind_expression<_Bind<_Signature> >
1388 { static const bool value = true; };
1390 template<typename _Signature>
1391 const bool is_bind_expression<_Bind<_Signature> >::value;
1393 /// Class template _Bind_result is always a bind expression.
1394 template<typename _Result, typename _Signature>
1395 struct is_bind_expression<_Bind_result<_Result, _Signature> >
1396 { static const bool value = true; };
1398 template<typename _Result, typename _Signature>
1399 const bool is_bind_expression<_Bind_result<_Result, _Signature> >::value;
1402 template<typename _Functor, typename... _ArgTypes>
1404 _Bind<typename _Maybe_wrap_member_pointer<_Functor>::type(_ArgTypes...)>
1405 bind(_Functor __f, _ArgTypes... __args)
1407 typedef _Maybe_wrap_member_pointer<_Functor> __maybe_type;
1408 typedef typename __maybe_type::type __functor_type;
1409 typedef _Bind<__functor_type(_ArgTypes...)> __result_type;
1410 return __result_type(__maybe_type::__do_wrap(__f),
1411 std::forward<_ArgTypes>(__args)...);
1414 template<typename _Result, typename _Functor, typename... _ArgTypes>
1416 _Bind_result<_Result,
1417 typename _Maybe_wrap_member_pointer<_Functor>::type
1419 bind(_Functor __f, _ArgTypes... __args)
1421 typedef _Maybe_wrap_member_pointer<_Functor> __maybe_type;
1422 typedef typename __maybe_type::type __functor_type;
1423 typedef _Bind_result<_Result, __functor_type(_ArgTypes...)>
1425 return __result_type(__maybe_type::__do_wrap(__f),
1426 std::forward<_ArgTypes>(__args)...);
1430 * @brief Exception class thrown when class template function's
1431 * operator() is called with an empty target.
1432 * @ingroup exceptions
1434 class bad_function_call : public std::exception { };
1437 * The integral constant expression 0 can be converted into a
1438 * pointer to this type. It is used by the function template to
1439 * accept NULL pointers.
1441 struct _M_clear_type;
1444 * Trait identifying "location-invariant" types, meaning that the
1445 * address of the object (or any of its members) will not escape.
1446 * Also implies a trivial copy constructor and assignment operator.
1448 template<typename _Tp>
1449 struct __is_location_invariant
1450 : integral_constant<bool,
1451 (is_pointer<_Tp>::value
1452 || is_member_pointer<_Tp>::value)>
1456 class _Undefined_class;
1461 const void* _M_const_object;
1462 void (*_M_function_pointer)();
1463 void (_Undefined_class::*_M_member_pointer)();
1468 void* _M_access() { return &_M_pod_data[0]; }
1469 const void* _M_access() const { return &_M_pod_data[0]; }
1471 template<typename _Tp>
1474 { return *static_cast<_Tp*>(_M_access()); }
1476 template<typename _Tp>
1479 { return *static_cast<const _Tp*>(_M_access()); }
1481 _Nocopy_types _M_unused;
1482 char _M_pod_data[sizeof(_Nocopy_types)];
1485 enum _Manager_operation
1493 // Simple type wrapper that helps avoid annoying const problems
1494 // when casting between void pointers and pointers-to-pointers.
1495 template<typename _Tp>
1496 struct _Simple_type_wrapper
1498 _Simple_type_wrapper(_Tp __value) : __value(__value) { }
1503 template<typename _Tp>
1504 struct __is_location_invariant<_Simple_type_wrapper<_Tp> >
1505 : __is_location_invariant<_Tp>
1509 // Converts a reference to a function object into a callable
1511 template<typename _Functor>
1513 __callable_functor(_Functor& __f)
1516 template<typename _Member, typename _Class>
1517 inline _Mem_fn<_Member _Class::*>
1518 __callable_functor(_Member _Class::* &__p)
1519 { return mem_fn(__p); }
1521 template<typename _Member, typename _Class>
1522 inline _Mem_fn<_Member _Class::*>
1523 __callable_functor(_Member _Class::* const &__p)
1524 { return mem_fn(__p); }
1526 template<typename _Signature>
1529 /// Base class of all polymorphic function object wrappers.
1530 class _Function_base
1533 static const std::size_t _M_max_size = sizeof(_Nocopy_types);
1534 static const std::size_t _M_max_align = __alignof__(_Nocopy_types);
1536 template<typename _Functor>
1540 static const bool __stored_locally =
1541 (__is_location_invariant<_Functor>::value
1542 && sizeof(_Functor) <= _M_max_size
1543 && __alignof__(_Functor) <= _M_max_align
1544 && (_M_max_align % __alignof__(_Functor) == 0));
1546 typedef integral_constant<bool, __stored_locally> _Local_storage;
1548 // Retrieve a pointer to the function object
1550 _M_get_pointer(const _Any_data& __source)
1552 const _Functor* __ptr =
1553 __stored_locally? &__source._M_access<_Functor>()
1554 /* have stored a pointer */ : __source._M_access<_Functor*>();
1555 return const_cast<_Functor*>(__ptr);
1558 // Clone a location-invariant function object that fits within
1559 // an _Any_data structure.
1561 _M_clone(_Any_data& __dest, const _Any_data& __source, true_type)
1563 new (__dest._M_access()) _Functor(__source._M_access<_Functor>());
1566 // Clone a function object that is not location-invariant or
1567 // that cannot fit into an _Any_data structure.
1569 _M_clone(_Any_data& __dest, const _Any_data& __source, false_type)
1571 __dest._M_access<_Functor*>() =
1572 new _Functor(*__source._M_access<_Functor*>());
1575 // Destroying a location-invariant object may still require
1578 _M_destroy(_Any_data& __victim, true_type)
1580 __victim._M_access<_Functor>().~_Functor();
1583 // Destroying an object located on the heap.
1585 _M_destroy(_Any_data& __victim, false_type)
1587 delete __victim._M_access<_Functor*>();
1592 _M_manager(_Any_data& __dest, const _Any_data& __source,
1593 _Manager_operation __op)
1598 case __get_type_info:
1599 __dest._M_access<const type_info*>() = &typeid(_Functor);
1602 case __get_functor_ptr:
1603 __dest._M_access<_Functor*>() = _M_get_pointer(__source);
1606 case __clone_functor:
1607 _M_clone(__dest, __source, _Local_storage());
1610 case __destroy_functor:
1611 _M_destroy(__dest, _Local_storage());
1618 _M_init_functor(_Any_data& __functor, _Functor&& __f)
1619 { _M_init_functor(__functor, std::move(__f), _Local_storage()); }
1621 template<typename _Signature>
1623 _M_not_empty_function(const function<_Signature>& __f)
1624 { return static_cast<bool>(__f); }
1626 template<typename _Tp>
1628 _M_not_empty_function(const _Tp*& __fp)
1631 template<typename _Class, typename _Tp>
1633 _M_not_empty_function(_Tp _Class::* const& __mp)
1636 template<typename _Tp>
1638 _M_not_empty_function(const _Tp&)
1643 _M_init_functor(_Any_data& __functor, _Functor&& __f, true_type)
1644 { new (__functor._M_access()) _Functor(std::move(__f)); }
1647 _M_init_functor(_Any_data& __functor, _Functor&& __f, false_type)
1648 { __functor._M_access<_Functor*>() = new _Functor(std::move(__f)); }
1651 template<typename _Functor>
1652 class _Ref_manager : public _Base_manager<_Functor*>
1654 typedef _Function_base::_Base_manager<_Functor*> _Base;
1658 _M_manager(_Any_data& __dest, const _Any_data& __source,
1659 _Manager_operation __op)
1664 case __get_type_info:
1665 __dest._M_access<const type_info*>() = &typeid(_Functor);
1668 case __get_functor_ptr:
1669 __dest._M_access<_Functor*>() = *_Base::_M_get_pointer(__source);
1670 return is_const<_Functor>::value;
1674 _Base::_M_manager(__dest, __source, __op);
1680 _M_init_functor(_Any_data& __functor, reference_wrapper<_Functor> __f)
1682 // TBD: Use address_of function instead.
1683 _Base::_M_init_functor(__functor, &__f.get());
1687 _Function_base() : _M_manager(0) { }
1692 _M_manager(_M_functor, _M_functor, __destroy_functor);
1696 bool _M_empty() const { return !_M_manager; }
1698 typedef bool (*_Manager_type)(_Any_data&, const _Any_data&,
1699 _Manager_operation);
1701 _Any_data _M_functor;
1702 _Manager_type _M_manager;
1705 template<typename _Signature, typename _Functor>
1706 class _Function_handler;
1708 template<typename _Res, typename _Functor, typename... _ArgTypes>
1709 class _Function_handler<_Res(_ArgTypes...), _Functor>
1710 : public _Function_base::_Base_manager<_Functor>
1712 typedef _Function_base::_Base_manager<_Functor> _Base;
1716 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1718 return (*_Base::_M_get_pointer(__functor))(
1719 std::forward<_ArgTypes>(__args)...);
1723 template<typename _Functor, typename... _ArgTypes>
1724 class _Function_handler<void(_ArgTypes...), _Functor>
1725 : public _Function_base::_Base_manager<_Functor>
1727 typedef _Function_base::_Base_manager<_Functor> _Base;
1731 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1733 (*_Base::_M_get_pointer(__functor))(
1734 std::forward<_ArgTypes>(__args)...);
1738 template<typename _Res, typename _Functor, typename... _ArgTypes>
1739 class _Function_handler<_Res(_ArgTypes...), reference_wrapper<_Functor> >
1740 : public _Function_base::_Ref_manager<_Functor>
1742 typedef _Function_base::_Ref_manager<_Functor> _Base;
1746 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1748 return __callable_functor(**_Base::_M_get_pointer(__functor))(
1749 std::forward<_ArgTypes>(__args)...);
1753 template<typename _Functor, typename... _ArgTypes>
1754 class _Function_handler<void(_ArgTypes...), reference_wrapper<_Functor> >
1755 : public _Function_base::_Ref_manager<_Functor>
1757 typedef _Function_base::_Ref_manager<_Functor> _Base;
1761 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1763 __callable_functor(**_Base::_M_get_pointer(__functor))(
1764 std::forward<_ArgTypes>(__args)...);
1768 template<typename _Class, typename _Member, typename _Res,
1769 typename... _ArgTypes>
1770 class _Function_handler<_Res(_ArgTypes...), _Member _Class::*>
1771 : public _Function_handler<void(_ArgTypes...), _Member _Class::*>
1773 typedef _Function_handler<void(_ArgTypes...), _Member _Class::*>
1778 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1780 return mem_fn(_Base::_M_get_pointer(__functor)->__value)(
1781 std::forward<_ArgTypes>(__args)...);
1785 template<typename _Class, typename _Member, typename... _ArgTypes>
1786 class _Function_handler<void(_ArgTypes...), _Member _Class::*>
1787 : public _Function_base::_Base_manager<
1788 _Simple_type_wrapper< _Member _Class::* > >
1790 typedef _Member _Class::* _Functor;
1791 typedef _Simple_type_wrapper<_Functor> _Wrapper;
1792 typedef _Function_base::_Base_manager<_Wrapper> _Base;
1796 _M_manager(_Any_data& __dest, const _Any_data& __source,
1797 _Manager_operation __op)
1802 case __get_type_info:
1803 __dest._M_access<const type_info*>() = &typeid(_Functor);
1806 case __get_functor_ptr:
1807 __dest._M_access<_Functor*>() =
1808 &_Base::_M_get_pointer(__source)->__value;
1812 _Base::_M_manager(__dest, __source, __op);
1818 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1820 mem_fn(_Base::_M_get_pointer(__functor)->__value)(
1821 std::forward<_ArgTypes>(__args)...);
1826 template<typename _Res, typename... _ArgTypes>
1827 class function<_Res(_ArgTypes...)>
1828 : public _Maybe_unary_or_binary_function<_Res, _ArgTypes...>,
1829 private _Function_base
1831 typedef _Res _Signature_type(_ArgTypes...);
1833 struct _Useless { };
1836 typedef _Res result_type;
1838 // [3.7.2.1] construct/copy/destroy
1841 * @brief Default construct creates an empty function call wrapper.
1842 * @post @c !(bool)*this
1845 function() : _Function_base() { }
1848 * @brief Default construct creates an empty function call wrapper.
1849 * @post @c !(bool)*this
1851 function(_M_clear_type*) : _Function_base() { }
1854 * @brief %Function copy constructor.
1855 * @param x A %function object with identical call signature.
1856 * @post @c (bool)*this == (bool)x
1858 * The newly-created %function contains a copy of the target of @a
1859 * x (if it has one).
1861 function(const function& __x);
1864 * @brief %Function move constructor.
1865 * @param x A %function object rvalue with identical call signature.
1867 * The newly-created %function contains the target of @a x
1870 function(function&& __x) : _Function_base()
1875 // TODO: needs allocator_arg_t
1878 * @brief Builds a %function that targets a copy of the incoming
1880 * @param f A %function object that is callable with parameters of
1881 * type @c T1, @c T2, ..., @c TN and returns a value convertible
1884 * The newly-created %function object will target a copy of @a
1885 * f. If @a f is @c reference_wrapper<F>, then this function
1886 * object will contain a reference to the function object @c
1887 * f.get(). If @a f is a NULL function pointer or NULL
1888 * pointer-to-member, the newly-created object will be empty.
1890 * If @a f is a non-NULL function pointer or an object of type @c
1891 * reference_wrapper<F>, this function will not throw.
1893 template<typename _Functor>
1894 function(_Functor __f,
1895 typename __gnu_cxx::__enable_if<
1896 !is_integral<_Functor>::value, _Useless>::__type
1900 * @brief %Function assignment operator.
1901 * @param x A %function with identical call signature.
1902 * @post @c (bool)*this == (bool)x
1905 * The target of @a x is copied to @c *this. If @a x has no
1906 * target, then @c *this will be empty.
1908 * If @a x targets a function pointer or a reference to a function
1909 * object, then this operation will not throw an %exception.
1912 operator=(const function& __x)
1914 function(__x).swap(*this);
1919 * @brief %Function move-assignment operator.
1920 * @param x A %function rvalue with identical call signature.
1923 * The target of @a x is moved to @c *this. If @a x has no
1924 * target, then @c *this will be empty.
1926 * If @a x targets a function pointer or a reference to a function
1927 * object, then this operation will not throw an %exception.
1930 operator=(function&& __x)
1932 function(std::move(__x)).swap(*this);
1937 * @brief %Function assignment to zero.
1938 * @post @c !(bool)*this
1941 * The target of @c *this is deallocated, leaving it empty.
1944 operator=(_M_clear_type*)
1948 _M_manager(_M_functor, _M_functor, __destroy_functor);
1956 * @brief %Function assignment to a new target.
1957 * @param f A %function object that is callable with parameters of
1958 * type @c T1, @c T2, ..., @c TN and returns a value convertible
1962 * This %function object wrapper will target a copy of @a
1963 * f. If @a f is @c reference_wrapper<F>, then this function
1964 * object will contain a reference to the function object @c
1965 * f.get(). If @a f is a NULL function pointer or NULL
1966 * pointer-to-member, @c this object will be empty.
1968 * If @a f is a non-NULL function pointer or an object of type @c
1969 * reference_wrapper<F>, this function will not throw.
1971 template<typename _Functor>
1972 typename __gnu_cxx::__enable_if<!is_integral<_Functor>::value,
1974 operator=(_Functor&& __f)
1976 function(std::forward<_Functor>(__f)).swap(*this);
1981 template<typename _Functor>
1982 typename __gnu_cxx::__enable_if<!is_integral<_Functor>::value,
1984 operator=(reference_wrapper<_Functor> __f)
1986 function(__f).swap(*this);
1990 // [3.7.2.2] function modifiers
1993 * @brief Swap the targets of two %function objects.
1994 * @param f A %function with identical call signature.
1996 * Swap the targets of @c this function object and @a f. This
1997 * function will not throw an %exception.
1999 void swap(function& __x)
2001 _Any_data __old_functor = _M_functor;
2002 _M_functor = __x._M_functor;
2003 __x._M_functor = __old_functor;
2004 _Manager_type __old_manager = _M_manager;
2005 _M_manager = __x._M_manager;
2006 __x._M_manager = __old_manager;
2007 _Invoker_type __old_invoker = _M_invoker;
2008 _M_invoker = __x._M_invoker;
2009 __x._M_invoker = __old_invoker;
2012 // TODO: needs allocator_arg_t
2014 template<typename _Functor, typename _Alloc>
2016 assign(_Functor&& __f, const _Alloc& __a)
2018 function(allocator_arg, __a,
2019 std::forward<_Functor>(__f)).swap(*this);
2023 // [3.7.2.3] function capacity
2026 * @brief Determine if the %function wrapper has a target.
2028 * @return @c true when this %function object contains a target,
2029 * or @c false when it is empty.
2031 * This function will not throw an %exception.
2033 explicit operator bool() const
2034 { return !_M_empty(); }
2036 // [3.7.2.4] function invocation
2039 * @brief Invokes the function targeted by @c *this.
2040 * @returns the result of the target.
2041 * @throws bad_function_call when @c !(bool)*this
2043 * The function call operator invokes the target function object
2044 * stored by @c this.
2046 _Res operator()(_ArgTypes... __args) const;
2049 // [3.7.2.5] function target access
2051 * @brief Determine the type of the target of this function object
2054 * @returns the type identifier of the target function object, or
2055 * @c typeid(void) if @c !(bool)*this.
2057 * This function will not throw an %exception.
2059 const type_info& target_type() const;
2062 * @brief Access the stored target function object.
2064 * @return Returns a pointer to the stored target function object,
2065 * if @c typeid(Functor).equals(target_type()); otherwise, a NULL
2068 * This function will not throw an %exception.
2070 template<typename _Functor> _Functor* target();
2073 template<typename _Functor> const _Functor* target() const;
2076 // deleted overloads
2077 template<typename _Res2, typename... _ArgTypes2>
2078 void operator==(const function<_Res2(_ArgTypes2...)>&) const = delete;
2079 template<typename _Res2, typename... _ArgTypes2>
2080 void operator!=(const function<_Res2(_ArgTypes2...)>&) const = delete;
2083 typedef _Res (*_Invoker_type)(const _Any_data&, _ArgTypes...);
2084 _Invoker_type _M_invoker;
2087 template<typename _Res, typename... _ArgTypes>
2088 function<_Res(_ArgTypes...)>::
2089 function(const function& __x)
2092 if (static_cast<bool>(__x))
2094 _M_invoker = __x._M_invoker;
2095 _M_manager = __x._M_manager;
2096 __x._M_manager(_M_functor, __x._M_functor, __clone_functor);
2100 template<typename _Res, typename... _ArgTypes>
2101 template<typename _Functor>
2102 function<_Res(_ArgTypes...)>::
2103 function(_Functor __f,
2104 typename __gnu_cxx::__enable_if<
2105 !is_integral<_Functor>::value, _Useless>::__type)
2108 typedef _Function_handler<_Signature_type, _Functor> _My_handler;
2110 if (_My_handler::_M_not_empty_function(__f))
2112 _M_invoker = &_My_handler::_M_invoke;
2113 _M_manager = &_My_handler::_M_manager;
2114 _My_handler::_M_init_functor(_M_functor, std::move(__f));
2118 template<typename _Res, typename... _ArgTypes>
2120 function<_Res(_ArgTypes...)>::
2121 operator()(_ArgTypes... __args) const
2124 __throw_bad_function_call();
2125 return _M_invoker(_M_functor, std::forward<_ArgTypes>(__args)...);
2129 template<typename _Res, typename... _ArgTypes>
2131 function<_Res(_ArgTypes...)>::
2136 _Any_data __typeinfo_result;
2137 _M_manager(__typeinfo_result, _M_functor, __get_type_info);
2138 return *__typeinfo_result._M_access<const type_info*>();
2141 return typeid(void);
2144 template<typename _Res, typename... _ArgTypes>
2145 template<typename _Functor>
2147 function<_Res(_ArgTypes...)>::
2150 if (typeid(_Functor) == target_type() && _M_manager)
2153 if (_M_manager(__ptr, _M_functor, __get_functor_ptr)
2154 && !is_const<_Functor>::value)
2157 return __ptr._M_access<_Functor*>();
2163 template<typename _Res, typename... _ArgTypes>
2164 template<typename _Functor>
2166 function<_Res(_ArgTypes...)>::
2169 if (typeid(_Functor) == target_type() && _M_manager)
2172 _M_manager(__ptr, _M_functor, __get_functor_ptr);
2173 return __ptr._M_access<const _Functor*>();
2180 // [20.7.15.2.6] null pointer comparisons
2183 * @brief Compares a polymorphic function object wrapper against 0
2184 * (the NULL pointer).
2185 * @returns @c true if the wrapper has no target, @c false otherwise
2187 * This function will not throw an %exception.
2189 template<typename _Res, typename... _Args>
2191 operator==(const function<_Res(_Args...)>& __f, _M_clear_type*)
2192 { return !static_cast<bool>(__f); }
2195 template<typename _Res, typename... _Args>
2197 operator==(_M_clear_type*, const function<_Res(_Args...)>& __f)
2198 { return !static_cast<bool>(__f); }
2201 * @brief Compares a polymorphic function object wrapper against 0
2202 * (the NULL pointer).
2203 * @returns @c false if the wrapper has no target, @c true otherwise
2205 * This function will not throw an %exception.
2207 template<typename _Res, typename... _Args>
2209 operator!=(const function<_Res(_Args...)>& __f, _M_clear_type*)
2210 { return static_cast<bool>(__f); }
2213 template<typename _Res, typename... _Args>
2215 operator!=(_M_clear_type*, const function<_Res(_Args...)>& __f)
2216 { return static_cast<bool>(__f); }
2218 // [20.7.15.2.7] specialized algorithms
2221 * @brief Swap the targets of two polymorphic function object wrappers.
2223 * This function will not throw an %exception.
2225 template<typename _Res, typename... _Args>
2227 swap(function<_Res(_Args...)>& __x, function<_Res(_Args...)>& __y)
2231 #endif // __GXX_EXPERIMENTAL_CXX0X__
2233 #endif // _GLIBCXX_FUNCTIONAL