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 _GLIBCXX_HAS_NESTED_TYPE(result_type)
67 /// If we have found a result_type, extract it.
68 template<bool _Has_result_type, typename _Functor>
69 struct _Maybe_get_result_type
72 template<typename _Functor>
73 struct _Maybe_get_result_type<true, _Functor>
74 { typedef typename _Functor::result_type result_type; };
77 * Base class for any function object that has a weak result type, as
78 * defined in 3.3/3 of TR1.
80 template<typename _Functor>
81 struct _Weak_result_type_impl
82 : _Maybe_get_result_type<__has_result_type<_Functor>::value, _Functor>
85 /// Retrieve the result type for a function type.
86 template<typename _Res, typename... _ArgTypes>
87 struct _Weak_result_type_impl<_Res(_ArgTypes...)>
88 { typedef _Res result_type; };
90 template<typename _Res, typename... _ArgTypes>
91 struct _Weak_result_type_impl<_Res(_ArgTypes......)>
92 { typedef _Res result_type; };
94 template<typename _Res, typename... _ArgTypes>
95 struct _Weak_result_type_impl<_Res(_ArgTypes...) const>
96 { typedef _Res result_type; };
98 template<typename _Res, typename... _ArgTypes>
99 struct _Weak_result_type_impl<_Res(_ArgTypes......) const>
100 { typedef _Res result_type; };
102 template<typename _Res, typename... _ArgTypes>
103 struct _Weak_result_type_impl<_Res(_ArgTypes...) volatile>
104 { typedef _Res result_type; };
106 template<typename _Res, typename... _ArgTypes>
107 struct _Weak_result_type_impl<_Res(_ArgTypes......) volatile>
108 { typedef _Res result_type; };
110 template<typename _Res, typename... _ArgTypes>
111 struct _Weak_result_type_impl<_Res(_ArgTypes...) const volatile>
112 { typedef _Res result_type; };
114 template<typename _Res, typename... _ArgTypes>
115 struct _Weak_result_type_impl<_Res(_ArgTypes......) const volatile>
116 { typedef _Res result_type; };
118 /// Retrieve the result type for a function reference.
119 template<typename _Res, typename... _ArgTypes>
120 struct _Weak_result_type_impl<_Res(&)(_ArgTypes...)>
121 { typedef _Res result_type; };
123 template<typename _Res, typename... _ArgTypes>
124 struct _Weak_result_type_impl<_Res(&)(_ArgTypes......)>
125 { typedef _Res result_type; };
127 /// Retrieve the result type for a function pointer.
128 template<typename _Res, typename... _ArgTypes>
129 struct _Weak_result_type_impl<_Res(*)(_ArgTypes...)>
130 { typedef _Res result_type; };
132 template<typename _Res, typename... _ArgTypes>
133 struct _Weak_result_type_impl<_Res(*)(_ArgTypes......)>
134 { typedef _Res result_type; };
136 /// Retrieve result type for a member function pointer.
137 template<typename _Res, typename _Class, typename... _ArgTypes>
138 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...)>
139 { typedef _Res result_type; };
141 template<typename _Res, typename _Class, typename... _ArgTypes>
142 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes......)>
143 { typedef _Res result_type; };
145 /// Retrieve result type for a const member function pointer.
146 template<typename _Res, typename _Class, typename... _ArgTypes>
147 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) const>
148 { typedef _Res result_type; };
150 template<typename _Res, typename _Class, typename... _ArgTypes>
151 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes......) const>
152 { typedef _Res result_type; };
154 /// Retrieve result type for a volatile member function pointer.
155 template<typename _Res, typename _Class, typename... _ArgTypes>
156 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) volatile>
157 { typedef _Res result_type; };
159 template<typename _Res, typename _Class, typename... _ArgTypes>
160 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes......) volatile>
161 { typedef _Res result_type; };
163 /// Retrieve result type for a const volatile member function pointer.
164 template<typename _Res, typename _Class, typename... _ArgTypes>
165 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...)
167 { typedef _Res result_type; };
169 template<typename _Res, typename _Class, typename... _ArgTypes>
170 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes......)
172 { typedef _Res result_type; };
175 * Strip top-level cv-qualifiers from the function object and let
176 * _Weak_result_type_impl perform the real work.
178 template<typename _Functor>
179 struct _Weak_result_type
180 : _Weak_result_type_impl<typename remove_cv<_Functor>::type>
183 /// Determines if the type _Tp derives from unary_function.
184 template<typename _Tp>
185 struct _Derives_from_unary_function : __sfinae_types
188 template<typename _T1, typename _Res>
189 static __one __test(const volatile unary_function<_T1, _Res>*);
191 // It's tempting to change "..." to const volatile void*, but
192 // that fails when _Tp is a function type.
193 static __two __test(...);
196 static const bool value = sizeof(__test((_Tp*)0)) == 1;
199 /// Determines if the type _Tp derives from binary_function.
200 template<typename _Tp>
201 struct _Derives_from_binary_function : __sfinae_types
204 template<typename _T1, typename _T2, typename _Res>
205 static __one __test(const volatile binary_function<_T1, _T2, _Res>*);
207 // It's tempting to change "..." to const volatile void*, but
208 // that fails when _Tp is a function type.
209 static __two __test(...);
212 static const bool value = sizeof(__test((_Tp*)0)) == 1;
216 * Invoke a function object, which may be either a member pointer or a
217 * function object. The first parameter will tell which.
219 template<typename _Functor, typename... _Args>
222 (!is_member_pointer<_Functor>::value
223 && !is_function<_Functor>::value
224 && !is_function<typename remove_pointer<_Functor>::type>::value),
225 typename result_of<_Functor(_Args&&...)>::type
227 __invoke(_Functor& __f, _Args&&... __args)
229 return __f(std::forward<_Args>(__args)...);
232 template<typename _Functor, typename... _Args>
235 (is_member_pointer<_Functor>::value
236 && !is_function<_Functor>::value
237 && !is_function<typename remove_pointer<_Functor>::type>::value),
238 typename result_of<_Functor(_Args&&...)>::type
240 __invoke(_Functor& __f, _Args&&... __args)
242 return mem_fn(__f)(std::forward<_Args>(__args)...);
245 // To pick up function references (that will become function pointers)
246 template<typename _Functor, typename... _Args>
249 (is_pointer<_Functor>::value
250 && is_function<typename remove_pointer<_Functor>::type>::value),
251 typename result_of<_Functor(_Args&&...)>::type
253 __invoke(_Functor __f, _Args&&... __args)
255 return __f(std::forward<_Args>(__args)...);
259 * Knowing which of unary_function and binary_function _Tp derives
260 * from, derives from the same and ensures that reference_wrapper
261 * will have a weak result type. See cases below.
263 template<bool _Unary, bool _Binary, typename _Tp>
264 struct _Reference_wrapper_base_impl;
266 // None of the nested argument types.
267 template<typename _Tp>
268 struct _Reference_wrapper_base_impl<false, false, _Tp>
269 : _Weak_result_type<_Tp>
272 // Nested argument_type only.
273 template<typename _Tp>
274 struct _Reference_wrapper_base_impl<true, false, _Tp>
275 : _Weak_result_type<_Tp>
277 typedef typename _Tp::argument_type argument_type;
280 // Nested first_argument_type and second_argument_type only.
281 template<typename _Tp>
282 struct _Reference_wrapper_base_impl<false, true, _Tp>
283 : _Weak_result_type<_Tp>
285 typedef typename _Tp::first_argument_type first_argument_type;
286 typedef typename _Tp::second_argument_type second_argument_type;
289 // All the nested argument types.
290 template<typename _Tp>
291 struct _Reference_wrapper_base_impl<true, true, _Tp>
292 : _Weak_result_type<_Tp>
294 typedef typename _Tp::argument_type argument_type;
295 typedef typename _Tp::first_argument_type first_argument_type;
296 typedef typename _Tp::second_argument_type second_argument_type;
299 _GLIBCXX_HAS_NESTED_TYPE(argument_type)
300 _GLIBCXX_HAS_NESTED_TYPE(first_argument_type)
301 _GLIBCXX_HAS_NESTED_TYPE(second_argument_type)
304 * Derives from unary_function or binary_function when it
305 * can. Specializations handle all of the easy cases. The primary
306 * template determines what to do with a class type, which may
307 * derive from both unary_function and binary_function.
309 template<typename _Tp>
310 struct _Reference_wrapper_base
311 : _Reference_wrapper_base_impl<
312 __has_argument_type<_Tp>::value,
313 __has_first_argument_type<_Tp>::value
314 && __has_second_argument_type<_Tp>::value,
318 // - a function type (unary)
319 template<typename _Res, typename _T1>
320 struct _Reference_wrapper_base<_Res(_T1)>
321 : unary_function<_T1, _Res>
324 template<typename _Res, typename _T1>
325 struct _Reference_wrapper_base<_Res(_T1) const>
326 : unary_function<_T1, _Res>
329 template<typename _Res, typename _T1>
330 struct _Reference_wrapper_base<_Res(_T1) volatile>
331 : unary_function<_T1, _Res>
334 template<typename _Res, typename _T1>
335 struct _Reference_wrapper_base<_Res(_T1) const volatile>
336 : unary_function<_T1, _Res>
339 // - a function type (binary)
340 template<typename _Res, typename _T1, typename _T2>
341 struct _Reference_wrapper_base<_Res(_T1, _T2)>
342 : binary_function<_T1, _T2, _Res>
345 template<typename _Res, typename _T1, typename _T2>
346 struct _Reference_wrapper_base<_Res(_T1, _T2) const>
347 : binary_function<_T1, _T2, _Res>
350 template<typename _Res, typename _T1, typename _T2>
351 struct _Reference_wrapper_base<_Res(_T1, _T2) volatile>
352 : binary_function<_T1, _T2, _Res>
355 template<typename _Res, typename _T1, typename _T2>
356 struct _Reference_wrapper_base<_Res(_T1, _T2) const volatile>
357 : binary_function<_T1, _T2, _Res>
360 // - a function pointer type (unary)
361 template<typename _Res, typename _T1>
362 struct _Reference_wrapper_base<_Res(*)(_T1)>
363 : unary_function<_T1, _Res>
366 // - a function pointer type (binary)
367 template<typename _Res, typename _T1, typename _T2>
368 struct _Reference_wrapper_base<_Res(*)(_T1, _T2)>
369 : binary_function<_T1, _T2, _Res>
372 // - a pointer to member function type (unary, no qualifiers)
373 template<typename _Res, typename _T1>
374 struct _Reference_wrapper_base<_Res (_T1::*)()>
375 : unary_function<_T1*, _Res>
378 // - a pointer to member function type (binary, no qualifiers)
379 template<typename _Res, typename _T1, typename _T2>
380 struct _Reference_wrapper_base<_Res (_T1::*)(_T2)>
381 : binary_function<_T1*, _T2, _Res>
384 // - a pointer to member function type (unary, const)
385 template<typename _Res, typename _T1>
386 struct _Reference_wrapper_base<_Res (_T1::*)() const>
387 : unary_function<const _T1*, _Res>
390 // - a pointer to member function type (binary, const)
391 template<typename _Res, typename _T1, typename _T2>
392 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const>
393 : binary_function<const _T1*, _T2, _Res>
396 // - a pointer to member function type (unary, volatile)
397 template<typename _Res, typename _T1>
398 struct _Reference_wrapper_base<_Res (_T1::*)() volatile>
399 : unary_function<volatile _T1*, _Res>
402 // - a pointer to member function type (binary, volatile)
403 template<typename _Res, typename _T1, typename _T2>
404 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) volatile>
405 : binary_function<volatile _T1*, _T2, _Res>
408 // - a pointer to member function type (unary, const volatile)
409 template<typename _Res, typename _T1>
410 struct _Reference_wrapper_base<_Res (_T1::*)() const volatile>
411 : unary_function<const volatile _T1*, _Res>
414 // - a pointer to member function type (binary, const volatile)
415 template<typename _Res, typename _T1, typename _T2>
416 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const volatile>
417 : binary_function<const volatile _T1*, _T2, _Res>
421 * @brief Primary class template for reference_wrapper.
425 template<typename _Tp>
426 class reference_wrapper
427 : public _Reference_wrapper_base<typename remove_cv<_Tp>::type>
434 reference_wrapper(_Tp& __indata)
435 : _M_data(std::__addressof(__indata))
438 reference_wrapper(_Tp&&) = delete;
440 reference_wrapper(const reference_wrapper<_Tp>& __inref):
441 _M_data(__inref._M_data)
445 operator=(const reference_wrapper<_Tp>& __inref)
447 _M_data = __inref._M_data;
451 operator _Tp&() const
452 { return this->get(); }
458 template<typename... _Args>
459 typename result_of<_Tp&(_Args&&...)>::type
460 operator()(_Args&&... __args) const
462 return __invoke(get(), std::forward<_Args>(__args)...);
467 /// Denotes a reference should be taken to a variable.
468 template<typename _Tp>
469 inline reference_wrapper<_Tp>
471 { return reference_wrapper<_Tp>(__t); }
473 /// Denotes a const reference should be taken to a variable.
474 template<typename _Tp>
475 inline reference_wrapper<const _Tp>
477 { return reference_wrapper<const _Tp>(__t); }
479 template<typename _Tp>
480 void ref(const _Tp&&) = delete;
482 template<typename _Tp>
483 void cref(const _Tp&&) = delete;
485 /// Partial specialization.
486 template<typename _Tp>
487 inline reference_wrapper<_Tp>
488 ref(reference_wrapper<_Tp> __t)
489 { return ref(__t.get()); }
491 /// Partial specialization.
492 template<typename _Tp>
493 inline reference_wrapper<const _Tp>
494 cref(reference_wrapper<_Tp> __t)
495 { return cref(__t.get()); }
499 template<typename _MemberPointer>
503 * Derives from @c unary_function or @c binary_function, or perhaps
504 * nothing, depending on the number of arguments provided. The
505 * primary template is the basis case, which derives nothing.
507 template<typename _Res, typename... _ArgTypes>
508 struct _Maybe_unary_or_binary_function { };
510 /// Derives from @c unary_function, as appropriate.
511 template<typename _Res, typename _T1>
512 struct _Maybe_unary_or_binary_function<_Res, _T1>
513 : std::unary_function<_T1, _Res> { };
515 /// Derives from @c binary_function, as appropriate.
516 template<typename _Res, typename _T1, typename _T2>
517 struct _Maybe_unary_or_binary_function<_Res, _T1, _T2>
518 : std::binary_function<_T1, _T2, _Res> { };
520 /// Implementation of @c mem_fn for member function pointers.
521 template<typename _Res, typename _Class, typename... _ArgTypes>
522 class _Mem_fn<_Res (_Class::*)(_ArgTypes...)>
523 : public _Maybe_unary_or_binary_function<_Res, _Class*, _ArgTypes...>
525 typedef _Res (_Class::*_Functor)(_ArgTypes...);
527 template<typename _Tp>
529 _M_call(_Tp& __object, const volatile _Class *,
530 _ArgTypes... __args) const
531 { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); }
533 template<typename _Tp>
535 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
536 { return ((*__ptr).*__pmf)(std::forward<_ArgTypes>(__args)...); }
539 typedef _Res result_type;
541 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
545 operator()(_Class& __object, _ArgTypes... __args) const
546 { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); }
550 operator()(_Class* __object, _ArgTypes... __args) const
551 { return (__object->*__pmf)(std::forward<_ArgTypes>(__args)...); }
553 // Handle smart pointers, references and pointers to derived
554 template<typename _Tp>
556 operator()(_Tp& __object, _ArgTypes... __args) const
558 return _M_call(__object, &__object,
559 std::forward<_ArgTypes>(__args)...);
566 /// Implementation of @c mem_fn for const member function pointers.
567 template<typename _Res, typename _Class, typename... _ArgTypes>
568 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) const>
569 : public _Maybe_unary_or_binary_function<_Res, const _Class*,
572 typedef _Res (_Class::*_Functor)(_ArgTypes...) const;
574 template<typename _Tp>
576 _M_call(_Tp& __object, const volatile _Class *,
577 _ArgTypes... __args) const
578 { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); }
580 template<typename _Tp>
582 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
583 { return ((*__ptr).*__pmf)(std::forward<_ArgTypes>(__args)...); }
586 typedef _Res result_type;
588 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
592 operator()(const _Class& __object, _ArgTypes... __args) const
593 { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); }
597 operator()(const _Class* __object, _ArgTypes... __args) const
598 { return (__object->*__pmf)(std::forward<_ArgTypes>(__args)...); }
600 // Handle smart pointers, references and pointers to derived
601 template<typename _Tp>
602 _Res operator()(_Tp& __object, _ArgTypes... __args) const
604 return _M_call(__object, &__object,
605 std::forward<_ArgTypes>(__args)...);
612 /// Implementation of @c mem_fn for volatile member function pointers.
613 template<typename _Res, typename _Class, typename... _ArgTypes>
614 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) volatile>
615 : public _Maybe_unary_or_binary_function<_Res, volatile _Class*,
618 typedef _Res (_Class::*_Functor)(_ArgTypes...) volatile;
620 template<typename _Tp>
622 _M_call(_Tp& __object, const volatile _Class *,
623 _ArgTypes... __args) const
624 { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); }
626 template<typename _Tp>
628 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
629 { return ((*__ptr).*__pmf)(std::forward<_ArgTypes>(__args)...); }
632 typedef _Res result_type;
634 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
638 operator()(volatile _Class& __object, _ArgTypes... __args) const
639 { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); }
643 operator()(volatile _Class* __object, _ArgTypes... __args) const
644 { return (__object->*__pmf)(std::forward<_ArgTypes>(__args)...); }
646 // Handle smart pointers, references and pointers to derived
647 template<typename _Tp>
649 operator()(_Tp& __object, _ArgTypes... __args) const
651 return _M_call(__object, &__object,
652 std::forward<_ArgTypes>(__args)...);
659 /// Implementation of @c mem_fn for const volatile member function pointers.
660 template<typename _Res, typename _Class, typename... _ArgTypes>
661 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) const volatile>
662 : public _Maybe_unary_or_binary_function<_Res, const volatile _Class*,
665 typedef _Res (_Class::*_Functor)(_ArgTypes...) const volatile;
667 template<typename _Tp>
669 _M_call(_Tp& __object, const volatile _Class *,
670 _ArgTypes... __args) const
671 { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); }
673 template<typename _Tp>
675 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
676 { return ((*__ptr).*__pmf)(std::forward<_ArgTypes>(__args)...); }
679 typedef _Res result_type;
681 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
685 operator()(const volatile _Class& __object, _ArgTypes... __args) const
686 { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); }
690 operator()(const volatile _Class* __object, _ArgTypes... __args) const
691 { return (__object->*__pmf)(std::forward<_ArgTypes>(__args)...); }
693 // Handle smart pointers, references and pointers to derived
694 template<typename _Tp>
695 _Res operator()(_Tp& __object, _ArgTypes... __args) const
697 return _M_call(__object, &__object,
698 std::forward<_ArgTypes>(__args)...);
706 template<typename _Tp, bool>
707 struct _Mem_fn_const_or_non
709 typedef const _Tp& type;
712 template<typename _Tp>
713 struct _Mem_fn_const_or_non<_Tp, false>
718 template<typename _Res, typename _Class>
719 class _Mem_fn<_Res _Class::*>
721 // This bit of genius is due to Peter Dimov, improved slightly by
723 template<typename _Tp>
725 _M_call(_Tp& __object, _Class *) const
726 { return __object.*__pm; }
728 template<typename _Tp, typename _Up>
730 _M_call(_Tp& __object, _Up * const *) const
731 { return (*__object).*__pm; }
733 template<typename _Tp, typename _Up>
735 _M_call(_Tp& __object, const _Up * const *) const
736 { return (*__object).*__pm; }
738 template<typename _Tp>
740 _M_call(_Tp& __object, const _Class *) const
741 { return __object.*__pm; }
743 template<typename _Tp>
745 _M_call(_Tp& __ptr, const volatile void*) const
746 { return (*__ptr).*__pm; }
748 template<typename _Tp> static _Tp& __get_ref();
750 template<typename _Tp>
751 static __sfinae_types::__one __check_const(_Tp&, _Class*);
752 template<typename _Tp, typename _Up>
753 static __sfinae_types::__one __check_const(_Tp&, _Up * const *);
754 template<typename _Tp, typename _Up>
755 static __sfinae_types::__two __check_const(_Tp&, const _Up * const *);
756 template<typename _Tp>
757 static __sfinae_types::__two __check_const(_Tp&, const _Class*);
758 template<typename _Tp>
759 static __sfinae_types::__two __check_const(_Tp&, const volatile void*);
762 template<typename _Tp>
764 : _Mem_fn_const_or_non<_Res,
765 (sizeof(__sfinae_types::__two)
766 == sizeof(__check_const<_Tp>(__get_ref<_Tp>(), (_Tp*)0)))>
769 template<typename _Signature>
772 template<typename _CVMem, typename _Tp>
773 struct result<_CVMem(_Tp)>
774 : public _Result_type<_Tp> { };
776 template<typename _CVMem, typename _Tp>
777 struct result<_CVMem(_Tp&)>
778 : public _Result_type<_Tp> { };
781 _Mem_fn(_Res _Class::*__pm) : __pm(__pm) { }
785 operator()(_Class& __object) const
786 { return __object.*__pm; }
789 operator()(const _Class& __object) const
790 { return __object.*__pm; }
794 operator()(_Class* __object) const
795 { return __object->*__pm; }
798 operator()(const _Class* __object) const
799 { return __object->*__pm; }
801 // Handle smart pointers and derived
802 template<typename _Tp>
803 typename _Result_type<_Tp>::type
804 operator()(_Tp& __unknown) const
805 { return _M_call(__unknown, &__unknown); }
812 * @brief Returns a function object that forwards to the member
816 template<typename _Tp, typename _Class>
817 inline _Mem_fn<_Tp _Class::*>
818 mem_fn(_Tp _Class::* __pm)
820 return _Mem_fn<_Tp _Class::*>(__pm);
824 * @brief Determines if the given type _Tp is a function object
825 * should be treated as a subexpression when evaluating calls to
826 * function objects returned by bind(). [TR1 3.6.1]
829 template<typename _Tp>
830 struct is_bind_expression
831 : public false_type { };
834 * @brief Determines if the given type _Tp is a placeholder in a
835 * bind() expression and, if so, which placeholder it is. [TR1 3.6.2]
838 template<typename _Tp>
839 struct is_placeholder
840 : public integral_constant<int, 0>
843 /// The type of placeholder objects defined by libstdc++.
844 template<int _Num> struct _Placeholder { };
846 _GLIBCXX_END_NAMESPACE_VERSION
848 /** @namespace std::placeholders
849 * @brief ISO C++ 0x entities sub namespace for functional.
852 * Define a large number of placeholders. There is no way to
853 * simplify this with variadic templates, because we're introducing
854 * unique names for each.
856 namespace placeholders
858 _GLIBCXX_BEGIN_NAMESPACE_VERSION
859 extern const _Placeholder<1> _1;
860 extern const _Placeholder<2> _2;
861 extern const _Placeholder<3> _3;
862 extern const _Placeholder<4> _4;
863 extern const _Placeholder<5> _5;
864 extern const _Placeholder<6> _6;
865 extern const _Placeholder<7> _7;
866 extern const _Placeholder<8> _8;
867 extern const _Placeholder<9> _9;
868 extern const _Placeholder<10> _10;
869 extern const _Placeholder<11> _11;
870 extern const _Placeholder<12> _12;
871 extern const _Placeholder<13> _13;
872 extern const _Placeholder<14> _14;
873 extern const _Placeholder<15> _15;
874 extern const _Placeholder<16> _16;
875 extern const _Placeholder<17> _17;
876 extern const _Placeholder<18> _18;
877 extern const _Placeholder<19> _19;
878 extern const _Placeholder<20> _20;
879 extern const _Placeholder<21> _21;
880 extern const _Placeholder<22> _22;
881 extern const _Placeholder<23> _23;
882 extern const _Placeholder<24> _24;
883 extern const _Placeholder<25> _25;
884 extern const _Placeholder<26> _26;
885 extern const _Placeholder<27> _27;
886 extern const _Placeholder<28> _28;
887 extern const _Placeholder<29> _29;
888 _GLIBCXX_END_NAMESPACE_VERSION
891 _GLIBCXX_BEGIN_NAMESPACE_VERSION
894 * Partial specialization of is_placeholder that provides the placeholder
895 * number for the placeholder objects defined by libstdc++.
899 struct is_placeholder<_Placeholder<_Num> >
900 : public integral_constant<int, _Num>
904 * Used by _Safe_tuple_element to indicate that there is no tuple
905 * element at this position.
907 struct _No_tuple_element;
910 * Implementation helper for _Safe_tuple_element. This primary
911 * template handles the case where it is safe to use @c
914 template<int __i, typename _Tuple, bool _IsSafe>
915 struct _Safe_tuple_element_impl
916 : tuple_element<__i, _Tuple> { };
919 * Implementation helper for _Safe_tuple_element. This partial
920 * specialization handles the case where it is not safe to use @c
921 * tuple_element. We just return @c _No_tuple_element.
923 template<int __i, typename _Tuple>
924 struct _Safe_tuple_element_impl<__i, _Tuple, false>
926 typedef _No_tuple_element type;
930 * Like tuple_element, but returns @c _No_tuple_element when
931 * tuple_element would return an error.
933 template<int __i, typename _Tuple>
934 struct _Safe_tuple_element
935 : _Safe_tuple_element_impl<__i, _Tuple,
936 (__i >= 0 && __i < tuple_size<_Tuple>::value)>
940 * Maps an argument to bind() into an actual argument to the bound
941 * function object [TR1 3.6.3/5]. Only the first parameter should
942 * be specified: the rest are used to determine among the various
943 * implementations. Note that, although this class is a function
944 * object, it isn't entirely normal because it takes only two
945 * parameters regardless of the number of parameters passed to the
946 * bind expression. The first parameter is the bound argument and
947 * the second parameter is a tuple containing references to the
948 * rest of the arguments.
950 template<typename _Arg,
951 bool _IsBindExp = is_bind_expression<_Arg>::value,
952 bool _IsPlaceholder = (is_placeholder<_Arg>::value > 0)>
956 * If the argument is reference_wrapper<_Tp>, returns the
957 * underlying reference. [TR1 3.6.3/5 bullet 1]
959 template<typename _Tp>
960 class _Mu<reference_wrapper<_Tp>, false, false>
963 typedef _Tp& result_type;
965 /* Note: This won't actually work for const volatile
966 * reference_wrappers, because reference_wrapper::get() is const
967 * but not volatile-qualified. This might be a defect in the TR.
969 template<typename _CVRef, typename _Tuple>
971 operator()(_CVRef& __arg, _Tuple&) const volatile
972 { return __arg.get(); }
976 * If the argument is a bind expression, we invoke the underlying
977 * function object with the same cv-qualifiers as we are given and
978 * pass along all of our arguments (unwrapped). [TR1 3.6.3/5 bullet 2]
980 template<typename _Arg>
981 class _Mu<_Arg, true, false>
984 template<typename _CVArg, typename... _Args>
986 operator()(_CVArg& __arg,
987 tuple<_Args...>& __tuple) const volatile
988 -> decltype(__arg(declval<_Args>()...))
990 // Construct an index tuple and forward to __call
991 typedef typename _Build_index_tuple<sizeof...(_Args)>::__type
993 return this->__call(__arg, __tuple, _Indexes());
997 // Invokes the underlying function object __arg by unpacking all
998 // of the arguments in the tuple.
999 template<typename _CVArg, typename... _Args, int... _Indexes>
1001 __call(_CVArg& __arg, tuple<_Args...>& __tuple,
1002 const _Index_tuple<_Indexes...>&) const volatile
1003 -> decltype(__arg(declval<_Args>()...))
1005 return __arg(std::forward<_Args>(get<_Indexes>(__tuple))...);
1010 * If the argument is a placeholder for the Nth argument, returns
1011 * a reference to the Nth argument to the bind function object.
1012 * [TR1 3.6.3/5 bullet 3]
1014 template<typename _Arg>
1015 class _Mu<_Arg, false, true>
1018 template<typename _Signature> class result;
1020 template<typename _CVMu, typename _CVArg, typename _Tuple>
1021 class result<_CVMu(_CVArg, _Tuple)>
1023 // Add a reference, if it hasn't already been done for us.
1024 // This allows us to be a little bit sloppy in constructing
1025 // the tuple that we pass to result_of<...>.
1026 typedef typename _Safe_tuple_element<(is_placeholder<_Arg>::value
1031 typedef typename add_rvalue_reference<__base_type>::type type;
1034 template<typename _Tuple>
1035 typename result<_Mu(_Arg, _Tuple)>::type
1036 operator()(const volatile _Arg&, _Tuple& __tuple) const volatile
1038 return std::forward<typename result<_Mu(_Arg, _Tuple)>::type>(
1039 ::std::get<(is_placeholder<_Arg>::value - 1)>(__tuple));
1044 * If the argument is just a value, returns a reference to that
1045 * value. The cv-qualifiers on the reference are the same as the
1046 * cv-qualifiers on the _Mu object. [TR1 3.6.3/5 bullet 4]
1048 template<typename _Arg>
1049 class _Mu<_Arg, false, false>
1052 template<typename _Signature> struct result;
1054 template<typename _CVMu, typename _CVArg, typename _Tuple>
1055 struct result<_CVMu(_CVArg, _Tuple)>
1057 typedef typename add_lvalue_reference<_CVArg>::type type;
1060 // Pick up the cv-qualifiers of the argument
1061 template<typename _CVArg, typename _Tuple>
1063 operator()(_CVArg&& __arg, _Tuple&) const volatile
1064 { return std::forward<_CVArg>(__arg); }
1068 * Maps member pointers into instances of _Mem_fn but leaves all
1069 * other function objects untouched. Used by tr1::bind(). The
1070 * primary template handles the non--member-pointer case.
1072 template<typename _Tp>
1073 struct _Maybe_wrap_member_pointer
1078 __do_wrap(const _Tp& __x)
1082 __do_wrap(_Tp&& __x)
1083 { return static_cast<_Tp&&>(__x); }
1087 * Maps member pointers into instances of _Mem_fn but leaves all
1088 * other function objects untouched. Used by tr1::bind(). This
1089 * partial specialization handles the member pointer case.
1091 template<typename _Tp, typename _Class>
1092 struct _Maybe_wrap_member_pointer<_Tp _Class::*>
1094 typedef _Mem_fn<_Tp _Class::*> type;
1097 __do_wrap(_Tp _Class::* __pm)
1098 { return type(__pm); }
1101 // Specialization needed to prevent "forming reference to void" errors when
1102 // bind<void>() is called, because argument deduction instantiates
1103 // _Maybe_wrap_member_pointer<void> outside the immediate context where
1106 struct _Maybe_wrap_member_pointer<void>
1111 // std::get<I> for volatile-qualified tuples
1112 template<size_t _Ind, typename... _Tp>
1114 __volget(volatile tuple<_Tp...>& __tuple)
1115 -> typename tuple_element<_Ind, tuple<_Tp...>>::type volatile&
1116 { return std::get<_Ind>(const_cast<tuple<_Tp...>&>(__tuple)); }
1118 // std::get<I> for const-volatile-qualified tuples
1119 template<size_t _Ind, typename... _Tp>
1121 __volget(const volatile tuple<_Tp...>& __tuple)
1122 -> typename tuple_element<_Ind, tuple<_Tp...>>::type const volatile&
1123 { return std::get<_Ind>(const_cast<const tuple<_Tp...>&>(__tuple)); }
1125 /// Type of the function object returned from bind().
1126 template<typename _Signature>
1129 template<typename _Functor, typename... _Bound_args>
1130 class _Bind<_Functor(_Bound_args...)>
1131 : public _Weak_result_type<_Functor>
1133 typedef _Bind __self_type;
1134 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
1138 tuple<_Bound_args...> _M_bound_args;
1141 template<typename _Result, typename... _Args, int... _Indexes>
1143 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>)
1145 return _M_f(_Mu<_Bound_args>()
1146 (get<_Indexes>(_M_bound_args), __args)...);
1150 template<typename _Result, typename... _Args, int... _Indexes>
1152 __call_c(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>) const
1154 return _M_f(_Mu<_Bound_args>()
1155 (get<_Indexes>(_M_bound_args), __args)...);
1159 template<typename _Result, typename... _Args, int... _Indexes>
1161 __call_v(tuple<_Args...>&& __args,
1162 _Index_tuple<_Indexes...>) volatile
1164 return _M_f(_Mu<_Bound_args>()
1165 (__volget<_Indexes>(_M_bound_args), __args)...);
1168 // Call as const volatile
1169 template<typename _Result, typename... _Args, int... _Indexes>
1171 __call_c_v(tuple<_Args...>&& __args,
1172 _Index_tuple<_Indexes...>) const volatile
1174 return _M_f(_Mu<_Bound_args>()
1175 (__volget<_Indexes>(_M_bound_args), __args)...);
1179 template<typename... _Args>
1180 explicit _Bind(const _Functor& __f, _Args&&... __args)
1181 : _M_f(__f), _M_bound_args(std::forward<_Args>(__args)...)
1184 template<typename... _Args>
1185 explicit _Bind(_Functor&& __f, _Args&&... __args)
1186 : _M_f(std::move(__f)), _M_bound_args(std::forward<_Args>(__args)...)
1189 _Bind(const _Bind&) = default;
1192 : _M_f(std::move(__b._M_f)), _M_bound_args(std::move(__b._M_bound_args))
1196 template<typename... _Args, typename _Result
1197 = decltype( std::declval<_Functor>()(
1198 _Mu<_Bound_args>()( std::declval<_Bound_args&>(),
1199 std::declval<tuple<_Args...>&>() )... ) )>
1201 operator()(_Args&&... __args)
1203 return this->__call<_Result>(
1204 std::forward_as_tuple(std::forward<_Args>(__args)...),
1209 template<typename... _Args, typename _Result
1210 = decltype( std::declval<const _Functor>()(
1211 _Mu<_Bound_args>()( std::declval<const _Bound_args&>(),
1212 std::declval<tuple<_Args...>&>() )... ) )>
1214 operator()(_Args&&... __args) const
1216 return this->__call_c<_Result>(
1217 std::forward_as_tuple(std::forward<_Args>(__args)...),
1222 template<typename... _Args, typename _Result
1223 = decltype( std::declval<volatile _Functor>()(
1224 _Mu<_Bound_args>()( std::declval<volatile _Bound_args&>(),
1225 std::declval<tuple<_Args...>&>() )... ) )>
1227 operator()(_Args&&... __args) volatile
1229 return this->__call_v<_Result>(
1230 std::forward_as_tuple(std::forward<_Args>(__args)...),
1234 // Call as const volatile
1235 template<typename... _Args, typename _Result
1236 = decltype( std::declval<const volatile _Functor>()(
1237 _Mu<_Bound_args>()( std::declval<const volatile _Bound_args&>(),
1238 std::declval<tuple<_Args...>&>() )... ) )>
1240 operator()(_Args&&... __args) const volatile
1242 return this->__call_c_v<_Result>(
1243 std::forward_as_tuple(std::forward<_Args>(__args)...),
1248 /// Type of the function object returned from bind<R>().
1249 template<typename _Result, typename _Signature>
1250 struct _Bind_result;
1252 template<typename _Result, typename _Functor, typename... _Bound_args>
1253 class _Bind_result<_Result, _Functor(_Bound_args...)>
1255 typedef _Bind_result __self_type;
1256 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
1260 tuple<_Bound_args...> _M_bound_args;
1263 template<typename _Res>
1264 struct __enable_if_void : enable_if<is_void<_Res>::value, int> { };
1265 template<typename _Res>
1266 struct __disable_if_void : enable_if<!is_void<_Res>::value, int> { };
1269 template<typename _Res, typename... _Args, int... _Indexes>
1271 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1272 typename __disable_if_void<_Res>::type = 0)
1274 return _M_f(_Mu<_Bound_args>()
1275 (get<_Indexes>(_M_bound_args), __args)...);
1278 // Call unqualified, return void
1279 template<typename _Res, typename... _Args, int... _Indexes>
1281 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1282 typename __enable_if_void<_Res>::type = 0)
1284 _M_f(_Mu<_Bound_args>()
1285 (get<_Indexes>(_M_bound_args), __args)...);
1289 template<typename _Res, typename... _Args, int... _Indexes>
1291 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1292 typename __disable_if_void<_Res>::type = 0) const
1294 return _M_f(_Mu<_Bound_args>()
1295 (get<_Indexes>(_M_bound_args), __args)...);
1298 // Call as const, return void
1299 template<typename _Res, typename... _Args, int... _Indexes>
1301 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1302 typename __enable_if_void<_Res>::type = 0) const
1304 _M_f(_Mu<_Bound_args>()
1305 (get<_Indexes>(_M_bound_args), __args)...);
1309 template<typename _Res, typename... _Args, int... _Indexes>
1311 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1312 typename __disable_if_void<_Res>::type = 0) volatile
1314 return _M_f(_Mu<_Bound_args>()
1315 (__volget<_Indexes>(_M_bound_args), __args)...);
1318 // Call as volatile, return void
1319 template<typename _Res, typename... _Args, int... _Indexes>
1321 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1322 typename __enable_if_void<_Res>::type = 0) volatile
1324 _M_f(_Mu<_Bound_args>()
1325 (__volget<_Indexes>(_M_bound_args), __args)...);
1328 // Call as const volatile
1329 template<typename _Res, typename... _Args, int... _Indexes>
1331 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1332 typename __disable_if_void<_Res>::type = 0) const volatile
1334 return _M_f(_Mu<_Bound_args>()
1335 (__volget<_Indexes>(_M_bound_args), __args)...);
1338 // Call as const volatile, return void
1339 template<typename _Res, typename... _Args, int... _Indexes>
1341 __call(tuple<_Args...>&& __args,
1342 _Index_tuple<_Indexes...>,
1343 typename __enable_if_void<_Res>::type = 0) const volatile
1345 _M_f(_Mu<_Bound_args>()
1346 (__volget<_Indexes>(_M_bound_args), __args)...);
1350 typedef _Result result_type;
1352 template<typename... _Args>
1353 explicit _Bind_result(const _Functor& __f, _Args&&... __args)
1354 : _M_f(__f), _M_bound_args(std::forward<_Args>(__args)...)
1357 template<typename... _Args>
1358 explicit _Bind_result(_Functor&& __f, _Args&&... __args)
1359 : _M_f(std::move(__f)), _M_bound_args(std::forward<_Args>(__args)...)
1362 _Bind_result(const _Bind_result&) = default;
1364 _Bind_result(_Bind_result&& __b)
1365 : _M_f(std::move(__b._M_f)), _M_bound_args(std::move(__b._M_bound_args))
1369 template<typename... _Args>
1371 operator()(_Args&&... __args)
1373 return this->__call<_Result>(
1374 std::forward_as_tuple(std::forward<_Args>(__args)...),
1379 template<typename... _Args>
1381 operator()(_Args&&... __args) const
1383 return this->__call<_Result>(
1384 std::forward_as_tuple(std::forward<_Args>(__args)...),
1389 template<typename... _Args>
1391 operator()(_Args&&... __args) volatile
1393 return this->__call<_Result>(
1394 std::forward_as_tuple(std::forward<_Args>(__args)...),
1398 // Call as const volatile
1399 template<typename... _Args>
1401 operator()(_Args&&... __args) const volatile
1403 return this->__call<_Result>(
1404 std::forward_as_tuple(std::forward<_Args>(__args)...),
1410 * @brief Class template _Bind is always a bind expression.
1413 template<typename _Signature>
1414 struct is_bind_expression<_Bind<_Signature> >
1415 : public true_type { };
1418 * @brief Class template _Bind is always a bind expression.
1421 template<typename _Result, typename _Signature>
1422 struct is_bind_expression<_Bind_result<_Result, _Signature> >
1423 : public true_type { };
1425 template<typename _Functor, typename... _ArgTypes>
1428 typedef _Maybe_wrap_member_pointer<typename decay<_Functor>::type>
1430 typedef typename __maybe_type::type __functor_type;
1431 typedef _Bind<__functor_type(typename decay<_ArgTypes>::type...)> type;
1435 * @brief Function template for std::bind.
1438 template<typename _Functor, typename... _ArgTypes>
1440 typename _Bind_helper<_Functor, _ArgTypes...>::type
1441 bind(_Functor&& __f, _ArgTypes&&... __args)
1443 typedef _Bind_helper<_Functor, _ArgTypes...> __helper_type;
1444 typedef typename __helper_type::__maybe_type __maybe_type;
1445 typedef typename __helper_type::type __result_type;
1446 return __result_type(__maybe_type::__do_wrap(std::forward<_Functor>(__f)),
1447 std::forward<_ArgTypes>(__args)...);
1450 template<typename _Result, typename _Functor, typename... _ArgTypes>
1451 struct _Bindres_helper
1453 typedef _Maybe_wrap_member_pointer<typename decay<_Functor>::type>
1455 typedef typename __maybe_type::type __functor_type;
1456 typedef _Bind_result<_Result,
1457 __functor_type(typename decay<_ArgTypes>::type...)>
1462 * @brief Function template for std::bind<R>.
1465 template<typename _Result, typename _Functor, typename... _ArgTypes>
1467 typename _Bindres_helper<_Result, _Functor, _ArgTypes...>::type
1468 bind(_Functor&& __f, _ArgTypes&&... __args)
1470 typedef _Bindres_helper<_Result, _Functor, _ArgTypes...> __helper_type;
1471 typedef typename __helper_type::__maybe_type __maybe_type;
1472 typedef typename __helper_type::type __result_type;
1473 return __result_type(__maybe_type::__do_wrap(std::forward<_Functor>(__f)),
1474 std::forward<_ArgTypes>(__args)...);
1478 * @brief Exception class thrown when class template function's
1479 * operator() is called with an empty target.
1480 * @ingroup exceptions
1482 class bad_function_call : public std::exception
1485 virtual ~bad_function_call() throw();
1489 * Trait identifying "location-invariant" types, meaning that the
1490 * address of the object (or any of its members) will not escape.
1491 * Also implies a trivial copy constructor and assignment operator.
1493 template<typename _Tp>
1494 struct __is_location_invariant
1495 : integral_constant<bool, (is_pointer<_Tp>::value
1496 || is_member_pointer<_Tp>::value)>
1499 class _Undefined_class;
1504 const void* _M_const_object;
1505 void (*_M_function_pointer)();
1506 void (_Undefined_class::*_M_member_pointer)();
1511 void* _M_access() { return &_M_pod_data[0]; }
1512 const void* _M_access() const { return &_M_pod_data[0]; }
1514 template<typename _Tp>
1517 { return *static_cast<_Tp*>(_M_access()); }
1519 template<typename _Tp>
1522 { return *static_cast<const _Tp*>(_M_access()); }
1524 _Nocopy_types _M_unused;
1525 char _M_pod_data[sizeof(_Nocopy_types)];
1528 enum _Manager_operation
1536 // Simple type wrapper that helps avoid annoying const problems
1537 // when casting between void pointers and pointers-to-pointers.
1538 template<typename _Tp>
1539 struct _Simple_type_wrapper
1541 _Simple_type_wrapper(_Tp __value) : __value(__value) { }
1546 template<typename _Tp>
1547 struct __is_location_invariant<_Simple_type_wrapper<_Tp> >
1548 : __is_location_invariant<_Tp>
1551 // Converts a reference to a function object into a callable
1553 template<typename _Functor>
1555 __callable_functor(_Functor& __f)
1558 template<typename _Member, typename _Class>
1559 inline _Mem_fn<_Member _Class::*>
1560 __callable_functor(_Member _Class::* &__p)
1561 { return mem_fn(__p); }
1563 template<typename _Member, typename _Class>
1564 inline _Mem_fn<_Member _Class::*>
1565 __callable_functor(_Member _Class::* const &__p)
1566 { return mem_fn(__p); }
1568 template<typename _Signature>
1571 /// Base class of all polymorphic function object wrappers.
1572 class _Function_base
1575 static const std::size_t _M_max_size = sizeof(_Nocopy_types);
1576 static const std::size_t _M_max_align = __alignof__(_Nocopy_types);
1578 template<typename _Functor>
1582 static const bool __stored_locally =
1583 (__is_location_invariant<_Functor>::value
1584 && sizeof(_Functor) <= _M_max_size
1585 && __alignof__(_Functor) <= _M_max_align
1586 && (_M_max_align % __alignof__(_Functor) == 0));
1588 typedef integral_constant<bool, __stored_locally> _Local_storage;
1590 // Retrieve a pointer to the function object
1592 _M_get_pointer(const _Any_data& __source)
1594 const _Functor* __ptr =
1595 __stored_locally? std::__addressof(__source._M_access<_Functor>())
1596 /* have stored a pointer */ : __source._M_access<_Functor*>();
1597 return const_cast<_Functor*>(__ptr);
1600 // Clone a location-invariant function object that fits within
1601 // an _Any_data structure.
1603 _M_clone(_Any_data& __dest, const _Any_data& __source, true_type)
1605 new (__dest._M_access()) _Functor(__source._M_access<_Functor>());
1608 // Clone a function object that is not location-invariant or
1609 // that cannot fit into an _Any_data structure.
1611 _M_clone(_Any_data& __dest, const _Any_data& __source, false_type)
1613 __dest._M_access<_Functor*>() =
1614 new _Functor(*__source._M_access<_Functor*>());
1617 // Destroying a location-invariant object may still require
1620 _M_destroy(_Any_data& __victim, true_type)
1622 __victim._M_access<_Functor>().~_Functor();
1625 // Destroying an object located on the heap.
1627 _M_destroy(_Any_data& __victim, false_type)
1629 delete __victim._M_access<_Functor*>();
1634 _M_manager(_Any_data& __dest, const _Any_data& __source,
1635 _Manager_operation __op)
1640 case __get_type_info:
1641 __dest._M_access<const type_info*>() = &typeid(_Functor);
1644 case __get_functor_ptr:
1645 __dest._M_access<_Functor*>() = _M_get_pointer(__source);
1648 case __clone_functor:
1649 _M_clone(__dest, __source, _Local_storage());
1652 case __destroy_functor:
1653 _M_destroy(__dest, _Local_storage());
1660 _M_init_functor(_Any_data& __functor, _Functor&& __f)
1661 { _M_init_functor(__functor, std::move(__f), _Local_storage()); }
1663 template<typename _Signature>
1665 _M_not_empty_function(const function<_Signature>& __f)
1666 { return static_cast<bool>(__f); }
1668 template<typename _Tp>
1670 _M_not_empty_function(const _Tp*& __fp)
1673 template<typename _Class, typename _Tp>
1675 _M_not_empty_function(_Tp _Class::* const& __mp)
1678 template<typename _Tp>
1680 _M_not_empty_function(const _Tp&)
1685 _M_init_functor(_Any_data& __functor, _Functor&& __f, true_type)
1686 { new (__functor._M_access()) _Functor(std::move(__f)); }
1689 _M_init_functor(_Any_data& __functor, _Functor&& __f, false_type)
1690 { __functor._M_access<_Functor*>() = new _Functor(std::move(__f)); }
1693 template<typename _Functor>
1694 class _Ref_manager : public _Base_manager<_Functor*>
1696 typedef _Function_base::_Base_manager<_Functor*> _Base;
1700 _M_manager(_Any_data& __dest, const _Any_data& __source,
1701 _Manager_operation __op)
1706 case __get_type_info:
1707 __dest._M_access<const type_info*>() = &typeid(_Functor);
1710 case __get_functor_ptr:
1711 __dest._M_access<_Functor*>() = *_Base::_M_get_pointer(__source);
1712 return is_const<_Functor>::value;
1716 _Base::_M_manager(__dest, __source, __op);
1722 _M_init_functor(_Any_data& __functor, reference_wrapper<_Functor> __f)
1724 // TBD: Use address_of function instead.
1725 _Base::_M_init_functor(__functor, &__f.get());
1729 _Function_base() : _M_manager(0) { }
1734 _M_manager(_M_functor, _M_functor, __destroy_functor);
1738 bool _M_empty() const { return !_M_manager; }
1740 typedef bool (*_Manager_type)(_Any_data&, const _Any_data&,
1741 _Manager_operation);
1743 _Any_data _M_functor;
1744 _Manager_type _M_manager;
1747 template<typename _Signature, typename _Functor>
1748 class _Function_handler;
1750 template<typename _Res, typename _Functor, typename... _ArgTypes>
1751 class _Function_handler<_Res(_ArgTypes...), _Functor>
1752 : public _Function_base::_Base_manager<_Functor>
1754 typedef _Function_base::_Base_manager<_Functor> _Base;
1758 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1760 return (*_Base::_M_get_pointer(__functor))(
1761 std::forward<_ArgTypes>(__args)...);
1765 template<typename _Functor, typename... _ArgTypes>
1766 class _Function_handler<void(_ArgTypes...), _Functor>
1767 : public _Function_base::_Base_manager<_Functor>
1769 typedef _Function_base::_Base_manager<_Functor> _Base;
1773 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1775 (*_Base::_M_get_pointer(__functor))(
1776 std::forward<_ArgTypes>(__args)...);
1780 template<typename _Res, typename _Functor, typename... _ArgTypes>
1781 class _Function_handler<_Res(_ArgTypes...), reference_wrapper<_Functor> >
1782 : public _Function_base::_Ref_manager<_Functor>
1784 typedef _Function_base::_Ref_manager<_Functor> _Base;
1788 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1790 return __callable_functor(**_Base::_M_get_pointer(__functor))(
1791 std::forward<_ArgTypes>(__args)...);
1795 template<typename _Functor, typename... _ArgTypes>
1796 class _Function_handler<void(_ArgTypes...), reference_wrapper<_Functor> >
1797 : public _Function_base::_Ref_manager<_Functor>
1799 typedef _Function_base::_Ref_manager<_Functor> _Base;
1803 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1805 __callable_functor(**_Base::_M_get_pointer(__functor))(
1806 std::forward<_ArgTypes>(__args)...);
1810 template<typename _Class, typename _Member, typename _Res,
1811 typename... _ArgTypes>
1812 class _Function_handler<_Res(_ArgTypes...), _Member _Class::*>
1813 : public _Function_handler<void(_ArgTypes...), _Member _Class::*>
1815 typedef _Function_handler<void(_ArgTypes...), _Member _Class::*>
1820 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1822 return mem_fn(_Base::_M_get_pointer(__functor)->__value)(
1823 std::forward<_ArgTypes>(__args)...);
1827 template<typename _Class, typename _Member, typename... _ArgTypes>
1828 class _Function_handler<void(_ArgTypes...), _Member _Class::*>
1829 : public _Function_base::_Base_manager<
1830 _Simple_type_wrapper< _Member _Class::* > >
1832 typedef _Member _Class::* _Functor;
1833 typedef _Simple_type_wrapper<_Functor> _Wrapper;
1834 typedef _Function_base::_Base_manager<_Wrapper> _Base;
1838 _M_manager(_Any_data& __dest, const _Any_data& __source,
1839 _Manager_operation __op)
1844 case __get_type_info:
1845 __dest._M_access<const type_info*>() = &typeid(_Functor);
1848 case __get_functor_ptr:
1849 __dest._M_access<_Functor*>() =
1850 &_Base::_M_get_pointer(__source)->__value;
1854 _Base::_M_manager(__dest, __source, __op);
1860 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1862 mem_fn(_Base::_M_get_pointer(__functor)->__value)(
1863 std::forward<_ArgTypes>(__args)...);
1868 * @brief Primary class template for std::function.
1871 * Polymorphic function wrapper.
1873 template<typename _Res, typename... _ArgTypes>
1874 class function<_Res(_ArgTypes...)>
1875 : public _Maybe_unary_or_binary_function<_Res, _ArgTypes...>,
1876 private _Function_base
1878 typedef _Res _Signature_type(_ArgTypes...);
1880 struct _Useless { };
1883 typedef _Res result_type;
1885 // [3.7.2.1] construct/copy/destroy
1888 * @brief Default construct creates an empty function call wrapper.
1889 * @post @c !(bool)*this
1891 function() : _Function_base() { }
1894 * @brief Creates an empty function call wrapper.
1895 * @post @c !(bool)*this
1897 function(nullptr_t) : _Function_base() { }
1900 * @brief %Function copy constructor.
1901 * @param x A %function object with identical call signature.
1902 * @post @c (bool)*this == (bool)x
1904 * The newly-created %function contains a copy of the target of @a
1905 * x (if it has one).
1907 function(const function& __x);
1910 * @brief %Function move constructor.
1911 * @param x A %function object rvalue with identical call signature.
1913 * The newly-created %function contains the target of @a x
1916 function(function&& __x) : _Function_base()
1921 // TODO: needs allocator_arg_t
1924 * @brief Builds a %function that targets a copy of the incoming
1926 * @param f A %function object that is callable with parameters of
1927 * type @c T1, @c T2, ..., @c TN and returns a value convertible
1930 * The newly-created %function object will target a copy of @a
1931 * f. If @a f is @c reference_wrapper<F>, then this function
1932 * object will contain a reference to the function object @c
1933 * f.get(). If @a f is a NULL function pointer or NULL
1934 * pointer-to-member, the newly-created object will be empty.
1936 * If @a f is a non-NULL function pointer or an object of type @c
1937 * reference_wrapper<F>, this function will not throw.
1939 template<typename _Functor>
1940 function(_Functor __f,
1942 !is_integral<_Functor>::value, _Useless>::type
1946 * @brief %Function assignment operator.
1947 * @param x A %function with identical call signature.
1948 * @post @c (bool)*this == (bool)x
1951 * The target of @a x is copied to @c *this. If @a x has no
1952 * target, then @c *this will be empty.
1954 * If @a x targets a function pointer or a reference to a function
1955 * object, then this operation will not throw an %exception.
1958 operator=(const function& __x)
1960 function(__x).swap(*this);
1965 * @brief %Function move-assignment operator.
1966 * @param x A %function rvalue with identical call signature.
1969 * The target of @a x is moved to @c *this. If @a x has no
1970 * target, then @c *this will be empty.
1972 * If @a x targets a function pointer or a reference to a function
1973 * object, then this operation will not throw an %exception.
1976 operator=(function&& __x)
1978 function(std::move(__x)).swap(*this);
1983 * @brief %Function assignment to zero.
1984 * @post @c !(bool)*this
1987 * The target of @c *this is deallocated, leaving it empty.
1990 operator=(nullptr_t)
1994 _M_manager(_M_functor, _M_functor, __destroy_functor);
2002 * @brief %Function assignment to a new target.
2003 * @param f A %function object that is callable with parameters of
2004 * type @c T1, @c T2, ..., @c TN and returns a value convertible
2008 * This %function object wrapper will target a copy of @a
2009 * f. If @a f is @c reference_wrapper<F>, then this function
2010 * object will contain a reference to the function object @c
2011 * f.get(). If @a f is a NULL function pointer or NULL
2012 * pointer-to-member, @c this object will be empty.
2014 * If @a f is a non-NULL function pointer or an object of type @c
2015 * reference_wrapper<F>, this function will not throw.
2017 template<typename _Functor>
2018 typename enable_if<!is_integral<_Functor>::value, function&>::type
2019 operator=(_Functor&& __f)
2021 function(std::forward<_Functor>(__f)).swap(*this);
2026 template<typename _Functor>
2027 typename enable_if<!is_integral<_Functor>::value, function&>::type
2028 operator=(reference_wrapper<_Functor> __f)
2030 function(__f).swap(*this);
2034 // [3.7.2.2] function modifiers
2037 * @brief Swap the targets of two %function objects.
2038 * @param f A %function with identical call signature.
2040 * Swap the targets of @c this function object and @a f. This
2041 * function will not throw an %exception.
2043 void swap(function& __x)
2045 std::swap(_M_functor, __x._M_functor);
2046 std::swap(_M_manager, __x._M_manager);
2047 std::swap(_M_invoker, __x._M_invoker);
2050 // TODO: needs allocator_arg_t
2052 template<typename _Functor, typename _Alloc>
2054 assign(_Functor&& __f, const _Alloc& __a)
2056 function(allocator_arg, __a,
2057 std::forward<_Functor>(__f)).swap(*this);
2061 // [3.7.2.3] function capacity
2064 * @brief Determine if the %function wrapper has a target.
2066 * @return @c true when this %function object contains a target,
2067 * or @c false when it is empty.
2069 * This function will not throw an %exception.
2071 explicit operator bool() const
2072 { return !_M_empty(); }
2074 // [3.7.2.4] function invocation
2077 * @brief Invokes the function targeted by @c *this.
2078 * @returns the result of the target.
2079 * @throws bad_function_call when @c !(bool)*this
2081 * The function call operator invokes the target function object
2082 * stored by @c this.
2084 _Res operator()(_ArgTypes... __args) const;
2087 // [3.7.2.5] function target access
2089 * @brief Determine the type of the target of this function object
2092 * @returns the type identifier of the target function object, or
2093 * @c typeid(void) if @c !(bool)*this.
2095 * This function will not throw an %exception.
2097 const type_info& target_type() const;
2100 * @brief Access the stored target function object.
2102 * @return Returns a pointer to the stored target function object,
2103 * if @c typeid(Functor).equals(target_type()); otherwise, a NULL
2106 * This function will not throw an %exception.
2108 template<typename _Functor> _Functor* target();
2111 template<typename _Functor> const _Functor* target() const;
2115 typedef _Res (*_Invoker_type)(const _Any_data&, _ArgTypes...);
2116 _Invoker_type _M_invoker;
2119 // Out-of-line member definitions.
2120 template<typename _Res, typename... _ArgTypes>
2121 function<_Res(_ArgTypes...)>::
2122 function(const function& __x)
2125 if (static_cast<bool>(__x))
2127 _M_invoker = __x._M_invoker;
2128 _M_manager = __x._M_manager;
2129 __x._M_manager(_M_functor, __x._M_functor, __clone_functor);
2133 template<typename _Res, typename... _ArgTypes>
2134 template<typename _Functor>
2135 function<_Res(_ArgTypes...)>::
2136 function(_Functor __f,
2138 !is_integral<_Functor>::value, _Useless>::type)
2141 typedef _Function_handler<_Signature_type, _Functor> _My_handler;
2143 if (_My_handler::_M_not_empty_function(__f))
2145 _M_invoker = &_My_handler::_M_invoke;
2146 _M_manager = &_My_handler::_M_manager;
2147 _My_handler::_M_init_functor(_M_functor, std::move(__f));
2151 template<typename _Res, typename... _ArgTypes>
2153 function<_Res(_ArgTypes...)>::
2154 operator()(_ArgTypes... __args) const
2157 __throw_bad_function_call();
2158 return _M_invoker(_M_functor, std::forward<_ArgTypes>(__args)...);
2162 template<typename _Res, typename... _ArgTypes>
2164 function<_Res(_ArgTypes...)>::
2169 _Any_data __typeinfo_result;
2170 _M_manager(__typeinfo_result, _M_functor, __get_type_info);
2171 return *__typeinfo_result._M_access<const type_info*>();
2174 return typeid(void);
2177 template<typename _Res, typename... _ArgTypes>
2178 template<typename _Functor>
2180 function<_Res(_ArgTypes...)>::
2183 if (typeid(_Functor) == target_type() && _M_manager)
2186 if (_M_manager(__ptr, _M_functor, __get_functor_ptr)
2187 && !is_const<_Functor>::value)
2190 return __ptr._M_access<_Functor*>();
2196 template<typename _Res, typename... _ArgTypes>
2197 template<typename _Functor>
2199 function<_Res(_ArgTypes...)>::
2202 if (typeid(_Functor) == target_type() && _M_manager)
2205 _M_manager(__ptr, _M_functor, __get_functor_ptr);
2206 return __ptr._M_access<const _Functor*>();
2213 // [20.7.15.2.6] null pointer comparisons
2216 * @brief Compares a polymorphic function object wrapper against 0
2217 * (the NULL pointer).
2218 * @returns @c true if the wrapper has no target, @c false otherwise
2220 * This function will not throw an %exception.
2222 template<typename _Res, typename... _Args>
2224 operator==(const function<_Res(_Args...)>& __f, nullptr_t)
2225 { return !static_cast<bool>(__f); }
2228 template<typename _Res, typename... _Args>
2230 operator==(nullptr_t, const function<_Res(_Args...)>& __f)
2231 { return !static_cast<bool>(__f); }
2234 * @brief Compares a polymorphic function object wrapper against 0
2235 * (the NULL pointer).
2236 * @returns @c false if the wrapper has no target, @c true otherwise
2238 * This function will not throw an %exception.
2240 template<typename _Res, typename... _Args>
2242 operator!=(const function<_Res(_Args...)>& __f, nullptr_t)
2243 { return static_cast<bool>(__f); }
2246 template<typename _Res, typename... _Args>
2248 operator!=(nullptr_t, const function<_Res(_Args...)>& __f)
2249 { return static_cast<bool>(__f); }
2251 // [20.7.15.2.7] specialized algorithms
2254 * @brief Swap the targets of two polymorphic function object wrappers.
2256 * This function will not throw an %exception.
2258 template<typename _Res, typename... _Args>
2260 swap(function<_Res(_Args...)>& __x, function<_Res(_Args...)>& __y)
2263 _GLIBCXX_END_NAMESPACE_VERSION
2266 #endif // __GXX_EXPERIMENTAL_CXX0X__
2268 #endif // _GLIBCXX_FUNCTIONAL