1 // TR1 functional header -*- C++ -*-
3 // Copyright (C) 2004, 2005, 2006, 2007, 2009, 2010, 2011
4 // Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 // <http://www.gnu.org/licenses/>.
26 /** @file tr1/functional
27 * This is a TR1 C++ Library header.
30 #ifndef _GLIBCXX_TR1_FUNCTIONAL
31 #define _GLIBCXX_TR1_FUNCTIONAL 1
33 #pragma GCC system_header
35 #include <bits/c++config.h>
36 #include <bits/stl_function.h>
41 #include <tr1/type_traits>
42 #include <bits/stringfwd.h>
43 #include <tr1/functional_hash.h>
44 #include <ext/type_traits.h>
45 #include <bits/move.h> // for std::__addressof
47 namespace std _GLIBCXX_VISIBILITY(default)
51 _GLIBCXX_BEGIN_NAMESPACE_VERSION
53 template<typename _MemberPointer>
57 * Actual implementation of _Has_result_type, which uses SFINAE to
58 * determine if the type _Tp has a publicly-accessible member type
61 template<typename _Tp>
62 class _Has_result_type_helper : __sfinae_types
64 template<typename _Up>
68 template<typename _Up>
69 static __one __test(_Wrap_type<typename _Up::result_type>*);
71 template<typename _Up>
72 static __two __test(...);
75 static const bool value = sizeof(__test<_Tp>(0)) == 1;
78 template<typename _Tp>
79 struct _Has_result_type
80 : integral_constant<bool,
81 _Has_result_type_helper<typename remove_cv<_Tp>::type>::value>
87 /// If we have found a result_type, extract it.
88 template<bool _Has_result_type, typename _Functor>
89 struct _Maybe_get_result_type
92 template<typename _Functor>
93 struct _Maybe_get_result_type<true, _Functor>
95 typedef typename _Functor::result_type result_type;
99 * Base class for any function object that has a weak result type, as
100 * defined in 3.3/3 of TR1.
102 template<typename _Functor>
103 struct _Weak_result_type_impl
104 : _Maybe_get_result_type<_Has_result_type<_Functor>::value, _Functor>
108 /// Retrieve the result type for a function type.
109 template<typename _Res, typename... _ArgTypes>
110 struct _Weak_result_type_impl<_Res(_ArgTypes...)>
112 typedef _Res result_type;
115 /// Retrieve the result type for a function reference.
116 template<typename _Res, typename... _ArgTypes>
117 struct _Weak_result_type_impl<_Res(&)(_ArgTypes...)>
119 typedef _Res result_type;
122 /// Retrieve the result type for a function pointer.
123 template<typename _Res, typename... _ArgTypes>
124 struct _Weak_result_type_impl<_Res(*)(_ArgTypes...)>
126 typedef _Res result_type;
129 /// Retrieve result type for a member function pointer.
130 template<typename _Res, typename _Class, typename... _ArgTypes>
131 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...)>
133 typedef _Res result_type;
136 /// Retrieve result type for a const member function pointer.
137 template<typename _Res, typename _Class, typename... _ArgTypes>
138 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) const>
140 typedef _Res result_type;
143 /// Retrieve result type for a volatile member function pointer.
144 template<typename _Res, typename _Class, typename... _ArgTypes>
145 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) volatile>
147 typedef _Res result_type;
150 /// Retrieve result type for a const volatile member function pointer.
151 template<typename _Res, typename _Class, typename... _ArgTypes>
152 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...)const volatile>
154 typedef _Res result_type;
158 * Strip top-level cv-qualifiers from the function object and let
159 * _Weak_result_type_impl perform the real work.
161 template<typename _Functor>
162 struct _Weak_result_type
163 : _Weak_result_type_impl<typename remove_cv<_Functor>::type>
167 template<typename _Signature>
171 * Actual implementation of result_of. When _Has_result_type is
172 * true, gets its result from _Weak_result_type. Otherwise, uses
173 * the function object's member template result to extract the
176 template<bool _Has_result_type, typename _Signature>
177 struct _Result_of_impl;
179 // Handle member data pointers using _Mem_fn's logic
180 template<typename _Res, typename _Class, typename _T1>
181 struct _Result_of_impl<false, _Res _Class::*(_T1)>
183 typedef typename _Mem_fn<_Res _Class::*>
184 ::template _Result_type<_T1>::type type;
188 * Determine whether we can determine a result type from @c Functor
191 template<typename _Functor, typename... _ArgTypes>
192 class result_of<_Functor(_ArgTypes...)>
193 : public _Result_of_impl<
194 _Has_result_type<_Weak_result_type<_Functor> >::value,
195 _Functor(_ArgTypes...)>
199 /// We already know the result type for @c Functor; use it.
200 template<typename _Functor, typename... _ArgTypes>
201 struct _Result_of_impl<true, _Functor(_ArgTypes...)>
203 typedef typename _Weak_result_type<_Functor>::result_type type;
207 * We need to compute the result type for this invocation the hard
210 template<typename _Functor, typename... _ArgTypes>
211 struct _Result_of_impl<false, _Functor(_ArgTypes...)>
213 typedef typename _Functor
214 ::template result<_Functor(_ArgTypes...)>::type type;
218 * It is unsafe to access ::result when there are zero arguments, so we
219 * return @c void instead.
221 template<typename _Functor>
222 struct _Result_of_impl<false, _Functor()>
227 /// Determines if the type _Tp derives from unary_function.
228 template<typename _Tp>
229 struct _Derives_from_unary_function : __sfinae_types
232 template<typename _T1, typename _Res>
233 static __one __test(const volatile unary_function<_T1, _Res>*);
235 // It's tempting to change "..." to const volatile void*, but
236 // that fails when _Tp is a function type.
237 static __two __test(...);
240 static const bool value = sizeof(__test((_Tp*)0)) == 1;
243 /// Determines if the type _Tp derives from binary_function.
244 template<typename _Tp>
245 struct _Derives_from_binary_function : __sfinae_types
248 template<typename _T1, typename _T2, typename _Res>
249 static __one __test(const volatile binary_function<_T1, _T2, _Res>*);
251 // It's tempting to change "..." to const volatile void*, but
252 // that fails when _Tp is a function type.
253 static __two __test(...);
256 static const bool value = sizeof(__test((_Tp*)0)) == 1;
259 /// Turns a function type into a function pointer type
260 template<typename _Tp, bool _IsFunctionType = is_function<_Tp>::value>
261 struct _Function_to_function_pointer
266 template<typename _Tp>
267 struct _Function_to_function_pointer<_Tp, true>
273 * Invoke a function object, which may be either a member pointer or a
274 * function object. The first parameter will tell which.
276 template<typename _Functor, typename... _Args>
278 typename __gnu_cxx::__enable_if<
279 (!is_member_pointer<_Functor>::value
280 && !is_function<_Functor>::value
281 && !is_function<typename remove_pointer<_Functor>::type>::value),
282 typename result_of<_Functor(_Args...)>::type
284 __invoke(_Functor& __f, _Args&... __args)
286 return __f(__args...);
289 template<typename _Functor, typename... _Args>
291 typename __gnu_cxx::__enable_if<
292 (is_member_pointer<_Functor>::value
293 && !is_function<_Functor>::value
294 && !is_function<typename remove_pointer<_Functor>::type>::value),
295 typename result_of<_Functor(_Args...)>::type
297 __invoke(_Functor& __f, _Args&... __args)
299 return mem_fn(__f)(__args...);
302 // To pick up function references (that will become function pointers)
303 template<typename _Functor, typename... _Args>
305 typename __gnu_cxx::__enable_if<
306 (is_pointer<_Functor>::value
307 && is_function<typename remove_pointer<_Functor>::type>::value),
308 typename result_of<_Functor(_Args...)>::type
310 __invoke(_Functor __f, _Args&... __args)
312 return __f(__args...);
316 * Knowing which of unary_function and binary_function _Tp derives
317 * from, derives from the same and ensures that reference_wrapper
318 * will have a weak result type. See cases below.
320 template<bool _Unary, bool _Binary, typename _Tp>
321 struct _Reference_wrapper_base_impl;
323 // Not a unary_function or binary_function, so try a weak result type.
324 template<typename _Tp>
325 struct _Reference_wrapper_base_impl<false, false, _Tp>
326 : _Weak_result_type<_Tp>
329 // unary_function but not binary_function
330 template<typename _Tp>
331 struct _Reference_wrapper_base_impl<true, false, _Tp>
332 : unary_function<typename _Tp::argument_type,
333 typename _Tp::result_type>
336 // binary_function but not unary_function
337 template<typename _Tp>
338 struct _Reference_wrapper_base_impl<false, true, _Tp>
339 : binary_function<typename _Tp::first_argument_type,
340 typename _Tp::second_argument_type,
341 typename _Tp::result_type>
344 // Both unary_function and binary_function. Import result_type to
346 template<typename _Tp>
347 struct _Reference_wrapper_base_impl<true, true, _Tp>
348 : unary_function<typename _Tp::argument_type,
349 typename _Tp::result_type>,
350 binary_function<typename _Tp::first_argument_type,
351 typename _Tp::second_argument_type,
352 typename _Tp::result_type>
354 typedef typename _Tp::result_type result_type;
358 * Derives from unary_function or binary_function when it
359 * can. Specializations handle all of the easy cases. The primary
360 * template determines what to do with a class type, which may
361 * derive from both unary_function and binary_function.
363 template<typename _Tp>
364 struct _Reference_wrapper_base
365 : _Reference_wrapper_base_impl<
366 _Derives_from_unary_function<_Tp>::value,
367 _Derives_from_binary_function<_Tp>::value,
371 // - a function type (unary)
372 template<typename _Res, typename _T1>
373 struct _Reference_wrapper_base<_Res(_T1)>
374 : unary_function<_T1, _Res>
377 // - a function type (binary)
378 template<typename _Res, typename _T1, typename _T2>
379 struct _Reference_wrapper_base<_Res(_T1, _T2)>
380 : binary_function<_T1, _T2, _Res>
383 // - a function pointer type (unary)
384 template<typename _Res, typename _T1>
385 struct _Reference_wrapper_base<_Res(*)(_T1)>
386 : unary_function<_T1, _Res>
389 // - a function pointer type (binary)
390 template<typename _Res, typename _T1, typename _T2>
391 struct _Reference_wrapper_base<_Res(*)(_T1, _T2)>
392 : binary_function<_T1, _T2, _Res>
395 // - a pointer to member function type (unary, no qualifiers)
396 template<typename _Res, typename _T1>
397 struct _Reference_wrapper_base<_Res (_T1::*)()>
398 : unary_function<_T1*, _Res>
401 // - a pointer to member function type (binary, no qualifiers)
402 template<typename _Res, typename _T1, typename _T2>
403 struct _Reference_wrapper_base<_Res (_T1::*)(_T2)>
404 : binary_function<_T1*, _T2, _Res>
407 // - a pointer to member function type (unary, const)
408 template<typename _Res, typename _T1>
409 struct _Reference_wrapper_base<_Res (_T1::*)() const>
410 : unary_function<const _T1*, _Res>
413 // - a pointer to member function type (binary, const)
414 template<typename _Res, typename _T1, typename _T2>
415 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const>
416 : binary_function<const _T1*, _T2, _Res>
419 // - a pointer to member function type (unary, volatile)
420 template<typename _Res, typename _T1>
421 struct _Reference_wrapper_base<_Res (_T1::*)() volatile>
422 : unary_function<volatile _T1*, _Res>
425 // - a pointer to member function type (binary, volatile)
426 template<typename _Res, typename _T1, typename _T2>
427 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) volatile>
428 : binary_function<volatile _T1*, _T2, _Res>
431 // - a pointer to member function type (unary, const volatile)
432 template<typename _Res, typename _T1>
433 struct _Reference_wrapper_base<_Res (_T1::*)() const volatile>
434 : unary_function<const volatile _T1*, _Res>
437 // - a pointer to member function type (binary, const volatile)
438 template<typename _Res, typename _T1, typename _T2>
439 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const volatile>
440 : binary_function<const volatile _T1*, _T2, _Res>
443 /// reference_wrapper
444 template<typename _Tp>
445 class reference_wrapper
446 : public _Reference_wrapper_base<typename remove_cv<_Tp>::type>
448 // If _Tp is a function type, we can't form result_of<_Tp(...)>,
449 // so turn it into a function pointer type.
450 typedef typename _Function_to_function_pointer<_Tp>::type
458 reference_wrapper(_Tp& __indata)
459 : _M_data(std::__addressof(__indata))
462 reference_wrapper(const reference_wrapper<_Tp>& __inref):
463 _M_data(__inref._M_data)
467 operator=(const reference_wrapper<_Tp>& __inref)
469 _M_data = __inref._M_data;
473 operator _Tp&() const
474 { return this->get(); }
480 template<typename... _Args>
481 typename result_of<_M_func_type(_Args...)>::type
482 operator()(_Args&... __args) const
484 return __invoke(get(), __args...);
489 // Denotes a reference should be taken to a variable.
490 template<typename _Tp>
491 inline reference_wrapper<_Tp>
493 { return reference_wrapper<_Tp>(__t); }
495 // Denotes a const reference should be taken to a variable.
496 template<typename _Tp>
497 inline reference_wrapper<const _Tp>
499 { return reference_wrapper<const _Tp>(__t); }
501 template<typename _Tp>
502 inline reference_wrapper<_Tp>
503 ref(reference_wrapper<_Tp> __t)
504 { return ref(__t.get()); }
506 template<typename _Tp>
507 inline reference_wrapper<const _Tp>
508 cref(reference_wrapper<_Tp> __t)
509 { return cref(__t.get()); }
511 template<typename _Tp, bool>
512 struct _Mem_fn_const_or_non
514 typedef const _Tp& type;
517 template<typename _Tp>
518 struct _Mem_fn_const_or_non<_Tp, false>
524 * Derives from @c unary_function or @c binary_function, or perhaps
525 * nothing, depending on the number of arguments provided. The
526 * primary template is the basis case, which derives nothing.
528 template<typename _Res, typename... _ArgTypes>
529 struct _Maybe_unary_or_binary_function { };
531 /// Derives from @c unary_function, as appropriate.
532 template<typename _Res, typename _T1>
533 struct _Maybe_unary_or_binary_function<_Res, _T1>
534 : std::unary_function<_T1, _Res> { };
536 /// Derives from @c binary_function, as appropriate.
537 template<typename _Res, typename _T1, typename _T2>
538 struct _Maybe_unary_or_binary_function<_Res, _T1, _T2>
539 : std::binary_function<_T1, _T2, _Res> { };
541 /// Implementation of @c mem_fn for member function pointers.
542 template<typename _Res, typename _Class, typename... _ArgTypes>
543 class _Mem_fn<_Res (_Class::*)(_ArgTypes...)>
544 : public _Maybe_unary_or_binary_function<_Res, _Class*, _ArgTypes...>
546 typedef _Res (_Class::*_Functor)(_ArgTypes...);
548 template<typename _Tp>
550 _M_call(_Tp& __object, const volatile _Class *,
551 _ArgTypes... __args) const
552 { return (__object.*__pmf)(__args...); }
554 template<typename _Tp>
556 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
557 { return ((*__ptr).*__pmf)(__args...); }
560 typedef _Res result_type;
562 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
566 operator()(_Class& __object, _ArgTypes... __args) const
567 { return (__object.*__pmf)(__args...); }
571 operator()(_Class* __object, _ArgTypes... __args) const
572 { return (__object->*__pmf)(__args...); }
574 // Handle smart pointers, references and pointers to derived
575 template<typename _Tp>
577 operator()(_Tp& __object, _ArgTypes... __args) const
578 { return _M_call(__object, &__object, __args...); }
584 /// Implementation of @c mem_fn for const member function pointers.
585 template<typename _Res, typename _Class, typename... _ArgTypes>
586 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) const>
587 : public _Maybe_unary_or_binary_function<_Res, const _Class*,
590 typedef _Res (_Class::*_Functor)(_ArgTypes...) const;
592 template<typename _Tp>
594 _M_call(_Tp& __object, const volatile _Class *,
595 _ArgTypes... __args) const
596 { return (__object.*__pmf)(__args...); }
598 template<typename _Tp>
600 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
601 { return ((*__ptr).*__pmf)(__args...); }
604 typedef _Res result_type;
606 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
610 operator()(const _Class& __object, _ArgTypes... __args) const
611 { return (__object.*__pmf)(__args...); }
615 operator()(const _Class* __object, _ArgTypes... __args) const
616 { return (__object->*__pmf)(__args...); }
618 // Handle smart pointers, references and pointers to derived
619 template<typename _Tp>
620 _Res operator()(_Tp& __object, _ArgTypes... __args) const
621 { return _M_call(__object, &__object, __args...); }
627 /// Implementation of @c mem_fn for volatile member function pointers.
628 template<typename _Res, typename _Class, typename... _ArgTypes>
629 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) volatile>
630 : public _Maybe_unary_or_binary_function<_Res, volatile _Class*,
633 typedef _Res (_Class::*_Functor)(_ArgTypes...) volatile;
635 template<typename _Tp>
637 _M_call(_Tp& __object, const volatile _Class *,
638 _ArgTypes... __args) const
639 { return (__object.*__pmf)(__args...); }
641 template<typename _Tp>
643 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
644 { return ((*__ptr).*__pmf)(__args...); }
647 typedef _Res result_type;
649 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
653 operator()(volatile _Class& __object, _ArgTypes... __args) const
654 { return (__object.*__pmf)(__args...); }
658 operator()(volatile _Class* __object, _ArgTypes... __args) const
659 { return (__object->*__pmf)(__args...); }
661 // Handle smart pointers, references and pointers to derived
662 template<typename _Tp>
664 operator()(_Tp& __object, _ArgTypes... __args) const
665 { return _M_call(__object, &__object, __args...); }
671 /// Implementation of @c mem_fn for const volatile member function pointers.
672 template<typename _Res, typename _Class, typename... _ArgTypes>
673 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) const volatile>
674 : public _Maybe_unary_or_binary_function<_Res, const volatile _Class*,
677 typedef _Res (_Class::*_Functor)(_ArgTypes...) const volatile;
679 template<typename _Tp>
681 _M_call(_Tp& __object, const volatile _Class *,
682 _ArgTypes... __args) const
683 { return (__object.*__pmf)(__args...); }
685 template<typename _Tp>
687 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
688 { return ((*__ptr).*__pmf)(__args...); }
691 typedef _Res result_type;
693 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
697 operator()(const volatile _Class& __object, _ArgTypes... __args) const
698 { return (__object.*__pmf)(__args...); }
702 operator()(const volatile _Class* __object, _ArgTypes... __args) const
703 { return (__object->*__pmf)(__args...); }
705 // Handle smart pointers, references and pointers to derived
706 template<typename _Tp>
707 _Res operator()(_Tp& __object, _ArgTypes... __args) const
708 { return _M_call(__object, &__object, __args...); }
715 template<typename _Res, typename _Class>
716 class _Mem_fn<_Res _Class::*>
718 // This bit of genius is due to Peter Dimov, improved slightly by
720 template<typename _Tp>
722 _M_call(_Tp& __object, _Class *) const
723 { return __object.*__pm; }
725 template<typename _Tp, typename _Up>
727 _M_call(_Tp& __object, _Up * const *) const
728 { return (*__object).*__pm; }
730 template<typename _Tp, typename _Up>
732 _M_call(_Tp& __object, const _Up * const *) const
733 { return (*__object).*__pm; }
735 template<typename _Tp>
737 _M_call(_Tp& __object, const _Class *) const
738 { return __object.*__pm; }
740 template<typename _Tp>
742 _M_call(_Tp& __ptr, const volatile void*) const
743 { return (*__ptr).*__pm; }
745 template<typename _Tp> static _Tp& __get_ref();
747 template<typename _Tp>
748 static __sfinae_types::__one __check_const(_Tp&, _Class*);
749 template<typename _Tp, typename _Up>
750 static __sfinae_types::__one __check_const(_Tp&, _Up * const *);
751 template<typename _Tp, typename _Up>
752 static __sfinae_types::__two __check_const(_Tp&, const _Up * const *);
753 template<typename _Tp>
754 static __sfinae_types::__two __check_const(_Tp&, const _Class*);
755 template<typename _Tp>
756 static __sfinae_types::__two __check_const(_Tp&, const volatile void*);
759 template<typename _Tp>
761 : _Mem_fn_const_or_non<_Res,
762 (sizeof(__sfinae_types::__two)
763 == sizeof(__check_const<_Tp>(__get_ref<_Tp>(), (_Tp*)0)))>
766 template<typename _Signature>
769 template<typename _CVMem, typename _Tp>
770 struct result<_CVMem(_Tp)>
771 : public _Result_type<_Tp> { };
773 template<typename _CVMem, typename _Tp>
774 struct result<_CVMem(_Tp&)>
775 : public _Result_type<_Tp> { };
778 _Mem_fn(_Res _Class::*__pm) : __pm(__pm) { }
782 operator()(_Class& __object) const
783 { return __object.*__pm; }
786 operator()(const _Class& __object) const
787 { return __object.*__pm; }
791 operator()(_Class* __object) const
792 { return __object->*__pm; }
795 operator()(const _Class* __object) const
796 { return __object->*__pm; }
798 // Handle smart pointers and derived
799 template<typename _Tp>
800 typename _Result_type<_Tp>::type
801 operator()(_Tp& __unknown) const
802 { return _M_call(__unknown, &__unknown); }
809 * @brief Returns a function object that forwards to the member
812 template<typename _Tp, typename _Class>
813 inline _Mem_fn<_Tp _Class::*>
814 mem_fn(_Tp _Class::* __pm)
816 return _Mem_fn<_Tp _Class::*>(__pm);
820 * @brief Determines if the given type _Tp is a function object
821 * should be treated as a subexpression when evaluating calls to
822 * function objects returned by bind(). [TR1 3.6.1]
824 template<typename _Tp>
825 struct is_bind_expression
826 { static const bool value = false; };
828 template<typename _Tp>
829 const bool is_bind_expression<_Tp>::value;
832 * @brief Determines if the given type _Tp is a placeholder in a
833 * bind() expression and, if so, which placeholder it is. [TR1 3.6.2]
835 template<typename _Tp>
836 struct is_placeholder
837 { static const int value = 0; };
839 template<typename _Tp>
840 const int is_placeholder<_Tp>::value;
842 /// The type of placeholder objects defined by libstdc++.
843 template<int _Num> struct _Placeholder { };
845 _GLIBCXX_END_NAMESPACE_VERSION
847 /** @namespace std::placeholders
848 * @brief ISO C++ 0x entities sub namespace for functional.
850 * Define a large number of placeholders. There is no way to
851 * simplify this with variadic templates, because we're introducing
852 * unique names for each.
854 namespace placeholders
856 _GLIBCXX_BEGIN_NAMESPACE_VERSION
868 _Placeholder<10> _10;
869 _Placeholder<11> _11;
870 _Placeholder<12> _12;
871 _Placeholder<13> _13;
872 _Placeholder<14> _14;
873 _Placeholder<15> _15;
874 _Placeholder<16> _16;
875 _Placeholder<17> _17;
876 _Placeholder<18> _18;
877 _Placeholder<19> _19;
878 _Placeholder<20> _20;
879 _Placeholder<21> _21;
880 _Placeholder<22> _22;
881 _Placeholder<23> _23;
882 _Placeholder<24> _24;
883 _Placeholder<25> _25;
884 _Placeholder<26> _26;
885 _Placeholder<27> _27;
886 _Placeholder<28> _28;
887 _Placeholder<29> _29;
889 _GLIBCXX_END_NAMESPACE_VERSION
892 _GLIBCXX_BEGIN_NAMESPACE_VERSION
894 * Partial specialization of is_placeholder that provides the placeholder
895 * number for the placeholder objects defined by libstdc++.
898 struct is_placeholder<_Placeholder<_Num> >
899 { static const int value = _Num; };
902 const int is_placeholder<_Placeholder<_Num> >::value;
905 * Stores a tuple of indices. Used by bind() to extract the elements
908 template<int... _Indexes>
909 struct _Index_tuple { };
911 /// Builds an _Index_tuple<0, 1, 2, ..., _Num-1>.
912 template<std::size_t _Num, typename _Tuple = _Index_tuple<> >
913 struct _Build_index_tuple;
915 template<std::size_t _Num, int... _Indexes>
916 struct _Build_index_tuple<_Num, _Index_tuple<_Indexes...> >
917 : _Build_index_tuple<_Num - 1,
918 _Index_tuple<_Indexes..., sizeof...(_Indexes)> >
922 template<int... _Indexes>
923 struct _Build_index_tuple<0, _Index_tuple<_Indexes...> >
925 typedef _Index_tuple<_Indexes...> __type;
929 * Used by _Safe_tuple_element to indicate that there is no tuple
930 * element at this position.
932 struct _No_tuple_element;
935 * Implementation helper for _Safe_tuple_element. This primary
936 * template handles the case where it is safe to use @c
939 template<int __i, typename _Tuple, bool _IsSafe>
940 struct _Safe_tuple_element_impl
941 : tuple_element<__i, _Tuple> { };
944 * Implementation helper for _Safe_tuple_element. This partial
945 * specialization handles the case where it is not safe to use @c
946 * tuple_element. We just return @c _No_tuple_element.
948 template<int __i, typename _Tuple>
949 struct _Safe_tuple_element_impl<__i, _Tuple, false>
951 typedef _No_tuple_element type;
955 * Like tuple_element, but returns @c _No_tuple_element when
956 * tuple_element would return an error.
958 template<int __i, typename _Tuple>
959 struct _Safe_tuple_element
960 : _Safe_tuple_element_impl<__i, _Tuple,
961 (__i >= 0 && __i < tuple_size<_Tuple>::value)>
966 * Maps an argument to bind() into an actual argument to the bound
967 * function object [TR1 3.6.3/5]. Only the first parameter should
968 * be specified: the rest are used to determine among the various
969 * implementations. Note that, although this class is a function
970 * object, it isn't entirely normal because it takes only two
971 * parameters regardless of the number of parameters passed to the
972 * bind expression. The first parameter is the bound argument and
973 * the second parameter is a tuple containing references to the
974 * rest of the arguments.
976 template<typename _Arg,
977 bool _IsBindExp = is_bind_expression<_Arg>::value,
978 bool _IsPlaceholder = (is_placeholder<_Arg>::value > 0)>
982 * If the argument is reference_wrapper<_Tp>, returns the
983 * underlying reference. [TR1 3.6.3/5 bullet 1]
985 template<typename _Tp>
986 class _Mu<reference_wrapper<_Tp>, false, false>
989 typedef _Tp& result_type;
991 /* Note: This won't actually work for const volatile
992 * reference_wrappers, because reference_wrapper::get() is const
993 * but not volatile-qualified. This might be a defect in the TR.
995 template<typename _CVRef, typename _Tuple>
997 operator()(_CVRef& __arg, const _Tuple&) const volatile
998 { return __arg.get(); }
1002 * If the argument is a bind expression, we invoke the underlying
1003 * function object with the same cv-qualifiers as we are given and
1004 * pass along all of our arguments (unwrapped). [TR1 3.6.3/5 bullet 2]
1006 template<typename _Arg>
1007 class _Mu<_Arg, true, false>
1010 template<typename _Signature> class result;
1012 // Determine the result type when we pass the arguments along. This
1013 // involves passing along the cv-qualifiers placed on _Mu and
1014 // unwrapping the argument bundle.
1015 template<typename _CVMu, typename _CVArg, typename... _Args>
1016 class result<_CVMu(_CVArg, tuple<_Args...>)>
1017 : public result_of<_CVArg(_Args...)> { };
1019 template<typename _CVArg, typename... _Args>
1020 typename result_of<_CVArg(_Args...)>::type
1021 operator()(_CVArg& __arg,
1022 const tuple<_Args...>& __tuple) const volatile
1024 // Construct an index tuple and forward to __call
1025 typedef typename _Build_index_tuple<sizeof...(_Args)>::__type
1027 return this->__call(__arg, __tuple, _Indexes());
1031 // Invokes the underlying function object __arg by unpacking all
1032 // of the arguments in the tuple.
1033 template<typename _CVArg, typename... _Args, int... _Indexes>
1034 typename result_of<_CVArg(_Args...)>::type
1035 __call(_CVArg& __arg, const tuple<_Args...>& __tuple,
1036 const _Index_tuple<_Indexes...>&) const volatile
1038 return __arg(tr1::get<_Indexes>(__tuple)...);
1043 * If the argument is a placeholder for the Nth argument, returns
1044 * a reference to the Nth argument to the bind function object.
1045 * [TR1 3.6.3/5 bullet 3]
1047 template<typename _Arg>
1048 class _Mu<_Arg, false, true>
1051 template<typename _Signature> class result;
1053 template<typename _CVMu, typename _CVArg, typename _Tuple>
1054 class result<_CVMu(_CVArg, _Tuple)>
1056 // Add a reference, if it hasn't already been done for us.
1057 // This allows us to be a little bit sloppy in constructing
1058 // the tuple that we pass to result_of<...>.
1059 typedef typename _Safe_tuple_element<(is_placeholder<_Arg>::value
1064 typedef typename add_reference<__base_type>::type type;
1067 template<typename _Tuple>
1068 typename result<_Mu(_Arg, _Tuple)>::type
1069 operator()(const volatile _Arg&, const _Tuple& __tuple) const volatile
1071 return ::std::tr1::get<(is_placeholder<_Arg>::value - 1)>(__tuple);
1076 * If the argument is just a value, returns a reference to that
1077 * value. The cv-qualifiers on the reference are the same as the
1078 * cv-qualifiers on the _Mu object. [TR1 3.6.3/5 bullet 4]
1080 template<typename _Arg>
1081 class _Mu<_Arg, false, false>
1084 template<typename _Signature> struct result;
1086 template<typename _CVMu, typename _CVArg, typename _Tuple>
1087 struct result<_CVMu(_CVArg, _Tuple)>
1089 typedef typename add_reference<_CVArg>::type type;
1092 // Pick up the cv-qualifiers of the argument
1093 template<typename _CVArg, typename _Tuple>
1095 operator()(_CVArg& __arg, const _Tuple&) const volatile
1100 * Maps member pointers into instances of _Mem_fn but leaves all
1101 * other function objects untouched. Used by tr1::bind(). The
1102 * primary template handles the non--member-pointer case.
1104 template<typename _Tp>
1105 struct _Maybe_wrap_member_pointer
1110 __do_wrap(const _Tp& __x)
1115 * Maps member pointers into instances of _Mem_fn but leaves all
1116 * other function objects untouched. Used by tr1::bind(). This
1117 * partial specialization handles the member pointer case.
1119 template<typename _Tp, typename _Class>
1120 struct _Maybe_wrap_member_pointer<_Tp _Class::*>
1122 typedef _Mem_fn<_Tp _Class::*> type;
1125 __do_wrap(_Tp _Class::* __pm)
1126 { return type(__pm); }
1129 /// Type of the function object returned from bind().
1130 template<typename _Signature>
1133 template<typename _Functor, typename... _Bound_args>
1134 class _Bind<_Functor(_Bound_args...)>
1135 : public _Weak_result_type<_Functor>
1137 typedef _Bind __self_type;
1138 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
1142 tuple<_Bound_args...> _M_bound_args;
1145 template<typename... _Args, int... _Indexes>
1147 _Functor(typename result_of<_Mu<_Bound_args>
1148 (_Bound_args, tuple<_Args...>)>::type...)
1150 __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>)
1152 return _M_f(_Mu<_Bound_args>()
1153 (tr1::get<_Indexes>(_M_bound_args), __args)...);
1157 template<typename... _Args, int... _Indexes>
1159 const _Functor(typename result_of<_Mu<_Bound_args>
1160 (const _Bound_args, tuple<_Args...>)
1162 __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>) const
1164 return _M_f(_Mu<_Bound_args>()
1165 (tr1::get<_Indexes>(_M_bound_args), __args)...);
1169 template<typename... _Args, int... _Indexes>
1171 volatile _Functor(typename result_of<_Mu<_Bound_args>
1172 (volatile _Bound_args, tuple<_Args...>)
1174 __call(const tuple<_Args...>& __args,
1175 _Index_tuple<_Indexes...>) volatile
1177 return _M_f(_Mu<_Bound_args>()
1178 (tr1::get<_Indexes>(_M_bound_args), __args)...);
1181 // Call as const volatile
1182 template<typename... _Args, int... _Indexes>
1184 const volatile _Functor(typename result_of<_Mu<_Bound_args>
1185 (const volatile _Bound_args,
1188 __call(const tuple<_Args...>& __args,
1189 _Index_tuple<_Indexes...>) const volatile
1191 return _M_f(_Mu<_Bound_args>()
1192 (tr1::get<_Indexes>(_M_bound_args), __args)...);
1196 explicit _Bind(_Functor __f, _Bound_args... __bound_args)
1197 : _M_f(__f), _M_bound_args(__bound_args...) { }
1200 template<typename... _Args>
1202 _Functor(typename result_of<_Mu<_Bound_args>
1203 (_Bound_args, tuple<_Args...>)>::type...)
1205 operator()(_Args&... __args)
1207 return this->__call(tr1::tie(__args...), _Bound_indexes());
1211 template<typename... _Args>
1213 const _Functor(typename result_of<_Mu<_Bound_args>
1214 (const _Bound_args, tuple<_Args...>)>::type...)
1216 operator()(_Args&... __args) const
1218 return this->__call(tr1::tie(__args...), _Bound_indexes());
1223 template<typename... _Args>
1225 volatile _Functor(typename result_of<_Mu<_Bound_args>
1226 (volatile _Bound_args, tuple<_Args...>)>::type...)
1228 operator()(_Args&... __args) volatile
1230 return this->__call(tr1::tie(__args...), _Bound_indexes());
1234 // Call as const volatile
1235 template<typename... _Args>
1237 const volatile _Functor(typename result_of<_Mu<_Bound_args>
1238 (const volatile _Bound_args,
1239 tuple<_Args...>)>::type...)
1241 operator()(_Args&... __args) const volatile
1243 return this->__call(tr1::tie(__args...), _Bound_indexes());
1247 /// Type of the function object returned from bind<R>().
1248 template<typename _Result, typename _Signature>
1249 struct _Bind_result;
1251 template<typename _Result, typename _Functor, typename... _Bound_args>
1252 class _Bind_result<_Result, _Functor(_Bound_args...)>
1254 typedef _Bind_result __self_type;
1255 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
1259 tuple<_Bound_args...> _M_bound_args;
1262 template<typename... _Args, int... _Indexes>
1264 __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>)
1266 return _M_f(_Mu<_Bound_args>()
1267 (tr1::get<_Indexes>(_M_bound_args), __args)...);
1271 template<typename... _Args, int... _Indexes>
1273 __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>) const
1275 return _M_f(_Mu<_Bound_args>()
1276 (tr1::get<_Indexes>(_M_bound_args), __args)...);
1280 template<typename... _Args, int... _Indexes>
1282 __call(const tuple<_Args...>& __args,
1283 _Index_tuple<_Indexes...>) volatile
1285 return _M_f(_Mu<_Bound_args>()
1286 (tr1::get<_Indexes>(_M_bound_args), __args)...);
1289 // Call as const volatile
1290 template<typename... _Args, int... _Indexes>
1292 __call(const tuple<_Args...>& __args,
1293 _Index_tuple<_Indexes...>) const volatile
1295 return _M_f(_Mu<_Bound_args>()
1296 (tr1::get<_Indexes>(_M_bound_args), __args)...);
1300 typedef _Result result_type;
1303 _Bind_result(_Functor __f, _Bound_args... __bound_args)
1304 : _M_f(__f), _M_bound_args(__bound_args...) { }
1307 template<typename... _Args>
1309 operator()(_Args&... __args)
1311 return this->__call(tr1::tie(__args...), _Bound_indexes());
1315 template<typename... _Args>
1317 operator()(_Args&... __args) const
1319 return this->__call(tr1::tie(__args...), _Bound_indexes());
1323 template<typename... _Args>
1325 operator()(_Args&... __args) volatile
1327 return this->__call(tr1::tie(__args...), _Bound_indexes());
1330 // Call as const volatile
1331 template<typename... _Args>
1333 operator()(_Args&... __args) const volatile
1335 return this->__call(tr1::tie(__args...), _Bound_indexes());
1339 /// Class template _Bind is always a bind expression.
1340 template<typename _Signature>
1341 struct is_bind_expression<_Bind<_Signature> >
1342 { static const bool value = true; };
1344 template<typename _Signature>
1345 const bool is_bind_expression<_Bind<_Signature> >::value;
1347 /// Class template _Bind_result is always a bind expression.
1348 template<typename _Result, typename _Signature>
1349 struct is_bind_expression<_Bind_result<_Result, _Signature> >
1350 { static const bool value = true; };
1352 template<typename _Result, typename _Signature>
1353 const bool is_bind_expression<_Bind_result<_Result, _Signature> >::value;
1356 template<typename _Functor, typename... _ArgTypes>
1358 _Bind<typename _Maybe_wrap_member_pointer<_Functor>::type(_ArgTypes...)>
1359 bind(_Functor __f, _ArgTypes... __args)
1361 typedef _Maybe_wrap_member_pointer<_Functor> __maybe_type;
1362 typedef typename __maybe_type::type __functor_type;
1363 typedef _Bind<__functor_type(_ArgTypes...)> __result_type;
1364 return __result_type(__maybe_type::__do_wrap(__f), __args...);
1367 template<typename _Result, typename _Functor, typename... _ArgTypes>
1369 _Bind_result<_Result,
1370 typename _Maybe_wrap_member_pointer<_Functor>::type
1372 bind(_Functor __f, _ArgTypes... __args)
1374 typedef _Maybe_wrap_member_pointer<_Functor> __maybe_type;
1375 typedef typename __maybe_type::type __functor_type;
1376 typedef _Bind_result<_Result, __functor_type(_ArgTypes...)>
1378 return __result_type(__maybe_type::__do_wrap(__f), __args...);
1382 * @brief Exception class thrown when class template function's
1383 * operator() is called with an empty target.
1384 * @ingroup exceptions
1386 class bad_function_call : public std::exception { };
1389 * The integral constant expression 0 can be converted into a
1390 * pointer to this type. It is used by the function template to
1391 * accept NULL pointers.
1393 struct _M_clear_type;
1396 * Trait identifying @a location-invariant types, meaning that the
1397 * address of the object (or any of its members) will not escape.
1398 * Also implies a trivial copy constructor and assignment operator.
1400 template<typename _Tp>
1401 struct __is_location_invariant
1402 : integral_constant<bool,
1403 (is_pointer<_Tp>::value
1404 || is_member_pointer<_Tp>::value)>
1408 class _Undefined_class;
1413 const void* _M_const_object;
1414 void (*_M_function_pointer)();
1415 void (_Undefined_class::*_M_member_pointer)();
1420 void* _M_access() { return &_M_pod_data[0]; }
1421 const void* _M_access() const { return &_M_pod_data[0]; }
1423 template<typename _Tp>
1426 { return *static_cast<_Tp*>(_M_access()); }
1428 template<typename _Tp>
1431 { return *static_cast<const _Tp*>(_M_access()); }
1433 _Nocopy_types _M_unused;
1434 char _M_pod_data[sizeof(_Nocopy_types)];
1437 enum _Manager_operation
1445 // Simple type wrapper that helps avoid annoying const problems
1446 // when casting between void pointers and pointers-to-pointers.
1447 template<typename _Tp>
1448 struct _Simple_type_wrapper
1450 _Simple_type_wrapper(_Tp __value) : __value(__value) { }
1455 template<typename _Tp>
1456 struct __is_location_invariant<_Simple_type_wrapper<_Tp> >
1457 : __is_location_invariant<_Tp>
1461 // Converts a reference to a function object into a callable
1463 template<typename _Functor>
1465 __callable_functor(_Functor& __f)
1468 template<typename _Member, typename _Class>
1469 inline _Mem_fn<_Member _Class::*>
1470 __callable_functor(_Member _Class::* &__p)
1471 { return mem_fn(__p); }
1473 template<typename _Member, typename _Class>
1474 inline _Mem_fn<_Member _Class::*>
1475 __callable_functor(_Member _Class::* const &__p)
1476 { return mem_fn(__p); }
1478 template<typename _Signature>
1481 /// Base class of all polymorphic function object wrappers.
1482 class _Function_base
1485 static const std::size_t _M_max_size = sizeof(_Nocopy_types);
1486 static const std::size_t _M_max_align = __alignof__(_Nocopy_types);
1488 template<typename _Functor>
1492 static const bool __stored_locally =
1493 (__is_location_invariant<_Functor>::value
1494 && sizeof(_Functor) <= _M_max_size
1495 && __alignof__(_Functor) <= _M_max_align
1496 && (_M_max_align % __alignof__(_Functor) == 0));
1498 typedef integral_constant<bool, __stored_locally> _Local_storage;
1500 // Retrieve a pointer to the function object
1502 _M_get_pointer(const _Any_data& __source)
1504 const _Functor* __ptr =
1505 __stored_locally? &__source._M_access<_Functor>()
1506 /* have stored a pointer */ : __source._M_access<_Functor*>();
1507 return const_cast<_Functor*>(__ptr);
1510 // Clone a location-invariant function object that fits within
1511 // an _Any_data structure.
1513 _M_clone(_Any_data& __dest, const _Any_data& __source, true_type)
1515 new (__dest._M_access()) _Functor(__source._M_access<_Functor>());
1518 // Clone a function object that is not location-invariant or
1519 // that cannot fit into an _Any_data structure.
1521 _M_clone(_Any_data& __dest, const _Any_data& __source, false_type)
1523 __dest._M_access<_Functor*>() =
1524 new _Functor(*__source._M_access<_Functor*>());
1527 // Destroying a location-invariant object may still require
1530 _M_destroy(_Any_data& __victim, true_type)
1532 __victim._M_access<_Functor>().~_Functor();
1535 // Destroying an object located on the heap.
1537 _M_destroy(_Any_data& __victim, false_type)
1539 delete __victim._M_access<_Functor*>();
1544 _M_manager(_Any_data& __dest, const _Any_data& __source,
1545 _Manager_operation __op)
1550 case __get_type_info:
1551 __dest._M_access<const type_info*>() = &typeid(_Functor);
1554 case __get_functor_ptr:
1555 __dest._M_access<_Functor*>() = _M_get_pointer(__source);
1558 case __clone_functor:
1559 _M_clone(__dest, __source, _Local_storage());
1562 case __destroy_functor:
1563 _M_destroy(__dest, _Local_storage());
1570 _M_init_functor(_Any_data& __functor, const _Functor& __f)
1571 { _M_init_functor(__functor, __f, _Local_storage()); }
1573 template<typename _Signature>
1575 _M_not_empty_function(const function<_Signature>& __f)
1576 { return static_cast<bool>(__f); }
1578 template<typename _Tp>
1580 _M_not_empty_function(const _Tp*& __fp)
1583 template<typename _Class, typename _Tp>
1585 _M_not_empty_function(_Tp _Class::* const& __mp)
1588 template<typename _Tp>
1590 _M_not_empty_function(const _Tp&)
1595 _M_init_functor(_Any_data& __functor, const _Functor& __f, true_type)
1596 { new (__functor._M_access()) _Functor(__f); }
1599 _M_init_functor(_Any_data& __functor, const _Functor& __f, false_type)
1600 { __functor._M_access<_Functor*>() = new _Functor(__f); }
1603 template<typename _Functor>
1604 class _Ref_manager : public _Base_manager<_Functor*>
1606 typedef _Function_base::_Base_manager<_Functor*> _Base;
1610 _M_manager(_Any_data& __dest, const _Any_data& __source,
1611 _Manager_operation __op)
1616 case __get_type_info:
1617 __dest._M_access<const type_info*>() = &typeid(_Functor);
1620 case __get_functor_ptr:
1621 __dest._M_access<_Functor*>() = *_Base::_M_get_pointer(__source);
1622 return is_const<_Functor>::value;
1626 _Base::_M_manager(__dest, __source, __op);
1632 _M_init_functor(_Any_data& __functor, reference_wrapper<_Functor> __f)
1634 // TBD: Use address_of function instead.
1635 _Base::_M_init_functor(__functor, &__f.get());
1639 _Function_base() : _M_manager(0) { }
1644 _M_manager(_M_functor, _M_functor, __destroy_functor);
1648 bool _M_empty() const { return !_M_manager; }
1650 typedef bool (*_Manager_type)(_Any_data&, const _Any_data&,
1651 _Manager_operation);
1653 _Any_data _M_functor;
1654 _Manager_type _M_manager;
1657 template<typename _Signature, typename _Functor>
1658 class _Function_handler;
1660 template<typename _Res, typename _Functor, typename... _ArgTypes>
1661 class _Function_handler<_Res(_ArgTypes...), _Functor>
1662 : public _Function_base::_Base_manager<_Functor>
1664 typedef _Function_base::_Base_manager<_Functor> _Base;
1668 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1670 return (*_Base::_M_get_pointer(__functor))(__args...);
1674 template<typename _Functor, typename... _ArgTypes>
1675 class _Function_handler<void(_ArgTypes...), _Functor>
1676 : public _Function_base::_Base_manager<_Functor>
1678 typedef _Function_base::_Base_manager<_Functor> _Base;
1682 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1684 (*_Base::_M_get_pointer(__functor))(__args...);
1688 template<typename _Res, typename _Functor, typename... _ArgTypes>
1689 class _Function_handler<_Res(_ArgTypes...), reference_wrapper<_Functor> >
1690 : public _Function_base::_Ref_manager<_Functor>
1692 typedef _Function_base::_Ref_manager<_Functor> _Base;
1696 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1699 __callable_functor(**_Base::_M_get_pointer(__functor))(__args...);
1703 template<typename _Functor, typename... _ArgTypes>
1704 class _Function_handler<void(_ArgTypes...), reference_wrapper<_Functor> >
1705 : public _Function_base::_Ref_manager<_Functor>
1707 typedef _Function_base::_Ref_manager<_Functor> _Base;
1711 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1713 __callable_functor(**_Base::_M_get_pointer(__functor))(__args...);
1717 template<typename _Class, typename _Member, typename _Res,
1718 typename... _ArgTypes>
1719 class _Function_handler<_Res(_ArgTypes...), _Member _Class::*>
1720 : public _Function_handler<void(_ArgTypes...), _Member _Class::*>
1722 typedef _Function_handler<void(_ArgTypes...), _Member _Class::*>
1727 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1730 mem_fn(_Base::_M_get_pointer(__functor)->__value)(__args...);
1734 template<typename _Class, typename _Member, typename... _ArgTypes>
1735 class _Function_handler<void(_ArgTypes...), _Member _Class::*>
1736 : public _Function_base::_Base_manager<
1737 _Simple_type_wrapper< _Member _Class::* > >
1739 typedef _Member _Class::* _Functor;
1740 typedef _Simple_type_wrapper<_Functor> _Wrapper;
1741 typedef _Function_base::_Base_manager<_Wrapper> _Base;
1745 _M_manager(_Any_data& __dest, const _Any_data& __source,
1746 _Manager_operation __op)
1751 case __get_type_info:
1752 __dest._M_access<const type_info*>() = &typeid(_Functor);
1755 case __get_functor_ptr:
1756 __dest._M_access<_Functor*>() =
1757 &_Base::_M_get_pointer(__source)->__value;
1761 _Base::_M_manager(__dest, __source, __op);
1767 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1769 tr1::mem_fn(_Base::_M_get_pointer(__functor)->__value)(__args...);
1774 template<typename _Res, typename... _ArgTypes>
1775 class function<_Res(_ArgTypes...)>
1776 : public _Maybe_unary_or_binary_function<_Res, _ArgTypes...>,
1777 private _Function_base
1779 #ifndef __GXX_EXPERIMENTAL_CXX0X__
1780 /// This class is used to implement the safe_bool idiom.
1783 _Hidden_type* _M_bool;
1786 /// This typedef is used to implement the safe_bool idiom.
1787 typedef _Hidden_type* _Hidden_type::* _Safe_bool;
1790 typedef _Res _Signature_type(_ArgTypes...);
1792 struct _Useless { };
1795 typedef _Res result_type;
1797 // [3.7.2.1] construct/copy/destroy
1800 * @brief Default construct creates an empty function call wrapper.
1801 * @post @c !(bool)*this
1803 function() : _Function_base() { }
1806 * @brief Default construct creates an empty function call wrapper.
1807 * @post @c !(bool)*this
1809 function(_M_clear_type*) : _Function_base() { }
1812 * @brief %Function copy constructor.
1813 * @param x A %function object with identical call signature.
1814 * @post @c (bool)*this == (bool)x
1816 * The newly-created %function contains a copy of the target of @a
1817 * x (if it has one).
1819 function(const function& __x);
1822 * @brief Builds a %function that targets a copy of the incoming
1824 * @param f A %function object that is callable with parameters of
1825 * type @c T1, @c T2, ..., @c TN and returns a value convertible
1828 * The newly-created %function object will target a copy of @a
1829 * f. If @a f is @c reference_wrapper<F>, then this function
1830 * object will contain a reference to the function object @c
1831 * f.get(). If @a f is a NULL function pointer or NULL
1832 * pointer-to-member, the newly-created object will be empty.
1834 * If @a f is a non-NULL function pointer or an object of type @c
1835 * reference_wrapper<F>, this function will not throw.
1837 template<typename _Functor>
1838 function(_Functor __f,
1839 typename __gnu_cxx::__enable_if<
1840 !is_integral<_Functor>::value, _Useless>::__type
1844 * @brief %Function assignment operator.
1845 * @param x A %function with identical call signature.
1846 * @post @c (bool)*this == (bool)x
1849 * The target of @a x is copied to @c *this. If @a x has no
1850 * target, then @c *this will be empty.
1852 * If @a x targets a function pointer or a reference to a function
1853 * object, then this operation will not throw an %exception.
1856 operator=(const function& __x)
1858 function(__x).swap(*this);
1863 * @brief %Function assignment to zero.
1864 * @post @c !(bool)*this
1867 * The target of @c *this is deallocated, leaving it empty.
1870 operator=(_M_clear_type*)
1874 _M_manager(_M_functor, _M_functor, __destroy_functor);
1882 * @brief %Function assignment to a new target.
1883 * @param f A %function object that is callable with parameters of
1884 * type @c T1, @c T2, ..., @c TN and returns a value convertible
1888 * This %function object wrapper will target a copy of @a
1889 * f. If @a f is @c reference_wrapper<F>, then this function
1890 * object will contain a reference to the function object @c
1891 * f.get(). If @a f is a NULL function pointer or NULL
1892 * pointer-to-member, @c this object will be empty.
1894 * If @a f is a non-NULL function pointer or an object of type @c
1895 * reference_wrapper<F>, this function will not throw.
1897 template<typename _Functor>
1898 typename __gnu_cxx::__enable_if<!is_integral<_Functor>::value,
1900 operator=(_Functor __f)
1902 function(__f).swap(*this);
1906 // [3.7.2.2] function modifiers
1909 * @brief Swap the targets of two %function objects.
1910 * @param f A %function with identical call signature.
1912 * Swap the targets of @c this function object and @a f. This
1913 * function will not throw an %exception.
1915 void swap(function& __x)
1917 std::swap(_M_functor, __x._M_functor);
1918 std::swap(_M_manager, __x._M_manager);
1919 std::swap(_M_invoker, __x._M_invoker);
1922 // [3.7.2.3] function capacity
1925 * @brief Determine if the %function wrapper has a target.
1927 * @return @c true when this %function object contains a target,
1928 * or @c false when it is empty.
1930 * This function will not throw an %exception.
1932 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1933 explicit operator bool() const
1934 { return !_M_empty(); }
1936 operator _Safe_bool() const
1941 return &_Hidden_type::_M_bool;
1945 // [3.7.2.4] function invocation
1948 * @brief Invokes the function targeted by @c *this.
1949 * @returns the result of the target.
1950 * @throws bad_function_call when @c !(bool)*this
1952 * The function call operator invokes the target function object
1953 * stored by @c this.
1955 _Res operator()(_ArgTypes... __args) const;
1958 // [3.7.2.5] function target access
1960 * @brief Determine the type of the target of this function object
1963 * @returns the type identifier of the target function object, or
1964 * @c typeid(void) if @c !(bool)*this.
1966 * This function will not throw an %exception.
1968 const type_info& target_type() const;
1971 * @brief Access the stored target function object.
1973 * @return Returns a pointer to the stored target function object,
1974 * if @c typeid(Functor).equals(target_type()); otherwise, a NULL
1977 * This function will not throw an %exception.
1979 template<typename _Functor> _Functor* target();
1982 template<typename _Functor> const _Functor* target() const;
1986 // [3.7.2.6] undefined operators
1987 template<typename _Function>
1988 void operator==(const function<_Function>&) const;
1989 template<typename _Function>
1990 void operator!=(const function<_Function>&) const;
1992 typedef _Res (*_Invoker_type)(const _Any_data&, _ArgTypes...);
1993 _Invoker_type _M_invoker;
1996 template<typename _Res, typename... _ArgTypes>
1997 function<_Res(_ArgTypes...)>::
1998 function(const function& __x)
2001 if (static_cast<bool>(__x))
2003 _M_invoker = __x._M_invoker;
2004 _M_manager = __x._M_manager;
2005 __x._M_manager(_M_functor, __x._M_functor, __clone_functor);
2009 template<typename _Res, typename... _ArgTypes>
2010 template<typename _Functor>
2011 function<_Res(_ArgTypes...)>::
2012 function(_Functor __f,
2013 typename __gnu_cxx::__enable_if<
2014 !is_integral<_Functor>::value, _Useless>::__type)
2017 typedef _Function_handler<_Signature_type, _Functor> _My_handler;
2019 if (_My_handler::_M_not_empty_function(__f))
2021 _M_invoker = &_My_handler::_M_invoke;
2022 _M_manager = &_My_handler::_M_manager;
2023 _My_handler::_M_init_functor(_M_functor, __f);
2027 template<typename _Res, typename... _ArgTypes>
2029 function<_Res(_ArgTypes...)>::
2030 operator()(_ArgTypes... __args) const
2035 throw bad_function_call();
2040 return _M_invoker(_M_functor, __args...);
2044 template<typename _Res, typename... _ArgTypes>
2046 function<_Res(_ArgTypes...)>::
2051 _Any_data __typeinfo_result;
2052 _M_manager(__typeinfo_result, _M_functor, __get_type_info);
2053 return *__typeinfo_result._M_access<const type_info*>();
2056 return typeid(void);
2059 template<typename _Res, typename... _ArgTypes>
2060 template<typename _Functor>
2062 function<_Res(_ArgTypes...)>::
2065 if (typeid(_Functor) == target_type() && _M_manager)
2068 if (_M_manager(__ptr, _M_functor, __get_functor_ptr)
2069 && !is_const<_Functor>::value)
2072 return __ptr._M_access<_Functor*>();
2078 template<typename _Res, typename... _ArgTypes>
2079 template<typename _Functor>
2081 function<_Res(_ArgTypes...)>::
2084 if (typeid(_Functor) == target_type() && _M_manager)
2087 _M_manager(__ptr, _M_functor, __get_functor_ptr);
2088 return __ptr._M_access<const _Functor*>();
2095 // [3.7.2.7] null pointer comparisons
2098 * @brief Compares a polymorphic function object wrapper against 0
2099 * (the NULL pointer).
2100 * @returns @c true if the wrapper has no target, @c false otherwise
2102 * This function will not throw an %exception.
2104 template<typename _Signature>
2106 operator==(const function<_Signature>& __f, _M_clear_type*)
2107 { return !static_cast<bool>(__f); }
2110 template<typename _Signature>
2112 operator==(_M_clear_type*, const function<_Signature>& __f)
2113 { return !static_cast<bool>(__f); }
2116 * @brief Compares a polymorphic function object wrapper against 0
2117 * (the NULL pointer).
2118 * @returns @c false if the wrapper has no target, @c true otherwise
2120 * This function will not throw an %exception.
2122 template<typename _Signature>
2124 operator!=(const function<_Signature>& __f, _M_clear_type*)
2125 { return static_cast<bool>(__f); }
2128 template<typename _Signature>
2130 operator!=(_M_clear_type*, const function<_Signature>& __f)
2131 { return static_cast<bool>(__f); }
2133 // [3.7.2.8] specialized algorithms
2136 * @brief Swap the targets of two polymorphic function object wrappers.
2138 * This function will not throw an %exception.
2140 template<typename _Signature>
2142 swap(function<_Signature>& __x, function<_Signature>& __y)
2145 _GLIBCXX_END_NAMESPACE_VERSION
2149 #endif // _GLIBCXX_TR1_FUNCTIONAL