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
46 #ifdef __GXX_EXPERIMENTAL_CXX0X__
47 # include <type_traits> // for integral_constant, true_type, false_type
50 namespace std _GLIBCXX_VISIBILITY(default)
52 #ifdef __GXX_EXPERIMENTAL_CXX0X__
53 _GLIBCXX_BEGIN_NAMESPACE_VERSION
54 template<int> struct _Placeholder;
55 template<typename> class _Bind;
56 template<typename, typename> class _Bind_result;
57 _GLIBCXX_END_NAMESPACE_VERSION
62 _GLIBCXX_BEGIN_NAMESPACE_VERSION
64 template<typename _MemberPointer>
66 template<typename _Tp, typename _Class>
67 _Mem_fn<_Tp _Class::*>
68 mem_fn(_Tp _Class::*);
71 * Actual implementation of _Has_result_type, which uses SFINAE to
72 * determine if the type _Tp has a publicly-accessible member type
75 template<typename _Tp>
76 class _Has_result_type_helper : __sfinae_types
78 template<typename _Up>
82 template<typename _Up>
83 static __one __test(_Wrap_type<typename _Up::result_type>*);
85 template<typename _Up>
86 static __two __test(...);
89 static const bool value = sizeof(__test<_Tp>(0)) == 1;
92 template<typename _Tp>
93 struct _Has_result_type
94 : integral_constant<bool,
95 _Has_result_type_helper<typename remove_cv<_Tp>::type>::value>
101 /// If we have found a result_type, extract it.
102 template<bool _Has_result_type, typename _Functor>
103 struct _Maybe_get_result_type
106 template<typename _Functor>
107 struct _Maybe_get_result_type<true, _Functor>
109 typedef typename _Functor::result_type result_type;
113 * Base class for any function object that has a weak result type, as
114 * defined in 3.3/3 of TR1.
116 template<typename _Functor>
117 struct _Weak_result_type_impl
118 : _Maybe_get_result_type<_Has_result_type<_Functor>::value, _Functor>
122 /// Retrieve the result type for a function type.
123 template<typename _Res, typename... _ArgTypes>
124 struct _Weak_result_type_impl<_Res(_ArgTypes...)>
126 typedef _Res result_type;
129 /// Retrieve the result type for a function reference.
130 template<typename _Res, typename... _ArgTypes>
131 struct _Weak_result_type_impl<_Res(&)(_ArgTypes...)>
133 typedef _Res result_type;
136 /// Retrieve the result type for a function pointer.
137 template<typename _Res, typename... _ArgTypes>
138 struct _Weak_result_type_impl<_Res(*)(_ArgTypes...)>
140 typedef _Res result_type;
143 /// Retrieve result type for a member function pointer.
144 template<typename _Res, typename _Class, typename... _ArgTypes>
145 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...)>
147 typedef _Res result_type;
150 /// Retrieve result type for a const member function pointer.
151 template<typename _Res, typename _Class, typename... _ArgTypes>
152 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) const>
154 typedef _Res result_type;
157 /// Retrieve result type for a volatile member function pointer.
158 template<typename _Res, typename _Class, typename... _ArgTypes>
159 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) volatile>
161 typedef _Res result_type;
164 /// Retrieve result type for a const volatile member function pointer.
165 template<typename _Res, typename _Class, typename... _ArgTypes>
166 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...)const volatile>
168 typedef _Res result_type;
172 * Strip top-level cv-qualifiers from the function object and let
173 * _Weak_result_type_impl perform the real work.
175 template<typename _Functor>
176 struct _Weak_result_type
177 : _Weak_result_type_impl<typename remove_cv<_Functor>::type>
181 template<typename _Signature>
185 * Actual implementation of result_of. When _Has_result_type is
186 * true, gets its result from _Weak_result_type. Otherwise, uses
187 * the function object's member template result to extract the
190 template<bool _Has_result_type, typename _Signature>
191 struct _Result_of_impl;
193 // Handle member data pointers using _Mem_fn's logic
194 template<typename _Res, typename _Class, typename _T1>
195 struct _Result_of_impl<false, _Res _Class::*(_T1)>
197 typedef typename _Mem_fn<_Res _Class::*>
198 ::template _Result_type<_T1>::type type;
202 * Determine whether we can determine a result type from @c Functor
205 template<typename _Functor, typename... _ArgTypes>
206 class result_of<_Functor(_ArgTypes...)>
207 : public _Result_of_impl<
208 _Has_result_type<_Weak_result_type<_Functor> >::value,
209 _Functor(_ArgTypes...)>
213 /// We already know the result type for @c Functor; use it.
214 template<typename _Functor, typename... _ArgTypes>
215 struct _Result_of_impl<true, _Functor(_ArgTypes...)>
217 typedef typename _Weak_result_type<_Functor>::result_type type;
221 * We need to compute the result type for this invocation the hard
224 template<typename _Functor, typename... _ArgTypes>
225 struct _Result_of_impl<false, _Functor(_ArgTypes...)>
227 typedef typename _Functor
228 ::template result<_Functor(_ArgTypes...)>::type type;
232 * It is unsafe to access ::result when there are zero arguments, so we
233 * return @c void instead.
235 template<typename _Functor>
236 struct _Result_of_impl<false, _Functor()>
241 /// Determines if the type _Tp derives from unary_function.
242 template<typename _Tp>
243 struct _Derives_from_unary_function : __sfinae_types
246 template<typename _T1, typename _Res>
247 static __one __test(const volatile unary_function<_T1, _Res>*);
249 // It's tempting to change "..." to const volatile void*, but
250 // that fails when _Tp is a function type.
251 static __two __test(...);
254 static const bool value = sizeof(__test((_Tp*)0)) == 1;
257 /// Determines if the type _Tp derives from binary_function.
258 template<typename _Tp>
259 struct _Derives_from_binary_function : __sfinae_types
262 template<typename _T1, typename _T2, typename _Res>
263 static __one __test(const volatile binary_function<_T1, _T2, _Res>*);
265 // It's tempting to change "..." to const volatile void*, but
266 // that fails when _Tp is a function type.
267 static __two __test(...);
270 static const bool value = sizeof(__test((_Tp*)0)) == 1;
273 /// Turns a function type into a function pointer type
274 template<typename _Tp, bool _IsFunctionType = is_function<_Tp>::value>
275 struct _Function_to_function_pointer
280 template<typename _Tp>
281 struct _Function_to_function_pointer<_Tp, true>
287 * Invoke a function object, which may be either a member pointer or a
288 * function object. The first parameter will tell which.
290 template<typename _Functor, typename... _Args>
292 typename __gnu_cxx::__enable_if<
293 (!is_member_pointer<_Functor>::value
294 && !is_function<_Functor>::value
295 && !is_function<typename remove_pointer<_Functor>::type>::value),
296 typename result_of<_Functor(_Args...)>::type
298 __invoke(_Functor& __f, _Args&... __args)
300 return __f(__args...);
303 template<typename _Functor, typename... _Args>
305 typename __gnu_cxx::__enable_if<
306 (is_member_pointer<_Functor>::value
307 && !is_function<_Functor>::value
308 && !is_function<typename remove_pointer<_Functor>::type>::value),
309 typename result_of<_Functor(_Args...)>::type
311 __invoke(_Functor& __f, _Args&... __args)
313 return mem_fn(__f)(__args...);
316 // To pick up function references (that will become function pointers)
317 template<typename _Functor, typename... _Args>
319 typename __gnu_cxx::__enable_if<
320 (is_pointer<_Functor>::value
321 && is_function<typename remove_pointer<_Functor>::type>::value),
322 typename result_of<_Functor(_Args...)>::type
324 __invoke(_Functor __f, _Args&... __args)
326 return __f(__args...);
330 * Knowing which of unary_function and binary_function _Tp derives
331 * from, derives from the same and ensures that reference_wrapper
332 * will have a weak result type. See cases below.
334 template<bool _Unary, bool _Binary, typename _Tp>
335 struct _Reference_wrapper_base_impl;
337 // Not a unary_function or binary_function, so try a weak result type.
338 template<typename _Tp>
339 struct _Reference_wrapper_base_impl<false, false, _Tp>
340 : _Weak_result_type<_Tp>
343 // unary_function but not binary_function
344 template<typename _Tp>
345 struct _Reference_wrapper_base_impl<true, false, _Tp>
346 : unary_function<typename _Tp::argument_type,
347 typename _Tp::result_type>
350 // binary_function but not unary_function
351 template<typename _Tp>
352 struct _Reference_wrapper_base_impl<false, true, _Tp>
353 : binary_function<typename _Tp::first_argument_type,
354 typename _Tp::second_argument_type,
355 typename _Tp::result_type>
358 // Both unary_function and binary_function. Import result_type to
360 template<typename _Tp>
361 struct _Reference_wrapper_base_impl<true, true, _Tp>
362 : unary_function<typename _Tp::argument_type,
363 typename _Tp::result_type>,
364 binary_function<typename _Tp::first_argument_type,
365 typename _Tp::second_argument_type,
366 typename _Tp::result_type>
368 typedef typename _Tp::result_type result_type;
372 * Derives from unary_function or binary_function when it
373 * can. Specializations handle all of the easy cases. The primary
374 * template determines what to do with a class type, which may
375 * derive from both unary_function and binary_function.
377 template<typename _Tp>
378 struct _Reference_wrapper_base
379 : _Reference_wrapper_base_impl<
380 _Derives_from_unary_function<_Tp>::value,
381 _Derives_from_binary_function<_Tp>::value,
385 // - a function type (unary)
386 template<typename _Res, typename _T1>
387 struct _Reference_wrapper_base<_Res(_T1)>
388 : unary_function<_T1, _Res>
391 // - a function type (binary)
392 template<typename _Res, typename _T1, typename _T2>
393 struct _Reference_wrapper_base<_Res(_T1, _T2)>
394 : binary_function<_T1, _T2, _Res>
397 // - a function pointer type (unary)
398 template<typename _Res, typename _T1>
399 struct _Reference_wrapper_base<_Res(*)(_T1)>
400 : unary_function<_T1, _Res>
403 // - a function pointer type (binary)
404 template<typename _Res, typename _T1, typename _T2>
405 struct _Reference_wrapper_base<_Res(*)(_T1, _T2)>
406 : binary_function<_T1, _T2, _Res>
409 // - a pointer to member function type (unary, no qualifiers)
410 template<typename _Res, typename _T1>
411 struct _Reference_wrapper_base<_Res (_T1::*)()>
412 : unary_function<_T1*, _Res>
415 // - a pointer to member function type (binary, no qualifiers)
416 template<typename _Res, typename _T1, typename _T2>
417 struct _Reference_wrapper_base<_Res (_T1::*)(_T2)>
418 : binary_function<_T1*, _T2, _Res>
421 // - a pointer to member function type (unary, const)
422 template<typename _Res, typename _T1>
423 struct _Reference_wrapper_base<_Res (_T1::*)() const>
424 : unary_function<const _T1*, _Res>
427 // - a pointer to member function type (binary, const)
428 template<typename _Res, typename _T1, typename _T2>
429 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const>
430 : binary_function<const _T1*, _T2, _Res>
433 // - a pointer to member function type (unary, volatile)
434 template<typename _Res, typename _T1>
435 struct _Reference_wrapper_base<_Res (_T1::*)() volatile>
436 : unary_function<volatile _T1*, _Res>
439 // - a pointer to member function type (binary, volatile)
440 template<typename _Res, typename _T1, typename _T2>
441 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) volatile>
442 : binary_function<volatile _T1*, _T2, _Res>
445 // - a pointer to member function type (unary, const volatile)
446 template<typename _Res, typename _T1>
447 struct _Reference_wrapper_base<_Res (_T1::*)() const volatile>
448 : unary_function<const volatile _T1*, _Res>
451 // - a pointer to member function type (binary, const volatile)
452 template<typename _Res, typename _T1, typename _T2>
453 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const volatile>
454 : binary_function<const volatile _T1*, _T2, _Res>
457 /// reference_wrapper
458 template<typename _Tp>
459 class reference_wrapper
460 : public _Reference_wrapper_base<typename remove_cv<_Tp>::type>
462 // If _Tp is a function type, we can't form result_of<_Tp(...)>,
463 // so turn it into a function pointer type.
464 typedef typename _Function_to_function_pointer<_Tp>::type
472 reference_wrapper(_Tp& __indata)
473 : _M_data(std::__addressof(__indata))
476 reference_wrapper(const reference_wrapper<_Tp>& __inref):
477 _M_data(__inref._M_data)
481 operator=(const reference_wrapper<_Tp>& __inref)
483 _M_data = __inref._M_data;
487 operator _Tp&() const
488 { return this->get(); }
494 template<typename... _Args>
495 typename result_of<_M_func_type(_Args...)>::type
496 operator()(_Args&... __args) const
498 return __invoke(get(), __args...);
503 // Denotes a reference should be taken to a variable.
504 template<typename _Tp>
505 inline reference_wrapper<_Tp>
507 { return reference_wrapper<_Tp>(__t); }
509 // Denotes a const reference should be taken to a variable.
510 template<typename _Tp>
511 inline reference_wrapper<const _Tp>
513 { return reference_wrapper<const _Tp>(__t); }
515 template<typename _Tp>
516 inline reference_wrapper<_Tp>
517 ref(reference_wrapper<_Tp> __t)
518 { return ref(__t.get()); }
520 template<typename _Tp>
521 inline reference_wrapper<const _Tp>
522 cref(reference_wrapper<_Tp> __t)
523 { return cref(__t.get()); }
525 template<typename _Tp, bool>
526 struct _Mem_fn_const_or_non
528 typedef const _Tp& type;
531 template<typename _Tp>
532 struct _Mem_fn_const_or_non<_Tp, false>
538 * Derives from @c unary_function or @c binary_function, or perhaps
539 * nothing, depending on the number of arguments provided. The
540 * primary template is the basis case, which derives nothing.
542 template<typename _Res, typename... _ArgTypes>
543 struct _Maybe_unary_or_binary_function { };
545 /// Derives from @c unary_function, as appropriate.
546 template<typename _Res, typename _T1>
547 struct _Maybe_unary_or_binary_function<_Res, _T1>
548 : std::unary_function<_T1, _Res> { };
550 /// Derives from @c binary_function, as appropriate.
551 template<typename _Res, typename _T1, typename _T2>
552 struct _Maybe_unary_or_binary_function<_Res, _T1, _T2>
553 : std::binary_function<_T1, _T2, _Res> { };
555 /// Implementation of @c mem_fn for member function pointers.
556 template<typename _Res, typename _Class, typename... _ArgTypes>
557 class _Mem_fn<_Res (_Class::*)(_ArgTypes...)>
558 : public _Maybe_unary_or_binary_function<_Res, _Class*, _ArgTypes...>
560 typedef _Res (_Class::*_Functor)(_ArgTypes...);
562 template<typename _Tp>
564 _M_call(_Tp& __object, const volatile _Class *,
565 _ArgTypes... __args) const
566 { return (__object.*__pmf)(__args...); }
568 template<typename _Tp>
570 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
571 { return ((*__ptr).*__pmf)(__args...); }
574 typedef _Res result_type;
576 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
580 operator()(_Class& __object, _ArgTypes... __args) const
581 { return (__object.*__pmf)(__args...); }
585 operator()(_Class* __object, _ArgTypes... __args) const
586 { return (__object->*__pmf)(__args...); }
588 // Handle smart pointers, references and pointers to derived
589 template<typename _Tp>
591 operator()(_Tp& __object, _ArgTypes... __args) const
592 { return _M_call(__object, &__object, __args...); }
598 /// Implementation of @c mem_fn for const member function pointers.
599 template<typename _Res, typename _Class, typename... _ArgTypes>
600 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) const>
601 : public _Maybe_unary_or_binary_function<_Res, const _Class*,
604 typedef _Res (_Class::*_Functor)(_ArgTypes...) const;
606 template<typename _Tp>
608 _M_call(_Tp& __object, const volatile _Class *,
609 _ArgTypes... __args) const
610 { return (__object.*__pmf)(__args...); }
612 template<typename _Tp>
614 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
615 { return ((*__ptr).*__pmf)(__args...); }
618 typedef _Res result_type;
620 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
624 operator()(const _Class& __object, _ArgTypes... __args) const
625 { return (__object.*__pmf)(__args...); }
629 operator()(const _Class* __object, _ArgTypes... __args) const
630 { return (__object->*__pmf)(__args...); }
632 // Handle smart pointers, references and pointers to derived
633 template<typename _Tp>
634 _Res operator()(_Tp& __object, _ArgTypes... __args) const
635 { return _M_call(__object, &__object, __args...); }
641 /// Implementation of @c mem_fn for volatile member function pointers.
642 template<typename _Res, typename _Class, typename... _ArgTypes>
643 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) volatile>
644 : public _Maybe_unary_or_binary_function<_Res, volatile _Class*,
647 typedef _Res (_Class::*_Functor)(_ArgTypes...) volatile;
649 template<typename _Tp>
651 _M_call(_Tp& __object, const volatile _Class *,
652 _ArgTypes... __args) const
653 { return (__object.*__pmf)(__args...); }
655 template<typename _Tp>
657 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
658 { return ((*__ptr).*__pmf)(__args...); }
661 typedef _Res result_type;
663 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
667 operator()(volatile _Class& __object, _ArgTypes... __args) const
668 { return (__object.*__pmf)(__args...); }
672 operator()(volatile _Class* __object, _ArgTypes... __args) const
673 { return (__object->*__pmf)(__args...); }
675 // Handle smart pointers, references and pointers to derived
676 template<typename _Tp>
678 operator()(_Tp& __object, _ArgTypes... __args) const
679 { return _M_call(__object, &__object, __args...); }
685 /// Implementation of @c mem_fn for const volatile member function pointers.
686 template<typename _Res, typename _Class, typename... _ArgTypes>
687 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) const volatile>
688 : public _Maybe_unary_or_binary_function<_Res, const volatile _Class*,
691 typedef _Res (_Class::*_Functor)(_ArgTypes...) const volatile;
693 template<typename _Tp>
695 _M_call(_Tp& __object, const volatile _Class *,
696 _ArgTypes... __args) const
697 { return (__object.*__pmf)(__args...); }
699 template<typename _Tp>
701 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
702 { return ((*__ptr).*__pmf)(__args...); }
705 typedef _Res result_type;
707 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
711 operator()(const volatile _Class& __object, _ArgTypes... __args) const
712 { return (__object.*__pmf)(__args...); }
716 operator()(const volatile _Class* __object, _ArgTypes... __args) const
717 { return (__object->*__pmf)(__args...); }
719 // Handle smart pointers, references and pointers to derived
720 template<typename _Tp>
721 _Res operator()(_Tp& __object, _ArgTypes... __args) const
722 { return _M_call(__object, &__object, __args...); }
729 template<typename _Res, typename _Class>
730 class _Mem_fn<_Res _Class::*>
732 // This bit of genius is due to Peter Dimov, improved slightly by
734 template<typename _Tp>
736 _M_call(_Tp& __object, _Class *) const
737 { return __object.*__pm; }
739 template<typename _Tp, typename _Up>
741 _M_call(_Tp& __object, _Up * const *) const
742 { return (*__object).*__pm; }
744 template<typename _Tp, typename _Up>
746 _M_call(_Tp& __object, const _Up * const *) const
747 { return (*__object).*__pm; }
749 template<typename _Tp>
751 _M_call(_Tp& __object, const _Class *) const
752 { return __object.*__pm; }
754 template<typename _Tp>
756 _M_call(_Tp& __ptr, const volatile void*) const
757 { return (*__ptr).*__pm; }
759 template<typename _Tp> static _Tp& __get_ref();
761 template<typename _Tp>
762 static __sfinae_types::__one __check_const(_Tp&, _Class*);
763 template<typename _Tp, typename _Up>
764 static __sfinae_types::__one __check_const(_Tp&, _Up * const *);
765 template<typename _Tp, typename _Up>
766 static __sfinae_types::__two __check_const(_Tp&, const _Up * const *);
767 template<typename _Tp>
768 static __sfinae_types::__two __check_const(_Tp&, const _Class*);
769 template<typename _Tp>
770 static __sfinae_types::__two __check_const(_Tp&, const volatile void*);
773 template<typename _Tp>
775 : _Mem_fn_const_or_non<_Res,
776 (sizeof(__sfinae_types::__two)
777 == sizeof(__check_const<_Tp>(__get_ref<_Tp>(), (_Tp*)0)))>
780 template<typename _Signature>
783 template<typename _CVMem, typename _Tp>
784 struct result<_CVMem(_Tp)>
785 : public _Result_type<_Tp> { };
787 template<typename _CVMem, typename _Tp>
788 struct result<_CVMem(_Tp&)>
789 : public _Result_type<_Tp> { };
792 _Mem_fn(_Res _Class::*__pm) : __pm(__pm) { }
796 operator()(_Class& __object) const
797 { return __object.*__pm; }
800 operator()(const _Class& __object) const
801 { return __object.*__pm; }
805 operator()(_Class* __object) const
806 { return __object->*__pm; }
809 operator()(const _Class* __object) const
810 { return __object->*__pm; }
812 // Handle smart pointers and derived
813 template<typename _Tp>
814 typename _Result_type<_Tp>::type
815 operator()(_Tp& __unknown) const
816 { return _M_call(__unknown, &__unknown); }
823 * @brief Returns a function object that forwards to the member
826 template<typename _Tp, typename _Class>
827 inline _Mem_fn<_Tp _Class::*>
828 mem_fn(_Tp _Class::* __pm)
830 return _Mem_fn<_Tp _Class::*>(__pm);
834 * @brief Determines if the given type _Tp is a function object
835 * should be treated as a subexpression when evaluating calls to
836 * function objects returned by bind(). [TR1 3.6.1]
838 template<typename _Tp>
839 struct is_bind_expression
840 { static const bool value = false; };
842 template<typename _Tp>
843 const bool is_bind_expression<_Tp>::value;
846 * @brief Determines if the given type _Tp is a placeholder in a
847 * bind() expression and, if so, which placeholder it is. [TR1 3.6.2]
849 template<typename _Tp>
850 struct is_placeholder
851 { static const int value = 0; };
853 template<typename _Tp>
854 const int is_placeholder<_Tp>::value;
856 /// The type of placeholder objects defined by libstdc++.
857 template<int _Num> struct _Placeholder { };
859 _GLIBCXX_END_NAMESPACE_VERSION
861 /** @namespace std::tr1::placeholders
862 * @brief Sub-namespace for tr1/functional.
864 namespace placeholders
866 _GLIBCXX_BEGIN_NAMESPACE_VERSION
867 /* Define a large number of placeholders. There is no way to
868 * simplify this with variadic templates, because we're introducing
869 * unique names for each.
882 _Placeholder<10> _10;
883 _Placeholder<11> _11;
884 _Placeholder<12> _12;
885 _Placeholder<13> _13;
886 _Placeholder<14> _14;
887 _Placeholder<15> _15;
888 _Placeholder<16> _16;
889 _Placeholder<17> _17;
890 _Placeholder<18> _18;
891 _Placeholder<19> _19;
892 _Placeholder<20> _20;
893 _Placeholder<21> _21;
894 _Placeholder<22> _22;
895 _Placeholder<23> _23;
896 _Placeholder<24> _24;
897 _Placeholder<25> _25;
898 _Placeholder<26> _26;
899 _Placeholder<27> _27;
900 _Placeholder<28> _28;
901 _Placeholder<29> _29;
903 _GLIBCXX_END_NAMESPACE_VERSION
906 _GLIBCXX_BEGIN_NAMESPACE_VERSION
908 * Partial specialization of is_placeholder that provides the placeholder
909 * number for the placeholder objects defined by libstdc++.
912 struct is_placeholder<_Placeholder<_Num> >
913 { static const int value = _Num; };
916 const int is_placeholder<_Placeholder<_Num> >::value;
918 #ifdef __GXX_EXPERIMENTAL_CXX0X__
920 struct is_placeholder<std::_Placeholder<_Num>>
921 : std::integral_constant<int, _Num>
925 struct is_placeholder<const std::_Placeholder<_Num>>
926 : std::integral_constant<int, _Num>
931 * Stores a tuple of indices. Used by bind() to extract the elements
934 template<int... _Indexes>
935 struct _Index_tuple { };
937 /// Builds an _Index_tuple<0, 1, 2, ..., _Num-1>.
938 template<std::size_t _Num, typename _Tuple = _Index_tuple<> >
939 struct _Build_index_tuple;
941 template<std::size_t _Num, int... _Indexes>
942 struct _Build_index_tuple<_Num, _Index_tuple<_Indexes...> >
943 : _Build_index_tuple<_Num - 1,
944 _Index_tuple<_Indexes..., sizeof...(_Indexes)> >
948 template<int... _Indexes>
949 struct _Build_index_tuple<0, _Index_tuple<_Indexes...> >
951 typedef _Index_tuple<_Indexes...> __type;
955 * Used by _Safe_tuple_element to indicate that there is no tuple
956 * element at this position.
958 struct _No_tuple_element;
961 * Implementation helper for _Safe_tuple_element. This primary
962 * template handles the case where it is safe to use @c
965 template<int __i, typename _Tuple, bool _IsSafe>
966 struct _Safe_tuple_element_impl
967 : tuple_element<__i, _Tuple> { };
970 * Implementation helper for _Safe_tuple_element. This partial
971 * specialization handles the case where it is not safe to use @c
972 * tuple_element. We just return @c _No_tuple_element.
974 template<int __i, typename _Tuple>
975 struct _Safe_tuple_element_impl<__i, _Tuple, false>
977 typedef _No_tuple_element type;
981 * Like tuple_element, but returns @c _No_tuple_element when
982 * tuple_element would return an error.
984 template<int __i, typename _Tuple>
985 struct _Safe_tuple_element
986 : _Safe_tuple_element_impl<__i, _Tuple,
987 (__i >= 0 && __i < tuple_size<_Tuple>::value)>
992 * Maps an argument to bind() into an actual argument to the bound
993 * function object [TR1 3.6.3/5]. Only the first parameter should
994 * be specified: the rest are used to determine among the various
995 * implementations. Note that, although this class is a function
996 * object, it isn't entirely normal because it takes only two
997 * parameters regardless of the number of parameters passed to the
998 * bind expression. The first parameter is the bound argument and
999 * the second parameter is a tuple containing references to the
1000 * rest of the arguments.
1002 template<typename _Arg,
1003 bool _IsBindExp = is_bind_expression<_Arg>::value,
1004 bool _IsPlaceholder = (is_placeholder<_Arg>::value > 0)>
1008 * If the argument is reference_wrapper<_Tp>, returns the
1009 * underlying reference. [TR1 3.6.3/5 bullet 1]
1011 template<typename _Tp>
1012 class _Mu<reference_wrapper<_Tp>, false, false>
1015 typedef _Tp& result_type;
1017 /* Note: This won't actually work for const volatile
1018 * reference_wrappers, because reference_wrapper::get() is const
1019 * but not volatile-qualified. This might be a defect in the TR.
1021 template<typename _CVRef, typename _Tuple>
1023 operator()(_CVRef& __arg, const _Tuple&) const volatile
1024 { return __arg.get(); }
1028 * If the argument is a bind expression, we invoke the underlying
1029 * function object with the same cv-qualifiers as we are given and
1030 * pass along all of our arguments (unwrapped). [TR1 3.6.3/5 bullet 2]
1032 template<typename _Arg>
1033 class _Mu<_Arg, true, false>
1036 template<typename _Signature> class result;
1038 // Determine the result type when we pass the arguments along. This
1039 // involves passing along the cv-qualifiers placed on _Mu and
1040 // unwrapping the argument bundle.
1041 template<typename _CVMu, typename _CVArg, typename... _Args>
1042 class result<_CVMu(_CVArg, tuple<_Args...>)>
1043 : public result_of<_CVArg(_Args...)> { };
1045 template<typename _CVArg, typename... _Args>
1046 typename result_of<_CVArg(_Args...)>::type
1047 operator()(_CVArg& __arg,
1048 const tuple<_Args...>& __tuple) const volatile
1050 // Construct an index tuple and forward to __call
1051 typedef typename _Build_index_tuple<sizeof...(_Args)>::__type
1053 return this->__call(__arg, __tuple, _Indexes());
1057 // Invokes the underlying function object __arg by unpacking all
1058 // of the arguments in the tuple.
1059 template<typename _CVArg, typename... _Args, int... _Indexes>
1060 typename result_of<_CVArg(_Args...)>::type
1061 __call(_CVArg& __arg, const tuple<_Args...>& __tuple,
1062 const _Index_tuple<_Indexes...>&) const volatile
1064 return __arg(tr1::get<_Indexes>(__tuple)...);
1069 * If the argument is a placeholder for the Nth argument, returns
1070 * a reference to the Nth argument to the bind function object.
1071 * [TR1 3.6.3/5 bullet 3]
1073 template<typename _Arg>
1074 class _Mu<_Arg, false, true>
1077 template<typename _Signature> class result;
1079 template<typename _CVMu, typename _CVArg, typename _Tuple>
1080 class result<_CVMu(_CVArg, _Tuple)>
1082 // Add a reference, if it hasn't already been done for us.
1083 // This allows us to be a little bit sloppy in constructing
1084 // the tuple that we pass to result_of<...>.
1085 typedef typename _Safe_tuple_element<(is_placeholder<_Arg>::value
1090 typedef typename add_reference<__base_type>::type type;
1093 template<typename _Tuple>
1094 typename result<_Mu(_Arg, _Tuple)>::type
1095 operator()(const volatile _Arg&, const _Tuple& __tuple) const volatile
1097 return ::std::tr1::get<(is_placeholder<_Arg>::value - 1)>(__tuple);
1102 * If the argument is just a value, returns a reference to that
1103 * value. The cv-qualifiers on the reference are the same as the
1104 * cv-qualifiers on the _Mu object. [TR1 3.6.3/5 bullet 4]
1106 template<typename _Arg>
1107 class _Mu<_Arg, false, false>
1110 template<typename _Signature> struct result;
1112 template<typename _CVMu, typename _CVArg, typename _Tuple>
1113 struct result<_CVMu(_CVArg, _Tuple)>
1115 typedef typename add_reference<_CVArg>::type type;
1118 // Pick up the cv-qualifiers of the argument
1119 template<typename _CVArg, typename _Tuple>
1121 operator()(_CVArg& __arg, const _Tuple&) const volatile
1126 * Maps member pointers into instances of _Mem_fn but leaves all
1127 * other function objects untouched. Used by tr1::bind(). The
1128 * primary template handles the non--member-pointer case.
1130 template<typename _Tp>
1131 struct _Maybe_wrap_member_pointer
1136 __do_wrap(const _Tp& __x)
1141 * Maps member pointers into instances of _Mem_fn but leaves all
1142 * other function objects untouched. Used by tr1::bind(). This
1143 * partial specialization handles the member pointer case.
1145 template<typename _Tp, typename _Class>
1146 struct _Maybe_wrap_member_pointer<_Tp _Class::*>
1148 typedef _Mem_fn<_Tp _Class::*> type;
1151 __do_wrap(_Tp _Class::* __pm)
1152 { return type(__pm); }
1155 /// Type of the function object returned from bind().
1156 template<typename _Signature>
1159 template<typename _Functor, typename... _Bound_args>
1160 class _Bind<_Functor(_Bound_args...)>
1161 : public _Weak_result_type<_Functor>
1163 typedef _Bind __self_type;
1164 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
1168 tuple<_Bound_args...> _M_bound_args;
1171 template<typename... _Args, int... _Indexes>
1173 _Functor(typename result_of<_Mu<_Bound_args>
1174 (_Bound_args, tuple<_Args...>)>::type...)
1176 __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>)
1178 return _M_f(_Mu<_Bound_args>()
1179 (tr1::get<_Indexes>(_M_bound_args), __args)...);
1183 template<typename... _Args, int... _Indexes>
1185 const _Functor(typename result_of<_Mu<_Bound_args>
1186 (const _Bound_args, tuple<_Args...>)
1188 __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>) const
1190 return _M_f(_Mu<_Bound_args>()
1191 (tr1::get<_Indexes>(_M_bound_args), __args)...);
1195 template<typename... _Args, int... _Indexes>
1197 volatile _Functor(typename result_of<_Mu<_Bound_args>
1198 (volatile _Bound_args, tuple<_Args...>)
1200 __call(const tuple<_Args...>& __args,
1201 _Index_tuple<_Indexes...>) volatile
1203 return _M_f(_Mu<_Bound_args>()
1204 (tr1::get<_Indexes>(_M_bound_args), __args)...);
1207 // Call as const volatile
1208 template<typename... _Args, int... _Indexes>
1210 const volatile _Functor(typename result_of<_Mu<_Bound_args>
1211 (const volatile _Bound_args,
1214 __call(const tuple<_Args...>& __args,
1215 _Index_tuple<_Indexes...>) const volatile
1217 return _M_f(_Mu<_Bound_args>()
1218 (tr1::get<_Indexes>(_M_bound_args), __args)...);
1222 explicit _Bind(_Functor __f, _Bound_args... __bound_args)
1223 : _M_f(__f), _M_bound_args(__bound_args...) { }
1226 template<typename... _Args>
1228 _Functor(typename result_of<_Mu<_Bound_args>
1229 (_Bound_args, tuple<_Args...>)>::type...)
1231 operator()(_Args&... __args)
1233 return this->__call(tr1::tie(__args...), _Bound_indexes());
1237 template<typename... _Args>
1239 const _Functor(typename result_of<_Mu<_Bound_args>
1240 (const _Bound_args, tuple<_Args...>)>::type...)
1242 operator()(_Args&... __args) const
1244 return this->__call(tr1::tie(__args...), _Bound_indexes());
1249 template<typename... _Args>
1251 volatile _Functor(typename result_of<_Mu<_Bound_args>
1252 (volatile _Bound_args, tuple<_Args...>)>::type...)
1254 operator()(_Args&... __args) volatile
1256 return this->__call(tr1::tie(__args...), _Bound_indexes());
1260 // Call as const volatile
1261 template<typename... _Args>
1263 const volatile _Functor(typename result_of<_Mu<_Bound_args>
1264 (const volatile _Bound_args,
1265 tuple<_Args...>)>::type...)
1267 operator()(_Args&... __args) const volatile
1269 return this->__call(tr1::tie(__args...), _Bound_indexes());
1273 /// Type of the function object returned from bind<R>().
1274 template<typename _Result, typename _Signature>
1275 struct _Bind_result;
1277 template<typename _Result, typename _Functor, typename... _Bound_args>
1278 class _Bind_result<_Result, _Functor(_Bound_args...)>
1280 typedef _Bind_result __self_type;
1281 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
1285 tuple<_Bound_args...> _M_bound_args;
1288 template<typename... _Args, int... _Indexes>
1290 __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>)
1292 return _M_f(_Mu<_Bound_args>()
1293 (tr1::get<_Indexes>(_M_bound_args), __args)...);
1297 template<typename... _Args, int... _Indexes>
1299 __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>) const
1301 return _M_f(_Mu<_Bound_args>()
1302 (tr1::get<_Indexes>(_M_bound_args), __args)...);
1306 template<typename... _Args, int... _Indexes>
1308 __call(const tuple<_Args...>& __args,
1309 _Index_tuple<_Indexes...>) volatile
1311 return _M_f(_Mu<_Bound_args>()
1312 (tr1::get<_Indexes>(_M_bound_args), __args)...);
1315 // Call as const volatile
1316 template<typename... _Args, int... _Indexes>
1318 __call(const tuple<_Args...>& __args,
1319 _Index_tuple<_Indexes...>) const volatile
1321 return _M_f(_Mu<_Bound_args>()
1322 (tr1::get<_Indexes>(_M_bound_args), __args)...);
1326 typedef _Result result_type;
1329 _Bind_result(_Functor __f, _Bound_args... __bound_args)
1330 : _M_f(__f), _M_bound_args(__bound_args...) { }
1333 template<typename... _Args>
1335 operator()(_Args&... __args)
1337 return this->__call(tr1::tie(__args...), _Bound_indexes());
1341 template<typename... _Args>
1343 operator()(_Args&... __args) const
1345 return this->__call(tr1::tie(__args...), _Bound_indexes());
1349 template<typename... _Args>
1351 operator()(_Args&... __args) volatile
1353 return this->__call(tr1::tie(__args...), _Bound_indexes());
1356 // Call as const volatile
1357 template<typename... _Args>
1359 operator()(_Args&... __args) const volatile
1361 return this->__call(tr1::tie(__args...), _Bound_indexes());
1365 /// Class template _Bind is always a bind expression.
1366 template<typename _Signature>
1367 struct is_bind_expression<_Bind<_Signature> >
1368 { static const bool value = true; };
1370 template<typename _Signature>
1371 const bool is_bind_expression<_Bind<_Signature> >::value;
1373 /// Class template _Bind is always a bind expression.
1374 template<typename _Signature>
1375 struct is_bind_expression<const _Bind<_Signature> >
1376 { static const bool value = true; };
1378 template<typename _Signature>
1379 const bool is_bind_expression<const _Bind<_Signature> >::value;
1381 /// Class template _Bind is always a bind expression.
1382 template<typename _Signature>
1383 struct is_bind_expression<volatile _Bind<_Signature> >
1384 { static const bool value = true; };
1386 template<typename _Signature>
1387 const bool is_bind_expression<volatile _Bind<_Signature> >::value;
1389 /// Class template _Bind is always a bind expression.
1390 template<typename _Signature>
1391 struct is_bind_expression<const volatile _Bind<_Signature> >
1392 { static const bool value = true; };
1394 template<typename _Signature>
1395 const bool is_bind_expression<const volatile _Bind<_Signature> >::value;
1397 /// Class template _Bind_result is always a bind expression.
1398 template<typename _Result, typename _Signature>
1399 struct is_bind_expression<_Bind_result<_Result, _Signature> >
1400 { static const bool value = true; };
1402 template<typename _Result, typename _Signature>
1403 const bool is_bind_expression<_Bind_result<_Result, _Signature> >::value;
1405 /// Class template _Bind_result is always a bind expression.
1406 template<typename _Result, typename _Signature>
1407 struct is_bind_expression<const _Bind_result<_Result, _Signature> >
1408 { static const bool value = true; };
1410 template<typename _Result, typename _Signature>
1412 is_bind_expression<const _Bind_result<_Result, _Signature> >::value;
1414 /// Class template _Bind_result is always a bind expression.
1415 template<typename _Result, typename _Signature>
1416 struct is_bind_expression<volatile _Bind_result<_Result, _Signature> >
1417 { static const bool value = true; };
1419 template<typename _Result, typename _Signature>
1421 is_bind_expression<volatile _Bind_result<_Result, _Signature> >::value;
1423 /// Class template _Bind_result is always a bind expression.
1424 template<typename _Result, typename _Signature>
1426 is_bind_expression<const volatile _Bind_result<_Result, _Signature> >
1427 { static const bool value = true; };
1429 template<typename _Result, typename _Signature>
1431 is_bind_expression<const volatile _Bind_result<_Result,
1432 _Signature> >::value;
1434 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1435 template<typename _Signature>
1436 struct is_bind_expression<std::_Bind<_Signature>>
1439 template<typename _Signature>
1440 struct is_bind_expression<const std::_Bind<_Signature>>
1443 template<typename _Signature>
1444 struct is_bind_expression<volatile std::_Bind<_Signature>>
1447 template<typename _Signature>
1448 struct is_bind_expression<const volatile std::_Bind<_Signature>>
1451 template<typename _Result, typename _Signature>
1452 struct is_bind_expression<std::_Bind_result<_Result, _Signature>>
1455 template<typename _Result, typename _Signature>
1456 struct is_bind_expression<const std::_Bind_result<_Result, _Signature>>
1459 template<typename _Result, typename _Signature>
1460 struct is_bind_expression<volatile std::_Bind_result<_Result, _Signature>>
1463 template<typename _Result, typename _Signature>
1464 struct is_bind_expression<const volatile std::_Bind_result<_Result,
1470 template<typename _Functor, typename... _ArgTypes>
1472 _Bind<typename _Maybe_wrap_member_pointer<_Functor>::type(_ArgTypes...)>
1473 bind(_Functor __f, _ArgTypes... __args)
1475 typedef _Maybe_wrap_member_pointer<_Functor> __maybe_type;
1476 typedef typename __maybe_type::type __functor_type;
1477 typedef _Bind<__functor_type(_ArgTypes...)> __result_type;
1478 return __result_type(__maybe_type::__do_wrap(__f), __args...);
1481 template<typename _Result, typename _Functor, typename... _ArgTypes>
1483 _Bind_result<_Result,
1484 typename _Maybe_wrap_member_pointer<_Functor>::type
1486 bind(_Functor __f, _ArgTypes... __args)
1488 typedef _Maybe_wrap_member_pointer<_Functor> __maybe_type;
1489 typedef typename __maybe_type::type __functor_type;
1490 typedef _Bind_result<_Result, __functor_type(_ArgTypes...)>
1492 return __result_type(__maybe_type::__do_wrap(__f), __args...);
1496 * @brief Exception class thrown when class template function's
1497 * operator() is called with an empty target.
1498 * @ingroup exceptions
1500 class bad_function_call : public std::exception { };
1503 * The integral constant expression 0 can be converted into a
1504 * pointer to this type. It is used by the function template to
1505 * accept NULL pointers.
1507 struct _M_clear_type;
1510 * Trait identifying @a location-invariant types, meaning that the
1511 * address of the object (or any of its members) will not escape.
1512 * Also implies a trivial copy constructor and assignment operator.
1514 template<typename _Tp>
1515 struct __is_location_invariant
1516 : integral_constant<bool,
1517 (is_pointer<_Tp>::value
1518 || is_member_pointer<_Tp>::value)>
1522 class _Undefined_class;
1527 const void* _M_const_object;
1528 void (*_M_function_pointer)();
1529 void (_Undefined_class::*_M_member_pointer)();
1534 void* _M_access() { return &_M_pod_data[0]; }
1535 const void* _M_access() const { return &_M_pod_data[0]; }
1537 template<typename _Tp>
1540 { return *static_cast<_Tp*>(_M_access()); }
1542 template<typename _Tp>
1545 { return *static_cast<const _Tp*>(_M_access()); }
1547 _Nocopy_types _M_unused;
1548 char _M_pod_data[sizeof(_Nocopy_types)];
1551 enum _Manager_operation
1559 // Simple type wrapper that helps avoid annoying const problems
1560 // when casting between void pointers and pointers-to-pointers.
1561 template<typename _Tp>
1562 struct _Simple_type_wrapper
1564 _Simple_type_wrapper(_Tp __value) : __value(__value) { }
1569 template<typename _Tp>
1570 struct __is_location_invariant<_Simple_type_wrapper<_Tp> >
1571 : __is_location_invariant<_Tp>
1575 // Converts a reference to a function object into a callable
1577 template<typename _Functor>
1579 __callable_functor(_Functor& __f)
1582 template<typename _Member, typename _Class>
1583 inline _Mem_fn<_Member _Class::*>
1584 __callable_functor(_Member _Class::* &__p)
1585 { return mem_fn(__p); }
1587 template<typename _Member, typename _Class>
1588 inline _Mem_fn<_Member _Class::*>
1589 __callable_functor(_Member _Class::* const &__p)
1590 { return mem_fn(__p); }
1592 template<typename _Signature>
1595 /// Base class of all polymorphic function object wrappers.
1596 class _Function_base
1599 static const std::size_t _M_max_size = sizeof(_Nocopy_types);
1600 static const std::size_t _M_max_align = __alignof__(_Nocopy_types);
1602 template<typename _Functor>
1606 static const bool __stored_locally =
1607 (__is_location_invariant<_Functor>::value
1608 && sizeof(_Functor) <= _M_max_size
1609 && __alignof__(_Functor) <= _M_max_align
1610 && (_M_max_align % __alignof__(_Functor) == 0));
1612 typedef integral_constant<bool, __stored_locally> _Local_storage;
1614 // Retrieve a pointer to the function object
1616 _M_get_pointer(const _Any_data& __source)
1618 const _Functor* __ptr =
1619 __stored_locally? &__source._M_access<_Functor>()
1620 /* have stored a pointer */ : __source._M_access<_Functor*>();
1621 return const_cast<_Functor*>(__ptr);
1624 // Clone a location-invariant function object that fits within
1625 // an _Any_data structure.
1627 _M_clone(_Any_data& __dest, const _Any_data& __source, true_type)
1629 new (__dest._M_access()) _Functor(__source._M_access<_Functor>());
1632 // Clone a function object that is not location-invariant or
1633 // that cannot fit into an _Any_data structure.
1635 _M_clone(_Any_data& __dest, const _Any_data& __source, false_type)
1637 __dest._M_access<_Functor*>() =
1638 new _Functor(*__source._M_access<_Functor*>());
1641 // Destroying a location-invariant object may still require
1644 _M_destroy(_Any_data& __victim, true_type)
1646 __victim._M_access<_Functor>().~_Functor();
1649 // Destroying an object located on the heap.
1651 _M_destroy(_Any_data& __victim, false_type)
1653 delete __victim._M_access<_Functor*>();
1658 _M_manager(_Any_data& __dest, const _Any_data& __source,
1659 _Manager_operation __op)
1664 case __get_type_info:
1665 __dest._M_access<const type_info*>() = &typeid(_Functor);
1668 case __get_functor_ptr:
1669 __dest._M_access<_Functor*>() = _M_get_pointer(__source);
1672 case __clone_functor:
1673 _M_clone(__dest, __source, _Local_storage());
1676 case __destroy_functor:
1677 _M_destroy(__dest, _Local_storage());
1684 _M_init_functor(_Any_data& __functor, const _Functor& __f)
1685 { _M_init_functor(__functor, __f, _Local_storage()); }
1687 template<typename _Signature>
1689 _M_not_empty_function(const function<_Signature>& __f)
1690 { return static_cast<bool>(__f); }
1692 template<typename _Tp>
1694 _M_not_empty_function(const _Tp*& __fp)
1697 template<typename _Class, typename _Tp>
1699 _M_not_empty_function(_Tp _Class::* const& __mp)
1702 template<typename _Tp>
1704 _M_not_empty_function(const _Tp&)
1709 _M_init_functor(_Any_data& __functor, const _Functor& __f, true_type)
1710 { new (__functor._M_access()) _Functor(__f); }
1713 _M_init_functor(_Any_data& __functor, const _Functor& __f, false_type)
1714 { __functor._M_access<_Functor*>() = new _Functor(__f); }
1717 template<typename _Functor>
1718 class _Ref_manager : public _Base_manager<_Functor*>
1720 typedef _Function_base::_Base_manager<_Functor*> _Base;
1724 _M_manager(_Any_data& __dest, const _Any_data& __source,
1725 _Manager_operation __op)
1730 case __get_type_info:
1731 __dest._M_access<const type_info*>() = &typeid(_Functor);
1734 case __get_functor_ptr:
1735 __dest._M_access<_Functor*>() = *_Base::_M_get_pointer(__source);
1736 return is_const<_Functor>::value;
1740 _Base::_M_manager(__dest, __source, __op);
1746 _M_init_functor(_Any_data& __functor, reference_wrapper<_Functor> __f)
1748 // TBD: Use address_of function instead.
1749 _Base::_M_init_functor(__functor, &__f.get());
1753 _Function_base() : _M_manager(0) { }
1758 _M_manager(_M_functor, _M_functor, __destroy_functor);
1762 bool _M_empty() const { return !_M_manager; }
1764 typedef bool (*_Manager_type)(_Any_data&, const _Any_data&,
1765 _Manager_operation);
1767 _Any_data _M_functor;
1768 _Manager_type _M_manager;
1771 template<typename _Signature, typename _Functor>
1772 class _Function_handler;
1774 template<typename _Res, typename _Functor, typename... _ArgTypes>
1775 class _Function_handler<_Res(_ArgTypes...), _Functor>
1776 : public _Function_base::_Base_manager<_Functor>
1778 typedef _Function_base::_Base_manager<_Functor> _Base;
1782 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1784 return (*_Base::_M_get_pointer(__functor))(__args...);
1788 template<typename _Functor, typename... _ArgTypes>
1789 class _Function_handler<void(_ArgTypes...), _Functor>
1790 : public _Function_base::_Base_manager<_Functor>
1792 typedef _Function_base::_Base_manager<_Functor> _Base;
1796 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1798 (*_Base::_M_get_pointer(__functor))(__args...);
1802 template<typename _Res, typename _Functor, typename... _ArgTypes>
1803 class _Function_handler<_Res(_ArgTypes...), reference_wrapper<_Functor> >
1804 : public _Function_base::_Ref_manager<_Functor>
1806 typedef _Function_base::_Ref_manager<_Functor> _Base;
1810 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1813 __callable_functor(**_Base::_M_get_pointer(__functor))(__args...);
1817 template<typename _Functor, typename... _ArgTypes>
1818 class _Function_handler<void(_ArgTypes...), reference_wrapper<_Functor> >
1819 : public _Function_base::_Ref_manager<_Functor>
1821 typedef _Function_base::_Ref_manager<_Functor> _Base;
1825 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1827 __callable_functor(**_Base::_M_get_pointer(__functor))(__args...);
1831 template<typename _Class, typename _Member, typename _Res,
1832 typename... _ArgTypes>
1833 class _Function_handler<_Res(_ArgTypes...), _Member _Class::*>
1834 : public _Function_handler<void(_ArgTypes...), _Member _Class::*>
1836 typedef _Function_handler<void(_ArgTypes...), _Member _Class::*>
1841 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1844 mem_fn(_Base::_M_get_pointer(__functor)->__value)(__args...);
1848 template<typename _Class, typename _Member, typename... _ArgTypes>
1849 class _Function_handler<void(_ArgTypes...), _Member _Class::*>
1850 : public _Function_base::_Base_manager<
1851 _Simple_type_wrapper< _Member _Class::* > >
1853 typedef _Member _Class::* _Functor;
1854 typedef _Simple_type_wrapper<_Functor> _Wrapper;
1855 typedef _Function_base::_Base_manager<_Wrapper> _Base;
1859 _M_manager(_Any_data& __dest, const _Any_data& __source,
1860 _Manager_operation __op)
1865 case __get_type_info:
1866 __dest._M_access<const type_info*>() = &typeid(_Functor);
1869 case __get_functor_ptr:
1870 __dest._M_access<_Functor*>() =
1871 &_Base::_M_get_pointer(__source)->__value;
1875 _Base::_M_manager(__dest, __source, __op);
1881 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1883 tr1::mem_fn(_Base::_M_get_pointer(__functor)->__value)(__args...);
1888 template<typename _Res, typename... _ArgTypes>
1889 class function<_Res(_ArgTypes...)>
1890 : public _Maybe_unary_or_binary_function<_Res, _ArgTypes...>,
1891 private _Function_base
1893 #ifndef __GXX_EXPERIMENTAL_CXX0X__
1894 /// This class is used to implement the safe_bool idiom.
1897 _Hidden_type* _M_bool;
1900 /// This typedef is used to implement the safe_bool idiom.
1901 typedef _Hidden_type* _Hidden_type::* _Safe_bool;
1904 typedef _Res _Signature_type(_ArgTypes...);
1906 struct _Useless { };
1909 typedef _Res result_type;
1911 // [3.7.2.1] construct/copy/destroy
1914 * @brief Default construct creates an empty function call wrapper.
1915 * @post @c !(bool)*this
1917 function() : _Function_base() { }
1920 * @brief Default construct creates an empty function call wrapper.
1921 * @post @c !(bool)*this
1923 function(_M_clear_type*) : _Function_base() { }
1926 * @brief %Function copy constructor.
1927 * @param x A %function object with identical call signature.
1928 * @post @c (bool)*this == (bool)x
1930 * The newly-created %function contains a copy of the target of @a
1931 * x (if it has one).
1933 function(const function& __x);
1936 * @brief Builds a %function that targets a copy of the incoming
1938 * @param f A %function object that is callable with parameters of
1939 * type @c T1, @c T2, ..., @c TN and returns a value convertible
1942 * The newly-created %function object will target a copy of @a
1943 * f. If @a f is @c reference_wrapper<F>, then this function
1944 * object will contain a reference to the function object @c
1945 * f.get(). If @a f is a NULL function pointer or NULL
1946 * pointer-to-member, the newly-created object will be empty.
1948 * If @a f is a non-NULL function pointer or an object of type @c
1949 * reference_wrapper<F>, this function will not throw.
1951 template<typename _Functor>
1952 function(_Functor __f,
1953 typename __gnu_cxx::__enable_if<
1954 !is_integral<_Functor>::value, _Useless>::__type
1958 * @brief %Function assignment operator.
1959 * @param x A %function with identical call signature.
1960 * @post @c (bool)*this == (bool)x
1963 * The target of @a x is copied to @c *this. If @a x has no
1964 * target, then @c *this will be empty.
1966 * If @a x targets a function pointer or a reference to a function
1967 * object, then this operation will not throw an %exception.
1970 operator=(const function& __x)
1972 function(__x).swap(*this);
1977 * @brief %Function assignment to zero.
1978 * @post @c !(bool)*this
1981 * The target of @c *this is deallocated, leaving it empty.
1984 operator=(_M_clear_type*)
1988 _M_manager(_M_functor, _M_functor, __destroy_functor);
1996 * @brief %Function assignment to a new target.
1997 * @param f A %function object that is callable with parameters of
1998 * type @c T1, @c T2, ..., @c TN and returns a value convertible
2002 * This %function object wrapper will target a copy of @a
2003 * f. If @a f is @c reference_wrapper<F>, then this function
2004 * object will contain a reference to the function object @c
2005 * f.get(). If @a f is a NULL function pointer or NULL
2006 * pointer-to-member, @c this object will be empty.
2008 * If @a f is a non-NULL function pointer or an object of type @c
2009 * reference_wrapper<F>, this function will not throw.
2011 template<typename _Functor>
2012 typename __gnu_cxx::__enable_if<!is_integral<_Functor>::value,
2014 operator=(_Functor __f)
2016 function(__f).swap(*this);
2020 // [3.7.2.2] function modifiers
2023 * @brief Swap the targets of two %function objects.
2024 * @param f A %function with identical call signature.
2026 * Swap the targets of @c this function object and @a f. This
2027 * function will not throw an %exception.
2029 void swap(function& __x)
2031 std::swap(_M_functor, __x._M_functor);
2032 std::swap(_M_manager, __x._M_manager);
2033 std::swap(_M_invoker, __x._M_invoker);
2036 // [3.7.2.3] function capacity
2039 * @brief Determine if the %function wrapper has a target.
2041 * @return @c true when this %function object contains a target,
2042 * or @c false when it is empty.
2044 * This function will not throw an %exception.
2046 #ifdef __GXX_EXPERIMENTAL_CXX0X__
2047 explicit operator bool() const
2048 { return !_M_empty(); }
2050 operator _Safe_bool() const
2055 return &_Hidden_type::_M_bool;
2059 // [3.7.2.4] function invocation
2062 * @brief Invokes the function targeted by @c *this.
2063 * @returns the result of the target.
2064 * @throws bad_function_call when @c !(bool)*this
2066 * The function call operator invokes the target function object
2067 * stored by @c this.
2069 _Res operator()(_ArgTypes... __args) const;
2072 // [3.7.2.5] function target access
2074 * @brief Determine the type of the target of this function object
2077 * @returns the type identifier of the target function object, or
2078 * @c typeid(void) if @c !(bool)*this.
2080 * This function will not throw an %exception.
2082 const type_info& target_type() const;
2085 * @brief Access the stored target function object.
2087 * @return Returns a pointer to the stored target function object,
2088 * if @c typeid(Functor).equals(target_type()); otherwise, a NULL
2091 * This function will not throw an %exception.
2093 template<typename _Functor> _Functor* target();
2096 template<typename _Functor> const _Functor* target() const;
2100 // [3.7.2.6] undefined operators
2101 template<typename _Function>
2102 void operator==(const function<_Function>&) const;
2103 template<typename _Function>
2104 void operator!=(const function<_Function>&) const;
2106 typedef _Res (*_Invoker_type)(const _Any_data&, _ArgTypes...);
2107 _Invoker_type _M_invoker;
2110 template<typename _Res, typename... _ArgTypes>
2111 function<_Res(_ArgTypes...)>::
2112 function(const function& __x)
2115 if (static_cast<bool>(__x))
2117 _M_invoker = __x._M_invoker;
2118 _M_manager = __x._M_manager;
2119 __x._M_manager(_M_functor, __x._M_functor, __clone_functor);
2123 template<typename _Res, typename... _ArgTypes>
2124 template<typename _Functor>
2125 function<_Res(_ArgTypes...)>::
2126 function(_Functor __f,
2127 typename __gnu_cxx::__enable_if<
2128 !is_integral<_Functor>::value, _Useless>::__type)
2131 typedef _Function_handler<_Signature_type, _Functor> _My_handler;
2133 if (_My_handler::_M_not_empty_function(__f))
2135 _M_invoker = &_My_handler::_M_invoke;
2136 _M_manager = &_My_handler::_M_manager;
2137 _My_handler::_M_init_functor(_M_functor, __f);
2141 template<typename _Res, typename... _ArgTypes>
2143 function<_Res(_ArgTypes...)>::
2144 operator()(_ArgTypes... __args) const
2149 throw bad_function_call();
2154 return _M_invoker(_M_functor, __args...);
2158 template<typename _Res, typename... _ArgTypes>
2160 function<_Res(_ArgTypes...)>::
2165 _Any_data __typeinfo_result;
2166 _M_manager(__typeinfo_result, _M_functor, __get_type_info);
2167 return *__typeinfo_result._M_access<const type_info*>();
2170 return typeid(void);
2173 template<typename _Res, typename... _ArgTypes>
2174 template<typename _Functor>
2176 function<_Res(_ArgTypes...)>::
2179 if (typeid(_Functor) == target_type() && _M_manager)
2182 if (_M_manager(__ptr, _M_functor, __get_functor_ptr)
2183 && !is_const<_Functor>::value)
2186 return __ptr._M_access<_Functor*>();
2192 template<typename _Res, typename... _ArgTypes>
2193 template<typename _Functor>
2195 function<_Res(_ArgTypes...)>::
2198 if (typeid(_Functor) == target_type() && _M_manager)
2201 _M_manager(__ptr, _M_functor, __get_functor_ptr);
2202 return __ptr._M_access<const _Functor*>();
2209 // [3.7.2.7] null pointer comparisons
2212 * @brief Compares a polymorphic function object wrapper against 0
2213 * (the NULL pointer).
2214 * @returns @c true if the wrapper has no target, @c false otherwise
2216 * This function will not throw an %exception.
2218 template<typename _Signature>
2220 operator==(const function<_Signature>& __f, _M_clear_type*)
2221 { return !static_cast<bool>(__f); }
2224 template<typename _Signature>
2226 operator==(_M_clear_type*, const function<_Signature>& __f)
2227 { return !static_cast<bool>(__f); }
2230 * @brief Compares a polymorphic function object wrapper against 0
2231 * (the NULL pointer).
2232 * @returns @c false if the wrapper has no target, @c true otherwise
2234 * This function will not throw an %exception.
2236 template<typename _Signature>
2238 operator!=(const function<_Signature>& __f, _M_clear_type*)
2239 { return static_cast<bool>(__f); }
2242 template<typename _Signature>
2244 operator!=(_M_clear_type*, const function<_Signature>& __f)
2245 { return static_cast<bool>(__f); }
2247 // [3.7.2.8] specialized algorithms
2250 * @brief Swap the targets of two polymorphic function object wrappers.
2252 * This function will not throw an %exception.
2254 template<typename _Signature>
2256 swap(function<_Signature>& __x, function<_Signature>& __y)
2259 _GLIBCXX_END_NAMESPACE_VERSION
2262 #ifdef __GXX_EXPERIMENTAL_CXX0X__
2263 _GLIBCXX_BEGIN_NAMESPACE_VERSION
2265 template<typename> struct is_placeholder;
2268 struct is_placeholder<tr1::_Placeholder<_Num>>
2269 : integral_constant<int, _Num>
2273 struct is_placeholder<const tr1::_Placeholder<_Num>>
2274 : integral_constant<int, _Num>
2277 template<typename> struct is_bind_expression;
2279 template<typename _Signature>
2280 struct is_bind_expression<tr1::_Bind<_Signature>>
2283 template<typename _Signature>
2284 struct is_bind_expression<const tr1::_Bind<_Signature>>
2287 template<typename _Signature>
2288 struct is_bind_expression<volatile tr1::_Bind<_Signature>>
2291 template<typename _Signature>
2292 struct is_bind_expression<const volatile tr1::_Bind<_Signature>>
2295 template<typename _Result, typename _Signature>
2296 struct is_bind_expression<tr1::_Bind_result<_Result, _Signature>>
2299 template<typename _Result, typename _Signature>
2300 struct is_bind_expression<const tr1::_Bind_result<_Result, _Signature>>
2303 template<typename _Result, typename _Signature>
2304 struct is_bind_expression<volatile tr1::_Bind_result<_Result, _Signature>>
2307 template<typename _Result, typename _Signature>
2308 struct is_bind_expression<const volatile tr1::_Bind_result<_Result,
2312 _GLIBCXX_END_NAMESPACE_VERSION
2316 #endif // _GLIBCXX_TR1_FUNCTIONAL