1 // <functional> -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
4 // 2011 Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 // <http://www.gnu.org/licenses/>.
28 * Silicon Graphics Computer Systems, Inc.
30 * Permission to use, copy, modify, distribute and sell this software
31 * and its documentation for any purpose is hereby granted without fee,
32 * provided that the above copyright notice appear in all copies and
33 * that both that copyright notice and this permission notice appear
34 * in supporting documentation. Silicon Graphics makes no
35 * representations about the suitability of this software for any
36 * purpose. It is provided "as is" without express or implied warranty.
40 /** @file include/functional
41 * This is a Standard C++ Library header.
44 #ifndef _GLIBCXX_FUNCTIONAL
45 #define _GLIBCXX_FUNCTIONAL 1
47 #pragma GCC system_header
49 #include <bits/c++config.h>
50 #include <bits/stl_function.h>
52 #ifdef __GXX_EXPERIMENTAL_CXX0X__
57 #include <type_traits>
58 #include <bits/functexcept.h>
59 #include <bits/functional_hash.h>
61 namespace std _GLIBCXX_VISIBILITY(default)
63 _GLIBCXX_BEGIN_NAMESPACE_VERSION
65 template<typename _MemberPointer>
67 template<typename _Tp, typename _Class>
68 _Mem_fn<_Tp _Class::*>
69 mem_fn(_Tp _Class::*);
71 _GLIBCXX_HAS_NESTED_TYPE(result_type)
73 /// If we have found a result_type, extract it.
74 template<bool _Has_result_type, typename _Functor>
75 struct _Maybe_get_result_type
78 template<typename _Functor>
79 struct _Maybe_get_result_type<true, _Functor>
80 { typedef typename _Functor::result_type result_type; };
83 * Base class for any function object that has a weak result type, as
84 * defined in 3.3/3 of TR1.
86 template<typename _Functor>
87 struct _Weak_result_type_impl
88 : _Maybe_get_result_type<__has_result_type<_Functor>::value, _Functor>
91 /// Retrieve the result type for a function type.
92 template<typename _Res, typename... _ArgTypes>
93 struct _Weak_result_type_impl<_Res(_ArgTypes...)>
94 { typedef _Res result_type; };
96 template<typename _Res, typename... _ArgTypes>
97 struct _Weak_result_type_impl<_Res(_ArgTypes......)>
98 { typedef _Res result_type; };
100 template<typename _Res, typename... _ArgTypes>
101 struct _Weak_result_type_impl<_Res(_ArgTypes...) const>
102 { typedef _Res result_type; };
104 template<typename _Res, typename... _ArgTypes>
105 struct _Weak_result_type_impl<_Res(_ArgTypes......) const>
106 { typedef _Res result_type; };
108 template<typename _Res, typename... _ArgTypes>
109 struct _Weak_result_type_impl<_Res(_ArgTypes...) volatile>
110 { typedef _Res result_type; };
112 template<typename _Res, typename... _ArgTypes>
113 struct _Weak_result_type_impl<_Res(_ArgTypes......) volatile>
114 { typedef _Res result_type; };
116 template<typename _Res, typename... _ArgTypes>
117 struct _Weak_result_type_impl<_Res(_ArgTypes...) const volatile>
118 { typedef _Res result_type; };
120 template<typename _Res, typename... _ArgTypes>
121 struct _Weak_result_type_impl<_Res(_ArgTypes......) const volatile>
122 { typedef _Res result_type; };
124 /// Retrieve the result type for a function reference.
125 template<typename _Res, typename... _ArgTypes>
126 struct _Weak_result_type_impl<_Res(&)(_ArgTypes...)>
127 { typedef _Res result_type; };
129 template<typename _Res, typename... _ArgTypes>
130 struct _Weak_result_type_impl<_Res(&)(_ArgTypes......)>
131 { typedef _Res result_type; };
133 /// Retrieve the result type for a function pointer.
134 template<typename _Res, typename... _ArgTypes>
135 struct _Weak_result_type_impl<_Res(*)(_ArgTypes...)>
136 { typedef _Res result_type; };
138 template<typename _Res, typename... _ArgTypes>
139 struct _Weak_result_type_impl<_Res(*)(_ArgTypes......)>
140 { typedef _Res result_type; };
142 /// Retrieve result type for a member function pointer.
143 template<typename _Res, typename _Class, typename... _ArgTypes>
144 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...)>
145 { typedef _Res result_type; };
147 template<typename _Res, typename _Class, typename... _ArgTypes>
148 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes......)>
149 { typedef _Res result_type; };
151 /// Retrieve result type for a const member function pointer.
152 template<typename _Res, typename _Class, typename... _ArgTypes>
153 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) const>
154 { typedef _Res result_type; };
156 template<typename _Res, typename _Class, typename... _ArgTypes>
157 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes......) const>
158 { typedef _Res result_type; };
160 /// Retrieve result type for a volatile member function pointer.
161 template<typename _Res, typename _Class, typename... _ArgTypes>
162 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) volatile>
163 { typedef _Res result_type; };
165 template<typename _Res, typename _Class, typename... _ArgTypes>
166 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes......) volatile>
167 { typedef _Res result_type; };
169 /// Retrieve result type for a const volatile member function pointer.
170 template<typename _Res, typename _Class, typename... _ArgTypes>
171 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...)
173 { typedef _Res result_type; };
175 template<typename _Res, typename _Class, typename... _ArgTypes>
176 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes......)
178 { typedef _Res result_type; };
181 * Strip top-level cv-qualifiers from the function object and let
182 * _Weak_result_type_impl perform the real work.
184 template<typename _Functor>
185 struct _Weak_result_type
186 : _Weak_result_type_impl<typename remove_cv<_Functor>::type>
189 /// Determines if the type _Tp derives from unary_function.
190 template<typename _Tp>
191 struct _Derives_from_unary_function : __sfinae_types
194 template<typename _T1, typename _Res>
195 static __one __test(const volatile unary_function<_T1, _Res>*);
197 // It's tempting to change "..." to const volatile void*, but
198 // that fails when _Tp is a function type.
199 static __two __test(...);
202 static const bool value = sizeof(__test((_Tp*)0)) == 1;
205 /// Determines if the type _Tp derives from binary_function.
206 template<typename _Tp>
207 struct _Derives_from_binary_function : __sfinae_types
210 template<typename _T1, typename _T2, typename _Res>
211 static __one __test(const volatile binary_function<_T1, _T2, _Res>*);
213 // It's tempting to change "..." to const volatile void*, but
214 // that fails when _Tp is a function type.
215 static __two __test(...);
218 static const bool value = sizeof(__test((_Tp*)0)) == 1;
222 * Invoke a function object, which may be either a member pointer or a
223 * function object. The first parameter will tell which.
225 template<typename _Functor, typename... _Args>
228 (!is_member_pointer<_Functor>::value
229 && !is_function<_Functor>::value
230 && !is_function<typename remove_pointer<_Functor>::type>::value),
231 typename result_of<_Functor(_Args&&...)>::type
233 __invoke(_Functor& __f, _Args&&... __args)
235 return __f(std::forward<_Args>(__args)...);
238 template<typename _Functor, typename... _Args>
241 (is_member_pointer<_Functor>::value
242 && !is_function<_Functor>::value
243 && !is_function<typename remove_pointer<_Functor>::type>::value),
244 typename result_of<_Functor(_Args&&...)>::type
246 __invoke(_Functor& __f, _Args&&... __args)
248 return mem_fn(__f)(std::forward<_Args>(__args)...);
251 // To pick up function references (that will become function pointers)
252 template<typename _Functor, typename... _Args>
255 (is_pointer<_Functor>::value
256 && is_function<typename remove_pointer<_Functor>::type>::value),
257 typename result_of<_Functor(_Args&&...)>::type
259 __invoke(_Functor __f, _Args&&... __args)
261 return __f(std::forward<_Args>(__args)...);
265 * Knowing which of unary_function and binary_function _Tp derives
266 * from, derives from the same and ensures that reference_wrapper
267 * will have a weak result type. See cases below.
269 template<bool _Unary, bool _Binary, typename _Tp>
270 struct _Reference_wrapper_base_impl;
272 // None of the nested argument types.
273 template<typename _Tp>
274 struct _Reference_wrapper_base_impl<false, false, _Tp>
275 : _Weak_result_type<_Tp>
278 // Nested argument_type only.
279 template<typename _Tp>
280 struct _Reference_wrapper_base_impl<true, false, _Tp>
281 : _Weak_result_type<_Tp>
283 typedef typename _Tp::argument_type argument_type;
286 // Nested first_argument_type and second_argument_type only.
287 template<typename _Tp>
288 struct _Reference_wrapper_base_impl<false, true, _Tp>
289 : _Weak_result_type<_Tp>
291 typedef typename _Tp::first_argument_type first_argument_type;
292 typedef typename _Tp::second_argument_type second_argument_type;
295 // All the nested argument types.
296 template<typename _Tp>
297 struct _Reference_wrapper_base_impl<true, true, _Tp>
298 : _Weak_result_type<_Tp>
300 typedef typename _Tp::argument_type argument_type;
301 typedef typename _Tp::first_argument_type first_argument_type;
302 typedef typename _Tp::second_argument_type second_argument_type;
305 _GLIBCXX_HAS_NESTED_TYPE(argument_type)
306 _GLIBCXX_HAS_NESTED_TYPE(first_argument_type)
307 _GLIBCXX_HAS_NESTED_TYPE(second_argument_type)
310 * Derives from unary_function or binary_function when it
311 * can. Specializations handle all of the easy cases. The primary
312 * template determines what to do with a class type, which may
313 * derive from both unary_function and binary_function.
315 template<typename _Tp>
316 struct _Reference_wrapper_base
317 : _Reference_wrapper_base_impl<
318 __has_argument_type<_Tp>::value,
319 __has_first_argument_type<_Tp>::value
320 && __has_second_argument_type<_Tp>::value,
324 // - a function type (unary)
325 template<typename _Res, typename _T1>
326 struct _Reference_wrapper_base<_Res(_T1)>
327 : unary_function<_T1, _Res>
330 template<typename _Res, typename _T1>
331 struct _Reference_wrapper_base<_Res(_T1) const>
332 : unary_function<_T1, _Res>
335 template<typename _Res, typename _T1>
336 struct _Reference_wrapper_base<_Res(_T1) volatile>
337 : unary_function<_T1, _Res>
340 template<typename _Res, typename _T1>
341 struct _Reference_wrapper_base<_Res(_T1) const volatile>
342 : unary_function<_T1, _Res>
345 // - a function type (binary)
346 template<typename _Res, typename _T1, typename _T2>
347 struct _Reference_wrapper_base<_Res(_T1, _T2)>
348 : binary_function<_T1, _T2, _Res>
351 template<typename _Res, typename _T1, typename _T2>
352 struct _Reference_wrapper_base<_Res(_T1, _T2) const>
353 : binary_function<_T1, _T2, _Res>
356 template<typename _Res, typename _T1, typename _T2>
357 struct _Reference_wrapper_base<_Res(_T1, _T2) volatile>
358 : binary_function<_T1, _T2, _Res>
361 template<typename _Res, typename _T1, typename _T2>
362 struct _Reference_wrapper_base<_Res(_T1, _T2) const volatile>
363 : binary_function<_T1, _T2, _Res>
366 // - a function pointer type (unary)
367 template<typename _Res, typename _T1>
368 struct _Reference_wrapper_base<_Res(*)(_T1)>
369 : unary_function<_T1, _Res>
372 // - a function pointer type (binary)
373 template<typename _Res, typename _T1, typename _T2>
374 struct _Reference_wrapper_base<_Res(*)(_T1, _T2)>
375 : binary_function<_T1, _T2, _Res>
378 // - a pointer to member function type (unary, no qualifiers)
379 template<typename _Res, typename _T1>
380 struct _Reference_wrapper_base<_Res (_T1::*)()>
381 : unary_function<_T1*, _Res>
384 // - a pointer to member function type (binary, no qualifiers)
385 template<typename _Res, typename _T1, typename _T2>
386 struct _Reference_wrapper_base<_Res (_T1::*)(_T2)>
387 : binary_function<_T1*, _T2, _Res>
390 // - a pointer to member function type (unary, const)
391 template<typename _Res, typename _T1>
392 struct _Reference_wrapper_base<_Res (_T1::*)() const>
393 : unary_function<const _T1*, _Res>
396 // - a pointer to member function type (binary, const)
397 template<typename _Res, typename _T1, typename _T2>
398 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const>
399 : binary_function<const _T1*, _T2, _Res>
402 // - a pointer to member function type (unary, volatile)
403 template<typename _Res, typename _T1>
404 struct _Reference_wrapper_base<_Res (_T1::*)() volatile>
405 : unary_function<volatile _T1*, _Res>
408 // - a pointer to member function type (binary, volatile)
409 template<typename _Res, typename _T1, typename _T2>
410 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) volatile>
411 : binary_function<volatile _T1*, _T2, _Res>
414 // - a pointer to member function type (unary, const volatile)
415 template<typename _Res, typename _T1>
416 struct _Reference_wrapper_base<_Res (_T1::*)() const volatile>
417 : unary_function<const volatile _T1*, _Res>
420 // - a pointer to member function type (binary, const volatile)
421 template<typename _Res, typename _T1, typename _T2>
422 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const volatile>
423 : binary_function<const volatile _T1*, _T2, _Res>
427 * @brief Primary class template for reference_wrapper.
431 template<typename _Tp>
432 class reference_wrapper
433 : public _Reference_wrapper_base<typename remove_cv<_Tp>::type>
440 reference_wrapper(_Tp& __indata)
441 : _M_data(std::__addressof(__indata))
444 reference_wrapper(_Tp&&) = delete;
446 reference_wrapper(const reference_wrapper<_Tp>& __inref):
447 _M_data(__inref._M_data)
451 operator=(const reference_wrapper<_Tp>& __inref)
453 _M_data = __inref._M_data;
457 operator _Tp&() const
458 { return this->get(); }
464 template<typename... _Args>
465 typename result_of<_Tp&(_Args&&...)>::type
466 operator()(_Args&&... __args) const
468 return __invoke(get(), std::forward<_Args>(__args)...);
473 /// Denotes a reference should be taken to a variable.
474 template<typename _Tp>
475 inline reference_wrapper<_Tp>
477 { return reference_wrapper<_Tp>(__t); }
479 /// Denotes a const reference should be taken to a variable.
480 template<typename _Tp>
481 inline reference_wrapper<const _Tp>
483 { return reference_wrapper<const _Tp>(__t); }
485 template<typename _Tp>
486 void ref(const _Tp&&) = delete;
488 template<typename _Tp>
489 void cref(const _Tp&&) = delete;
491 /// Partial specialization.
492 template<typename _Tp>
493 inline reference_wrapper<_Tp>
494 ref(reference_wrapper<_Tp> __t)
495 { return ref(__t.get()); }
497 /// Partial specialization.
498 template<typename _Tp>
499 inline reference_wrapper<const _Tp>
500 cref(reference_wrapper<_Tp> __t)
501 { return cref(__t.get()); }
506 * Derives from @c unary_function or @c binary_function, or perhaps
507 * nothing, depending on the number of arguments provided. The
508 * primary template is the basis case, which derives nothing.
510 template<typename _Res, typename... _ArgTypes>
511 struct _Maybe_unary_or_binary_function { };
513 /// Derives from @c unary_function, as appropriate.
514 template<typename _Res, typename _T1>
515 struct _Maybe_unary_or_binary_function<_Res, _T1>
516 : std::unary_function<_T1, _Res> { };
518 /// Derives from @c binary_function, as appropriate.
519 template<typename _Res, typename _T1, typename _T2>
520 struct _Maybe_unary_or_binary_function<_Res, _T1, _T2>
521 : std::binary_function<_T1, _T2, _Res> { };
523 /// Implementation of @c mem_fn for member function pointers.
524 template<typename _Res, typename _Class, typename... _ArgTypes>
525 class _Mem_fn<_Res (_Class::*)(_ArgTypes...)>
526 : public _Maybe_unary_or_binary_function<_Res, _Class*, _ArgTypes...>
528 typedef _Res (_Class::*_Functor)(_ArgTypes...);
530 template<typename _Tp>
532 _M_call(_Tp& __object, const volatile _Class *,
533 _ArgTypes... __args) const
534 { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); }
536 template<typename _Tp>
538 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
539 { return ((*__ptr).*__pmf)(std::forward<_ArgTypes>(__args)...); }
542 typedef _Res result_type;
544 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
548 operator()(_Class& __object, _ArgTypes... __args) const
549 { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); }
553 operator()(_Class* __object, _ArgTypes... __args) const
554 { return (__object->*__pmf)(std::forward<_ArgTypes>(__args)...); }
556 // Handle smart pointers, references and pointers to derived
557 template<typename _Tp>
559 operator()(_Tp& __object, _ArgTypes... __args) const
561 return _M_call(__object, &__object,
562 std::forward<_ArgTypes>(__args)...);
569 /// Implementation of @c mem_fn for const member function pointers.
570 template<typename _Res, typename _Class, typename... _ArgTypes>
571 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) const>
572 : public _Maybe_unary_or_binary_function<_Res, const _Class*,
575 typedef _Res (_Class::*_Functor)(_ArgTypes...) const;
577 template<typename _Tp>
579 _M_call(_Tp& __object, const volatile _Class *,
580 _ArgTypes... __args) const
581 { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); }
583 template<typename _Tp>
585 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
586 { return ((*__ptr).*__pmf)(std::forward<_ArgTypes>(__args)...); }
589 typedef _Res result_type;
591 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
595 operator()(const _Class& __object, _ArgTypes... __args) const
596 { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); }
600 operator()(const _Class* __object, _ArgTypes... __args) const
601 { return (__object->*__pmf)(std::forward<_ArgTypes>(__args)...); }
603 // Handle smart pointers, references and pointers to derived
604 template<typename _Tp>
605 _Res operator()(_Tp& __object, _ArgTypes... __args) const
607 return _M_call(__object, &__object,
608 std::forward<_ArgTypes>(__args)...);
615 /// Implementation of @c mem_fn for volatile member function pointers.
616 template<typename _Res, typename _Class, typename... _ArgTypes>
617 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) volatile>
618 : public _Maybe_unary_or_binary_function<_Res, volatile _Class*,
621 typedef _Res (_Class::*_Functor)(_ArgTypes...) volatile;
623 template<typename _Tp>
625 _M_call(_Tp& __object, const volatile _Class *,
626 _ArgTypes... __args) const
627 { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); }
629 template<typename _Tp>
631 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
632 { return ((*__ptr).*__pmf)(std::forward<_ArgTypes>(__args)...); }
635 typedef _Res result_type;
637 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
641 operator()(volatile _Class& __object, _ArgTypes... __args) const
642 { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); }
646 operator()(volatile _Class* __object, _ArgTypes... __args) const
647 { return (__object->*__pmf)(std::forward<_ArgTypes>(__args)...); }
649 // Handle smart pointers, references and pointers to derived
650 template<typename _Tp>
652 operator()(_Tp& __object, _ArgTypes... __args) const
654 return _M_call(__object, &__object,
655 std::forward<_ArgTypes>(__args)...);
662 /// Implementation of @c mem_fn for const volatile member function pointers.
663 template<typename _Res, typename _Class, typename... _ArgTypes>
664 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) const volatile>
665 : public _Maybe_unary_or_binary_function<_Res, const volatile _Class*,
668 typedef _Res (_Class::*_Functor)(_ArgTypes...) const volatile;
670 template<typename _Tp>
672 _M_call(_Tp& __object, const volatile _Class *,
673 _ArgTypes... __args) const
674 { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); }
676 template<typename _Tp>
678 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
679 { return ((*__ptr).*__pmf)(std::forward<_ArgTypes>(__args)...); }
682 typedef _Res result_type;
684 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
688 operator()(const volatile _Class& __object, _ArgTypes... __args) const
689 { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); }
693 operator()(const volatile _Class* __object, _ArgTypes... __args) const
694 { return (__object->*__pmf)(std::forward<_ArgTypes>(__args)...); }
696 // Handle smart pointers, references and pointers to derived
697 template<typename _Tp>
698 _Res operator()(_Tp& __object, _ArgTypes... __args) const
700 return _M_call(__object, &__object,
701 std::forward<_ArgTypes>(__args)...);
709 template<typename _Tp, bool>
710 struct _Mem_fn_const_or_non
712 typedef const _Tp& type;
715 template<typename _Tp>
716 struct _Mem_fn_const_or_non<_Tp, false>
721 template<typename _Res, typename _Class>
722 class _Mem_fn<_Res _Class::*>
724 // This bit of genius is due to Peter Dimov, improved slightly by
726 template<typename _Tp>
728 _M_call(_Tp& __object, _Class *) const
729 { return __object.*__pm; }
731 template<typename _Tp, typename _Up>
733 _M_call(_Tp& __object, _Up * const *) const
734 { return (*__object).*__pm; }
736 template<typename _Tp, typename _Up>
738 _M_call(_Tp& __object, const _Up * const *) const
739 { return (*__object).*__pm; }
741 template<typename _Tp>
743 _M_call(_Tp& __object, const _Class *) const
744 { return __object.*__pm; }
746 template<typename _Tp>
748 _M_call(_Tp& __ptr, const volatile void*) const
749 { return (*__ptr).*__pm; }
751 template<typename _Tp> static _Tp& __get_ref();
753 template<typename _Tp>
754 static __sfinae_types::__one __check_const(_Tp&, _Class*);
755 template<typename _Tp, typename _Up>
756 static __sfinae_types::__one __check_const(_Tp&, _Up * const *);
757 template<typename _Tp, typename _Up>
758 static __sfinae_types::__two __check_const(_Tp&, const _Up * const *);
759 template<typename _Tp>
760 static __sfinae_types::__two __check_const(_Tp&, const _Class*);
761 template<typename _Tp>
762 static __sfinae_types::__two __check_const(_Tp&, const volatile void*);
765 template<typename _Tp>
767 : _Mem_fn_const_or_non<_Res,
768 (sizeof(__sfinae_types::__two)
769 == sizeof(__check_const<_Tp>(__get_ref<_Tp>(), (_Tp*)0)))>
772 template<typename _Signature>
775 template<typename _CVMem, typename _Tp>
776 struct result<_CVMem(_Tp)>
777 : public _Result_type<_Tp> { };
779 template<typename _CVMem, typename _Tp>
780 struct result<_CVMem(_Tp&)>
781 : public _Result_type<_Tp> { };
784 _Mem_fn(_Res _Class::*__pm) : __pm(__pm) { }
788 operator()(_Class& __object) const
789 { return __object.*__pm; }
792 operator()(const _Class& __object) const
793 { return __object.*__pm; }
797 operator()(_Class* __object) const
798 { return __object->*__pm; }
801 operator()(const _Class* __object) const
802 { return __object->*__pm; }
804 // Handle smart pointers and derived
805 template<typename _Tp>
806 typename _Result_type<_Tp>::type
807 operator()(_Tp& __unknown) const
808 { return _M_call(__unknown, &__unknown); }
815 * @brief Returns a function object that forwards to the member
819 template<typename _Tp, typename _Class>
820 inline _Mem_fn<_Tp _Class::*>
821 mem_fn(_Tp _Class::* __pm)
823 return _Mem_fn<_Tp _Class::*>(__pm);
827 * @brief Determines if the given type _Tp is a function object
828 * should be treated as a subexpression when evaluating calls to
829 * function objects returned by bind(). [TR1 3.6.1]
832 template<typename _Tp>
833 struct is_bind_expression
834 : public false_type { };
837 * @brief Determines if the given type _Tp is a placeholder in a
838 * bind() expression and, if so, which placeholder it is. [TR1 3.6.2]
841 template<typename _Tp>
842 struct is_placeholder
843 : public integral_constant<int, 0>
846 /// The type of placeholder objects defined by libstdc++.
847 template<int _Num> struct _Placeholder { };
849 _GLIBCXX_END_NAMESPACE_VERSION
851 /** @namespace std::placeholders
852 * @brief ISO C++ 0x entities sub namespace for functional.
855 * Define a large number of placeholders. There is no way to
856 * simplify this with variadic templates, because we're introducing
857 * unique names for each.
859 namespace placeholders
861 _GLIBCXX_BEGIN_NAMESPACE_VERSION
862 extern const _Placeholder<1> _1;
863 extern const _Placeholder<2> _2;
864 extern const _Placeholder<3> _3;
865 extern const _Placeholder<4> _4;
866 extern const _Placeholder<5> _5;
867 extern const _Placeholder<6> _6;
868 extern const _Placeholder<7> _7;
869 extern const _Placeholder<8> _8;
870 extern const _Placeholder<9> _9;
871 extern const _Placeholder<10> _10;
872 extern const _Placeholder<11> _11;
873 extern const _Placeholder<12> _12;
874 extern const _Placeholder<13> _13;
875 extern const _Placeholder<14> _14;
876 extern const _Placeholder<15> _15;
877 extern const _Placeholder<16> _16;
878 extern const _Placeholder<17> _17;
879 extern const _Placeholder<18> _18;
880 extern const _Placeholder<19> _19;
881 extern const _Placeholder<20> _20;
882 extern const _Placeholder<21> _21;
883 extern const _Placeholder<22> _22;
884 extern const _Placeholder<23> _23;
885 extern const _Placeholder<24> _24;
886 extern const _Placeholder<25> _25;
887 extern const _Placeholder<26> _26;
888 extern const _Placeholder<27> _27;
889 extern const _Placeholder<28> _28;
890 extern const _Placeholder<29> _29;
891 _GLIBCXX_END_NAMESPACE_VERSION
894 _GLIBCXX_BEGIN_NAMESPACE_VERSION
897 * Partial specialization of is_placeholder that provides the placeholder
898 * number for the placeholder objects defined by libstdc++.
902 struct is_placeholder<_Placeholder<_Num> >
903 : public integral_constant<int, _Num>
907 * Used by _Safe_tuple_element to indicate that there is no tuple
908 * element at this position.
910 struct _No_tuple_element;
913 * Implementation helper for _Safe_tuple_element. This primary
914 * template handles the case where it is safe to use @c
917 template<int __i, typename _Tuple, bool _IsSafe>
918 struct _Safe_tuple_element_impl
919 : tuple_element<__i, _Tuple> { };
922 * Implementation helper for _Safe_tuple_element. This partial
923 * specialization handles the case where it is not safe to use @c
924 * tuple_element. We just return @c _No_tuple_element.
926 template<int __i, typename _Tuple>
927 struct _Safe_tuple_element_impl<__i, _Tuple, false>
929 typedef _No_tuple_element type;
933 * Like tuple_element, but returns @c _No_tuple_element when
934 * tuple_element would return an error.
936 template<int __i, typename _Tuple>
937 struct _Safe_tuple_element
938 : _Safe_tuple_element_impl<__i, _Tuple,
939 (__i >= 0 && __i < tuple_size<_Tuple>::value)>
943 * Maps an argument to bind() into an actual argument to the bound
944 * function object [TR1 3.6.3/5]. Only the first parameter should
945 * be specified: the rest are used to determine among the various
946 * implementations. Note that, although this class is a function
947 * object, it isn't entirely normal because it takes only two
948 * parameters regardless of the number of parameters passed to the
949 * bind expression. The first parameter is the bound argument and
950 * the second parameter is a tuple containing references to the
951 * rest of the arguments.
953 template<typename _Arg,
954 bool _IsBindExp = is_bind_expression<_Arg>::value,
955 bool _IsPlaceholder = (is_placeholder<_Arg>::value > 0)>
959 * If the argument is reference_wrapper<_Tp>, returns the
960 * underlying reference. [TR1 3.6.3/5 bullet 1]
962 template<typename _Tp>
963 class _Mu<reference_wrapper<_Tp>, false, false>
966 typedef _Tp& result_type;
968 /* Note: This won't actually work for const volatile
969 * reference_wrappers, because reference_wrapper::get() is const
970 * but not volatile-qualified. This might be a defect in the TR.
972 template<typename _CVRef, typename _Tuple>
974 operator()(_CVRef& __arg, _Tuple&) const volatile
975 { return __arg.get(); }
979 * If the argument is a bind expression, we invoke the underlying
980 * function object with the same cv-qualifiers as we are given and
981 * pass along all of our arguments (unwrapped). [TR1 3.6.3/5 bullet 2]
983 template<typename _Arg>
984 class _Mu<_Arg, true, false>
987 template<typename _CVArg, typename... _Args>
989 operator()(_CVArg& __arg,
990 tuple<_Args...>& __tuple) const volatile
991 -> decltype(__arg(declval<_Args>()...))
993 // Construct an index tuple and forward to __call
994 typedef typename _Build_index_tuple<sizeof...(_Args)>::__type
996 return this->__call(__arg, __tuple, _Indexes());
1000 // Invokes the underlying function object __arg by unpacking all
1001 // of the arguments in the tuple.
1002 template<typename _CVArg, typename... _Args, int... _Indexes>
1004 __call(_CVArg& __arg, tuple<_Args...>& __tuple,
1005 const _Index_tuple<_Indexes...>&) const volatile
1006 -> decltype(__arg(declval<_Args>()...))
1008 return __arg(std::forward<_Args>(get<_Indexes>(__tuple))...);
1013 * If the argument is a placeholder for the Nth argument, returns
1014 * a reference to the Nth argument to the bind function object.
1015 * [TR1 3.6.3/5 bullet 3]
1017 template<typename _Arg>
1018 class _Mu<_Arg, false, true>
1021 template<typename _Signature> class result;
1023 template<typename _CVMu, typename _CVArg, typename _Tuple>
1024 class result<_CVMu(_CVArg, _Tuple)>
1026 // Add a reference, if it hasn't already been done for us.
1027 // This allows us to be a little bit sloppy in constructing
1028 // the tuple that we pass to result_of<...>.
1029 typedef typename _Safe_tuple_element<(is_placeholder<_Arg>::value
1034 typedef typename add_rvalue_reference<__base_type>::type type;
1037 template<typename _Tuple>
1038 typename result<_Mu(_Arg, _Tuple)>::type
1039 operator()(const volatile _Arg&, _Tuple& __tuple) const volatile
1041 return std::forward<typename result<_Mu(_Arg, _Tuple)>::type>(
1042 ::std::get<(is_placeholder<_Arg>::value - 1)>(__tuple));
1047 * If the argument is just a value, returns a reference to that
1048 * value. The cv-qualifiers on the reference are the same as the
1049 * cv-qualifiers on the _Mu object. [TR1 3.6.3/5 bullet 4]
1051 template<typename _Arg>
1052 class _Mu<_Arg, false, false>
1055 template<typename _Signature> struct result;
1057 template<typename _CVMu, typename _CVArg, typename _Tuple>
1058 struct result<_CVMu(_CVArg, _Tuple)>
1060 typedef typename add_lvalue_reference<_CVArg>::type type;
1063 // Pick up the cv-qualifiers of the argument
1064 template<typename _CVArg, typename _Tuple>
1066 operator()(_CVArg&& __arg, _Tuple&) const volatile
1067 { return std::forward<_CVArg>(__arg); }
1071 * Maps member pointers into instances of _Mem_fn but leaves all
1072 * other function objects untouched. Used by tr1::bind(). The
1073 * primary template handles the non--member-pointer case.
1075 template<typename _Tp>
1076 struct _Maybe_wrap_member_pointer
1081 __do_wrap(const _Tp& __x)
1085 __do_wrap(_Tp&& __x)
1086 { return static_cast<_Tp&&>(__x); }
1090 * Maps member pointers into instances of _Mem_fn but leaves all
1091 * other function objects untouched. Used by tr1::bind(). This
1092 * partial specialization handles the member pointer case.
1094 template<typename _Tp, typename _Class>
1095 struct _Maybe_wrap_member_pointer<_Tp _Class::*>
1097 typedef _Mem_fn<_Tp _Class::*> type;
1100 __do_wrap(_Tp _Class::* __pm)
1101 { return type(__pm); }
1104 // Specialization needed to prevent "forming reference to void" errors when
1105 // bind<void>() is called, because argument deduction instantiates
1106 // _Maybe_wrap_member_pointer<void> outside the immediate context where
1109 struct _Maybe_wrap_member_pointer<void>
1114 // std::get<I> for volatile-qualified tuples
1115 template<size_t _Ind, typename... _Tp>
1117 __volget(volatile tuple<_Tp...>& __tuple)
1118 -> typename tuple_element<_Ind, tuple<_Tp...>>::type volatile&
1119 { return std::get<_Ind>(const_cast<tuple<_Tp...>&>(__tuple)); }
1121 // std::get<I> for const-volatile-qualified tuples
1122 template<size_t _Ind, typename... _Tp>
1124 __volget(const volatile tuple<_Tp...>& __tuple)
1125 -> typename tuple_element<_Ind, tuple<_Tp...>>::type const volatile&
1126 { return std::get<_Ind>(const_cast<const tuple<_Tp...>&>(__tuple)); }
1128 /// Type of the function object returned from bind().
1129 template<typename _Signature>
1132 template<typename _Functor, typename... _Bound_args>
1133 class _Bind<_Functor(_Bound_args...)>
1134 : public _Weak_result_type<_Functor>
1136 typedef _Bind __self_type;
1137 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
1141 tuple<_Bound_args...> _M_bound_args;
1144 template<typename _Result, typename... _Args, int... _Indexes>
1146 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>)
1148 return _M_f(_Mu<_Bound_args>()
1149 (get<_Indexes>(_M_bound_args), __args)...);
1153 template<typename _Result, typename... _Args, int... _Indexes>
1155 __call_c(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>) const
1157 return _M_f(_Mu<_Bound_args>()
1158 (get<_Indexes>(_M_bound_args), __args)...);
1162 template<typename _Result, typename... _Args, int... _Indexes>
1164 __call_v(tuple<_Args...>&& __args,
1165 _Index_tuple<_Indexes...>) volatile
1167 return _M_f(_Mu<_Bound_args>()
1168 (__volget<_Indexes>(_M_bound_args), __args)...);
1171 // Call as const volatile
1172 template<typename _Result, typename... _Args, int... _Indexes>
1174 __call_c_v(tuple<_Args...>&& __args,
1175 _Index_tuple<_Indexes...>) const volatile
1177 return _M_f(_Mu<_Bound_args>()
1178 (__volget<_Indexes>(_M_bound_args), __args)...);
1182 template<typename... _Args>
1183 explicit _Bind(const _Functor& __f, _Args&&... __args)
1184 : _M_f(__f), _M_bound_args(std::forward<_Args>(__args)...)
1187 template<typename... _Args>
1188 explicit _Bind(_Functor&& __f, _Args&&... __args)
1189 : _M_f(std::move(__f)), _M_bound_args(std::forward<_Args>(__args)...)
1192 _Bind(const _Bind&) = default;
1195 : _M_f(std::move(__b._M_f)), _M_bound_args(std::move(__b._M_bound_args))
1199 template<typename... _Args, typename _Result
1200 = decltype( std::declval<_Functor>()(
1201 _Mu<_Bound_args>()( std::declval<_Bound_args&>(),
1202 std::declval<tuple<_Args...>&>() )... ) )>
1204 operator()(_Args&&... __args)
1206 return this->__call<_Result>(
1207 std::forward_as_tuple(std::forward<_Args>(__args)...),
1212 template<typename... _Args, typename _Result
1213 = decltype( std::declval<const _Functor>()(
1214 _Mu<_Bound_args>()( std::declval<const _Bound_args&>(),
1215 std::declval<tuple<_Args...>&>() )... ) )>
1217 operator()(_Args&&... __args) const
1219 return this->__call_c<_Result>(
1220 std::forward_as_tuple(std::forward<_Args>(__args)...),
1225 template<typename... _Args, typename _Result
1226 = decltype( std::declval<volatile _Functor>()(
1227 _Mu<_Bound_args>()( std::declval<volatile _Bound_args&>(),
1228 std::declval<tuple<_Args...>&>() )... ) )>
1230 operator()(_Args&&... __args) volatile
1232 return this->__call_v<_Result>(
1233 std::forward_as_tuple(std::forward<_Args>(__args)...),
1237 // Call as const volatile
1238 template<typename... _Args, typename _Result
1239 = decltype( std::declval<const volatile _Functor>()(
1240 _Mu<_Bound_args>()( std::declval<const volatile _Bound_args&>(),
1241 std::declval<tuple<_Args...>&>() )... ) )>
1243 operator()(_Args&&... __args) const volatile
1245 return this->__call_c_v<_Result>(
1246 std::forward_as_tuple(std::forward<_Args>(__args)...),
1251 /// Type of the function object returned from bind<R>().
1252 template<typename _Result, typename _Signature>
1253 struct _Bind_result;
1255 template<typename _Result, typename _Functor, typename... _Bound_args>
1256 class _Bind_result<_Result, _Functor(_Bound_args...)>
1258 typedef _Bind_result __self_type;
1259 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
1263 tuple<_Bound_args...> _M_bound_args;
1266 template<typename _Res>
1267 struct __enable_if_void : enable_if<is_void<_Res>::value, int> { };
1268 template<typename _Res>
1269 struct __disable_if_void : enable_if<!is_void<_Res>::value, int> { };
1272 template<typename _Res, typename... _Args, int... _Indexes>
1274 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1275 typename __disable_if_void<_Res>::type = 0)
1277 return _M_f(_Mu<_Bound_args>()
1278 (get<_Indexes>(_M_bound_args), __args)...);
1281 // Call unqualified, return void
1282 template<typename _Res, typename... _Args, int... _Indexes>
1284 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1285 typename __enable_if_void<_Res>::type = 0)
1287 _M_f(_Mu<_Bound_args>()
1288 (get<_Indexes>(_M_bound_args), __args)...);
1292 template<typename _Res, typename... _Args, int... _Indexes>
1294 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1295 typename __disable_if_void<_Res>::type = 0) const
1297 return _M_f(_Mu<_Bound_args>()
1298 (get<_Indexes>(_M_bound_args), __args)...);
1301 // Call as const, return void
1302 template<typename _Res, typename... _Args, int... _Indexes>
1304 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1305 typename __enable_if_void<_Res>::type = 0) const
1307 _M_f(_Mu<_Bound_args>()
1308 (get<_Indexes>(_M_bound_args), __args)...);
1312 template<typename _Res, typename... _Args, int... _Indexes>
1314 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1315 typename __disable_if_void<_Res>::type = 0) volatile
1317 return _M_f(_Mu<_Bound_args>()
1318 (__volget<_Indexes>(_M_bound_args), __args)...);
1321 // Call as volatile, return void
1322 template<typename _Res, typename... _Args, int... _Indexes>
1324 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1325 typename __enable_if_void<_Res>::type = 0) volatile
1327 _M_f(_Mu<_Bound_args>()
1328 (__volget<_Indexes>(_M_bound_args), __args)...);
1331 // Call as const volatile
1332 template<typename _Res, typename... _Args, int... _Indexes>
1334 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1335 typename __disable_if_void<_Res>::type = 0) const volatile
1337 return _M_f(_Mu<_Bound_args>()
1338 (__volget<_Indexes>(_M_bound_args), __args)...);
1341 // Call as const volatile, return void
1342 template<typename _Res, typename... _Args, int... _Indexes>
1344 __call(tuple<_Args...>&& __args,
1345 _Index_tuple<_Indexes...>,
1346 typename __enable_if_void<_Res>::type = 0) const volatile
1348 _M_f(_Mu<_Bound_args>()
1349 (__volget<_Indexes>(_M_bound_args), __args)...);
1353 typedef _Result result_type;
1355 template<typename... _Args>
1356 explicit _Bind_result(const _Functor& __f, _Args&&... __args)
1357 : _M_f(__f), _M_bound_args(std::forward<_Args>(__args)...)
1360 template<typename... _Args>
1361 explicit _Bind_result(_Functor&& __f, _Args&&... __args)
1362 : _M_f(std::move(__f)), _M_bound_args(std::forward<_Args>(__args)...)
1365 _Bind_result(const _Bind_result&) = default;
1367 _Bind_result(_Bind_result&& __b)
1368 : _M_f(std::move(__b._M_f)), _M_bound_args(std::move(__b._M_bound_args))
1372 template<typename... _Args>
1374 operator()(_Args&&... __args)
1376 return this->__call<_Result>(
1377 std::forward_as_tuple(std::forward<_Args>(__args)...),
1382 template<typename... _Args>
1384 operator()(_Args&&... __args) const
1386 return this->__call<_Result>(
1387 std::forward_as_tuple(std::forward<_Args>(__args)...),
1392 template<typename... _Args>
1394 operator()(_Args&&... __args) volatile
1396 return this->__call<_Result>(
1397 std::forward_as_tuple(std::forward<_Args>(__args)...),
1401 // Call as const volatile
1402 template<typename... _Args>
1404 operator()(_Args&&... __args) const volatile
1406 return this->__call<_Result>(
1407 std::forward_as_tuple(std::forward<_Args>(__args)...),
1413 * @brief Class template _Bind is always a bind expression.
1416 template<typename _Signature>
1417 struct is_bind_expression<_Bind<_Signature> >
1418 : public true_type { };
1421 * @brief Class template _Bind is always a bind expression.
1424 template<typename _Result, typename _Signature>
1425 struct is_bind_expression<_Bind_result<_Result, _Signature> >
1426 : public true_type { };
1428 // Trait type used to remove std::bind() from overload set via SFINAE
1429 // when first argument has integer type, so that std::bind() will
1430 // not be a better match than ::bind() from the BSD Sockets API.
1431 template<typename _Tp>
1432 class __is_socketlike
1434 typedef typename decay<_Tp>::type _Tp2;
1436 static const bool value =
1437 is_integral<_Tp2>::value || is_enum<_Tp2>::value;
1440 template<bool _SocketLike, typename _Func, typename... _BoundArgs>
1443 typedef _Maybe_wrap_member_pointer<typename decay<_Func>::type>
1445 typedef typename __maybe_type::type __func_type;
1446 typedef _Bind<__func_type(typename decay<_BoundArgs>::type...)> type;
1449 // Partial specialization for is_socketlike == true, does not define
1450 // nested type so std::bind() will not participate in overload resolution
1451 // when the first argument might be a socket file descriptor.
1452 template<typename _Func, typename... _BoundArgs>
1453 struct _Bind_helper<true, _Func, _BoundArgs...>
1457 * @brief Function template for std::bind.
1460 template<typename _Func, typename... _BoundArgs>
1462 _Bind_helper<__is_socketlike<_Func>::value, _Func, _BoundArgs...>::type
1463 bind(_Func&& __f, _BoundArgs&&... __args)
1465 typedef _Bind_helper<false, _Func, _BoundArgs...> __helper_type;
1466 typedef typename __helper_type::__maybe_type __maybe_type;
1467 typedef typename __helper_type::type __result_type;
1468 return __result_type(__maybe_type::__do_wrap(std::forward<_Func>(__f)),
1469 std::forward<_BoundArgs>(__args)...);
1472 template<typename _Result, typename _Func, typename... _BoundArgs>
1473 struct _Bindres_helper
1475 typedef _Maybe_wrap_member_pointer<typename decay<_Func>::type>
1477 typedef typename __maybe_type::type __functor_type;
1478 typedef _Bind_result<_Result,
1479 __functor_type(typename decay<_BoundArgs>::type...)>
1484 * @brief Function template for std::bind<R>.
1487 template<typename _Result, typename _Func, typename... _BoundArgs>
1489 typename _Bindres_helper<_Result, _Func, _BoundArgs...>::type
1490 bind(_Func&& __f, _BoundArgs&&... __args)
1492 typedef _Bindres_helper<_Result, _Func, _BoundArgs...> __helper_type;
1493 typedef typename __helper_type::__maybe_type __maybe_type;
1494 typedef typename __helper_type::type __result_type;
1495 return __result_type(__maybe_type::__do_wrap(std::forward<_Func>(__f)),
1496 std::forward<_BoundArgs>(__args)...);
1500 * @brief Exception class thrown when class template function's
1501 * operator() is called with an empty target.
1502 * @ingroup exceptions
1504 class bad_function_call : public std::exception
1507 virtual ~bad_function_call() throw();
1511 * Trait identifying "location-invariant" types, meaning that the
1512 * address of the object (or any of its members) will not escape.
1513 * Also implies a trivial copy constructor and assignment operator.
1515 template<typename _Tp>
1516 struct __is_location_invariant
1517 : integral_constant<bool, (is_pointer<_Tp>::value
1518 || is_member_pointer<_Tp>::value)>
1521 class _Undefined_class;
1526 const void* _M_const_object;
1527 void (*_M_function_pointer)();
1528 void (_Undefined_class::*_M_member_pointer)();
1533 void* _M_access() { return &_M_pod_data[0]; }
1534 const void* _M_access() const { return &_M_pod_data[0]; }
1536 template<typename _Tp>
1539 { return *static_cast<_Tp*>(_M_access()); }
1541 template<typename _Tp>
1544 { return *static_cast<const _Tp*>(_M_access()); }
1546 _Nocopy_types _M_unused;
1547 char _M_pod_data[sizeof(_Nocopy_types)];
1550 enum _Manager_operation
1558 // Simple type wrapper that helps avoid annoying const problems
1559 // when casting between void pointers and pointers-to-pointers.
1560 template<typename _Tp>
1561 struct _Simple_type_wrapper
1563 _Simple_type_wrapper(_Tp __value) : __value(__value) { }
1568 template<typename _Tp>
1569 struct __is_location_invariant<_Simple_type_wrapper<_Tp> >
1570 : __is_location_invariant<_Tp>
1573 // Converts a reference to a function object into a callable
1575 template<typename _Functor>
1577 __callable_functor(_Functor& __f)
1580 template<typename _Member, typename _Class>
1581 inline _Mem_fn<_Member _Class::*>
1582 __callable_functor(_Member _Class::* &__p)
1583 { return mem_fn(__p); }
1585 template<typename _Member, typename _Class>
1586 inline _Mem_fn<_Member _Class::*>
1587 __callable_functor(_Member _Class::* const &__p)
1588 { return mem_fn(__p); }
1590 template<typename _Signature>
1593 /// Base class of all polymorphic function object wrappers.
1594 class _Function_base
1597 static const std::size_t _M_max_size = sizeof(_Nocopy_types);
1598 static const std::size_t _M_max_align = __alignof__(_Nocopy_types);
1600 template<typename _Functor>
1604 static const bool __stored_locally =
1605 (__is_location_invariant<_Functor>::value
1606 && sizeof(_Functor) <= _M_max_size
1607 && __alignof__(_Functor) <= _M_max_align
1608 && (_M_max_align % __alignof__(_Functor) == 0));
1610 typedef integral_constant<bool, __stored_locally> _Local_storage;
1612 // Retrieve a pointer to the function object
1614 _M_get_pointer(const _Any_data& __source)
1616 const _Functor* __ptr =
1617 __stored_locally? std::__addressof(__source._M_access<_Functor>())
1618 /* have stored a pointer */ : __source._M_access<_Functor*>();
1619 return const_cast<_Functor*>(__ptr);
1622 // Clone a location-invariant function object that fits within
1623 // an _Any_data structure.
1625 _M_clone(_Any_data& __dest, const _Any_data& __source, true_type)
1627 new (__dest._M_access()) _Functor(__source._M_access<_Functor>());
1630 // Clone a function object that is not location-invariant or
1631 // that cannot fit into an _Any_data structure.
1633 _M_clone(_Any_data& __dest, const _Any_data& __source, false_type)
1635 __dest._M_access<_Functor*>() =
1636 new _Functor(*__source._M_access<_Functor*>());
1639 // Destroying a location-invariant object may still require
1642 _M_destroy(_Any_data& __victim, true_type)
1644 __victim._M_access<_Functor>().~_Functor();
1647 // Destroying an object located on the heap.
1649 _M_destroy(_Any_data& __victim, false_type)
1651 delete __victim._M_access<_Functor*>();
1656 _M_manager(_Any_data& __dest, const _Any_data& __source,
1657 _Manager_operation __op)
1662 case __get_type_info:
1663 __dest._M_access<const type_info*>() = &typeid(_Functor);
1666 case __get_functor_ptr:
1667 __dest._M_access<_Functor*>() = _M_get_pointer(__source);
1670 case __clone_functor:
1671 _M_clone(__dest, __source, _Local_storage());
1674 case __destroy_functor:
1675 _M_destroy(__dest, _Local_storage());
1682 _M_init_functor(_Any_data& __functor, _Functor&& __f)
1683 { _M_init_functor(__functor, std::move(__f), _Local_storage()); }
1685 template<typename _Signature>
1687 _M_not_empty_function(const function<_Signature>& __f)
1688 { return static_cast<bool>(__f); }
1690 template<typename _Tp>
1692 _M_not_empty_function(const _Tp*& __fp)
1695 template<typename _Class, typename _Tp>
1697 _M_not_empty_function(_Tp _Class::* const& __mp)
1700 template<typename _Tp>
1702 _M_not_empty_function(const _Tp&)
1707 _M_init_functor(_Any_data& __functor, _Functor&& __f, true_type)
1708 { new (__functor._M_access()) _Functor(std::move(__f)); }
1711 _M_init_functor(_Any_data& __functor, _Functor&& __f, false_type)
1712 { __functor._M_access<_Functor*>() = new _Functor(std::move(__f)); }
1715 template<typename _Functor>
1716 class _Ref_manager : public _Base_manager<_Functor*>
1718 typedef _Function_base::_Base_manager<_Functor*> _Base;
1722 _M_manager(_Any_data& __dest, const _Any_data& __source,
1723 _Manager_operation __op)
1728 case __get_type_info:
1729 __dest._M_access<const type_info*>() = &typeid(_Functor);
1732 case __get_functor_ptr:
1733 __dest._M_access<_Functor*>() = *_Base::_M_get_pointer(__source);
1734 return is_const<_Functor>::value;
1738 _Base::_M_manager(__dest, __source, __op);
1744 _M_init_functor(_Any_data& __functor, reference_wrapper<_Functor> __f)
1746 // TBD: Use address_of function instead.
1747 _Base::_M_init_functor(__functor, &__f.get());
1751 _Function_base() : _M_manager(0) { }
1756 _M_manager(_M_functor, _M_functor, __destroy_functor);
1760 bool _M_empty() const { return !_M_manager; }
1762 typedef bool (*_Manager_type)(_Any_data&, const _Any_data&,
1763 _Manager_operation);
1765 _Any_data _M_functor;
1766 _Manager_type _M_manager;
1769 template<typename _Signature, typename _Functor>
1770 class _Function_handler;
1772 template<typename _Res, typename _Functor, typename... _ArgTypes>
1773 class _Function_handler<_Res(_ArgTypes...), _Functor>
1774 : public _Function_base::_Base_manager<_Functor>
1776 typedef _Function_base::_Base_manager<_Functor> _Base;
1780 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1782 return (*_Base::_M_get_pointer(__functor))(
1783 std::forward<_ArgTypes>(__args)...);
1787 template<typename _Functor, typename... _ArgTypes>
1788 class _Function_handler<void(_ArgTypes...), _Functor>
1789 : public _Function_base::_Base_manager<_Functor>
1791 typedef _Function_base::_Base_manager<_Functor> _Base;
1795 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1797 (*_Base::_M_get_pointer(__functor))(
1798 std::forward<_ArgTypes>(__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)
1812 return __callable_functor(**_Base::_M_get_pointer(__functor))(
1813 std::forward<_ArgTypes>(__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))(
1828 std::forward<_ArgTypes>(__args)...);
1832 template<typename _Class, typename _Member, typename _Res,
1833 typename... _ArgTypes>
1834 class _Function_handler<_Res(_ArgTypes...), _Member _Class::*>
1835 : public _Function_handler<void(_ArgTypes...), _Member _Class::*>
1837 typedef _Function_handler<void(_ArgTypes...), _Member _Class::*>
1842 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1844 return mem_fn(_Base::_M_get_pointer(__functor)->__value)(
1845 std::forward<_ArgTypes>(__args)...);
1849 template<typename _Class, typename _Member, typename... _ArgTypes>
1850 class _Function_handler<void(_ArgTypes...), _Member _Class::*>
1851 : public _Function_base::_Base_manager<
1852 _Simple_type_wrapper< _Member _Class::* > >
1854 typedef _Member _Class::* _Functor;
1855 typedef _Simple_type_wrapper<_Functor> _Wrapper;
1856 typedef _Function_base::_Base_manager<_Wrapper> _Base;
1860 _M_manager(_Any_data& __dest, const _Any_data& __source,
1861 _Manager_operation __op)
1866 case __get_type_info:
1867 __dest._M_access<const type_info*>() = &typeid(_Functor);
1870 case __get_functor_ptr:
1871 __dest._M_access<_Functor*>() =
1872 &_Base::_M_get_pointer(__source)->__value;
1876 _Base::_M_manager(__dest, __source, __op);
1882 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1884 mem_fn(_Base::_M_get_pointer(__functor)->__value)(
1885 std::forward<_ArgTypes>(__args)...);
1890 * @brief Primary class template for std::function.
1893 * Polymorphic function wrapper.
1895 template<typename _Res, typename... _ArgTypes>
1896 class function<_Res(_ArgTypes...)>
1897 : public _Maybe_unary_or_binary_function<_Res, _ArgTypes...>,
1898 private _Function_base
1900 typedef _Res _Signature_type(_ArgTypes...);
1902 struct _Useless { };
1905 typedef _Res result_type;
1907 // [3.7.2.1] construct/copy/destroy
1910 * @brief Default construct creates an empty function call wrapper.
1911 * @post @c !(bool)*this
1913 function() : _Function_base() { }
1916 * @brief Creates an empty function call wrapper.
1917 * @post @c !(bool)*this
1919 function(nullptr_t) : _Function_base() { }
1922 * @brief %Function copy constructor.
1923 * @param x A %function object with identical call signature.
1924 * @post @c (bool)*this == (bool)x
1926 * The newly-created %function contains a copy of the target of @a
1927 * x (if it has one).
1929 function(const function& __x);
1932 * @brief %Function move constructor.
1933 * @param x A %function object rvalue with identical call signature.
1935 * The newly-created %function contains the target of @a x
1938 function(function&& __x) : _Function_base()
1943 // TODO: needs allocator_arg_t
1946 * @brief Builds a %function that targets a copy of the incoming
1948 * @param f A %function object that is callable with parameters of
1949 * type @c T1, @c T2, ..., @c TN and returns a value convertible
1952 * The newly-created %function object will target a copy of @a
1953 * f. If @a f is @c reference_wrapper<F>, then this function
1954 * object will contain a reference to the function object @c
1955 * f.get(). If @a f is a NULL function pointer or NULL
1956 * pointer-to-member, the newly-created object will be empty.
1958 * If @a f is a non-NULL function pointer or an object of type @c
1959 * reference_wrapper<F>, this function will not throw.
1961 template<typename _Functor>
1962 function(_Functor __f,
1964 !is_integral<_Functor>::value, _Useless>::type
1968 * @brief %Function assignment operator.
1969 * @param x A %function with identical call signature.
1970 * @post @c (bool)*this == (bool)x
1973 * The target of @a x is copied to @c *this. If @a x has no
1974 * target, then @c *this will be empty.
1976 * If @a x targets a function pointer or a reference to a function
1977 * object, then this operation will not throw an %exception.
1980 operator=(const function& __x)
1982 function(__x).swap(*this);
1987 * @brief %Function move-assignment operator.
1988 * @param x A %function rvalue with identical call signature.
1991 * The target of @a x is moved to @c *this. If @a x has no
1992 * target, then @c *this will be empty.
1994 * If @a x targets a function pointer or a reference to a function
1995 * object, then this operation will not throw an %exception.
1998 operator=(function&& __x)
2000 function(std::move(__x)).swap(*this);
2005 * @brief %Function assignment to zero.
2006 * @post @c !(bool)*this
2009 * The target of @c *this is deallocated, leaving it empty.
2012 operator=(nullptr_t)
2016 _M_manager(_M_functor, _M_functor, __destroy_functor);
2024 * @brief %Function assignment to a new target.
2025 * @param f A %function object that is callable with parameters of
2026 * type @c T1, @c T2, ..., @c TN and returns a value convertible
2030 * This %function object wrapper will target a copy of @a
2031 * f. If @a f is @c reference_wrapper<F>, then this function
2032 * object will contain a reference to the function object @c
2033 * f.get(). If @a f is a NULL function pointer or NULL
2034 * pointer-to-member, @c this object will be empty.
2036 * If @a f is a non-NULL function pointer or an object of type @c
2037 * reference_wrapper<F>, this function will not throw.
2039 template<typename _Functor>
2040 typename enable_if<!is_integral<_Functor>::value, function&>::type
2041 operator=(_Functor&& __f)
2043 function(std::forward<_Functor>(__f)).swap(*this);
2048 template<typename _Functor>
2049 typename enable_if<!is_integral<_Functor>::value, function&>::type
2050 operator=(reference_wrapper<_Functor> __f)
2052 function(__f).swap(*this);
2056 // [3.7.2.2] function modifiers
2059 * @brief Swap the targets of two %function objects.
2060 * @param f A %function with identical call signature.
2062 * Swap the targets of @c this function object and @a f. This
2063 * function will not throw an %exception.
2065 void swap(function& __x)
2067 std::swap(_M_functor, __x._M_functor);
2068 std::swap(_M_manager, __x._M_manager);
2069 std::swap(_M_invoker, __x._M_invoker);
2072 // TODO: needs allocator_arg_t
2074 template<typename _Functor, typename _Alloc>
2076 assign(_Functor&& __f, const _Alloc& __a)
2078 function(allocator_arg, __a,
2079 std::forward<_Functor>(__f)).swap(*this);
2083 // [3.7.2.3] function capacity
2086 * @brief Determine if the %function wrapper has a target.
2088 * @return @c true when this %function object contains a target,
2089 * or @c false when it is empty.
2091 * This function will not throw an %exception.
2093 explicit operator bool() const
2094 { return !_M_empty(); }
2096 // [3.7.2.4] function invocation
2099 * @brief Invokes the function targeted by @c *this.
2100 * @returns the result of the target.
2101 * @throws bad_function_call when @c !(bool)*this
2103 * The function call operator invokes the target function object
2104 * stored by @c this.
2106 _Res operator()(_ArgTypes... __args) const;
2109 // [3.7.2.5] function target access
2111 * @brief Determine the type of the target of this function object
2114 * @returns the type identifier of the target function object, or
2115 * @c typeid(void) if @c !(bool)*this.
2117 * This function will not throw an %exception.
2119 const type_info& target_type() const;
2122 * @brief Access the stored target function object.
2124 * @return Returns a pointer to the stored target function object,
2125 * if @c typeid(Functor).equals(target_type()); otherwise, a NULL
2128 * This function will not throw an %exception.
2130 template<typename _Functor> _Functor* target();
2133 template<typename _Functor> const _Functor* target() const;
2137 typedef _Res (*_Invoker_type)(const _Any_data&, _ArgTypes...);
2138 _Invoker_type _M_invoker;
2141 // Out-of-line member definitions.
2142 template<typename _Res, typename... _ArgTypes>
2143 function<_Res(_ArgTypes...)>::
2144 function(const function& __x)
2147 if (static_cast<bool>(__x))
2149 _M_invoker = __x._M_invoker;
2150 _M_manager = __x._M_manager;
2151 __x._M_manager(_M_functor, __x._M_functor, __clone_functor);
2155 template<typename _Res, typename... _ArgTypes>
2156 template<typename _Functor>
2157 function<_Res(_ArgTypes...)>::
2158 function(_Functor __f,
2160 !is_integral<_Functor>::value, _Useless>::type)
2163 typedef _Function_handler<_Signature_type, _Functor> _My_handler;
2165 if (_My_handler::_M_not_empty_function(__f))
2167 _M_invoker = &_My_handler::_M_invoke;
2168 _M_manager = &_My_handler::_M_manager;
2169 _My_handler::_M_init_functor(_M_functor, std::move(__f));
2173 template<typename _Res, typename... _ArgTypes>
2175 function<_Res(_ArgTypes...)>::
2176 operator()(_ArgTypes... __args) const
2179 __throw_bad_function_call();
2180 return _M_invoker(_M_functor, std::forward<_ArgTypes>(__args)...);
2184 template<typename _Res, typename... _ArgTypes>
2186 function<_Res(_ArgTypes...)>::
2191 _Any_data __typeinfo_result;
2192 _M_manager(__typeinfo_result, _M_functor, __get_type_info);
2193 return *__typeinfo_result._M_access<const type_info*>();
2196 return typeid(void);
2199 template<typename _Res, typename... _ArgTypes>
2200 template<typename _Functor>
2202 function<_Res(_ArgTypes...)>::
2205 if (typeid(_Functor) == target_type() && _M_manager)
2208 if (_M_manager(__ptr, _M_functor, __get_functor_ptr)
2209 && !is_const<_Functor>::value)
2212 return __ptr._M_access<_Functor*>();
2218 template<typename _Res, typename... _ArgTypes>
2219 template<typename _Functor>
2221 function<_Res(_ArgTypes...)>::
2224 if (typeid(_Functor) == target_type() && _M_manager)
2227 _M_manager(__ptr, _M_functor, __get_functor_ptr);
2228 return __ptr._M_access<const _Functor*>();
2235 // [20.7.15.2.6] null pointer comparisons
2238 * @brief Compares a polymorphic function object wrapper against 0
2239 * (the NULL pointer).
2240 * @returns @c true if the wrapper has no target, @c false otherwise
2242 * This function will not throw an %exception.
2244 template<typename _Res, typename... _Args>
2246 operator==(const function<_Res(_Args...)>& __f, nullptr_t)
2247 { return !static_cast<bool>(__f); }
2250 template<typename _Res, typename... _Args>
2252 operator==(nullptr_t, const function<_Res(_Args...)>& __f)
2253 { return !static_cast<bool>(__f); }
2256 * @brief Compares a polymorphic function object wrapper against 0
2257 * (the NULL pointer).
2258 * @returns @c false if the wrapper has no target, @c true otherwise
2260 * This function will not throw an %exception.
2262 template<typename _Res, typename... _Args>
2264 operator!=(const function<_Res(_Args...)>& __f, nullptr_t)
2265 { return static_cast<bool>(__f); }
2268 template<typename _Res, typename... _Args>
2270 operator!=(nullptr_t, const function<_Res(_Args...)>& __f)
2271 { return static_cast<bool>(__f); }
2273 // [20.7.15.2.7] specialized algorithms
2276 * @brief Swap the targets of two polymorphic function object wrappers.
2278 * This function will not throw an %exception.
2280 template<typename _Res, typename... _Args>
2282 swap(function<_Res(_Args...)>& __x, function<_Res(_Args...)>& __y)
2285 _GLIBCXX_END_NAMESPACE_VERSION
2288 #endif // __GXX_EXPERIMENTAL_CXX0X__
2290 #endif // _GLIBCXX_FUNCTIONAL