1 // TR1 functional header -*- C++ -*-
3 // Copyright (C) 2007, 2008 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
30 /** @file tr1_impl/functional
31 * This is an internal header file, included by other library headers.
32 * You should not attempt to use it directly.
37 _GLIBCXX_BEGIN_NAMESPACE_TR1
39 template<typename _MemberPointer>
43 * Actual implementation of _Has_result_type, which uses SFINAE to
44 * determine if the type _Tp has a publicly-accessible member type
47 template<typename _Tp>
48 class _Has_result_type_helper : __sfinae_types
50 template<typename _Up>
54 template<typename _Up>
55 static __one __test(_Wrap_type<typename _Up::result_type>*);
57 template<typename _Up>
58 static __two __test(...);
61 static const bool value = sizeof(__test<_Tp>(0)) == 1;
64 template<typename _Tp>
65 struct _Has_result_type
66 : integral_constant<bool,
67 _Has_result_type_helper<typename remove_cv<_Tp>::type>::value>
73 /// If we have found a result_type, extract it.
74 template<bool _Has_result_type, typename _Functor>
75 struct _Maybe_get_result_type
78 template<typename _Functor>
79 struct _Maybe_get_result_type<true, _Functor>
81 typedef typename _Functor::result_type result_type;
85 * Base class for any function object that has a weak result type, as
86 * defined in 3.3/3 of TR1.
88 template<typename _Functor>
89 struct _Weak_result_type_impl
90 : _Maybe_get_result_type<_Has_result_type<_Functor>::value, _Functor>
94 /// Retrieve the result type for a function type.
95 template<typename _Res, typename... _ArgTypes>
96 struct _Weak_result_type_impl<_Res(_ArgTypes...)>
98 typedef _Res result_type;
101 /// Retrieve the result type for a function reference.
102 template<typename _Res, typename... _ArgTypes>
103 struct _Weak_result_type_impl<_Res(&)(_ArgTypes...)>
105 typedef _Res result_type;
108 /// Retrieve the result type for a function pointer.
109 template<typename _Res, typename... _ArgTypes>
110 struct _Weak_result_type_impl<_Res(*)(_ArgTypes...)>
112 typedef _Res result_type;
115 /// Retrieve result type for a member function pointer.
116 template<typename _Res, typename _Class, typename... _ArgTypes>
117 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...)>
119 typedef _Res result_type;
122 /// Retrieve result type for a const member function pointer.
123 template<typename _Res, typename _Class, typename... _ArgTypes>
124 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) const>
126 typedef _Res result_type;
129 /// Retrieve result type for a volatile member function pointer.
130 template<typename _Res, typename _Class, typename... _ArgTypes>
131 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) volatile>
133 typedef _Res result_type;
136 /// Retrieve result type for a const volatile member function pointer.
137 template<typename _Res, typename _Class, typename... _ArgTypes>
138 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...)const volatile>
140 typedef _Res result_type;
144 * Strip top-level cv-qualifiers from the function object and let
145 * _Weak_result_type_impl perform the real work.
147 template<typename _Functor>
148 struct _Weak_result_type
149 : _Weak_result_type_impl<typename remove_cv<_Functor>::type>
153 template<typename _Signature>
156 #ifdef _GLIBCXX_INCLUDE_AS_TR1
158 * Actual implementation of result_of. When _Has_result_type is
159 * true, gets its result from _Weak_result_type. Otherwise, uses
160 * the function object's member template result to extract the
163 template<bool _Has_result_type, typename _Signature>
164 struct _Result_of_impl;
166 // Handle member data pointers using _Mem_fn's logic
167 template<typename _Res, typename _Class, typename _T1>
168 struct _Result_of_impl<false, _Res _Class::*(_T1)>
170 typedef typename _Mem_fn<_Res _Class::*>
171 ::template _Result_type<_T1>::type type;
175 * Determine whether we can determine a result type from @c Functor
178 template<typename _Functor, typename... _ArgTypes>
179 class result_of<_Functor(_ArgTypes...)>
180 : public _Result_of_impl<
181 _Has_result_type<_Weak_result_type<_Functor> >::value,
182 _Functor(_ArgTypes...)>
186 /// We already know the result type for @c Functor; use it.
187 template<typename _Functor, typename... _ArgTypes>
188 struct _Result_of_impl<true, _Functor(_ArgTypes...)>
190 typedef typename _Weak_result_type<_Functor>::result_type type;
194 * We need to compute the result type for this invocation the hard
197 template<typename _Functor, typename... _ArgTypes>
198 struct _Result_of_impl<false, _Functor(_ArgTypes...)>
200 typedef typename _Functor
201 ::template result<_Functor(_ArgTypes...)>::type type;
205 * It is unsafe to access ::result when there are zero arguments, so we
206 * return @c void instead.
208 template<typename _Functor>
209 struct _Result_of_impl<false, _Functor()>
216 * Actual implementation of std::result_of.
218 template<bool _Is_mem_obj_ptr, bool _Is_mem_fun_ptr, typename _Signature>
219 struct _Result_of_impl;
221 // Helper functions used by _Result_of_impl.
222 template<typename _ArgT>
223 struct _Result_of_arg
225 // http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-active.html#904
227 static typename conditional<is_reference<_ArgT>::value,
228 typename add_lvalue_reference<_ArgT>::type,
229 typename add_rvalue_reference<_ArgT>::type>::type
232 static typename add_rvalue_reference<_ArgT>::type
237 template<typename _Tp, bool _NoDeref>
238 struct _Result_of_lhs
242 template<typename _Tp>
243 struct _Result_of_lhs<_Tp, false>
245 static decltype( *_Result_of_lhs<_Tp,true>::_S_fwd() ) _S_fwd();
248 // Handle member data pointers.
249 template<typename _Res, typename _Class, typename _T1>
250 class _Result_of_impl<true, false, _Res _Class::* (_T1)>
252 typedef typename remove_reference<_T1>::type _T2;
253 typedef _Result_of_lhs<_T1, is_base_of<_Class, _T2>::value> _Fwd_lhs;
254 typedef _Res _Class::*_MemPtr;
257 typedef decltype( (_Fwd_lhs::_S_fwd().*(_MemPtr())) ) type;
260 // Handle member function pointers.
261 template<typename _MemFun, typename _T1, typename... _ArgTypes>
262 class _Result_of_impl<false, true, _MemFun (_T1, _ArgTypes...)>
267 template<typename _Res, typename _Class, typename... _Args>
268 struct _Get_class<_Res (_Class::*)(_Args...)>
269 { typedef _Class type; };
270 template<typename _Res, typename _Class, typename... _Args>
271 struct _Get_class<_Res (_Class::*)(_Args...) const>
272 { typedef _Class type; };
273 template<typename _Res, typename _Class, typename... _Args>
274 struct _Get_class<_Res (_Class::*)(_Args...) volatile>
275 { typedef _Class type; };
276 template<typename _Res, typename _Class, typename... _Args>
277 struct _Get_class<_Res (_Class::*)(_Args...) const volatile>
278 { typedef _Class type; };
280 typedef typename _Get_class<_MemFun>::type _Class;
281 typedef typename remove_reference<_T1>::type _T2;
282 typedef _Result_of_lhs<_T1, is_base_of<_Class, _T2>::value> _Fwd_lhs;
285 typedef decltype( (_Fwd_lhs::_S_fwd().*(_MemFun())) (
286 _Result_of_arg<_ArgTypes>::_S_fwd() ... ) ) type;
289 // Handle other callable types.
290 template<typename _Functor, typename... _ArgTypes>
291 class _Result_of_impl<false, false, _Functor(_ArgTypes...)>
293 // get an example of the callable type
294 static typename add_rvalue_reference<_Functor>::type
298 typedef decltype( _Result_of_impl::_S_fwd_functor() (
299 _Result_of_arg<_ArgTypes>::_S_fwd() ... ) ) type;
302 template<typename _Functor, typename... _ArgTypes>
303 class result_of<_Functor(_ArgTypes...)>
304 : public _Result_of_impl<
305 is_member_object_pointer<_Functor>::value,
306 is_member_function_pointer<_Functor>::value,
307 _Functor(_ArgTypes...)>
312 /// Determines if the type _Tp derives from unary_function.
313 template<typename _Tp>
314 struct _Derives_from_unary_function : __sfinae_types
317 template<typename _T1, typename _Res>
318 static __one __test(const volatile unary_function<_T1, _Res>*);
320 // It's tempting to change "..." to const volatile void*, but
321 // that fails when _Tp is a function type.
322 static __two __test(...);
325 static const bool value = sizeof(__test((_Tp*)0)) == 1;
328 /// Determines if the type _Tp derives from binary_function.
329 template<typename _Tp>
330 struct _Derives_from_binary_function : __sfinae_types
333 template<typename _T1, typename _T2, typename _Res>
334 static __one __test(const volatile binary_function<_T1, _T2, _Res>*);
336 // It's tempting to change "..." to const volatile void*, but
337 // that fails when _Tp is a function type.
338 static __two __test(...);
341 static const bool value = sizeof(__test((_Tp*)0)) == 1;
344 /// Turns a function type into a function pointer type
345 template<typename _Tp, bool _IsFunctionType = is_function<_Tp>::value>
346 struct _Function_to_function_pointer
351 template<typename _Tp>
352 struct _Function_to_function_pointer<_Tp, true>
357 #ifdef _GLIBCXX_INCLUDE_AS_TR1
359 * Invoke a function object, which may be either a member pointer or a
360 * function object. The first parameter will tell which.
362 template<typename _Functor, typename... _Args>
364 typename __gnu_cxx::__enable_if<
365 (!is_member_pointer<_Functor>::value
366 && !is_function<_Functor>::value
367 && !is_function<typename remove_pointer<_Functor>::type>::value),
368 typename result_of<_Functor(_Args...)>::type
370 __invoke(_Functor& __f, _Args&... __args)
372 return __f(__args...);
375 template<typename _Functor, typename... _Args>
377 typename __gnu_cxx::__enable_if<
378 (is_member_pointer<_Functor>::value
379 && !is_function<_Functor>::value
380 && !is_function<typename remove_pointer<_Functor>::type>::value),
381 typename result_of<_Functor(_Args...)>::type
383 __invoke(_Functor& __f, _Args&... __args)
385 return mem_fn(__f)(__args...);
388 // To pick up function references (that will become function pointers)
389 template<typename _Functor, typename... _Args>
391 typename __gnu_cxx::__enable_if<
392 (is_pointer<_Functor>::value
393 && is_function<typename remove_pointer<_Functor>::type>::value),
394 typename result_of<_Functor(_Args...)>::type
396 __invoke(_Functor __f, _Args&... __args)
398 return __f(__args...);
402 * Invoke a function object, which may be either a member pointer or a
403 * function object. The first parameter will tell which.
405 template<typename _Functor, typename... _Args>
408 (!is_member_pointer<_Functor>::value
409 && !is_function<_Functor>::value
410 && !is_function<typename remove_pointer<_Functor>::type>::value),
411 typename result_of<_Functor(_Args...)>::type
413 __invoke(_Functor& __f, _Args&&... __args)
415 return __f(std::forward<_Args>(__args)...);
418 template<typename _Functor, typename... _Args>
421 (is_member_pointer<_Functor>::value
422 && !is_function<_Functor>::value
423 && !is_function<typename remove_pointer<_Functor>::type>::value),
424 typename result_of<_Functor(_Args...)>::type
426 __invoke(_Functor& __f, _Args&&... __args)
428 return mem_fn(__f)(std::forward<_Args>(__args)...);
431 // To pick up function references (that will become function pointers)
432 template<typename _Functor, typename... _Args>
435 (is_pointer<_Functor>::value
436 && is_function<typename remove_pointer<_Functor>::type>::value),
437 typename result_of<_Functor(_Args...)>::type
439 __invoke(_Functor __f, _Args&&... __args)
441 return __f(std::forward<_Args>(__args)...);
446 * Knowing which of unary_function and binary_function _Tp derives
447 * from, derives from the same and ensures that reference_wrapper
448 * will have a weak result type. See cases below.
450 template<bool _Unary, bool _Binary, typename _Tp>
451 struct _Reference_wrapper_base_impl;
453 // Not a unary_function or binary_function, so try a weak result type.
454 template<typename _Tp>
455 struct _Reference_wrapper_base_impl<false, false, _Tp>
456 : _Weak_result_type<_Tp>
459 // unary_function but not binary_function
460 template<typename _Tp>
461 struct _Reference_wrapper_base_impl<true, false, _Tp>
462 : unary_function<typename _Tp::argument_type,
463 typename _Tp::result_type>
466 // binary_function but not unary_function
467 template<typename _Tp>
468 struct _Reference_wrapper_base_impl<false, true, _Tp>
469 : binary_function<typename _Tp::first_argument_type,
470 typename _Tp::second_argument_type,
471 typename _Tp::result_type>
474 // Both unary_function and binary_function. Import result_type to
476 template<typename _Tp>
477 struct _Reference_wrapper_base_impl<true, true, _Tp>
478 : unary_function<typename _Tp::argument_type,
479 typename _Tp::result_type>,
480 binary_function<typename _Tp::first_argument_type,
481 typename _Tp::second_argument_type,
482 typename _Tp::result_type>
484 typedef typename _Tp::result_type result_type;
488 * Derives from unary_function or binary_function when it
489 * can. Specializations handle all of the easy cases. The primary
490 * template determines what to do with a class type, which may
491 * derive from both unary_function and binary_function.
493 template<typename _Tp>
494 struct _Reference_wrapper_base
495 : _Reference_wrapper_base_impl<
496 _Derives_from_unary_function<_Tp>::value,
497 _Derives_from_binary_function<_Tp>::value,
501 // - a function type (unary)
502 template<typename _Res, typename _T1>
503 struct _Reference_wrapper_base<_Res(_T1)>
504 : unary_function<_T1, _Res>
507 // - a function type (binary)
508 template<typename _Res, typename _T1, typename _T2>
509 struct _Reference_wrapper_base<_Res(_T1, _T2)>
510 : binary_function<_T1, _T2, _Res>
513 // - a function pointer type (unary)
514 template<typename _Res, typename _T1>
515 struct _Reference_wrapper_base<_Res(*)(_T1)>
516 : unary_function<_T1, _Res>
519 // - a function pointer type (binary)
520 template<typename _Res, typename _T1, typename _T2>
521 struct _Reference_wrapper_base<_Res(*)(_T1, _T2)>
522 : binary_function<_T1, _T2, _Res>
525 // - a pointer to member function type (unary, no qualifiers)
526 template<typename _Res, typename _T1>
527 struct _Reference_wrapper_base<_Res (_T1::*)()>
528 : unary_function<_T1*, _Res>
531 // - a pointer to member function type (binary, no qualifiers)
532 template<typename _Res, typename _T1, typename _T2>
533 struct _Reference_wrapper_base<_Res (_T1::*)(_T2)>
534 : binary_function<_T1*, _T2, _Res>
537 // - a pointer to member function type (unary, const)
538 template<typename _Res, typename _T1>
539 struct _Reference_wrapper_base<_Res (_T1::*)() const>
540 : unary_function<const _T1*, _Res>
543 // - a pointer to member function type (binary, const)
544 template<typename _Res, typename _T1, typename _T2>
545 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const>
546 : binary_function<const _T1*, _T2, _Res>
549 // - a pointer to member function type (unary, volatile)
550 template<typename _Res, typename _T1>
551 struct _Reference_wrapper_base<_Res (_T1::*)() volatile>
552 : unary_function<volatile _T1*, _Res>
555 // - a pointer to member function type (binary, volatile)
556 template<typename _Res, typename _T1, typename _T2>
557 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) volatile>
558 : binary_function<volatile _T1*, _T2, _Res>
561 // - a pointer to member function type (unary, const volatile)
562 template<typename _Res, typename _T1>
563 struct _Reference_wrapper_base<_Res (_T1::*)() const volatile>
564 : unary_function<const volatile _T1*, _Res>
567 // - a pointer to member function type (binary, const volatile)
568 template<typename _Res, typename _T1, typename _T2>
569 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const volatile>
570 : binary_function<const volatile _T1*, _T2, _Res>
573 /// reference_wrapper
574 template<typename _Tp>
575 class reference_wrapper
576 : public _Reference_wrapper_base<typename remove_cv<_Tp>::type>
578 // If _Tp is a function type, we can't form result_of<_Tp(...)>,
579 // so turn it into a function pointer type.
580 typedef typename _Function_to_function_pointer<_Tp>::type
587 #ifdef _GLIBCXX_INCLUDE_AS_TR1
589 reference_wrapper(_Tp& __indata): _M_data(&__indata)
592 reference_wrapper(_Tp& __indata): _M_data(&__indata)
596 reference_wrapper(_Tp&&) = delete;
599 reference_wrapper(const reference_wrapper<_Tp>& __inref):
600 _M_data(__inref._M_data)
604 operator=(const reference_wrapper<_Tp>& __inref)
606 _M_data = __inref._M_data;
610 operator _Tp&() const
611 { return this->get(); }
617 #ifdef _GLIBCXX_INCLUDE_AS_TR1
618 template<typename... _Args>
619 typename result_of<_M_func_type(_Args...)>::type
620 operator()(_Args&... __args) const
622 return __invoke(get(), __args...);
625 template<typename... _Args>
626 typename result_of<_M_func_type(_Args...)>::type
627 operator()(_Args&&... __args) const
629 return __invoke(get(), std::forward<_Args>(__args)...);
635 // Denotes a reference should be taken to a variable.
636 template<typename _Tp>
637 inline reference_wrapper<_Tp>
639 { return reference_wrapper<_Tp>(__t); }
641 // Denotes a const reference should be taken to a variable.
642 template<typename _Tp>
643 inline reference_wrapper<const _Tp>
645 { return reference_wrapper<const _Tp>(__t); }
647 template<typename _Tp>
648 inline reference_wrapper<_Tp>
649 ref(reference_wrapper<_Tp> __t)
650 { return ref(__t.get()); }
652 template<typename _Tp>
653 inline reference_wrapper<const _Tp>
654 cref(reference_wrapper<_Tp> __t)
655 { return cref(__t.get()); }
657 template<typename _Tp, bool>
658 struct _Mem_fn_const_or_non
660 typedef const _Tp& type;
663 template<typename _Tp>
664 struct _Mem_fn_const_or_non<_Tp, false>
670 * Derives from @c unary_function or @c binary_function, or perhaps
671 * nothing, depending on the number of arguments provided. The
672 * primary template is the basis case, which derives nothing.
674 template<typename _Res, typename... _ArgTypes>
675 struct _Maybe_unary_or_binary_function { };
677 /// Derives from @c unary_function, as appropriate.
678 template<typename _Res, typename _T1>
679 struct _Maybe_unary_or_binary_function<_Res, _T1>
680 : std::unary_function<_T1, _Res> { };
682 /// Derives from @c binary_function, as appropriate.
683 template<typename _Res, typename _T1, typename _T2>
684 struct _Maybe_unary_or_binary_function<_Res, _T1, _T2>
685 : std::binary_function<_T1, _T2, _Res> { };
687 /// Implementation of @c mem_fn for member function pointers.
688 template<typename _Res, typename _Class, typename... _ArgTypes>
689 class _Mem_fn<_Res (_Class::*)(_ArgTypes...)>
690 : public _Maybe_unary_or_binary_function<_Res, _Class*, _ArgTypes...>
692 typedef _Res (_Class::*_Functor)(_ArgTypes...);
694 template<typename _Tp>
696 _M_call(_Tp& __object, const volatile _Class *,
697 _ArgTypes... __args) const
698 { return (__object.*__pmf)(__args...); }
700 template<typename _Tp>
702 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
703 { return ((*__ptr).*__pmf)(__args...); }
706 typedef _Res result_type;
708 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
712 operator()(_Class& __object, _ArgTypes... __args) const
713 { return (__object.*__pmf)(__args...); }
717 operator()(_Class* __object, _ArgTypes... __args) const
718 { return (__object->*__pmf)(__args...); }
720 // Handle smart pointers, references and pointers to derived
721 template<typename _Tp>
723 operator()(_Tp& __object, _ArgTypes... __args) const
724 { return _M_call(__object, &__object, __args...); }
730 /// Implementation of @c mem_fn for const member function pointers.
731 template<typename _Res, typename _Class, typename... _ArgTypes>
732 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) const>
733 : public _Maybe_unary_or_binary_function<_Res, const _Class*,
736 typedef _Res (_Class::*_Functor)(_ArgTypes...) const;
738 template<typename _Tp>
740 _M_call(_Tp& __object, const volatile _Class *,
741 _ArgTypes... __args) const
742 { return (__object.*__pmf)(__args...); }
744 template<typename _Tp>
746 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
747 { return ((*__ptr).*__pmf)(__args...); }
750 typedef _Res result_type;
752 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
756 operator()(const _Class& __object, _ArgTypes... __args) const
757 { return (__object.*__pmf)(__args...); }
761 operator()(const _Class* __object, _ArgTypes... __args) const
762 { return (__object->*__pmf)(__args...); }
764 // Handle smart pointers, references and pointers to derived
765 template<typename _Tp>
766 _Res operator()(_Tp& __object, _ArgTypes... __args) const
767 { return _M_call(__object, &__object, __args...); }
773 /// Implementation of @c mem_fn for volatile member function pointers.
774 template<typename _Res, typename _Class, typename... _ArgTypes>
775 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) volatile>
776 : public _Maybe_unary_or_binary_function<_Res, volatile _Class*,
779 typedef _Res (_Class::*_Functor)(_ArgTypes...) volatile;
781 template<typename _Tp>
783 _M_call(_Tp& __object, const volatile _Class *,
784 _ArgTypes... __args) const
785 { return (__object.*__pmf)(__args...); }
787 template<typename _Tp>
789 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
790 { return ((*__ptr).*__pmf)(__args...); }
793 typedef _Res result_type;
795 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
799 operator()(volatile _Class& __object, _ArgTypes... __args) const
800 { return (__object.*__pmf)(__args...); }
804 operator()(volatile _Class* __object, _ArgTypes... __args) const
805 { return (__object->*__pmf)(__args...); }
807 // Handle smart pointers, references and pointers to derived
808 template<typename _Tp>
810 operator()(_Tp& __object, _ArgTypes... __args) const
811 { return _M_call(__object, &__object, __args...); }
817 /// Implementation of @c mem_fn for const volatile member function pointers.
818 template<typename _Res, typename _Class, typename... _ArgTypes>
819 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) const volatile>
820 : public _Maybe_unary_or_binary_function<_Res, const volatile _Class*,
823 typedef _Res (_Class::*_Functor)(_ArgTypes...) const volatile;
825 template<typename _Tp>
827 _M_call(_Tp& __object, const volatile _Class *,
828 _ArgTypes... __args) const
829 { return (__object.*__pmf)(__args...); }
831 template<typename _Tp>
833 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
834 { return ((*__ptr).*__pmf)(__args...); }
837 typedef _Res result_type;
839 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
843 operator()(const volatile _Class& __object, _ArgTypes... __args) const
844 { return (__object.*__pmf)(__args...); }
848 operator()(const volatile _Class* __object, _ArgTypes... __args) const
849 { return (__object->*__pmf)(__args...); }
851 // Handle smart pointers, references and pointers to derived
852 template<typename _Tp>
853 _Res operator()(_Tp& __object, _ArgTypes... __args) const
854 { return _M_call(__object, &__object, __args...); }
861 template<typename _Res, typename _Class>
862 class _Mem_fn<_Res _Class::*>
864 // This bit of genius is due to Peter Dimov, improved slightly by
866 template<typename _Tp>
868 _M_call(_Tp& __object, _Class *) const
869 { return __object.*__pm; }
871 template<typename _Tp, typename _Up>
873 _M_call(_Tp& __object, _Up * const *) const
874 { return (*__object).*__pm; }
876 template<typename _Tp, typename _Up>
878 _M_call(_Tp& __object, const _Up * const *) const
879 { return (*__object).*__pm; }
881 template<typename _Tp>
883 _M_call(_Tp& __object, const _Class *) const
884 { return __object.*__pm; }
886 template<typename _Tp>
888 _M_call(_Tp& __ptr, const volatile void*) const
889 { return (*__ptr).*__pm; }
891 template<typename _Tp> static _Tp& __get_ref();
893 template<typename _Tp>
894 static __sfinae_types::__one __check_const(_Tp&, _Class*);
895 template<typename _Tp, typename _Up>
896 static __sfinae_types::__one __check_const(_Tp&, _Up * const *);
897 template<typename _Tp, typename _Up>
898 static __sfinae_types::__two __check_const(_Tp&, const _Up * const *);
899 template<typename _Tp>
900 static __sfinae_types::__two __check_const(_Tp&, const _Class*);
901 template<typename _Tp>
902 static __sfinae_types::__two __check_const(_Tp&, const volatile void*);
905 template<typename _Tp>
907 : _Mem_fn_const_or_non<_Res,
908 (sizeof(__sfinae_types::__two)
909 == sizeof(__check_const<_Tp>(__get_ref<_Tp>(), (_Tp*)0)))>
912 template<typename _Signature>
915 template<typename _CVMem, typename _Tp>
916 struct result<_CVMem(_Tp)>
917 : public _Result_type<_Tp> { };
919 template<typename _CVMem, typename _Tp>
920 struct result<_CVMem(_Tp&)>
921 : public _Result_type<_Tp> { };
924 _Mem_fn(_Res _Class::*__pm) : __pm(__pm) { }
928 operator()(_Class& __object) const
929 { return __object.*__pm; }
932 operator()(const _Class& __object) const
933 { return __object.*__pm; }
937 operator()(_Class* __object) const
938 { return __object->*__pm; }
941 operator()(const _Class* __object) const
942 { return __object->*__pm; }
944 // Handle smart pointers and derived
945 template<typename _Tp>
946 typename _Result_type<_Tp>::type
947 operator()(_Tp& __unknown) const
948 { return _M_call(__unknown, &__unknown); }
955 * @brief Returns a function object that forwards to the member
958 template<typename _Tp, typename _Class>
959 inline _Mem_fn<_Tp _Class::*>
960 mem_fn(_Tp _Class::* __pm)
962 return _Mem_fn<_Tp _Class::*>(__pm);
966 * @brief Determines if the given type _Tp is a function object
967 * should be treated as a subexpression when evaluating calls to
968 * function objects returned by bind(). [TR1 3.6.1]
970 template<typename _Tp>
971 struct is_bind_expression
972 { static const bool value = false; };
974 template<typename _Tp>
975 const bool is_bind_expression<_Tp>::value;
978 * @brief Determines if the given type _Tp is a placeholder in a
979 * bind() expression and, if so, which placeholder it is. [TR1 3.6.2]
981 template<typename _Tp>
982 struct is_placeholder
983 { static const int value = 0; };
985 template<typename _Tp>
986 const int is_placeholder<_Tp>::value;
988 /// The type of placeholder objects defined by libstdc++.
989 template<int _Num> struct _Placeholder { };
991 // Define a large number of placeholders. There is no way to
992 // simplify this with variadic templates, because we're introducing
993 // unique names for each.
994 namespace placeholders
1007 _Placeholder<10> _10;
1008 _Placeholder<11> _11;
1009 _Placeholder<12> _12;
1010 _Placeholder<13> _13;
1011 _Placeholder<14> _14;
1012 _Placeholder<15> _15;
1013 _Placeholder<16> _16;
1014 _Placeholder<17> _17;
1015 _Placeholder<18> _18;
1016 _Placeholder<19> _19;
1017 _Placeholder<20> _20;
1018 _Placeholder<21> _21;
1019 _Placeholder<22> _22;
1020 _Placeholder<23> _23;
1021 _Placeholder<24> _24;
1022 _Placeholder<25> _25;
1023 _Placeholder<26> _26;
1024 _Placeholder<27> _27;
1025 _Placeholder<28> _28;
1026 _Placeholder<29> _29;
1031 * Partial specialization of is_placeholder that provides the placeholder
1032 * number for the placeholder objects defined by libstdc++.
1035 struct is_placeholder<_Placeholder<_Num> >
1036 { static const int value = _Num; };
1039 const int is_placeholder<_Placeholder<_Num> >::value;
1042 * Stores a tuple of indices. Used by bind() to extract the elements
1045 template<int... _Indexes>
1046 struct _Index_tuple { };
1048 /// Builds an _Index_tuple<0, 1, 2, ..., _Num-1>.
1049 template<std::size_t _Num, typename _Tuple = _Index_tuple<> >
1050 struct _Build_index_tuple;
1052 template<std::size_t _Num, int... _Indexes>
1053 struct _Build_index_tuple<_Num, _Index_tuple<_Indexes...> >
1054 : _Build_index_tuple<_Num - 1,
1055 _Index_tuple<_Indexes..., sizeof...(_Indexes)> >
1059 template<int... _Indexes>
1060 struct _Build_index_tuple<0, _Index_tuple<_Indexes...> >
1062 typedef _Index_tuple<_Indexes...> __type;
1066 * Used by _Safe_tuple_element to indicate that there is no tuple
1067 * element at this position.
1069 struct _No_tuple_element;
1072 * Implementation helper for _Safe_tuple_element. This primary
1073 * template handles the case where it is safe to use @c
1076 template<int __i, typename _Tuple, bool _IsSafe>
1077 struct _Safe_tuple_element_impl
1078 : tuple_element<__i, _Tuple> { };
1081 * Implementation helper for _Safe_tuple_element. This partial
1082 * specialization handles the case where it is not safe to use @c
1083 * tuple_element. We just return @c _No_tuple_element.
1085 template<int __i, typename _Tuple>
1086 struct _Safe_tuple_element_impl<__i, _Tuple, false>
1088 typedef _No_tuple_element type;
1092 * Like tuple_element, but returns @c _No_tuple_element when
1093 * tuple_element would return an error.
1095 template<int __i, typename _Tuple>
1096 struct _Safe_tuple_element
1097 : _Safe_tuple_element_impl<__i, _Tuple,
1098 (__i >= 0 && __i < tuple_size<_Tuple>::value)>
1103 * Maps an argument to bind() into an actual argument to the bound
1104 * function object [TR1 3.6.3/5]. Only the first parameter should
1105 * be specified: the rest are used to determine among the various
1106 * implementations. Note that, although this class is a function
1107 * object, isn't not entirely normal because it takes only two
1108 * parameters regardless of the number of parameters passed to the
1109 * bind expression. The first parameter is the bound argument and
1110 * the second parameter is a tuple containing references to the
1111 * rest of the arguments.
1113 template<typename _Arg,
1114 bool _IsBindExp = is_bind_expression<_Arg>::value,
1115 bool _IsPlaceholder = (is_placeholder<_Arg>::value > 0)>
1119 * If the argument is reference_wrapper<_Tp>, returns the
1120 * underlying reference. [TR1 3.6.3/5 bullet 1]
1122 template<typename _Tp>
1123 class _Mu<reference_wrapper<_Tp>, false, false>
1126 typedef _Tp& result_type;
1128 /* Note: This won't actually work for const volatile
1129 * reference_wrappers, because reference_wrapper::get() is const
1130 * but not volatile-qualified. This might be a defect in the TR.
1132 template<typename _CVRef, typename _Tuple>
1134 operator()(_CVRef& __arg, const _Tuple&) const volatile
1135 { return __arg.get(); }
1139 * If the argument is a bind expression, we invoke the underlying
1140 * function object with the same cv-qualifiers as we are given and
1141 * pass along all of our arguments (unwrapped). [TR1 3.6.3/5 bullet 2]
1143 template<typename _Arg>
1144 class _Mu<_Arg, true, false>
1147 template<typename _Signature> class result;
1149 // Determine the result type when we pass the arguments along. This
1150 // involves passing along the cv-qualifiers placed on _Mu and
1151 // unwrapping the argument bundle.
1152 template<typename _CVMu, typename _CVArg, typename... _Args>
1153 class result<_CVMu(_CVArg, tuple<_Args...>)>
1154 : public result_of<_CVArg(_Args...)> { };
1156 template<typename _CVArg, typename... _Args>
1157 typename result_of<_CVArg(_Args...)>::type
1158 operator()(_CVArg& __arg,
1159 const tuple<_Args...>& __tuple) const volatile
1161 // Construct an index tuple and forward to __call
1162 typedef typename _Build_index_tuple<sizeof...(_Args)>::__type
1164 return this->__call(__arg, __tuple, _Indexes());
1168 // Invokes the underlying function object __arg by unpacking all
1169 // of the arguments in the tuple.
1170 template<typename _CVArg, typename... _Args, int... _Indexes>
1171 typename result_of<_CVArg(_Args...)>::type
1172 __call(_CVArg& __arg, const tuple<_Args...>& __tuple,
1173 const _Index_tuple<_Indexes...>&) const volatile
1175 return __arg(_GLIBCXX_TR1 get<_Indexes>(__tuple)...);
1180 * If the argument is a placeholder for the Nth argument, returns
1181 * a reference to the Nth argument to the bind function object.
1182 * [TR1 3.6.3/5 bullet 3]
1184 template<typename _Arg>
1185 class _Mu<_Arg, false, true>
1188 template<typename _Signature> class result;
1190 template<typename _CVMu, typename _CVArg, typename _Tuple>
1191 class result<_CVMu(_CVArg, _Tuple)>
1193 // Add a reference, if it hasn't already been done for us.
1194 // This allows us to be a little bit sloppy in constructing
1195 // the tuple that we pass to result_of<...>.
1196 typedef typename _Safe_tuple_element<(is_placeholder<_Arg>::value
1201 #ifdef _GLIBCXX_INCLUDE_AS_CXX0X
1202 typedef typename add_lvalue_reference<__base_type>::type type;
1204 typedef typename add_reference<__base_type>::type type;
1208 template<typename _Tuple>
1209 typename result<_Mu(_Arg, _Tuple)>::type
1210 operator()(const volatile _Arg&, const _Tuple& __tuple) const volatile
1212 return ::std::_GLIBCXX_TR1 get<(is_placeholder<_Arg>::value
1218 * If the argument is just a value, returns a reference to that
1219 * value. The cv-qualifiers on the reference are the same as the
1220 * cv-qualifiers on the _Mu object. [TR1 3.6.3/5 bullet 4]
1222 template<typename _Arg>
1223 class _Mu<_Arg, false, false>
1226 template<typename _Signature> struct result;
1228 template<typename _CVMu, typename _CVArg, typename _Tuple>
1229 struct result<_CVMu(_CVArg, _Tuple)>
1231 #ifdef _GLIBCXX_INCLUDE_AS_CXX0X
1232 typedef typename add_lvalue_reference<_CVArg>::type type;
1234 typedef typename add_reference<_CVArg>::type type;
1238 // Pick up the cv-qualifiers of the argument
1239 template<typename _CVArg, typename _Tuple>
1241 operator()(_CVArg& __arg, const _Tuple&) const volatile
1246 * Maps member pointers into instances of _Mem_fn but leaves all
1247 * other function objects untouched. Used by tr1::bind(). The
1248 * primary template handles the non--member-pointer case.
1250 template<typename _Tp>
1251 struct _Maybe_wrap_member_pointer
1256 __do_wrap(const _Tp& __x)
1261 * Maps member pointers into instances of _Mem_fn but leaves all
1262 * other function objects untouched. Used by tr1::bind(). This
1263 * partial specialization handles the member pointer case.
1265 template<typename _Tp, typename _Class>
1266 struct _Maybe_wrap_member_pointer<_Tp _Class::*>
1268 typedef _Mem_fn<_Tp _Class::*> type;
1271 __do_wrap(_Tp _Class::* __pm)
1272 { return type(__pm); }
1275 /// Type of the function object returned from bind().
1276 template<typename _Signature>
1279 template<typename _Functor, typename... _Bound_args>
1280 class _Bind<_Functor(_Bound_args...)>
1281 : public _Weak_result_type<_Functor>
1283 typedef _Bind __self_type;
1284 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
1288 tuple<_Bound_args...> _M_bound_args;
1291 template<typename... _Args, int... _Indexes>
1293 _Functor(typename result_of<_Mu<_Bound_args>
1294 (_Bound_args, tuple<_Args...>)>::type...)
1296 __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>)
1298 return _M_f(_Mu<_Bound_args>()
1299 (_GLIBCXX_TR1 get<_Indexes>(_M_bound_args), __args)...);
1303 template<typename... _Args, int... _Indexes>
1305 const _Functor(typename result_of<_Mu<_Bound_args>
1306 (const _Bound_args, tuple<_Args...>)
1308 __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>) const
1310 return _M_f(_Mu<_Bound_args>()
1311 (_GLIBCXX_TR1 get<_Indexes>(_M_bound_args), __args)...);
1315 template<typename... _Args, int... _Indexes>
1317 volatile _Functor(typename result_of<_Mu<_Bound_args>
1318 (volatile _Bound_args, tuple<_Args...>)
1320 __call(const tuple<_Args...>& __args,
1321 _Index_tuple<_Indexes...>) volatile
1323 return _M_f(_Mu<_Bound_args>()
1324 (_GLIBCXX_TR1 get<_Indexes>(_M_bound_args), __args)...);
1327 // Call as const volatile
1328 template<typename... _Args, int... _Indexes>
1330 const volatile _Functor(typename result_of<_Mu<_Bound_args>
1331 (const volatile _Bound_args,
1334 __call(const tuple<_Args...>& __args,
1335 _Index_tuple<_Indexes...>) const volatile
1337 return _M_f(_Mu<_Bound_args>()
1338 (_GLIBCXX_TR1 get<_Indexes>(_M_bound_args), __args)...);
1342 explicit _Bind(_Functor __f, _Bound_args... __bound_args)
1343 : _M_f(__f), _M_bound_args(__bound_args...) { }
1346 template<typename... _Args>
1348 _Functor(typename result_of<_Mu<_Bound_args>
1349 (_Bound_args, tuple<_Args...>)>::type...)
1351 operator()(_Args&... __args)
1353 return this->__call(_GLIBCXX_TR1 tie(__args...), _Bound_indexes());
1357 template<typename... _Args>
1359 const _Functor(typename result_of<_Mu<_Bound_args>
1360 (const _Bound_args, tuple<_Args...>)>::type...)
1362 operator()(_Args&... __args) const
1364 return this->__call(_GLIBCXX_TR1 tie(__args...), _Bound_indexes());
1369 template<typename... _Args>
1371 volatile _Functor(typename result_of<_Mu<_Bound_args>
1372 (volatile _Bound_args, tuple<_Args...>)>::type...)
1374 operator()(_Args&... __args) volatile
1376 return this->__call(_GLIBCXX_TR1 tie(__args...), _Bound_indexes());
1380 // Call as const volatile
1381 template<typename... _Args>
1383 const volatile _Functor(typename result_of<_Mu<_Bound_args>
1384 (const volatile _Bound_args,
1385 tuple<_Args...>)>::type...)
1387 operator()(_Args&... __args) const volatile
1389 return this->__call(_GLIBCXX_TR1 tie(__args...), _Bound_indexes());
1393 /// Type of the function object returned from bind<R>().
1394 template<typename _Result, typename _Signature>
1395 struct _Bind_result;
1397 template<typename _Result, typename _Functor, typename... _Bound_args>
1398 class _Bind_result<_Result, _Functor(_Bound_args...)>
1400 typedef _Bind_result __self_type;
1401 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
1405 tuple<_Bound_args...> _M_bound_args;
1408 template<typename... _Args, int... _Indexes>
1410 __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>)
1412 return _M_f(_Mu<_Bound_args>()
1413 (_GLIBCXX_TR1 get<_Indexes>(_M_bound_args), __args)...);
1417 template<typename... _Args, int... _Indexes>
1419 __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>) const
1421 return _M_f(_Mu<_Bound_args>()
1422 (_GLIBCXX_TR1 get<_Indexes>(_M_bound_args), __args)...);
1426 template<typename... _Args, int... _Indexes>
1428 __call(const tuple<_Args...>& __args,
1429 _Index_tuple<_Indexes...>) volatile
1431 return _M_f(_Mu<_Bound_args>()
1432 (_GLIBCXX_TR1 get<_Indexes>(_M_bound_args), __args)...);
1435 // Call as const volatile
1436 template<typename... _Args, int... _Indexes>
1438 __call(const tuple<_Args...>& __args,
1439 _Index_tuple<_Indexes...>) const volatile
1441 return _M_f(_Mu<_Bound_args>()
1442 (_GLIBCXX_TR1 get<_Indexes>(_M_bound_args), __args)...);
1446 typedef _Result result_type;
1449 _Bind_result(_Functor __f, _Bound_args... __bound_args)
1450 : _M_f(__f), _M_bound_args(__bound_args...) { }
1453 template<typename... _Args>
1455 operator()(_Args&... __args)
1457 return this->__call(_GLIBCXX_TR1 tie(__args...), _Bound_indexes());
1461 template<typename... _Args>
1463 operator()(_Args&... __args) const
1465 return this->__call(_GLIBCXX_TR1 tie(__args...), _Bound_indexes());
1469 template<typename... _Args>
1471 operator()(_Args&... __args) volatile
1473 return this->__call(_GLIBCXX_TR1 tie(__args...), _Bound_indexes());
1476 // Call as const volatile
1477 template<typename... _Args>
1479 operator()(_Args&... __args) const volatile
1481 return this->__call(_GLIBCXX_TR1 tie(__args...), _Bound_indexes());
1485 /// Class template _Bind is always a bind expression.
1486 template<typename _Signature>
1487 struct is_bind_expression<_Bind<_Signature> >
1488 { static const bool value = true; };
1490 template<typename _Signature>
1491 const bool is_bind_expression<_Bind<_Signature> >::value;
1493 /// Class template _Bind_result is always a bind expression.
1494 template<typename _Result, typename _Signature>
1495 struct is_bind_expression<_Bind_result<_Result, _Signature> >
1496 { static const bool value = true; };
1498 template<typename _Result, typename _Signature>
1499 const bool is_bind_expression<_Bind_result<_Result, _Signature> >::value;
1502 template<typename _Functor, typename... _ArgTypes>
1504 _Bind<typename _Maybe_wrap_member_pointer<_Functor>::type(_ArgTypes...)>
1505 bind(_Functor __f, _ArgTypes... __args)
1507 typedef _Maybe_wrap_member_pointer<_Functor> __maybe_type;
1508 typedef typename __maybe_type::type __functor_type;
1509 typedef _Bind<__functor_type(_ArgTypes...)> __result_type;
1510 return __result_type(__maybe_type::__do_wrap(__f), __args...);
1513 template<typename _Result, typename _Functor, typename... _ArgTypes>
1515 _Bind_result<_Result,
1516 typename _Maybe_wrap_member_pointer<_Functor>::type
1518 bind(_Functor __f, _ArgTypes... __args)
1520 typedef _Maybe_wrap_member_pointer<_Functor> __maybe_type;
1521 typedef typename __maybe_type::type __functor_type;
1522 typedef _Bind_result<_Result, __functor_type(_ArgTypes...)>
1524 return __result_type(__maybe_type::__do_wrap(__f), __args...);
1528 * @brief Exception class thrown when class template function's
1529 * operator() is called with an empty target.
1532 class bad_function_call : public std::exception { };
1535 * The integral constant expression 0 can be converted into a
1536 * pointer to this type. It is used by the function template to
1537 * accept NULL pointers.
1539 struct _M_clear_type;
1542 * Trait identifying "location-invariant" types, meaning that the
1543 * address of the object (or any of its members) will not escape.
1544 * Also implies a trivial copy constructor and assignment operator.
1546 template<typename _Tp>
1547 struct __is_location_invariant
1548 : integral_constant<bool,
1549 (is_pointer<_Tp>::value
1550 || is_member_pointer<_Tp>::value)>
1554 class _Undefined_class;
1559 const void* _M_const_object;
1560 void (*_M_function_pointer)();
1561 void (_Undefined_class::*_M_member_pointer)();
1566 void* _M_access() { return &_M_pod_data[0]; }
1567 const void* _M_access() const { return &_M_pod_data[0]; }
1569 template<typename _Tp>
1572 { return *static_cast<_Tp*>(_M_access()); }
1574 template<typename _Tp>
1577 { return *static_cast<const _Tp*>(_M_access()); }
1579 _Nocopy_types _M_unused;
1580 char _M_pod_data[sizeof(_Nocopy_types)];
1583 enum _Manager_operation
1591 // Simple type wrapper that helps avoid annoying const problems
1592 // when casting between void pointers and pointers-to-pointers.
1593 template<typename _Tp>
1594 struct _Simple_type_wrapper
1596 _Simple_type_wrapper(_Tp __value) : __value(__value) { }
1601 template<typename _Tp>
1602 struct __is_location_invariant<_Simple_type_wrapper<_Tp> >
1603 : __is_location_invariant<_Tp>
1607 // Converts a reference to a function object into a callable
1609 template<typename _Functor>
1611 __callable_functor(_Functor& __f)
1614 template<typename _Member, typename _Class>
1615 inline _Mem_fn<_Member _Class::*>
1616 __callable_functor(_Member _Class::* &__p)
1617 { return mem_fn(__p); }
1619 template<typename _Member, typename _Class>
1620 inline _Mem_fn<_Member _Class::*>
1621 __callable_functor(_Member _Class::* const &__p)
1622 { return mem_fn(__p); }
1624 template<typename _Signature>
1627 /// Base class of all polymorphic function object wrappers.
1628 class _Function_base
1631 static const std::size_t _M_max_size = sizeof(_Nocopy_types);
1632 static const std::size_t _M_max_align = __alignof__(_Nocopy_types);
1634 template<typename _Functor>
1638 static const bool __stored_locally =
1639 (__is_location_invariant<_Functor>::value
1640 && sizeof(_Functor) <= _M_max_size
1641 && __alignof__(_Functor) <= _M_max_align
1642 && (_M_max_align % __alignof__(_Functor) == 0));
1644 typedef integral_constant<bool, __stored_locally> _Local_storage;
1646 // Retrieve a pointer to the function object
1648 _M_get_pointer(const _Any_data& __source)
1650 const _Functor* __ptr =
1651 __stored_locally? &__source._M_access<_Functor>()
1652 /* have stored a pointer */ : __source._M_access<_Functor*>();
1653 return const_cast<_Functor*>(__ptr);
1656 // Clone a location-invariant function object that fits within
1657 // an _Any_data structure.
1659 _M_clone(_Any_data& __dest, const _Any_data& __source, true_type)
1661 new (__dest._M_access()) _Functor(__source._M_access<_Functor>());
1664 // Clone a function object that is not location-invariant or
1665 // that cannot fit into an _Any_data structure.
1667 _M_clone(_Any_data& __dest, const _Any_data& __source, false_type)
1669 __dest._M_access<_Functor*>() =
1670 new _Functor(*__source._M_access<_Functor*>());
1673 // Destroying a location-invariant object may still require
1676 _M_destroy(_Any_data& __victim, true_type)
1678 __victim._M_access<_Functor>().~_Functor();
1681 // Destroying an object located on the heap.
1683 _M_destroy(_Any_data& __victim, false_type)
1685 delete __victim._M_access<_Functor*>();
1690 _M_manager(_Any_data& __dest, const _Any_data& __source,
1691 _Manager_operation __op)
1695 case __get_type_info:
1696 __dest._M_access<const type_info*>() = &typeid(_Functor);
1699 case __get_functor_ptr:
1700 __dest._M_access<_Functor*>() = _M_get_pointer(__source);
1703 case __clone_functor:
1704 _M_clone(__dest, __source, _Local_storage());
1707 case __destroy_functor:
1708 _M_destroy(__dest, _Local_storage());
1715 _M_init_functor(_Any_data& __functor, const _Functor& __f)
1716 { _M_init_functor(__functor, __f, _Local_storage()); }
1718 template<typename _Signature>
1720 _M_not_empty_function(const function<_Signature>& __f)
1723 template<typename _Tp>
1725 _M_not_empty_function(const _Tp*& __fp)
1728 template<typename _Class, typename _Tp>
1730 _M_not_empty_function(_Tp _Class::* const& __mp)
1733 template<typename _Tp>
1735 _M_not_empty_function(const _Tp&)
1740 _M_init_functor(_Any_data& __functor, const _Functor& __f, true_type)
1741 { new (__functor._M_access()) _Functor(__f); }
1744 _M_init_functor(_Any_data& __functor, const _Functor& __f, false_type)
1745 { __functor._M_access<_Functor*>() = new _Functor(__f); }
1748 template<typename _Functor>
1749 class _Ref_manager : public _Base_manager<_Functor*>
1751 typedef _Function_base::_Base_manager<_Functor*> _Base;
1755 _M_manager(_Any_data& __dest, const _Any_data& __source,
1756 _Manager_operation __op)
1760 case __get_type_info:
1761 __dest._M_access<const type_info*>() = &typeid(_Functor);
1764 case __get_functor_ptr:
1765 __dest._M_access<_Functor*>() = *_Base::_M_get_pointer(__source);
1766 return is_const<_Functor>::value;
1770 _Base::_M_manager(__dest, __source, __op);
1776 _M_init_functor(_Any_data& __functor, reference_wrapper<_Functor> __f)
1778 // TBD: Use address_of function instead.
1779 _Base::_M_init_functor(__functor, &__f.get());
1783 _Function_base() : _M_manager(0) { }
1788 _M_manager(_M_functor, _M_functor, __destroy_functor);
1792 bool _M_empty() const { return !_M_manager; }
1794 typedef bool (*_Manager_type)(_Any_data&, const _Any_data&,
1795 _Manager_operation);
1797 _Any_data _M_functor;
1798 _Manager_type _M_manager;
1801 template<typename _Signature, typename _Functor>
1802 class _Function_handler;
1804 template<typename _Res, typename _Functor, typename... _ArgTypes>
1805 class _Function_handler<_Res(_ArgTypes...), _Functor>
1806 : public _Function_base::_Base_manager<_Functor>
1808 typedef _Function_base::_Base_manager<_Functor> _Base;
1812 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1814 return (*_Base::_M_get_pointer(__functor))(__args...);
1818 template<typename _Functor, typename... _ArgTypes>
1819 class _Function_handler<void(_ArgTypes...), _Functor>
1820 : public _Function_base::_Base_manager<_Functor>
1822 typedef _Function_base::_Base_manager<_Functor> _Base;
1826 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1828 (*_Base::_M_get_pointer(__functor))(__args...);
1832 template<typename _Res, typename _Functor, typename... _ArgTypes>
1833 class _Function_handler<_Res(_ArgTypes...), reference_wrapper<_Functor> >
1834 : public _Function_base::_Ref_manager<_Functor>
1836 typedef _Function_base::_Ref_manager<_Functor> _Base;
1840 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1843 __callable_functor(**_Base::_M_get_pointer(__functor))(__args...);
1847 template<typename _Functor, typename... _ArgTypes>
1848 class _Function_handler<void(_ArgTypes...), reference_wrapper<_Functor> >
1849 : public _Function_base::_Ref_manager<_Functor>
1851 typedef _Function_base::_Ref_manager<_Functor> _Base;
1855 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1857 __callable_functor(**_Base::_M_get_pointer(__functor))(__args...);
1861 template<typename _Class, typename _Member, typename _Res,
1862 typename... _ArgTypes>
1863 class _Function_handler<_Res(_ArgTypes...), _Member _Class::*>
1864 : public _Function_handler<void(_ArgTypes...), _Member _Class::*>
1866 typedef _Function_handler<void(_ArgTypes...), _Member _Class::*>
1871 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1874 mem_fn(_Base::_M_get_pointer(__functor)->__value)(__args...);
1878 template<typename _Class, typename _Member, typename... _ArgTypes>
1879 class _Function_handler<void(_ArgTypes...), _Member _Class::*>
1880 : public _Function_base::_Base_manager<
1881 _Simple_type_wrapper< _Member _Class::* > >
1883 typedef _Member _Class::* _Functor;
1884 typedef _Simple_type_wrapper<_Functor> _Wrapper;
1885 typedef _Function_base::_Base_manager<_Wrapper> _Base;
1889 _M_manager(_Any_data& __dest, const _Any_data& __source,
1890 _Manager_operation __op)
1894 case __get_type_info:
1895 __dest._M_access<const type_info*>() = &typeid(_Functor);
1898 case __get_functor_ptr:
1899 __dest._M_access<_Functor*>() =
1900 &_Base::_M_get_pointer(__source)->__value;
1904 _Base::_M_manager(__dest, __source, __op);
1910 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1913 mem_fn(_Base::_M_get_pointer(__functor)->__value)(__args...);
1918 template<typename _Res, typename... _ArgTypes>
1919 class function<_Res(_ArgTypes...)>
1920 : public _Maybe_unary_or_binary_function<_Res, _ArgTypes...>,
1921 private _Function_base
1923 /// This class is used to implement the safe_bool idiom.
1926 _Hidden_type* _M_bool;
1929 /// This typedef is used to implement the safe_bool idiom.
1930 typedef _Hidden_type* _Hidden_type::* _Safe_bool;
1932 typedef _Res _Signature_type(_ArgTypes...);
1934 struct _Useless { };
1937 typedef _Res result_type;
1939 // [3.7.2.1] construct/copy/destroy
1942 * @brief Default construct creates an empty function call wrapper.
1943 * @post @c !(bool)*this
1945 function() : _Function_base() { }
1948 * @brief Default construct creates an empty function call wrapper.
1949 * @post @c !(bool)*this
1951 function(_M_clear_type*) : _Function_base() { }
1954 * @brief %Function copy constructor.
1955 * @param x A %function object with identical call signature.
1956 * @pre @c (bool)*this == (bool)x
1958 * The newly-created %function contains a copy of the target of @a
1959 * x (if it has one).
1961 function(const function& __x);
1964 * @brief Builds a %function that targets a copy of the incoming
1966 * @param f A %function object that is callable with parameters of
1967 * type @c T1, @c T2, ..., @c TN and returns a value convertible
1970 * The newly-created %function object will target a copy of @a
1971 * f. If @a f is @c reference_wrapper<F>, then this function
1972 * object will contain a reference to the function object @c
1973 * f.get(). If @a f is a NULL function pointer or NULL
1974 * pointer-to-member, the newly-created object will be empty.
1976 * If @a f is a non-NULL function pointer or an object of type @c
1977 * reference_wrapper<F>, this function will not throw.
1979 template<typename _Functor>
1980 function(_Functor __f,
1981 typename __gnu_cxx::__enable_if<
1982 !is_integral<_Functor>::value, _Useless>::__type
1986 * @brief %Function assignment operator.
1987 * @param x A %function with identical call signature.
1988 * @post @c (bool)*this == (bool)x
1991 * The target of @a x is copied to @c *this. If @a x has no
1992 * target, then @c *this will be empty.
1994 * If @a x targets a function pointer or a reference to a function
1995 * object, then this operation will not throw an exception.
1998 operator=(const function& __x)
2000 function(__x).swap(*this);
2005 * @brief %Function assignment to zero.
2006 * @post @c !(bool)*this
2009 * The target of @a *this is deallocated, leaving it empty.
2012 operator=(_M_clear_type*)
2016 _M_manager(_M_functor, _M_functor, __destroy_functor);
2024 * @brief %Function assignment to a new target.
2025 * @param f A %function object that is callable with parameters of
2026 * type @c T1, @c T2, ..., @c TN and returns a value convertible
2030 * This %function object wrapper will target a copy of @a
2031 * f. If @a f is @c reference_wrapper<F>, then this function
2032 * object will contain a reference to the function object @c
2033 * f.get(). If @a f is a NULL function pointer or NULL
2034 * pointer-to-member, @c this object will be empty.
2036 * If @a f is a non-NULL function pointer or an object of type @c
2037 * reference_wrapper<F>, this function will not throw.
2039 template<typename _Functor>
2040 typename __gnu_cxx::__enable_if<!is_integral<_Functor>::value,
2042 operator=(_Functor __f)
2044 function(__f).swap(*this);
2048 // [3.7.2.2] function modifiers
2051 * @brief Swap the targets of two %function objects.
2052 * @param f A %function with identical call signature.
2054 * Swap the targets of @c this function object and @a f. This
2055 * function will not throw an exception.
2057 void swap(function& __x)
2059 _Any_data __old_functor = _M_functor;
2060 _M_functor = __x._M_functor;
2061 __x._M_functor = __old_functor;
2062 _Manager_type __old_manager = _M_manager;
2063 _M_manager = __x._M_manager;
2064 __x._M_manager = __old_manager;
2065 _Invoker_type __old_invoker = _M_invoker;
2066 _M_invoker = __x._M_invoker;
2067 __x._M_invoker = __old_invoker;
2070 // [3.7.2.3] function capacity
2073 * @brief Determine if the %function wrapper has a target.
2075 * @return @c true when this %function object contains a target,
2076 * or @c false when it is empty.
2078 * This function will not throw an exception.
2080 operator _Safe_bool() const
2085 return &_Hidden_type::_M_bool;
2088 // [3.7.2.4] function invocation
2091 * @brief Invokes the function targeted by @c *this.
2092 * @returns the result of the target.
2093 * @throws bad_function_call when @c !(bool)*this
2095 * The function call operator invokes the target function object
2096 * stored by @c this.
2098 _Res operator()(_ArgTypes... __args) const;
2100 // [3.7.2.5] function target access
2102 * @brief Determine the type of the target of this function object
2105 * @returns the type identifier of the target function object, or
2106 * @c typeid(void) if @c !(bool)*this.
2108 * This function will not throw an exception.
2110 const type_info& target_type() const;
2113 * @brief Access the stored target function object.
2115 * @return Returns a pointer to the stored target function object,
2116 * if @c typeid(Functor).equals(target_type()); otherwise, a NULL
2119 * This function will not throw an exception.
2121 template<typename _Functor> _Functor* target();
2124 template<typename _Functor> const _Functor* target() const;
2127 // [3.7.2.6] undefined operators
2128 template<typename _Function>
2129 void operator==(const function<_Function>&) const;
2130 template<typename _Function>
2131 void operator!=(const function<_Function>&) const;
2133 typedef _Res (*_Invoker_type)(const _Any_data&, _ArgTypes...);
2134 _Invoker_type _M_invoker;
2137 template<typename _Res, typename... _ArgTypes>
2138 function<_Res(_ArgTypes...)>::
2139 function(const function& __x)
2144 _M_invoker = __x._M_invoker;
2145 _M_manager = __x._M_manager;
2146 __x._M_manager(_M_functor, __x._M_functor, __clone_functor);
2150 template<typename _Res, typename... _ArgTypes>
2151 template<typename _Functor>
2152 function<_Res(_ArgTypes...)>::
2153 function(_Functor __f,
2154 typename __gnu_cxx::__enable_if<
2155 !is_integral<_Functor>::value, _Useless>::__type)
2158 typedef _Function_handler<_Signature_type, _Functor> _My_handler;
2160 if (_My_handler::_M_not_empty_function(__f))
2162 _M_invoker = &_My_handler::_M_invoke;
2163 _M_manager = &_My_handler::_M_manager;
2164 _My_handler::_M_init_functor(_M_functor, __f);
2168 template<typename _Res, typename... _ArgTypes>
2170 function<_Res(_ArgTypes...)>::
2171 operator()(_ArgTypes... __args) const
2176 throw bad_function_call();
2181 return _M_invoker(_M_functor, __args...);
2184 template<typename _Res, typename... _ArgTypes>
2186 function<_Res(_ArgTypes...)>::
2191 _Any_data __typeinfo_result;
2192 _M_manager(__typeinfo_result, _M_functor, __get_type_info);
2193 return *__typeinfo_result._M_access<const type_info*>();
2196 return typeid(void);
2199 template<typename _Res, typename... _ArgTypes>
2200 template<typename _Functor>
2202 function<_Res(_ArgTypes...)>::
2205 if (typeid(_Functor) == target_type() && _M_manager)
2208 if (_M_manager(__ptr, _M_functor, __get_functor_ptr)
2209 && !is_const<_Functor>::value)
2212 return __ptr._M_access<_Functor*>();
2218 template<typename _Res, typename... _ArgTypes>
2219 template<typename _Functor>
2221 function<_Res(_ArgTypes...)>::
2224 if (typeid(_Functor) == target_type() && _M_manager)
2227 _M_manager(__ptr, _M_functor, __get_functor_ptr);
2228 return __ptr._M_access<const _Functor*>();
2234 // [3.7.2.7] null pointer comparisons
2237 * @brief Compares a polymorphic function object wrapper against 0
2238 * (the NULL pointer).
2239 * @returns @c true if the wrapper has no target, @c false otherwise
2241 * This function will not throw an exception.
2243 template<typename _Signature>
2245 operator==(const function<_Signature>& __f, _M_clear_type*)
2249 template<typename _Signature>
2251 operator==(_M_clear_type*, const function<_Signature>& __f)
2255 * @brief Compares a polymorphic function object wrapper against 0
2256 * (the NULL pointer).
2257 * @returns @c false if the wrapper has no target, @c true otherwise
2259 * This function will not throw an exception.
2261 template<typename _Signature>
2263 operator!=(const function<_Signature>& __f, _M_clear_type*)
2267 template<typename _Signature>
2269 operator!=(_M_clear_type*, const function<_Signature>& __f)
2272 // [3.7.2.8] specialized algorithms
2275 * @brief Swap the targets of two polymorphic function object wrappers.
2277 * This function will not throw an exception.
2279 template<typename _Signature>
2281 swap(function<_Signature>& __x, function<_Signature>& __y)
2284 _GLIBCXX_END_NAMESPACE_TR1