1 // TR1 functional header -*- C++ -*-
3 // Copyright (C) 2007, 2008, 2009 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>
157 * Actual implementation of result_of. When _Has_result_type is
158 * true, gets its result from _Weak_result_type. Otherwise, uses
159 * the function object's member template result to extract the
162 template<bool _Has_result_type, typename _Signature>
163 struct _Result_of_impl;
165 // Handle member data pointers using _Mem_fn's logic
166 template<typename _Res, typename _Class, typename _T1>
167 struct _Result_of_impl<false, _Res _Class::*(_T1)>
169 typedef typename _Mem_fn<_Res _Class::*>
170 ::template _Result_type<_T1>::type type;
174 * Determine whether we can determine a result type from @c Functor
177 template<typename _Functor, typename... _ArgTypes>
178 class result_of<_Functor(_ArgTypes...)>
179 : public _Result_of_impl<
180 _Has_result_type<_Weak_result_type<_Functor> >::value,
181 _Functor(_ArgTypes...)>
185 /// We already know the result type for @c Functor; use it.
186 template<typename _Functor, typename... _ArgTypes>
187 struct _Result_of_impl<true, _Functor(_ArgTypes...)>
189 typedef typename _Weak_result_type<_Functor>::result_type type;
193 * We need to compute the result type for this invocation the hard
196 template<typename _Functor, typename... _ArgTypes>
197 struct _Result_of_impl<false, _Functor(_ArgTypes...)>
199 typedef typename _Functor
200 ::template result<_Functor(_ArgTypes...)>::type type;
204 * It is unsafe to access ::result when there are zero arguments, so we
205 * return @c void instead.
207 template<typename _Functor>
208 struct _Result_of_impl<false, _Functor()>
213 /// Determines if the type _Tp derives from unary_function.
214 template<typename _Tp>
215 struct _Derives_from_unary_function : __sfinae_types
218 template<typename _T1, typename _Res>
219 static __one __test(const volatile unary_function<_T1, _Res>*);
221 // It's tempting to change "..." to const volatile void*, but
222 // that fails when _Tp is a function type.
223 static __two __test(...);
226 static const bool value = sizeof(__test((_Tp*)0)) == 1;
229 /// Determines if the type _Tp derives from binary_function.
230 template<typename _Tp>
231 struct _Derives_from_binary_function : __sfinae_types
234 template<typename _T1, typename _T2, typename _Res>
235 static __one __test(const volatile binary_function<_T1, _T2, _Res>*);
237 // It's tempting to change "..." to const volatile void*, but
238 // that fails when _Tp is a function type.
239 static __two __test(...);
242 static const bool value = sizeof(__test((_Tp*)0)) == 1;
245 /// Turns a function type into a function pointer type
246 template<typename _Tp, bool _IsFunctionType = is_function<_Tp>::value>
247 struct _Function_to_function_pointer
252 template<typename _Tp>
253 struct _Function_to_function_pointer<_Tp, true>
259 * Invoke a function object, which may be either a member pointer or a
260 * function object. The first parameter will tell which.
262 template<typename _Functor, typename... _Args>
264 typename __gnu_cxx::__enable_if<
265 (!is_member_pointer<_Functor>::value
266 && !is_function<_Functor>::value
267 && !is_function<typename remove_pointer<_Functor>::type>::value),
268 typename result_of<_Functor(_Args...)>::type
270 __invoke(_Functor& __f, _Args&... __args)
272 return __f(__args...);
275 template<typename _Functor, typename... _Args>
277 typename __gnu_cxx::__enable_if<
278 (is_member_pointer<_Functor>::value
279 && !is_function<_Functor>::value
280 && !is_function<typename remove_pointer<_Functor>::type>::value),
281 typename result_of<_Functor(_Args...)>::type
283 __invoke(_Functor& __f, _Args&... __args)
285 return mem_fn(__f)(__args...);
288 // To pick up function references (that will become function pointers)
289 template<typename _Functor, typename... _Args>
291 typename __gnu_cxx::__enable_if<
292 (is_pointer<_Functor>::value
293 && is_function<typename remove_pointer<_Functor>::type>::value),
294 typename result_of<_Functor(_Args...)>::type
296 __invoke(_Functor __f, _Args&... __args)
298 return __f(__args...);
302 * Knowing which of unary_function and binary_function _Tp derives
303 * from, derives from the same and ensures that reference_wrapper
304 * will have a weak result type. See cases below.
306 template<bool _Unary, bool _Binary, typename _Tp>
307 struct _Reference_wrapper_base_impl;
309 // Not a unary_function or binary_function, so try a weak result type.
310 template<typename _Tp>
311 struct _Reference_wrapper_base_impl<false, false, _Tp>
312 : _Weak_result_type<_Tp>
315 // unary_function but not binary_function
316 template<typename _Tp>
317 struct _Reference_wrapper_base_impl<true, false, _Tp>
318 : unary_function<typename _Tp::argument_type,
319 typename _Tp::result_type>
322 // binary_function but not unary_function
323 template<typename _Tp>
324 struct _Reference_wrapper_base_impl<false, true, _Tp>
325 : binary_function<typename _Tp::first_argument_type,
326 typename _Tp::second_argument_type,
327 typename _Tp::result_type>
330 // Both unary_function and binary_function. Import result_type to
332 template<typename _Tp>
333 struct _Reference_wrapper_base_impl<true, true, _Tp>
334 : unary_function<typename _Tp::argument_type,
335 typename _Tp::result_type>,
336 binary_function<typename _Tp::first_argument_type,
337 typename _Tp::second_argument_type,
338 typename _Tp::result_type>
340 typedef typename _Tp::result_type result_type;
344 * Derives from unary_function or binary_function when it
345 * can. Specializations handle all of the easy cases. The primary
346 * template determines what to do with a class type, which may
347 * derive from both unary_function and binary_function.
349 template<typename _Tp>
350 struct _Reference_wrapper_base
351 : _Reference_wrapper_base_impl<
352 _Derives_from_unary_function<_Tp>::value,
353 _Derives_from_binary_function<_Tp>::value,
357 // - a function type (unary)
358 template<typename _Res, typename _T1>
359 struct _Reference_wrapper_base<_Res(_T1)>
360 : unary_function<_T1, _Res>
363 // - a function type (binary)
364 template<typename _Res, typename _T1, typename _T2>
365 struct _Reference_wrapper_base<_Res(_T1, _T2)>
366 : binary_function<_T1, _T2, _Res>
369 // - a function pointer type (unary)
370 template<typename _Res, typename _T1>
371 struct _Reference_wrapper_base<_Res(*)(_T1)>
372 : unary_function<_T1, _Res>
375 // - a function pointer type (binary)
376 template<typename _Res, typename _T1, typename _T2>
377 struct _Reference_wrapper_base<_Res(*)(_T1, _T2)>
378 : binary_function<_T1, _T2, _Res>
381 // - a pointer to member function type (unary, no qualifiers)
382 template<typename _Res, typename _T1>
383 struct _Reference_wrapper_base<_Res (_T1::*)()>
384 : unary_function<_T1*, _Res>
387 // - a pointer to member function type (binary, no qualifiers)
388 template<typename _Res, typename _T1, typename _T2>
389 struct _Reference_wrapper_base<_Res (_T1::*)(_T2)>
390 : binary_function<_T1*, _T2, _Res>
393 // - a pointer to member function type (unary, const)
394 template<typename _Res, typename _T1>
395 struct _Reference_wrapper_base<_Res (_T1::*)() const>
396 : unary_function<const _T1*, _Res>
399 // - a pointer to member function type (binary, const)
400 template<typename _Res, typename _T1, typename _T2>
401 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const>
402 : binary_function<const _T1*, _T2, _Res>
405 // - a pointer to member function type (unary, volatile)
406 template<typename _Res, typename _T1>
407 struct _Reference_wrapper_base<_Res (_T1::*)() volatile>
408 : unary_function<volatile _T1*, _Res>
411 // - a pointer to member function type (binary, volatile)
412 template<typename _Res, typename _T1, typename _T2>
413 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) volatile>
414 : binary_function<volatile _T1*, _T2, _Res>
417 // - a pointer to member function type (unary, const volatile)
418 template<typename _Res, typename _T1>
419 struct _Reference_wrapper_base<_Res (_T1::*)() const volatile>
420 : unary_function<const volatile _T1*, _Res>
423 // - a pointer to member function type (binary, const volatile)
424 template<typename _Res, typename _T1, typename _T2>
425 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const volatile>
426 : binary_function<const volatile _T1*, _T2, _Res>
429 /// reference_wrapper
430 template<typename _Tp>
431 class reference_wrapper
432 : public _Reference_wrapper_base<typename remove_cv<_Tp>::type>
434 // If _Tp is a function type, we can't form result_of<_Tp(...)>,
435 // so turn it into a function pointer type.
436 typedef typename _Function_to_function_pointer<_Tp>::type
444 reference_wrapper(_Tp& __indata): _M_data(&__indata)
447 reference_wrapper(const reference_wrapper<_Tp>& __inref):
448 _M_data(__inref._M_data)
452 operator=(const reference_wrapper<_Tp>& __inref)
454 _M_data = __inref._M_data;
458 operator _Tp&() const
459 { return this->get(); }
465 template<typename... _Args>
466 typename result_of<_M_func_type(_Args...)>::type
467 operator()(_Args&... __args) const
469 return __invoke(get(), __args...);
474 // Denotes a reference should be taken to a variable.
475 template<typename _Tp>
476 inline reference_wrapper<_Tp>
478 { return reference_wrapper<_Tp>(__t); }
480 // Denotes a const reference should be taken to a variable.
481 template<typename _Tp>
482 inline reference_wrapper<const _Tp>
484 { return reference_wrapper<const _Tp>(__t); }
486 template<typename _Tp>
487 inline reference_wrapper<_Tp>
488 ref(reference_wrapper<_Tp> __t)
489 { return ref(__t.get()); }
491 template<typename _Tp>
492 inline reference_wrapper<const _Tp>
493 cref(reference_wrapper<_Tp> __t)
494 { return cref(__t.get()); }
496 template<typename _Tp, bool>
497 struct _Mem_fn_const_or_non
499 typedef const _Tp& type;
502 template<typename _Tp>
503 struct _Mem_fn_const_or_non<_Tp, false>
509 * Derives from @c unary_function or @c binary_function, or perhaps
510 * nothing, depending on the number of arguments provided. The
511 * primary template is the basis case, which derives nothing.
513 template<typename _Res, typename... _ArgTypes>
514 struct _Maybe_unary_or_binary_function { };
516 /// Derives from @c unary_function, as appropriate.
517 template<typename _Res, typename _T1>
518 struct _Maybe_unary_or_binary_function<_Res, _T1>
519 : std::unary_function<_T1, _Res> { };
521 /// Derives from @c binary_function, as appropriate.
522 template<typename _Res, typename _T1, typename _T2>
523 struct _Maybe_unary_or_binary_function<_Res, _T1, _T2>
524 : std::binary_function<_T1, _T2, _Res> { };
526 /// Implementation of @c mem_fn for member function pointers.
527 template<typename _Res, typename _Class, typename... _ArgTypes>
528 class _Mem_fn<_Res (_Class::*)(_ArgTypes...)>
529 : public _Maybe_unary_or_binary_function<_Res, _Class*, _ArgTypes...>
531 typedef _Res (_Class::*_Functor)(_ArgTypes...);
533 template<typename _Tp>
535 _M_call(_Tp& __object, const volatile _Class *,
536 _ArgTypes... __args) const
537 { return (__object.*__pmf)(__args...); }
539 template<typename _Tp>
541 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
542 { return ((*__ptr).*__pmf)(__args...); }
545 typedef _Res result_type;
547 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
551 operator()(_Class& __object, _ArgTypes... __args) const
552 { return (__object.*__pmf)(__args...); }
556 operator()(_Class* __object, _ArgTypes... __args) const
557 { return (__object->*__pmf)(__args...); }
559 // Handle smart pointers, references and pointers to derived
560 template<typename _Tp>
562 operator()(_Tp& __object, _ArgTypes... __args) const
563 { return _M_call(__object, &__object, __args...); }
569 /// Implementation of @c mem_fn for const member function pointers.
570 template<typename _Res, typename _Class, typename... _ArgTypes>
571 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) const>
572 : public _Maybe_unary_or_binary_function<_Res, const _Class*,
575 typedef _Res (_Class::*_Functor)(_ArgTypes...) const;
577 template<typename _Tp>
579 _M_call(_Tp& __object, const volatile _Class *,
580 _ArgTypes... __args) const
581 { return (__object.*__pmf)(__args...); }
583 template<typename _Tp>
585 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
586 { return ((*__ptr).*__pmf)(__args...); }
589 typedef _Res result_type;
591 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
595 operator()(const _Class& __object, _ArgTypes... __args) const
596 { return (__object.*__pmf)(__args...); }
600 operator()(const _Class* __object, _ArgTypes... __args) const
601 { return (__object->*__pmf)(__args...); }
603 // Handle smart pointers, references and pointers to derived
604 template<typename _Tp>
605 _Res operator()(_Tp& __object, _ArgTypes... __args) const
606 { return _M_call(__object, &__object, __args...); }
612 /// Implementation of @c mem_fn for volatile member function pointers.
613 template<typename _Res, typename _Class, typename... _ArgTypes>
614 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) volatile>
615 : public _Maybe_unary_or_binary_function<_Res, volatile _Class*,
618 typedef _Res (_Class::*_Functor)(_ArgTypes...) volatile;
620 template<typename _Tp>
622 _M_call(_Tp& __object, const volatile _Class *,
623 _ArgTypes... __args) const
624 { return (__object.*__pmf)(__args...); }
626 template<typename _Tp>
628 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
629 { return ((*__ptr).*__pmf)(__args...); }
632 typedef _Res result_type;
634 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
638 operator()(volatile _Class& __object, _ArgTypes... __args) const
639 { return (__object.*__pmf)(__args...); }
643 operator()(volatile _Class* __object, _ArgTypes... __args) const
644 { return (__object->*__pmf)(__args...); }
646 // Handle smart pointers, references and pointers to derived
647 template<typename _Tp>
649 operator()(_Tp& __object, _ArgTypes... __args) const
650 { return _M_call(__object, &__object, __args...); }
656 /// Implementation of @c mem_fn for const volatile member function pointers.
657 template<typename _Res, typename _Class, typename... _ArgTypes>
658 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) const volatile>
659 : public _Maybe_unary_or_binary_function<_Res, const volatile _Class*,
662 typedef _Res (_Class::*_Functor)(_ArgTypes...) const volatile;
664 template<typename _Tp>
666 _M_call(_Tp& __object, const volatile _Class *,
667 _ArgTypes... __args) const
668 { return (__object.*__pmf)(__args...); }
670 template<typename _Tp>
672 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
673 { return ((*__ptr).*__pmf)(__args...); }
676 typedef _Res result_type;
678 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
682 operator()(const volatile _Class& __object, _ArgTypes... __args) const
683 { return (__object.*__pmf)(__args...); }
687 operator()(const volatile _Class* __object, _ArgTypes... __args) const
688 { return (__object->*__pmf)(__args...); }
690 // Handle smart pointers, references and pointers to derived
691 template<typename _Tp>
692 _Res operator()(_Tp& __object, _ArgTypes... __args) const
693 { return _M_call(__object, &__object, __args...); }
700 template<typename _Res, typename _Class>
701 class _Mem_fn<_Res _Class::*>
703 // This bit of genius is due to Peter Dimov, improved slightly by
705 template<typename _Tp>
707 _M_call(_Tp& __object, _Class *) const
708 { return __object.*__pm; }
710 template<typename _Tp, typename _Up>
712 _M_call(_Tp& __object, _Up * const *) const
713 { return (*__object).*__pm; }
715 template<typename _Tp, typename _Up>
717 _M_call(_Tp& __object, const _Up * const *) const
718 { return (*__object).*__pm; }
720 template<typename _Tp>
722 _M_call(_Tp& __object, const _Class *) const
723 { return __object.*__pm; }
725 template<typename _Tp>
727 _M_call(_Tp& __ptr, const volatile void*) const
728 { return (*__ptr).*__pm; }
730 template<typename _Tp> static _Tp& __get_ref();
732 template<typename _Tp>
733 static __sfinae_types::__one __check_const(_Tp&, _Class*);
734 template<typename _Tp, typename _Up>
735 static __sfinae_types::__one __check_const(_Tp&, _Up * const *);
736 template<typename _Tp, typename _Up>
737 static __sfinae_types::__two __check_const(_Tp&, const _Up * const *);
738 template<typename _Tp>
739 static __sfinae_types::__two __check_const(_Tp&, const _Class*);
740 template<typename _Tp>
741 static __sfinae_types::__two __check_const(_Tp&, const volatile void*);
744 template<typename _Tp>
746 : _Mem_fn_const_or_non<_Res,
747 (sizeof(__sfinae_types::__two)
748 == sizeof(__check_const<_Tp>(__get_ref<_Tp>(), (_Tp*)0)))>
751 template<typename _Signature>
754 template<typename _CVMem, typename _Tp>
755 struct result<_CVMem(_Tp)>
756 : public _Result_type<_Tp> { };
758 template<typename _CVMem, typename _Tp>
759 struct result<_CVMem(_Tp&)>
760 : public _Result_type<_Tp> { };
763 _Mem_fn(_Res _Class::*__pm) : __pm(__pm) { }
767 operator()(_Class& __object) const
768 { return __object.*__pm; }
771 operator()(const _Class& __object) const
772 { return __object.*__pm; }
776 operator()(_Class* __object) const
777 { return __object->*__pm; }
780 operator()(const _Class* __object) const
781 { return __object->*__pm; }
783 // Handle smart pointers and derived
784 template<typename _Tp>
785 typename _Result_type<_Tp>::type
786 operator()(_Tp& __unknown) const
787 { return _M_call(__unknown, &__unknown); }
794 * @brief Returns a function object that forwards to the member
797 template<typename _Tp, typename _Class>
798 inline _Mem_fn<_Tp _Class::*>
799 mem_fn(_Tp _Class::* __pm)
801 return _Mem_fn<_Tp _Class::*>(__pm);
805 * @brief Determines if the given type _Tp is a function object
806 * should be treated as a subexpression when evaluating calls to
807 * function objects returned by bind(). [TR1 3.6.1]
809 template<typename _Tp>
810 struct is_bind_expression
811 { static const bool value = false; };
813 template<typename _Tp>
814 const bool is_bind_expression<_Tp>::value;
817 * @brief Determines if the given type _Tp is a placeholder in a
818 * bind() expression and, if so, which placeholder it is. [TR1 3.6.2]
820 template<typename _Tp>
821 struct is_placeholder
822 { static const int value = 0; };
824 template<typename _Tp>
825 const int is_placeholder<_Tp>::value;
827 /// The type of placeholder objects defined by libstdc++.
828 template<int _Num> struct _Placeholder { };
830 /** @namespace std::placeholders
831 * @brief ISO C++ 0x entities sub namespace for functional.
833 * Define a large number of placeholders. There is no way to
834 * simplify this with variadic templates, because we're introducing
835 * unique names for each.
837 namespace placeholders
850 _Placeholder<10> _10;
851 _Placeholder<11> _11;
852 _Placeholder<12> _12;
853 _Placeholder<13> _13;
854 _Placeholder<14> _14;
855 _Placeholder<15> _15;
856 _Placeholder<16> _16;
857 _Placeholder<17> _17;
858 _Placeholder<18> _18;
859 _Placeholder<19> _19;
860 _Placeholder<20> _20;
861 _Placeholder<21> _21;
862 _Placeholder<22> _22;
863 _Placeholder<23> _23;
864 _Placeholder<24> _24;
865 _Placeholder<25> _25;
866 _Placeholder<26> _26;
867 _Placeholder<27> _27;
868 _Placeholder<28> _28;
869 _Placeholder<29> _29;
874 * Partial specialization of is_placeholder that provides the placeholder
875 * number for the placeholder objects defined by libstdc++.
878 struct is_placeholder<_Placeholder<_Num> >
879 { static const int value = _Num; };
882 const int is_placeholder<_Placeholder<_Num> >::value;
885 * Stores a tuple of indices. Used by bind() to extract the elements
888 template<int... _Indexes>
889 struct _Index_tuple { };
891 /// Builds an _Index_tuple<0, 1, 2, ..., _Num-1>.
892 template<std::size_t _Num, typename _Tuple = _Index_tuple<> >
893 struct _Build_index_tuple;
895 template<std::size_t _Num, int... _Indexes>
896 struct _Build_index_tuple<_Num, _Index_tuple<_Indexes...> >
897 : _Build_index_tuple<_Num - 1,
898 _Index_tuple<_Indexes..., sizeof...(_Indexes)> >
902 template<int... _Indexes>
903 struct _Build_index_tuple<0, _Index_tuple<_Indexes...> >
905 typedef _Index_tuple<_Indexes...> __type;
909 * Used by _Safe_tuple_element to indicate that there is no tuple
910 * element at this position.
912 struct _No_tuple_element;
915 * Implementation helper for _Safe_tuple_element. This primary
916 * template handles the case where it is safe to use @c
919 template<int __i, typename _Tuple, bool _IsSafe>
920 struct _Safe_tuple_element_impl
921 : tuple_element<__i, _Tuple> { };
924 * Implementation helper for _Safe_tuple_element. This partial
925 * specialization handles the case where it is not safe to use @c
926 * tuple_element. We just return @c _No_tuple_element.
928 template<int __i, typename _Tuple>
929 struct _Safe_tuple_element_impl<__i, _Tuple, false>
931 typedef _No_tuple_element type;
935 * Like tuple_element, but returns @c _No_tuple_element when
936 * tuple_element would return an error.
938 template<int __i, typename _Tuple>
939 struct _Safe_tuple_element
940 : _Safe_tuple_element_impl<__i, _Tuple,
941 (__i >= 0 && __i < tuple_size<_Tuple>::value)>
946 * Maps an argument to bind() into an actual argument to the bound
947 * function object [TR1 3.6.3/5]. Only the first parameter should
948 * be specified: the rest are used to determine among the various
949 * implementations. Note that, although this class is a function
950 * object, it isn't entirely normal because it takes only two
951 * parameters regardless of the number of parameters passed to the
952 * bind expression. The first parameter is the bound argument and
953 * the second parameter is a tuple containing references to the
954 * rest of the arguments.
956 template<typename _Arg,
957 bool _IsBindExp = is_bind_expression<_Arg>::value,
958 bool _IsPlaceholder = (is_placeholder<_Arg>::value > 0)>
962 * If the argument is reference_wrapper<_Tp>, returns the
963 * underlying reference. [TR1 3.6.3/5 bullet 1]
965 template<typename _Tp>
966 class _Mu<reference_wrapper<_Tp>, false, false>
969 typedef _Tp& result_type;
971 /* Note: This won't actually work for const volatile
972 * reference_wrappers, because reference_wrapper::get() is const
973 * but not volatile-qualified. This might be a defect in the TR.
975 template<typename _CVRef, typename _Tuple>
977 operator()(_CVRef& __arg, const _Tuple&) const volatile
978 { return __arg.get(); }
982 * If the argument is a bind expression, we invoke the underlying
983 * function object with the same cv-qualifiers as we are given and
984 * pass along all of our arguments (unwrapped). [TR1 3.6.3/5 bullet 2]
986 template<typename _Arg>
987 class _Mu<_Arg, true, false>
990 template<typename _Signature> class result;
992 // Determine the result type when we pass the arguments along. This
993 // involves passing along the cv-qualifiers placed on _Mu and
994 // unwrapping the argument bundle.
995 template<typename _CVMu, typename _CVArg, typename... _Args>
996 class result<_CVMu(_CVArg, tuple<_Args...>)>
997 : public result_of<_CVArg(_Args...)> { };
999 template<typename _CVArg, typename... _Args>
1000 typename result_of<_CVArg(_Args...)>::type
1001 operator()(_CVArg& __arg,
1002 const tuple<_Args...>& __tuple) const volatile
1004 // Construct an index tuple and forward to __call
1005 typedef typename _Build_index_tuple<sizeof...(_Args)>::__type
1007 return this->__call(__arg, __tuple, _Indexes());
1011 // Invokes the underlying function object __arg by unpacking all
1012 // of the arguments in the tuple.
1013 template<typename _CVArg, typename... _Args, int... _Indexes>
1014 typename result_of<_CVArg(_Args...)>::type
1015 __call(_CVArg& __arg, const tuple<_Args...>& __tuple,
1016 const _Index_tuple<_Indexes...>&) const volatile
1018 return __arg(_GLIBCXX_TR1 get<_Indexes>(__tuple)...);
1023 * If the argument is a placeholder for the Nth argument, returns
1024 * a reference to the Nth argument to the bind function object.
1025 * [TR1 3.6.3/5 bullet 3]
1027 template<typename _Arg>
1028 class _Mu<_Arg, false, true>
1031 template<typename _Signature> class result;
1033 template<typename _CVMu, typename _CVArg, typename _Tuple>
1034 class result<_CVMu(_CVArg, _Tuple)>
1036 // Add a reference, if it hasn't already been done for us.
1037 // This allows us to be a little bit sloppy in constructing
1038 // the tuple that we pass to result_of<...>.
1039 typedef typename _Safe_tuple_element<(is_placeholder<_Arg>::value
1044 #ifdef _GLIBCXX_INCLUDE_AS_CXX0X
1045 typedef typename add_lvalue_reference<__base_type>::type type;
1047 typedef typename add_reference<__base_type>::type type;
1051 template<typename _Tuple>
1052 typename result<_Mu(_Arg, _Tuple)>::type
1053 operator()(const volatile _Arg&, const _Tuple& __tuple) const volatile
1055 return ::std::_GLIBCXX_TR1 get<(is_placeholder<_Arg>::value
1061 * If the argument is just a value, returns a reference to that
1062 * value. The cv-qualifiers on the reference are the same as the
1063 * cv-qualifiers on the _Mu object. [TR1 3.6.3/5 bullet 4]
1065 template<typename _Arg>
1066 class _Mu<_Arg, false, false>
1069 template<typename _Signature> struct result;
1071 template<typename _CVMu, typename _CVArg, typename _Tuple>
1072 struct result<_CVMu(_CVArg, _Tuple)>
1074 #ifdef _GLIBCXX_INCLUDE_AS_CXX0X
1075 typedef typename add_lvalue_reference<_CVArg>::type type;
1077 typedef typename add_reference<_CVArg>::type type;
1081 // Pick up the cv-qualifiers of the argument
1082 template<typename _CVArg, typename _Tuple>
1084 operator()(_CVArg& __arg, const _Tuple&) const volatile
1089 * Maps member pointers into instances of _Mem_fn but leaves all
1090 * other function objects untouched. Used by tr1::bind(). The
1091 * primary template handles the non--member-pointer case.
1093 template<typename _Tp>
1094 struct _Maybe_wrap_member_pointer
1099 __do_wrap(const _Tp& __x)
1104 * Maps member pointers into instances of _Mem_fn but leaves all
1105 * other function objects untouched. Used by tr1::bind(). This
1106 * partial specialization handles the member pointer case.
1108 template<typename _Tp, typename _Class>
1109 struct _Maybe_wrap_member_pointer<_Tp _Class::*>
1111 typedef _Mem_fn<_Tp _Class::*> type;
1114 __do_wrap(_Tp _Class::* __pm)
1115 { return type(__pm); }
1118 /// Type of the function object returned from bind().
1119 template<typename _Signature>
1122 template<typename _Functor, typename... _Bound_args>
1123 class _Bind<_Functor(_Bound_args...)>
1124 : public _Weak_result_type<_Functor>
1126 typedef _Bind __self_type;
1127 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
1131 tuple<_Bound_args...> _M_bound_args;
1134 template<typename... _Args, int... _Indexes>
1136 _Functor(typename result_of<_Mu<_Bound_args>
1137 (_Bound_args, tuple<_Args...>)>::type...)
1139 __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>)
1141 return _M_f(_Mu<_Bound_args>()
1142 (_GLIBCXX_TR1 get<_Indexes>(_M_bound_args), __args)...);
1146 template<typename... _Args, int... _Indexes>
1148 const _Functor(typename result_of<_Mu<_Bound_args>
1149 (const _Bound_args, tuple<_Args...>)
1151 __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>) const
1153 return _M_f(_Mu<_Bound_args>()
1154 (_GLIBCXX_TR1 get<_Indexes>(_M_bound_args), __args)...);
1158 template<typename... _Args, int... _Indexes>
1160 volatile _Functor(typename result_of<_Mu<_Bound_args>
1161 (volatile _Bound_args, tuple<_Args...>)
1163 __call(const tuple<_Args...>& __args,
1164 _Index_tuple<_Indexes...>) volatile
1166 return _M_f(_Mu<_Bound_args>()
1167 (_GLIBCXX_TR1 get<_Indexes>(_M_bound_args), __args)...);
1170 // Call as const volatile
1171 template<typename... _Args, int... _Indexes>
1173 const volatile _Functor(typename result_of<_Mu<_Bound_args>
1174 (const volatile _Bound_args,
1177 __call(const tuple<_Args...>& __args,
1178 _Index_tuple<_Indexes...>) const volatile
1180 return _M_f(_Mu<_Bound_args>()
1181 (_GLIBCXX_TR1 get<_Indexes>(_M_bound_args), __args)...);
1185 explicit _Bind(_Functor __f, _Bound_args... __bound_args)
1186 : _M_f(__f), _M_bound_args(__bound_args...) { }
1189 template<typename... _Args>
1191 _Functor(typename result_of<_Mu<_Bound_args>
1192 (_Bound_args, tuple<_Args...>)>::type...)
1194 operator()(_Args&... __args)
1196 return this->__call(_GLIBCXX_TR1 tie(__args...), _Bound_indexes());
1200 template<typename... _Args>
1202 const _Functor(typename result_of<_Mu<_Bound_args>
1203 (const _Bound_args, tuple<_Args...>)>::type...)
1205 operator()(_Args&... __args) const
1207 return this->__call(_GLIBCXX_TR1 tie(__args...), _Bound_indexes());
1212 template<typename... _Args>
1214 volatile _Functor(typename result_of<_Mu<_Bound_args>
1215 (volatile _Bound_args, tuple<_Args...>)>::type...)
1217 operator()(_Args&... __args) volatile
1219 return this->__call(_GLIBCXX_TR1 tie(__args...), _Bound_indexes());
1223 // Call as const volatile
1224 template<typename... _Args>
1226 const volatile _Functor(typename result_of<_Mu<_Bound_args>
1227 (const volatile _Bound_args,
1228 tuple<_Args...>)>::type...)
1230 operator()(_Args&... __args) const volatile
1232 return this->__call(_GLIBCXX_TR1 tie(__args...), _Bound_indexes());
1236 /// Type of the function object returned from bind<R>().
1237 template<typename _Result, typename _Signature>
1238 struct _Bind_result;
1240 template<typename _Result, typename _Functor, typename... _Bound_args>
1241 class _Bind_result<_Result, _Functor(_Bound_args...)>
1243 typedef _Bind_result __self_type;
1244 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
1248 tuple<_Bound_args...> _M_bound_args;
1251 template<typename... _Args, int... _Indexes>
1253 __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>)
1255 return _M_f(_Mu<_Bound_args>()
1256 (_GLIBCXX_TR1 get<_Indexes>(_M_bound_args), __args)...);
1260 template<typename... _Args, int... _Indexes>
1262 __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>) const
1264 return _M_f(_Mu<_Bound_args>()
1265 (_GLIBCXX_TR1 get<_Indexes>(_M_bound_args), __args)...);
1269 template<typename... _Args, int... _Indexes>
1271 __call(const tuple<_Args...>& __args,
1272 _Index_tuple<_Indexes...>) volatile
1274 return _M_f(_Mu<_Bound_args>()
1275 (_GLIBCXX_TR1 get<_Indexes>(_M_bound_args), __args)...);
1278 // Call as const volatile
1279 template<typename... _Args, int... _Indexes>
1281 __call(const tuple<_Args...>& __args,
1282 _Index_tuple<_Indexes...>) const volatile
1284 return _M_f(_Mu<_Bound_args>()
1285 (_GLIBCXX_TR1 get<_Indexes>(_M_bound_args), __args)...);
1289 typedef _Result result_type;
1292 _Bind_result(_Functor __f, _Bound_args... __bound_args)
1293 : _M_f(__f), _M_bound_args(__bound_args...) { }
1296 template<typename... _Args>
1298 operator()(_Args&... __args)
1300 return this->__call(_GLIBCXX_TR1 tie(__args...), _Bound_indexes());
1304 template<typename... _Args>
1306 operator()(_Args&... __args) const
1308 return this->__call(_GLIBCXX_TR1 tie(__args...), _Bound_indexes());
1312 template<typename... _Args>
1314 operator()(_Args&... __args) volatile
1316 return this->__call(_GLIBCXX_TR1 tie(__args...), _Bound_indexes());
1319 // Call as const volatile
1320 template<typename... _Args>
1322 operator()(_Args&... __args) const volatile
1324 return this->__call(_GLIBCXX_TR1 tie(__args...), _Bound_indexes());
1328 /// Class template _Bind is always a bind expression.
1329 template<typename _Signature>
1330 struct is_bind_expression<_Bind<_Signature> >
1331 { static const bool value = true; };
1333 template<typename _Signature>
1334 const bool is_bind_expression<_Bind<_Signature> >::value;
1336 /// Class template _Bind_result is always a bind expression.
1337 template<typename _Result, typename _Signature>
1338 struct is_bind_expression<_Bind_result<_Result, _Signature> >
1339 { static const bool value = true; };
1341 template<typename _Result, typename _Signature>
1342 const bool is_bind_expression<_Bind_result<_Result, _Signature> >::value;
1345 template<typename _Functor, typename... _ArgTypes>
1347 _Bind<typename _Maybe_wrap_member_pointer<_Functor>::type(_ArgTypes...)>
1348 bind(_Functor __f, _ArgTypes... __args)
1350 typedef _Maybe_wrap_member_pointer<_Functor> __maybe_type;
1351 typedef typename __maybe_type::type __functor_type;
1352 typedef _Bind<__functor_type(_ArgTypes...)> __result_type;
1353 return __result_type(__maybe_type::__do_wrap(__f), __args...);
1356 template<typename _Result, typename _Functor, typename... _ArgTypes>
1358 _Bind_result<_Result,
1359 typename _Maybe_wrap_member_pointer<_Functor>::type
1361 bind(_Functor __f, _ArgTypes... __args)
1363 typedef _Maybe_wrap_member_pointer<_Functor> __maybe_type;
1364 typedef typename __maybe_type::type __functor_type;
1365 typedef _Bind_result<_Result, __functor_type(_ArgTypes...)>
1367 return __result_type(__maybe_type::__do_wrap(__f), __args...);
1371 * @brief Exception class thrown when class template function's
1372 * operator() is called with an empty target.
1375 class bad_function_call : public std::exception { };
1378 * The integral constant expression 0 can be converted into a
1379 * pointer to this type. It is used by the function template to
1380 * accept NULL pointers.
1382 struct _M_clear_type;
1385 * Trait identifying "location-invariant" types, meaning that the
1386 * address of the object (or any of its members) will not escape.
1387 * Also implies a trivial copy constructor and assignment operator.
1389 template<typename _Tp>
1390 struct __is_location_invariant
1391 : integral_constant<bool,
1392 (is_pointer<_Tp>::value
1393 || is_member_pointer<_Tp>::value)>
1397 class _Undefined_class;
1402 const void* _M_const_object;
1403 void (*_M_function_pointer)();
1404 void (_Undefined_class::*_M_member_pointer)();
1409 void* _M_access() { return &_M_pod_data[0]; }
1410 const void* _M_access() const { return &_M_pod_data[0]; }
1412 template<typename _Tp>
1415 { return *static_cast<_Tp*>(_M_access()); }
1417 template<typename _Tp>
1420 { return *static_cast<const _Tp*>(_M_access()); }
1422 _Nocopy_types _M_unused;
1423 char _M_pod_data[sizeof(_Nocopy_types)];
1426 enum _Manager_operation
1434 // Simple type wrapper that helps avoid annoying const problems
1435 // when casting between void pointers and pointers-to-pointers.
1436 template<typename _Tp>
1437 struct _Simple_type_wrapper
1439 _Simple_type_wrapper(_Tp __value) : __value(__value) { }
1444 template<typename _Tp>
1445 struct __is_location_invariant<_Simple_type_wrapper<_Tp> >
1446 : __is_location_invariant<_Tp>
1450 // Converts a reference to a function object into a callable
1452 template<typename _Functor>
1454 __callable_functor(_Functor& __f)
1457 template<typename _Member, typename _Class>
1458 inline _Mem_fn<_Member _Class::*>
1459 __callable_functor(_Member _Class::* &__p)
1460 { return mem_fn(__p); }
1462 template<typename _Member, typename _Class>
1463 inline _Mem_fn<_Member _Class::*>
1464 __callable_functor(_Member _Class::* const &__p)
1465 { return mem_fn(__p); }
1467 template<typename _Signature>
1470 /// Base class of all polymorphic function object wrappers.
1471 class _Function_base
1474 static const std::size_t _M_max_size = sizeof(_Nocopy_types);
1475 static const std::size_t _M_max_align = __alignof__(_Nocopy_types);
1477 template<typename _Functor>
1481 static const bool __stored_locally =
1482 (__is_location_invariant<_Functor>::value
1483 && sizeof(_Functor) <= _M_max_size
1484 && __alignof__(_Functor) <= _M_max_align
1485 && (_M_max_align % __alignof__(_Functor) == 0));
1487 typedef integral_constant<bool, __stored_locally> _Local_storage;
1489 // Retrieve a pointer to the function object
1491 _M_get_pointer(const _Any_data& __source)
1493 const _Functor* __ptr =
1494 __stored_locally? &__source._M_access<_Functor>()
1495 /* have stored a pointer */ : __source._M_access<_Functor*>();
1496 return const_cast<_Functor*>(__ptr);
1499 // Clone a location-invariant function object that fits within
1500 // an _Any_data structure.
1502 _M_clone(_Any_data& __dest, const _Any_data& __source, true_type)
1504 new (__dest._M_access()) _Functor(__source._M_access<_Functor>());
1507 // Clone a function object that is not location-invariant or
1508 // that cannot fit into an _Any_data structure.
1510 _M_clone(_Any_data& __dest, const _Any_data& __source, false_type)
1512 __dest._M_access<_Functor*>() =
1513 new _Functor(*__source._M_access<_Functor*>());
1516 // Destroying a location-invariant object may still require
1519 _M_destroy(_Any_data& __victim, true_type)
1521 __victim._M_access<_Functor>().~_Functor();
1524 // Destroying an object located on the heap.
1526 _M_destroy(_Any_data& __victim, false_type)
1528 delete __victim._M_access<_Functor*>();
1533 _M_manager(_Any_data& __dest, const _Any_data& __source,
1534 _Manager_operation __op)
1539 case __get_type_info:
1540 __dest._M_access<const type_info*>() = &typeid(_Functor);
1543 case __get_functor_ptr:
1544 __dest._M_access<_Functor*>() = _M_get_pointer(__source);
1547 case __clone_functor:
1548 _M_clone(__dest, __source, _Local_storage());
1551 case __destroy_functor:
1552 _M_destroy(__dest, _Local_storage());
1559 _M_init_functor(_Any_data& __functor, const _Functor& __f)
1560 { _M_init_functor(__functor, __f, _Local_storage()); }
1562 template<typename _Signature>
1564 _M_not_empty_function(const function<_Signature>& __f)
1567 template<typename _Tp>
1569 _M_not_empty_function(const _Tp*& __fp)
1572 template<typename _Class, typename _Tp>
1574 _M_not_empty_function(_Tp _Class::* const& __mp)
1577 template<typename _Tp>
1579 _M_not_empty_function(const _Tp&)
1584 _M_init_functor(_Any_data& __functor, const _Functor& __f, true_type)
1585 { new (__functor._M_access()) _Functor(__f); }
1588 _M_init_functor(_Any_data& __functor, const _Functor& __f, false_type)
1589 { __functor._M_access<_Functor*>() = new _Functor(__f); }
1592 template<typename _Functor>
1593 class _Ref_manager : public _Base_manager<_Functor*>
1595 typedef _Function_base::_Base_manager<_Functor*> _Base;
1599 _M_manager(_Any_data& __dest, const _Any_data& __source,
1600 _Manager_operation __op)
1605 case __get_type_info:
1606 __dest._M_access<const type_info*>() = &typeid(_Functor);
1609 case __get_functor_ptr:
1610 __dest._M_access<_Functor*>() = *_Base::_M_get_pointer(__source);
1611 return is_const<_Functor>::value;
1615 _Base::_M_manager(__dest, __source, __op);
1621 _M_init_functor(_Any_data& __functor, reference_wrapper<_Functor> __f)
1623 // TBD: Use address_of function instead.
1624 _Base::_M_init_functor(__functor, &__f.get());
1628 _Function_base() : _M_manager(0) { }
1633 _M_manager(_M_functor, _M_functor, __destroy_functor);
1637 bool _M_empty() const { return !_M_manager; }
1639 typedef bool (*_Manager_type)(_Any_data&, const _Any_data&,
1640 _Manager_operation);
1642 _Any_data _M_functor;
1643 _Manager_type _M_manager;
1646 template<typename _Signature, typename _Functor>
1647 class _Function_handler;
1649 template<typename _Res, typename _Functor, typename... _ArgTypes>
1650 class _Function_handler<_Res(_ArgTypes...), _Functor>
1651 : public _Function_base::_Base_manager<_Functor>
1653 typedef _Function_base::_Base_manager<_Functor> _Base;
1657 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1659 return (*_Base::_M_get_pointer(__functor))(__args...);
1663 template<typename _Functor, typename... _ArgTypes>
1664 class _Function_handler<void(_ArgTypes...), _Functor>
1665 : public _Function_base::_Base_manager<_Functor>
1667 typedef _Function_base::_Base_manager<_Functor> _Base;
1671 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1673 (*_Base::_M_get_pointer(__functor))(__args...);
1677 template<typename _Res, typename _Functor, typename... _ArgTypes>
1678 class _Function_handler<_Res(_ArgTypes...), reference_wrapper<_Functor> >
1679 : public _Function_base::_Ref_manager<_Functor>
1681 typedef _Function_base::_Ref_manager<_Functor> _Base;
1685 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1688 __callable_functor(**_Base::_M_get_pointer(__functor))(__args...);
1692 template<typename _Functor, typename... _ArgTypes>
1693 class _Function_handler<void(_ArgTypes...), reference_wrapper<_Functor> >
1694 : public _Function_base::_Ref_manager<_Functor>
1696 typedef _Function_base::_Ref_manager<_Functor> _Base;
1700 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1702 __callable_functor(**_Base::_M_get_pointer(__functor))(__args...);
1706 template<typename _Class, typename _Member, typename _Res,
1707 typename... _ArgTypes>
1708 class _Function_handler<_Res(_ArgTypes...), _Member _Class::*>
1709 : public _Function_handler<void(_ArgTypes...), _Member _Class::*>
1711 typedef _Function_handler<void(_ArgTypes...), _Member _Class::*>
1716 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1719 mem_fn(_Base::_M_get_pointer(__functor)->__value)(__args...);
1723 template<typename _Class, typename _Member, typename... _ArgTypes>
1724 class _Function_handler<void(_ArgTypes...), _Member _Class::*>
1725 : public _Function_base::_Base_manager<
1726 _Simple_type_wrapper< _Member _Class::* > >
1728 typedef _Member _Class::* _Functor;
1729 typedef _Simple_type_wrapper<_Functor> _Wrapper;
1730 typedef _Function_base::_Base_manager<_Wrapper> _Base;
1734 _M_manager(_Any_data& __dest, const _Any_data& __source,
1735 _Manager_operation __op)
1740 case __get_type_info:
1741 __dest._M_access<const type_info*>() = &typeid(_Functor);
1744 case __get_functor_ptr:
1745 __dest._M_access<_Functor*>() =
1746 &_Base::_M_get_pointer(__source)->__value;
1750 _Base::_M_manager(__dest, __source, __op);
1756 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1759 mem_fn(_Base::_M_get_pointer(__functor)->__value)(__args...);
1764 template<typename _Res, typename... _ArgTypes>
1765 class function<_Res(_ArgTypes...)>
1766 : public _Maybe_unary_or_binary_function<_Res, _ArgTypes...>,
1767 private _Function_base
1769 /// This class is used to implement the safe_bool idiom.
1772 _Hidden_type* _M_bool;
1775 /// This typedef is used to implement the safe_bool idiom.
1776 typedef _Hidden_type* _Hidden_type::* _Safe_bool;
1778 typedef _Res _Signature_type(_ArgTypes...);
1780 struct _Useless { };
1783 typedef _Res result_type;
1785 // [3.7.2.1] construct/copy/destroy
1788 * @brief Default construct creates an empty function call wrapper.
1789 * @post @c !(bool)*this
1791 function() : _Function_base() { }
1794 * @brief Default construct creates an empty function call wrapper.
1795 * @post @c !(bool)*this
1797 function(_M_clear_type*) : _Function_base() { }
1800 * @brief %Function copy constructor.
1801 * @param x A %function object with identical call signature.
1802 * @pre @c (bool)*this == (bool)x
1804 * The newly-created %function contains a copy of the target of @a
1805 * x (if it has one).
1807 function(const function& __x);
1810 * @brief Builds a %function that targets a copy of the incoming
1812 * @param f A %function object that is callable with parameters of
1813 * type @c T1, @c T2, ..., @c TN and returns a value convertible
1816 * The newly-created %function object will target a copy of @a
1817 * f. If @a f is @c reference_wrapper<F>, then this function
1818 * object will contain a reference to the function object @c
1819 * f.get(). If @a f is a NULL function pointer or NULL
1820 * pointer-to-member, the newly-created object will be empty.
1822 * If @a f is a non-NULL function pointer or an object of type @c
1823 * reference_wrapper<F>, this function will not throw.
1825 template<typename _Functor>
1826 function(_Functor __f,
1827 typename __gnu_cxx::__enable_if<
1828 !is_integral<_Functor>::value, _Useless>::__type
1832 * @brief %Function assignment operator.
1833 * @param x A %function with identical call signature.
1834 * @post @c (bool)*this == (bool)x
1837 * The target of @a x is copied to @c *this. If @a x has no
1838 * target, then @c *this will be empty.
1840 * If @a x targets a function pointer or a reference to a function
1841 * object, then this operation will not throw an exception.
1844 operator=(const function& __x)
1846 function(__x).swap(*this);
1851 * @brief %Function assignment to zero.
1852 * @post @c !(bool)*this
1855 * The target of @a *this is deallocated, leaving it empty.
1858 operator=(_M_clear_type*)
1862 _M_manager(_M_functor, _M_functor, __destroy_functor);
1870 * @brief %Function assignment to a new target.
1871 * @param f A %function object that is callable with parameters of
1872 * type @c T1, @c T2, ..., @c TN and returns a value convertible
1876 * This %function object wrapper will target a copy of @a
1877 * f. If @a f is @c reference_wrapper<F>, then this function
1878 * object will contain a reference to the function object @c
1879 * f.get(). If @a f is a NULL function pointer or NULL
1880 * pointer-to-member, @c this object will be empty.
1882 * If @a f is a non-NULL function pointer or an object of type @c
1883 * reference_wrapper<F>, this function will not throw.
1885 template<typename _Functor>
1886 typename __gnu_cxx::__enable_if<!is_integral<_Functor>::value,
1888 operator=(_Functor __f)
1890 function(__f).swap(*this);
1894 // [3.7.2.2] function modifiers
1897 * @brief Swap the targets of two %function objects.
1898 * @param f A %function with identical call signature.
1900 * Swap the targets of @c this function object and @a f. This
1901 * function will not throw an exception.
1903 void swap(function& __x)
1905 _Any_data __old_functor = _M_functor;
1906 _M_functor = __x._M_functor;
1907 __x._M_functor = __old_functor;
1908 _Manager_type __old_manager = _M_manager;
1909 _M_manager = __x._M_manager;
1910 __x._M_manager = __old_manager;
1911 _Invoker_type __old_invoker = _M_invoker;
1912 _M_invoker = __x._M_invoker;
1913 __x._M_invoker = __old_invoker;
1916 // [3.7.2.3] function capacity
1919 * @brief Determine if the %function wrapper has a target.
1921 * @return @c true when this %function object contains a target,
1922 * or @c false when it is empty.
1924 * This function will not throw an exception.
1926 operator _Safe_bool() const
1931 return &_Hidden_type::_M_bool;
1934 // [3.7.2.4] function invocation
1937 * @brief Invokes the function targeted by @c *this.
1938 * @returns the result of the target.
1939 * @throws bad_function_call when @c !(bool)*this
1941 * The function call operator invokes the target function object
1942 * stored by @c this.
1944 _Res operator()(_ArgTypes... __args) const;
1947 // [3.7.2.5] function target access
1949 * @brief Determine the type of the target of this function object
1952 * @returns the type identifier of the target function object, or
1953 * @c typeid(void) if @c !(bool)*this.
1955 * This function will not throw an exception.
1957 const type_info& target_type() const;
1960 * @brief Access the stored target function object.
1962 * @return Returns a pointer to the stored target function object,
1963 * if @c typeid(Functor).equals(target_type()); otherwise, a NULL
1966 * This function will not throw an exception.
1968 template<typename _Functor> _Functor* target();
1971 template<typename _Functor> const _Functor* target() const;
1975 // [3.7.2.6] undefined operators
1976 template<typename _Function>
1977 void operator==(const function<_Function>&) const;
1978 template<typename _Function>
1979 void operator!=(const function<_Function>&) const;
1981 typedef _Res (*_Invoker_type)(const _Any_data&, _ArgTypes...);
1982 _Invoker_type _M_invoker;
1985 template<typename _Res, typename... _ArgTypes>
1986 function<_Res(_ArgTypes...)>::
1987 function(const function& __x)
1992 _M_invoker = __x._M_invoker;
1993 _M_manager = __x._M_manager;
1994 __x._M_manager(_M_functor, __x._M_functor, __clone_functor);
1998 template<typename _Res, typename... _ArgTypes>
1999 template<typename _Functor>
2000 function<_Res(_ArgTypes...)>::
2001 function(_Functor __f,
2002 typename __gnu_cxx::__enable_if<
2003 !is_integral<_Functor>::value, _Useless>::__type)
2006 typedef _Function_handler<_Signature_type, _Functor> _My_handler;
2008 if (_My_handler::_M_not_empty_function(__f))
2010 _M_invoker = &_My_handler::_M_invoke;
2011 _M_manager = &_My_handler::_M_manager;
2012 _My_handler::_M_init_functor(_M_functor, __f);
2016 template<typename _Res, typename... _ArgTypes>
2018 function<_Res(_ArgTypes...)>::
2019 operator()(_ArgTypes... __args) const
2024 throw bad_function_call();
2029 return _M_invoker(_M_functor, __args...);
2033 template<typename _Res, typename... _ArgTypes>
2035 function<_Res(_ArgTypes...)>::
2040 _Any_data __typeinfo_result;
2041 _M_manager(__typeinfo_result, _M_functor, __get_type_info);
2042 return *__typeinfo_result._M_access<const type_info*>();
2045 return typeid(void);
2048 template<typename _Res, typename... _ArgTypes>
2049 template<typename _Functor>
2051 function<_Res(_ArgTypes...)>::
2054 if (typeid(_Functor) == target_type() && _M_manager)
2057 if (_M_manager(__ptr, _M_functor, __get_functor_ptr)
2058 && !is_const<_Functor>::value)
2061 return __ptr._M_access<_Functor*>();
2067 template<typename _Res, typename... _ArgTypes>
2068 template<typename _Functor>
2070 function<_Res(_ArgTypes...)>::
2073 if (typeid(_Functor) == target_type() && _M_manager)
2076 _M_manager(__ptr, _M_functor, __get_functor_ptr);
2077 return __ptr._M_access<const _Functor*>();
2084 // [3.7.2.7] null pointer comparisons
2087 * @brief Compares a polymorphic function object wrapper against 0
2088 * (the NULL pointer).
2089 * @returns @c true if the wrapper has no target, @c false otherwise
2091 * This function will not throw an exception.
2093 template<typename _Signature>
2095 operator==(const function<_Signature>& __f, _M_clear_type*)
2099 template<typename _Signature>
2101 operator==(_M_clear_type*, const function<_Signature>& __f)
2105 * @brief Compares a polymorphic function object wrapper against 0
2106 * (the NULL pointer).
2107 * @returns @c false if the wrapper has no target, @c true otherwise
2109 * This function will not throw an exception.
2111 template<typename _Signature>
2113 operator!=(const function<_Signature>& __f, _M_clear_type*)
2117 template<typename _Signature>
2119 operator!=(_M_clear_type*, const function<_Signature>& __f)
2122 // [3.7.2.8] specialized algorithms
2125 * @brief Swap the targets of two polymorphic function object wrappers.
2127 * This function will not throw an exception.
2129 template<typename _Signature>
2131 swap(function<_Signature>& __x, function<_Signature>& __y)
2134 _GLIBCXX_END_NAMESPACE_TR1