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 // Trait type used to remove std::bind() from overload set via SFINAE
1426 // when first argument has integer type, so that std::bind() will
1427 // not be a better match than ::bind() from the BSD Sockets API.
1428 template<typename _Tp>
1429 class __is_socketlike
1431 typedef typename decay<_Tp>::type _Tp2;
1433 static const bool value =
1434 is_integral<_Tp2>::value || is_enum<_Tp2>::value;
1437 template<bool _SocketLike, typename _Func, typename... _BoundArgs>
1440 typedef _Maybe_wrap_member_pointer<typename decay<_Func>::type>
1442 typedef typename __maybe_type::type __func_type;
1443 typedef _Bind<__func_type(typename decay<_BoundArgs>::type...)> type;
1446 // Partial specialization for is_socketlike == true, does not define
1447 // nested type so std::bind() will not participate in overload resolution
1448 // when the first argument might be a socket file descriptor.
1449 template<typename _Func, typename... _BoundArgs>
1450 struct _Bind_helper<true, _Func, _BoundArgs...>
1454 * @brief Function template for std::bind.
1457 template<typename _Func, typename... _BoundArgs>
1459 _Bind_helper<__is_socketlike<_Func>::value, _Func, _BoundArgs...>::type
1460 bind(_Func&& __f, _BoundArgs&&... __args)
1462 typedef _Bind_helper<false, _Func, _BoundArgs...> __helper_type;
1463 typedef typename __helper_type::__maybe_type __maybe_type;
1464 typedef typename __helper_type::type __result_type;
1465 return __result_type(__maybe_type::__do_wrap(std::forward<_Func>(__f)),
1466 std::forward<_BoundArgs>(__args)...);
1469 template<typename _Result, typename _Func, typename... _BoundArgs>
1470 struct _Bindres_helper
1472 typedef _Maybe_wrap_member_pointer<typename decay<_Func>::type>
1474 typedef typename __maybe_type::type __functor_type;
1475 typedef _Bind_result<_Result,
1476 __functor_type(typename decay<_BoundArgs>::type...)>
1481 * @brief Function template for std::bind<R>.
1484 template<typename _Result, typename _Func, typename... _BoundArgs>
1486 typename _Bindres_helper<_Result, _Func, _BoundArgs...>::type
1487 bind(_Func&& __f, _BoundArgs&&... __args)
1489 typedef _Bindres_helper<_Result, _Func, _BoundArgs...> __helper_type;
1490 typedef typename __helper_type::__maybe_type __maybe_type;
1491 typedef typename __helper_type::type __result_type;
1492 return __result_type(__maybe_type::__do_wrap(std::forward<_Func>(__f)),
1493 std::forward<_BoundArgs>(__args)...);
1497 * @brief Exception class thrown when class template function's
1498 * operator() is called with an empty target.
1499 * @ingroup exceptions
1501 class bad_function_call : public std::exception
1504 virtual ~bad_function_call() throw();
1508 * Trait identifying "location-invariant" types, meaning that the
1509 * address of the object (or any of its members) will not escape.
1510 * Also implies a trivial copy constructor and assignment operator.
1512 template<typename _Tp>
1513 struct __is_location_invariant
1514 : integral_constant<bool, (is_pointer<_Tp>::value
1515 || is_member_pointer<_Tp>::value)>
1518 class _Undefined_class;
1523 const void* _M_const_object;
1524 void (*_M_function_pointer)();
1525 void (_Undefined_class::*_M_member_pointer)();
1530 void* _M_access() { return &_M_pod_data[0]; }
1531 const void* _M_access() const { return &_M_pod_data[0]; }
1533 template<typename _Tp>
1536 { return *static_cast<_Tp*>(_M_access()); }
1538 template<typename _Tp>
1541 { return *static_cast<const _Tp*>(_M_access()); }
1543 _Nocopy_types _M_unused;
1544 char _M_pod_data[sizeof(_Nocopy_types)];
1547 enum _Manager_operation
1555 // Simple type wrapper that helps avoid annoying const problems
1556 // when casting between void pointers and pointers-to-pointers.
1557 template<typename _Tp>
1558 struct _Simple_type_wrapper
1560 _Simple_type_wrapper(_Tp __value) : __value(__value) { }
1565 template<typename _Tp>
1566 struct __is_location_invariant<_Simple_type_wrapper<_Tp> >
1567 : __is_location_invariant<_Tp>
1570 // Converts a reference to a function object into a callable
1572 template<typename _Functor>
1574 __callable_functor(_Functor& __f)
1577 template<typename _Member, typename _Class>
1578 inline _Mem_fn<_Member _Class::*>
1579 __callable_functor(_Member _Class::* &__p)
1580 { return mem_fn(__p); }
1582 template<typename _Member, typename _Class>
1583 inline _Mem_fn<_Member _Class::*>
1584 __callable_functor(_Member _Class::* const &__p)
1585 { return mem_fn(__p); }
1587 template<typename _Signature>
1590 /// Base class of all polymorphic function object wrappers.
1591 class _Function_base
1594 static const std::size_t _M_max_size = sizeof(_Nocopy_types);
1595 static const std::size_t _M_max_align = __alignof__(_Nocopy_types);
1597 template<typename _Functor>
1601 static const bool __stored_locally =
1602 (__is_location_invariant<_Functor>::value
1603 && sizeof(_Functor) <= _M_max_size
1604 && __alignof__(_Functor) <= _M_max_align
1605 && (_M_max_align % __alignof__(_Functor) == 0));
1607 typedef integral_constant<bool, __stored_locally> _Local_storage;
1609 // Retrieve a pointer to the function object
1611 _M_get_pointer(const _Any_data& __source)
1613 const _Functor* __ptr =
1614 __stored_locally? std::__addressof(__source._M_access<_Functor>())
1615 /* have stored a pointer */ : __source._M_access<_Functor*>();
1616 return const_cast<_Functor*>(__ptr);
1619 // Clone a location-invariant function object that fits within
1620 // an _Any_data structure.
1622 _M_clone(_Any_data& __dest, const _Any_data& __source, true_type)
1624 new (__dest._M_access()) _Functor(__source._M_access<_Functor>());
1627 // Clone a function object that is not location-invariant or
1628 // that cannot fit into an _Any_data structure.
1630 _M_clone(_Any_data& __dest, const _Any_data& __source, false_type)
1632 __dest._M_access<_Functor*>() =
1633 new _Functor(*__source._M_access<_Functor*>());
1636 // Destroying a location-invariant object may still require
1639 _M_destroy(_Any_data& __victim, true_type)
1641 __victim._M_access<_Functor>().~_Functor();
1644 // Destroying an object located on the heap.
1646 _M_destroy(_Any_data& __victim, false_type)
1648 delete __victim._M_access<_Functor*>();
1653 _M_manager(_Any_data& __dest, const _Any_data& __source,
1654 _Manager_operation __op)
1659 case __get_type_info:
1660 __dest._M_access<const type_info*>() = &typeid(_Functor);
1663 case __get_functor_ptr:
1664 __dest._M_access<_Functor*>() = _M_get_pointer(__source);
1667 case __clone_functor:
1668 _M_clone(__dest, __source, _Local_storage());
1671 case __destroy_functor:
1672 _M_destroy(__dest, _Local_storage());
1679 _M_init_functor(_Any_data& __functor, _Functor&& __f)
1680 { _M_init_functor(__functor, std::move(__f), _Local_storage()); }
1682 template<typename _Signature>
1684 _M_not_empty_function(const function<_Signature>& __f)
1685 { return static_cast<bool>(__f); }
1687 template<typename _Tp>
1689 _M_not_empty_function(const _Tp*& __fp)
1692 template<typename _Class, typename _Tp>
1694 _M_not_empty_function(_Tp _Class::* const& __mp)
1697 template<typename _Tp>
1699 _M_not_empty_function(const _Tp&)
1704 _M_init_functor(_Any_data& __functor, _Functor&& __f, true_type)
1705 { new (__functor._M_access()) _Functor(std::move(__f)); }
1708 _M_init_functor(_Any_data& __functor, _Functor&& __f, false_type)
1709 { __functor._M_access<_Functor*>() = new _Functor(std::move(__f)); }
1712 template<typename _Functor>
1713 class _Ref_manager : public _Base_manager<_Functor*>
1715 typedef _Function_base::_Base_manager<_Functor*> _Base;
1719 _M_manager(_Any_data& __dest, const _Any_data& __source,
1720 _Manager_operation __op)
1725 case __get_type_info:
1726 __dest._M_access<const type_info*>() = &typeid(_Functor);
1729 case __get_functor_ptr:
1730 __dest._M_access<_Functor*>() = *_Base::_M_get_pointer(__source);
1731 return is_const<_Functor>::value;
1735 _Base::_M_manager(__dest, __source, __op);
1741 _M_init_functor(_Any_data& __functor, reference_wrapper<_Functor> __f)
1743 // TBD: Use address_of function instead.
1744 _Base::_M_init_functor(__functor, &__f.get());
1748 _Function_base() : _M_manager(0) { }
1753 _M_manager(_M_functor, _M_functor, __destroy_functor);
1757 bool _M_empty() const { return !_M_manager; }
1759 typedef bool (*_Manager_type)(_Any_data&, const _Any_data&,
1760 _Manager_operation);
1762 _Any_data _M_functor;
1763 _Manager_type _M_manager;
1766 template<typename _Signature, typename _Functor>
1767 class _Function_handler;
1769 template<typename _Res, typename _Functor, typename... _ArgTypes>
1770 class _Function_handler<_Res(_ArgTypes...), _Functor>
1771 : public _Function_base::_Base_manager<_Functor>
1773 typedef _Function_base::_Base_manager<_Functor> _Base;
1777 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1779 return (*_Base::_M_get_pointer(__functor))(
1780 std::forward<_ArgTypes>(__args)...);
1784 template<typename _Functor, typename... _ArgTypes>
1785 class _Function_handler<void(_ArgTypes...), _Functor>
1786 : public _Function_base::_Base_manager<_Functor>
1788 typedef _Function_base::_Base_manager<_Functor> _Base;
1792 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1794 (*_Base::_M_get_pointer(__functor))(
1795 std::forward<_ArgTypes>(__args)...);
1799 template<typename _Res, typename _Functor, typename... _ArgTypes>
1800 class _Function_handler<_Res(_ArgTypes...), reference_wrapper<_Functor> >
1801 : public _Function_base::_Ref_manager<_Functor>
1803 typedef _Function_base::_Ref_manager<_Functor> _Base;
1807 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1809 return __callable_functor(**_Base::_M_get_pointer(__functor))(
1810 std::forward<_ArgTypes>(__args)...);
1814 template<typename _Functor, typename... _ArgTypes>
1815 class _Function_handler<void(_ArgTypes...), reference_wrapper<_Functor> >
1816 : public _Function_base::_Ref_manager<_Functor>
1818 typedef _Function_base::_Ref_manager<_Functor> _Base;
1822 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1824 __callable_functor(**_Base::_M_get_pointer(__functor))(
1825 std::forward<_ArgTypes>(__args)...);
1829 template<typename _Class, typename _Member, typename _Res,
1830 typename... _ArgTypes>
1831 class _Function_handler<_Res(_ArgTypes...), _Member _Class::*>
1832 : public _Function_handler<void(_ArgTypes...), _Member _Class::*>
1834 typedef _Function_handler<void(_ArgTypes...), _Member _Class::*>
1839 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1841 return mem_fn(_Base::_M_get_pointer(__functor)->__value)(
1842 std::forward<_ArgTypes>(__args)...);
1846 template<typename _Class, typename _Member, typename... _ArgTypes>
1847 class _Function_handler<void(_ArgTypes...), _Member _Class::*>
1848 : public _Function_base::_Base_manager<
1849 _Simple_type_wrapper< _Member _Class::* > >
1851 typedef _Member _Class::* _Functor;
1852 typedef _Simple_type_wrapper<_Functor> _Wrapper;
1853 typedef _Function_base::_Base_manager<_Wrapper> _Base;
1857 _M_manager(_Any_data& __dest, const _Any_data& __source,
1858 _Manager_operation __op)
1863 case __get_type_info:
1864 __dest._M_access<const type_info*>() = &typeid(_Functor);
1867 case __get_functor_ptr:
1868 __dest._M_access<_Functor*>() =
1869 &_Base::_M_get_pointer(__source)->__value;
1873 _Base::_M_manager(__dest, __source, __op);
1879 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1881 mem_fn(_Base::_M_get_pointer(__functor)->__value)(
1882 std::forward<_ArgTypes>(__args)...);
1887 * @brief Primary class template for std::function.
1890 * Polymorphic function wrapper.
1892 template<typename _Res, typename... _ArgTypes>
1893 class function<_Res(_ArgTypes...)>
1894 : public _Maybe_unary_or_binary_function<_Res, _ArgTypes...>,
1895 private _Function_base
1897 typedef _Res _Signature_type(_ArgTypes...);
1899 struct _Useless { };
1902 typedef _Res result_type;
1904 // [3.7.2.1] construct/copy/destroy
1907 * @brief Default construct creates an empty function call wrapper.
1908 * @post @c !(bool)*this
1910 function() : _Function_base() { }
1913 * @brief Creates an empty function call wrapper.
1914 * @post @c !(bool)*this
1916 function(nullptr_t) : _Function_base() { }
1919 * @brief %Function copy constructor.
1920 * @param x A %function object with identical call signature.
1921 * @post @c (bool)*this == (bool)x
1923 * The newly-created %function contains a copy of the target of @a
1924 * x (if it has one).
1926 function(const function& __x);
1929 * @brief %Function move constructor.
1930 * @param x A %function object rvalue with identical call signature.
1932 * The newly-created %function contains the target of @a x
1935 function(function&& __x) : _Function_base()
1940 // TODO: needs allocator_arg_t
1943 * @brief Builds a %function that targets a copy of the incoming
1945 * @param f A %function object that is callable with parameters of
1946 * type @c T1, @c T2, ..., @c TN and returns a value convertible
1949 * The newly-created %function object will target a copy of @a
1950 * f. If @a f is @c reference_wrapper<F>, then this function
1951 * object will contain a reference to the function object @c
1952 * f.get(). If @a f is a NULL function pointer or NULL
1953 * pointer-to-member, the newly-created object will be empty.
1955 * If @a f is a non-NULL function pointer or an object of type @c
1956 * reference_wrapper<F>, this function will not throw.
1958 template<typename _Functor>
1959 function(_Functor __f,
1961 !is_integral<_Functor>::value, _Useless>::type
1965 * @brief %Function assignment operator.
1966 * @param x A %function with identical call signature.
1967 * @post @c (bool)*this == (bool)x
1970 * The target of @a x is copied to @c *this. If @a x has no
1971 * target, then @c *this will be empty.
1973 * If @a x targets a function pointer or a reference to a function
1974 * object, then this operation will not throw an %exception.
1977 operator=(const function& __x)
1979 function(__x).swap(*this);
1984 * @brief %Function move-assignment operator.
1985 * @param x A %function rvalue with identical call signature.
1988 * The target of @a x is moved to @c *this. If @a x has no
1989 * target, then @c *this will be empty.
1991 * If @a x targets a function pointer or a reference to a function
1992 * object, then this operation will not throw an %exception.
1995 operator=(function&& __x)
1997 function(std::move(__x)).swap(*this);
2002 * @brief %Function assignment to zero.
2003 * @post @c !(bool)*this
2006 * The target of @c *this is deallocated, leaving it empty.
2009 operator=(nullptr_t)
2013 _M_manager(_M_functor, _M_functor, __destroy_functor);
2021 * @brief %Function assignment to a new target.
2022 * @param f A %function object that is callable with parameters of
2023 * type @c T1, @c T2, ..., @c TN and returns a value convertible
2027 * This %function object wrapper will target a copy of @a
2028 * f. If @a f is @c reference_wrapper<F>, then this function
2029 * object will contain a reference to the function object @c
2030 * f.get(). If @a f is a NULL function pointer or NULL
2031 * pointer-to-member, @c this object will be empty.
2033 * If @a f is a non-NULL function pointer or an object of type @c
2034 * reference_wrapper<F>, this function will not throw.
2036 template<typename _Functor>
2037 typename enable_if<!is_integral<_Functor>::value, function&>::type
2038 operator=(_Functor&& __f)
2040 function(std::forward<_Functor>(__f)).swap(*this);
2045 template<typename _Functor>
2046 typename enable_if<!is_integral<_Functor>::value, function&>::type
2047 operator=(reference_wrapper<_Functor> __f)
2049 function(__f).swap(*this);
2053 // [3.7.2.2] function modifiers
2056 * @brief Swap the targets of two %function objects.
2057 * @param f A %function with identical call signature.
2059 * Swap the targets of @c this function object and @a f. This
2060 * function will not throw an %exception.
2062 void swap(function& __x)
2064 std::swap(_M_functor, __x._M_functor);
2065 std::swap(_M_manager, __x._M_manager);
2066 std::swap(_M_invoker, __x._M_invoker);
2069 // TODO: needs allocator_arg_t
2071 template<typename _Functor, typename _Alloc>
2073 assign(_Functor&& __f, const _Alloc& __a)
2075 function(allocator_arg, __a,
2076 std::forward<_Functor>(__f)).swap(*this);
2080 // [3.7.2.3] function capacity
2083 * @brief Determine if the %function wrapper has a target.
2085 * @return @c true when this %function object contains a target,
2086 * or @c false when it is empty.
2088 * This function will not throw an %exception.
2090 explicit operator bool() const
2091 { return !_M_empty(); }
2093 // [3.7.2.4] function invocation
2096 * @brief Invokes the function targeted by @c *this.
2097 * @returns the result of the target.
2098 * @throws bad_function_call when @c !(bool)*this
2100 * The function call operator invokes the target function object
2101 * stored by @c this.
2103 _Res operator()(_ArgTypes... __args) const;
2106 // [3.7.2.5] function target access
2108 * @brief Determine the type of the target of this function object
2111 * @returns the type identifier of the target function object, or
2112 * @c typeid(void) if @c !(bool)*this.
2114 * This function will not throw an %exception.
2116 const type_info& target_type() const;
2119 * @brief Access the stored target function object.
2121 * @return Returns a pointer to the stored target function object,
2122 * if @c typeid(Functor).equals(target_type()); otherwise, a NULL
2125 * This function will not throw an %exception.
2127 template<typename _Functor> _Functor* target();
2130 template<typename _Functor> const _Functor* target() const;
2134 typedef _Res (*_Invoker_type)(const _Any_data&, _ArgTypes...);
2135 _Invoker_type _M_invoker;
2138 // Out-of-line member definitions.
2139 template<typename _Res, typename... _ArgTypes>
2140 function<_Res(_ArgTypes...)>::
2141 function(const function& __x)
2144 if (static_cast<bool>(__x))
2146 _M_invoker = __x._M_invoker;
2147 _M_manager = __x._M_manager;
2148 __x._M_manager(_M_functor, __x._M_functor, __clone_functor);
2152 template<typename _Res, typename... _ArgTypes>
2153 template<typename _Functor>
2154 function<_Res(_ArgTypes...)>::
2155 function(_Functor __f,
2157 !is_integral<_Functor>::value, _Useless>::type)
2160 typedef _Function_handler<_Signature_type, _Functor> _My_handler;
2162 if (_My_handler::_M_not_empty_function(__f))
2164 _M_invoker = &_My_handler::_M_invoke;
2165 _M_manager = &_My_handler::_M_manager;
2166 _My_handler::_M_init_functor(_M_functor, std::move(__f));
2170 template<typename _Res, typename... _ArgTypes>
2172 function<_Res(_ArgTypes...)>::
2173 operator()(_ArgTypes... __args) const
2176 __throw_bad_function_call();
2177 return _M_invoker(_M_functor, std::forward<_ArgTypes>(__args)...);
2181 template<typename _Res, typename... _ArgTypes>
2183 function<_Res(_ArgTypes...)>::
2188 _Any_data __typeinfo_result;
2189 _M_manager(__typeinfo_result, _M_functor, __get_type_info);
2190 return *__typeinfo_result._M_access<const type_info*>();
2193 return typeid(void);
2196 template<typename _Res, typename... _ArgTypes>
2197 template<typename _Functor>
2199 function<_Res(_ArgTypes...)>::
2202 if (typeid(_Functor) == target_type() && _M_manager)
2205 if (_M_manager(__ptr, _M_functor, __get_functor_ptr)
2206 && !is_const<_Functor>::value)
2209 return __ptr._M_access<_Functor*>();
2215 template<typename _Res, typename... _ArgTypes>
2216 template<typename _Functor>
2218 function<_Res(_ArgTypes...)>::
2221 if (typeid(_Functor) == target_type() && _M_manager)
2224 _M_manager(__ptr, _M_functor, __get_functor_ptr);
2225 return __ptr._M_access<const _Functor*>();
2232 // [20.7.15.2.6] null pointer comparisons
2235 * @brief Compares a polymorphic function object wrapper against 0
2236 * (the NULL pointer).
2237 * @returns @c true if the wrapper has no target, @c false otherwise
2239 * This function will not throw an %exception.
2241 template<typename _Res, typename... _Args>
2243 operator==(const function<_Res(_Args...)>& __f, nullptr_t)
2244 { return !static_cast<bool>(__f); }
2247 template<typename _Res, typename... _Args>
2249 operator==(nullptr_t, const function<_Res(_Args...)>& __f)
2250 { return !static_cast<bool>(__f); }
2253 * @brief Compares a polymorphic function object wrapper against 0
2254 * (the NULL pointer).
2255 * @returns @c false if the wrapper has no target, @c true otherwise
2257 * This function will not throw an %exception.
2259 template<typename _Res, typename... _Args>
2261 operator!=(const function<_Res(_Args...)>& __f, nullptr_t)
2262 { return static_cast<bool>(__f); }
2265 template<typename _Res, typename... _Args>
2267 operator!=(nullptr_t, const function<_Res(_Args...)>& __f)
2268 { return static_cast<bool>(__f); }
2270 // [20.7.15.2.7] specialized algorithms
2273 * @brief Swap the targets of two polymorphic function object wrappers.
2275 * This function will not throw an %exception.
2277 template<typename _Res, typename... _Args>
2279 swap(function<_Res(_Args...)>& __x, function<_Res(_Args...)>& __y)
2282 _GLIBCXX_END_NAMESPACE_VERSION
2285 #endif // __GXX_EXPERIMENTAL_CXX0X__
2287 #endif // _GLIBCXX_FUNCTIONAL