1 // <functional> -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 // Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 // <http://www.gnu.org/licenses/>.
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>
63 _GLIBCXX_HAS_NESTED_TYPE(result_type)
65 /// If we have found a result_type, extract it.
66 template<bool _Has_result_type, typename _Functor>
67 struct _Maybe_get_result_type
70 template<typename _Functor>
71 struct _Maybe_get_result_type<true, _Functor>
72 { typedef typename _Functor::result_type result_type; };
75 * Base class for any function object that has a weak result type, as
76 * defined in 3.3/3 of TR1.
78 template<typename _Functor>
79 struct _Weak_result_type_impl
80 : _Maybe_get_result_type<__has_result_type<_Functor>::value, _Functor>
83 /// Retrieve the result type for a function type.
84 template<typename _Res, typename... _ArgTypes>
85 struct _Weak_result_type_impl<_Res(_ArgTypes...)>
86 { typedef _Res result_type; };
88 template<typename _Res, typename... _ArgTypes>
89 struct _Weak_result_type_impl<_Res(_ArgTypes......)>
90 { typedef _Res result_type; };
92 template<typename _Res, typename... _ArgTypes>
93 struct _Weak_result_type_impl<_Res(_ArgTypes...) const>
94 { typedef _Res result_type; };
96 template<typename _Res, typename... _ArgTypes>
97 struct _Weak_result_type_impl<_Res(_ArgTypes......) const>
98 { typedef _Res result_type; };
100 template<typename _Res, typename... _ArgTypes>
101 struct _Weak_result_type_impl<_Res(_ArgTypes...) volatile>
102 { typedef _Res result_type; };
104 template<typename _Res, typename... _ArgTypes>
105 struct _Weak_result_type_impl<_Res(_ArgTypes......) volatile>
106 { typedef _Res result_type; };
108 template<typename _Res, typename... _ArgTypes>
109 struct _Weak_result_type_impl<_Res(_ArgTypes...) const volatile>
110 { typedef _Res result_type; };
112 template<typename _Res, typename... _ArgTypes>
113 struct _Weak_result_type_impl<_Res(_ArgTypes......) const volatile>
114 { typedef _Res result_type; };
116 /// Retrieve the result type for a function reference.
117 template<typename _Res, typename... _ArgTypes>
118 struct _Weak_result_type_impl<_Res(&)(_ArgTypes...)>
119 { typedef _Res result_type; };
121 template<typename _Res, typename... _ArgTypes>
122 struct _Weak_result_type_impl<_Res(&)(_ArgTypes......)>
123 { typedef _Res result_type; };
125 /// Retrieve the result type for a function pointer.
126 template<typename _Res, typename... _ArgTypes>
127 struct _Weak_result_type_impl<_Res(*)(_ArgTypes...)>
128 { typedef _Res result_type; };
130 template<typename _Res, typename... _ArgTypes>
131 struct _Weak_result_type_impl<_Res(*)(_ArgTypes......)>
132 { typedef _Res result_type; };
134 /// Retrieve result type for a member function pointer.
135 template<typename _Res, typename _Class, typename... _ArgTypes>
136 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...)>
137 { typedef _Res result_type; };
139 template<typename _Res, typename _Class, typename... _ArgTypes>
140 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes......)>
141 { typedef _Res result_type; };
143 /// Retrieve result type for a const member function pointer.
144 template<typename _Res, typename _Class, typename... _ArgTypes>
145 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) const>
146 { typedef _Res result_type; };
148 template<typename _Res, typename _Class, typename... _ArgTypes>
149 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes......) const>
150 { typedef _Res result_type; };
152 /// Retrieve result type for a volatile member function pointer.
153 template<typename _Res, typename _Class, typename... _ArgTypes>
154 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) volatile>
155 { typedef _Res result_type; };
157 template<typename _Res, typename _Class, typename... _ArgTypes>
158 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes......) volatile>
159 { typedef _Res result_type; };
161 /// Retrieve result type for a const volatile member function pointer.
162 template<typename _Res, typename _Class, typename... _ArgTypes>
163 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...)
165 { typedef _Res result_type; };
167 template<typename _Res, typename _Class, typename... _ArgTypes>
168 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes......)
170 { typedef _Res result_type; };
173 * Strip top-level cv-qualifiers from the function object and let
174 * _Weak_result_type_impl perform the real work.
176 template<typename _Functor>
177 struct _Weak_result_type
178 : _Weak_result_type_impl<typename remove_cv<_Functor>::type>
181 /// Determines if the type _Tp derives from unary_function.
182 template<typename _Tp>
183 struct _Derives_from_unary_function : __sfinae_types
186 template<typename _T1, typename _Res>
187 static __one __test(const volatile unary_function<_T1, _Res>*);
189 // It's tempting to change "..." to const volatile void*, but
190 // that fails when _Tp is a function type.
191 static __two __test(...);
194 static const bool value = sizeof(__test((_Tp*)0)) == 1;
197 /// Determines if the type _Tp derives from binary_function.
198 template<typename _Tp>
199 struct _Derives_from_binary_function : __sfinae_types
202 template<typename _T1, typename _T2, typename _Res>
203 static __one __test(const volatile binary_function<_T1, _T2, _Res>*);
205 // It's tempting to change "..." to const volatile void*, but
206 // that fails when _Tp is a function type.
207 static __two __test(...);
210 static const bool value = sizeof(__test((_Tp*)0)) == 1;
213 /// Turns a function type into a function pointer type
214 template<typename _Tp, bool _IsFunctionType = is_function<_Tp>::value>
215 struct _Function_to_function_pointer
220 template<typename _Tp>
221 struct _Function_to_function_pointer<_Tp, true>
227 * Invoke a function object, which may be either a member pointer or a
228 * function object. The first parameter will tell which.
230 template<typename _Functor, typename... _Args>
233 (!is_member_pointer<_Functor>::value
234 && !is_function<_Functor>::value
235 && !is_function<typename remove_pointer<_Functor>::type>::value),
236 typename result_of<_Functor(_Args...)>::type
238 __invoke(_Functor& __f, _Args&&... __args)
240 return __f(std::forward<_Args>(__args)...);
243 // To pick up function references (that will become function pointers)
244 template<typename _Functor, typename... _Args>
247 (is_pointer<_Functor>::value
248 && is_function<typename remove_pointer<_Functor>::type>::value),
249 typename result_of<_Functor(_Args...)>::type
251 __invoke(_Functor __f, _Args&&... __args)
253 return __f(std::forward<_Args>(__args)...);
257 * Knowing which of unary_function and binary_function _Tp derives
258 * from, derives from the same and ensures that reference_wrapper
259 * will have a weak result type. See cases below.
261 template<bool _Unary, bool _Binary, typename _Tp>
262 struct _Reference_wrapper_base_impl;
264 // Not a unary_function or binary_function, so try a weak result type.
265 template<typename _Tp>
266 struct _Reference_wrapper_base_impl<false, false, _Tp>
267 : _Weak_result_type<_Tp>
270 // unary_function but not binary_function
271 template<typename _Tp>
272 struct _Reference_wrapper_base_impl<true, false, _Tp>
273 : unary_function<typename _Tp::argument_type,
274 typename _Tp::result_type>
277 // binary_function but not unary_function
278 template<typename _Tp>
279 struct _Reference_wrapper_base_impl<false, true, _Tp>
280 : binary_function<typename _Tp::first_argument_type,
281 typename _Tp::second_argument_type,
282 typename _Tp::result_type>
285 // Both unary_function and binary_function. Import result_type to
287 template<typename _Tp>
288 struct _Reference_wrapper_base_impl<true, true, _Tp>
289 : unary_function<typename _Tp::argument_type,
290 typename _Tp::result_type>,
291 binary_function<typename _Tp::first_argument_type,
292 typename _Tp::second_argument_type,
293 typename _Tp::result_type>
295 typedef typename _Tp::result_type result_type;
299 * Derives from unary_function or binary_function when it
300 * can. Specializations handle all of the easy cases. The primary
301 * template determines what to do with a class type, which may
302 * derive from both unary_function and binary_function.
304 template<typename _Tp>
305 struct _Reference_wrapper_base
306 : _Reference_wrapper_base_impl<
307 _Derives_from_unary_function<_Tp>::value,
308 _Derives_from_binary_function<_Tp>::value,
312 // - a function type (unary)
313 template<typename _Res, typename _T1>
314 struct _Reference_wrapper_base<_Res(_T1)>
315 : unary_function<_T1, _Res>
318 template<typename _Res, typename _T1>
319 struct _Reference_wrapper_base<_Res(_T1) const>
320 : unary_function<_T1, _Res>
323 template<typename _Res, typename _T1>
324 struct _Reference_wrapper_base<_Res(_T1) volatile>
325 : unary_function<_T1, _Res>
328 template<typename _Res, typename _T1>
329 struct _Reference_wrapper_base<_Res(_T1) const volatile>
330 : unary_function<_T1, _Res>
333 // - a function type (binary)
334 template<typename _Res, typename _T1, typename _T2>
335 struct _Reference_wrapper_base<_Res(_T1, _T2)>
336 : binary_function<_T1, _T2, _Res>
339 template<typename _Res, typename _T1, typename _T2>
340 struct _Reference_wrapper_base<_Res(_T1, _T2) const>
341 : binary_function<_T1, _T2, _Res>
344 template<typename _Res, typename _T1, typename _T2>
345 struct _Reference_wrapper_base<_Res(_T1, _T2) volatile>
346 : binary_function<_T1, _T2, _Res>
349 template<typename _Res, typename _T1, typename _T2>
350 struct _Reference_wrapper_base<_Res(_T1, _T2) const volatile>
351 : binary_function<_T1, _T2, _Res>
354 // - a function pointer type (unary)
355 template<typename _Res, typename _T1>
356 struct _Reference_wrapper_base<_Res(*)(_T1)>
357 : unary_function<_T1, _Res>
360 // - a function pointer type (binary)
361 template<typename _Res, typename _T1, typename _T2>
362 struct _Reference_wrapper_base<_Res(*)(_T1, _T2)>
363 : binary_function<_T1, _T2, _Res>
366 // - a pointer to member function type (unary, no qualifiers)
367 template<typename _Res, typename _T1>
368 struct _Reference_wrapper_base<_Res (_T1::*)()>
369 : unary_function<_T1*, _Res>
372 // - a pointer to member function type (binary, no qualifiers)
373 template<typename _Res, typename _T1, typename _T2>
374 struct _Reference_wrapper_base<_Res (_T1::*)(_T2)>
375 : binary_function<_T1*, _T2, _Res>
378 // - a pointer to member function type (unary, const)
379 template<typename _Res, typename _T1>
380 struct _Reference_wrapper_base<_Res (_T1::*)() const>
381 : unary_function<const _T1*, _Res>
384 // - a pointer to member function type (binary, const)
385 template<typename _Res, typename _T1, typename _T2>
386 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const>
387 : binary_function<const _T1*, _T2, _Res>
390 // - a pointer to member function type (unary, volatile)
391 template<typename _Res, typename _T1>
392 struct _Reference_wrapper_base<_Res (_T1::*)() volatile>
393 : unary_function<volatile _T1*, _Res>
396 // - a pointer to member function type (binary, volatile)
397 template<typename _Res, typename _T1, typename _T2>
398 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) volatile>
399 : binary_function<volatile _T1*, _T2, _Res>
402 // - a pointer to member function type (unary, const volatile)
403 template<typename _Res, typename _T1>
404 struct _Reference_wrapper_base<_Res (_T1::*)() const volatile>
405 : unary_function<const volatile _T1*, _Res>
408 // - a pointer to member function type (binary, const volatile)
409 template<typename _Res, typename _T1, typename _T2>
410 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const volatile>
411 : binary_function<const volatile _T1*, _T2, _Res>
415 * @brief Primary class template for reference_wrapper.
419 template<typename _Tp>
420 class reference_wrapper
421 : public _Reference_wrapper_base<typename remove_cv<_Tp>::type>
423 // If _Tp is a function type, we can't form result_of<_Tp(...)>,
424 // so turn it into a function pointer type.
425 typedef typename _Function_to_function_pointer<_Tp>::type
432 reference_wrapper(_Tp& __indata)
433 : _M_data(std::__addressof(__indata))
436 reference_wrapper(_Tp&&) = delete;
438 reference_wrapper(const reference_wrapper<_Tp>& __inref):
439 _M_data(__inref._M_data)
443 operator=(const reference_wrapper<_Tp>& __inref)
445 _M_data = __inref._M_data;
449 operator _Tp&() const
450 { return this->get(); }
456 template<typename... _Args>
457 typename result_of<_M_func_type(_Args...)>::type
458 operator()(_Args&&... __args) const
460 return __invoke(get(), std::forward<_Args>(__args)...);
465 /// Denotes a reference should be taken to a variable.
466 template<typename _Tp>
467 inline reference_wrapper<_Tp>
469 { return reference_wrapper<_Tp>(__t); }
471 /// Denotes a const reference should be taken to a variable.
472 template<typename _Tp>
473 inline reference_wrapper<const _Tp>
475 { return reference_wrapper<const _Tp>(__t); }
477 /// Partial specialization.
478 template<typename _Tp>
479 inline reference_wrapper<_Tp>
480 ref(reference_wrapper<_Tp> __t)
481 { return ref(__t.get()); }
483 /// Partial specialization.
484 template<typename _Tp>
485 inline reference_wrapper<const _Tp>
486 cref(reference_wrapper<_Tp> __t)
487 { return cref(__t.get()); }
491 template<typename _MemberPointer>
495 * Derives from @c unary_function or @c binary_function, or perhaps
496 * nothing, depending on the number of arguments provided. The
497 * primary template is the basis case, which derives nothing.
499 template<typename _Res, typename... _ArgTypes>
500 struct _Maybe_unary_or_binary_function { };
502 /// Derives from @c unary_function, as appropriate.
503 template<typename _Res, typename _T1>
504 struct _Maybe_unary_or_binary_function<_Res, _T1>
505 : std::unary_function<_T1, _Res> { };
507 /// Derives from @c binary_function, as appropriate.
508 template<typename _Res, typename _T1, typename _T2>
509 struct _Maybe_unary_or_binary_function<_Res, _T1, _T2>
510 : std::binary_function<_T1, _T2, _Res> { };
512 /// Implementation of @c mem_fn for member function pointers.
513 template<typename _Res, typename _Class, typename... _ArgTypes>
514 class _Mem_fn<_Res (_Class::*)(_ArgTypes...)>
515 : public _Maybe_unary_or_binary_function<_Res, _Class*, _ArgTypes...>
517 typedef _Res (_Class::*_Functor)(_ArgTypes...);
519 template<typename _Tp>
521 _M_call(_Tp& __object, const volatile _Class *,
522 _ArgTypes... __args) const
523 { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); }
525 template<typename _Tp>
527 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
528 { return ((*__ptr).*__pmf)(std::forward<_ArgTypes>(__args)...); }
531 typedef _Res result_type;
533 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
537 operator()(_Class& __object, _ArgTypes... __args) const
538 { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); }
542 operator()(_Class* __object, _ArgTypes... __args) const
543 { return (__object->*__pmf)(std::forward<_ArgTypes>(__args)...); }
545 // Handle smart pointers, references and pointers to derived
546 template<typename _Tp>
548 operator()(_Tp& __object, _ArgTypes... __args) const
550 return _M_call(__object, &__object,
551 std::forward<_ArgTypes>(__args)...);
558 /// Implementation of @c mem_fn for const member function pointers.
559 template<typename _Res, typename _Class, typename... _ArgTypes>
560 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) const>
561 : public _Maybe_unary_or_binary_function<_Res, const _Class*,
564 typedef _Res (_Class::*_Functor)(_ArgTypes...) const;
566 template<typename _Tp>
568 _M_call(_Tp& __object, const volatile _Class *,
569 _ArgTypes... __args) const
570 { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); }
572 template<typename _Tp>
574 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
575 { return ((*__ptr).*__pmf)(std::forward<_ArgTypes>(__args)...); }
578 typedef _Res result_type;
580 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
584 operator()(const _Class& __object, _ArgTypes... __args) const
585 { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); }
589 operator()(const _Class* __object, _ArgTypes... __args) const
590 { return (__object->*__pmf)(std::forward<_ArgTypes>(__args)...); }
592 // Handle smart pointers, references and pointers to derived
593 template<typename _Tp>
594 _Res operator()(_Tp& __object, _ArgTypes... __args) const
596 return _M_call(__object, &__object,
597 std::forward<_ArgTypes>(__args)...);
604 /// Implementation of @c mem_fn for volatile member function pointers.
605 template<typename _Res, typename _Class, typename... _ArgTypes>
606 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) volatile>
607 : public _Maybe_unary_or_binary_function<_Res, volatile _Class*,
610 typedef _Res (_Class::*_Functor)(_ArgTypes...) volatile;
612 template<typename _Tp>
614 _M_call(_Tp& __object, const volatile _Class *,
615 _ArgTypes... __args) const
616 { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); }
618 template<typename _Tp>
620 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
621 { return ((*__ptr).*__pmf)(std::forward<_ArgTypes>(__args)...); }
624 typedef _Res result_type;
626 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
630 operator()(volatile _Class& __object, _ArgTypes... __args) const
631 { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); }
635 operator()(volatile _Class* __object, _ArgTypes... __args) const
636 { return (__object->*__pmf)(std::forward<_ArgTypes>(__args)...); }
638 // Handle smart pointers, references and pointers to derived
639 template<typename _Tp>
641 operator()(_Tp& __object, _ArgTypes... __args) const
643 return _M_call(__object, &__object,
644 std::forward<_ArgTypes>(__args)...);
651 /// Implementation of @c mem_fn for const volatile member function pointers.
652 template<typename _Res, typename _Class, typename... _ArgTypes>
653 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) const volatile>
654 : public _Maybe_unary_or_binary_function<_Res, const volatile _Class*,
657 typedef _Res (_Class::*_Functor)(_ArgTypes...) const volatile;
659 template<typename _Tp>
661 _M_call(_Tp& __object, const volatile _Class *,
662 _ArgTypes... __args) const
663 { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); }
665 template<typename _Tp>
667 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
668 { return ((*__ptr).*__pmf)(std::forward<_ArgTypes>(__args)...); }
671 typedef _Res result_type;
673 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
677 operator()(const volatile _Class& __object, _ArgTypes... __args) const
678 { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); }
682 operator()(const volatile _Class* __object, _ArgTypes... __args) const
683 { return (__object->*__pmf)(std::forward<_ArgTypes>(__args)...); }
685 // Handle smart pointers, references and pointers to derived
686 template<typename _Tp>
687 _Res operator()(_Tp& __object, _ArgTypes... __args) const
689 return _M_call(__object, &__object,
690 std::forward<_ArgTypes>(__args)...);
698 template<typename _Tp, bool>
699 struct _Mem_fn_const_or_non
701 typedef const _Tp& type;
704 template<typename _Tp>
705 struct _Mem_fn_const_or_non<_Tp, false>
710 template<typename _Res, typename _Class>
711 class _Mem_fn<_Res _Class::*>
713 // This bit of genius is due to Peter Dimov, improved slightly by
715 template<typename _Tp>
717 _M_call(_Tp& __object, _Class *) const
718 { return __object.*__pm; }
720 template<typename _Tp, typename _Up>
722 _M_call(_Tp& __object, _Up * const *) const
723 { return (*__object).*__pm; }
725 template<typename _Tp, typename _Up>
727 _M_call(_Tp& __object, const _Up * const *) const
728 { return (*__object).*__pm; }
730 template<typename _Tp>
732 _M_call(_Tp& __object, const _Class *) const
733 { return __object.*__pm; }
735 template<typename _Tp>
737 _M_call(_Tp& __ptr, const volatile void*) const
738 { return (*__ptr).*__pm; }
740 template<typename _Tp> static _Tp& __get_ref();
742 template<typename _Tp>
743 static __sfinae_types::__one __check_const(_Tp&, _Class*);
744 template<typename _Tp, typename _Up>
745 static __sfinae_types::__one __check_const(_Tp&, _Up * const *);
746 template<typename _Tp, typename _Up>
747 static __sfinae_types::__two __check_const(_Tp&, const _Up * const *);
748 template<typename _Tp>
749 static __sfinae_types::__two __check_const(_Tp&, const _Class*);
750 template<typename _Tp>
751 static __sfinae_types::__two __check_const(_Tp&, const volatile void*);
754 template<typename _Tp>
756 : _Mem_fn_const_or_non<_Res,
757 (sizeof(__sfinae_types::__two)
758 == sizeof(__check_const<_Tp>(__get_ref<_Tp>(), (_Tp*)0)))>
761 template<typename _Signature>
764 template<typename _CVMem, typename _Tp>
765 struct result<_CVMem(_Tp)>
766 : public _Result_type<_Tp> { };
768 template<typename _CVMem, typename _Tp>
769 struct result<_CVMem(_Tp&)>
770 : public _Result_type<_Tp> { };
773 _Mem_fn(_Res _Class::*__pm) : __pm(__pm) { }
777 operator()(_Class& __object) const
778 { return __object.*__pm; }
781 operator()(const _Class& __object) const
782 { return __object.*__pm; }
786 operator()(_Class* __object) const
787 { return __object->*__pm; }
790 operator()(const _Class* __object) const
791 { return __object->*__pm; }
793 // Handle smart pointers and derived
794 template<typename _Tp>
795 typename _Result_type<_Tp>::type
796 operator()(_Tp& __unknown) const
797 { return _M_call(__unknown, &__unknown); }
804 * @brief Returns a function object that forwards to the member
808 template<typename _Tp, typename _Class>
809 inline _Mem_fn<_Tp _Class::*>
810 mem_fn(_Tp _Class::* __pm)
812 return _Mem_fn<_Tp _Class::*>(__pm);
816 * @brief Determines if the given type _Tp is a function object
817 * should be treated as a subexpression when evaluating calls to
818 * function objects returned by bind(). [TR1 3.6.1]
821 template<typename _Tp>
822 struct is_bind_expression
823 : public false_type { };
826 * @brief Determines if the given type _Tp is a placeholder in a
827 * bind() expression and, if so, which placeholder it is. [TR1 3.6.2]
830 template<typename _Tp>
831 struct is_placeholder
832 : public integral_constant<int, 0>
835 /// The type of placeholder objects defined by libstdc++.
836 template<int _Num> struct _Placeholder { };
838 /** @namespace std::placeholders
839 * @brief ISO C++ 0x entities sub namespace for functional.
842 * Define a large number of placeholders. There is no way to
843 * simplify this with variadic templates, because we're introducing
844 * unique names for each.
846 namespace placeholders
859 _Placeholder<10> _10;
860 _Placeholder<11> _11;
861 _Placeholder<12> _12;
862 _Placeholder<13> _13;
863 _Placeholder<14> _14;
864 _Placeholder<15> _15;
865 _Placeholder<16> _16;
866 _Placeholder<17> _17;
867 _Placeholder<18> _18;
868 _Placeholder<19> _19;
869 _Placeholder<20> _20;
870 _Placeholder<21> _21;
871 _Placeholder<22> _22;
872 _Placeholder<23> _23;
873 _Placeholder<24> _24;
874 _Placeholder<25> _25;
875 _Placeholder<26> _26;
876 _Placeholder<27> _27;
877 _Placeholder<28> _28;
878 _Placeholder<29> _29;
883 * Partial specialization of is_placeholder that provides the placeholder
884 * number for the placeholder objects defined by libstdc++.
888 struct is_placeholder<_Placeholder<_Num> >
889 : public integral_constant<int, _Num>
893 * Used by _Safe_tuple_element to indicate that there is no tuple
894 * element at this position.
896 struct _No_tuple_element;
899 * Implementation helper for _Safe_tuple_element. This primary
900 * template handles the case where it is safe to use @c
903 template<int __i, typename _Tuple, bool _IsSafe>
904 struct _Safe_tuple_element_impl
905 : tuple_element<__i, _Tuple> { };
908 * Implementation helper for _Safe_tuple_element. This partial
909 * specialization handles the case where it is not safe to use @c
910 * tuple_element. We just return @c _No_tuple_element.
912 template<int __i, typename _Tuple>
913 struct _Safe_tuple_element_impl<__i, _Tuple, false>
915 typedef _No_tuple_element type;
919 * Like tuple_element, but returns @c _No_tuple_element when
920 * tuple_element would return an error.
922 template<int __i, typename _Tuple>
923 struct _Safe_tuple_element
924 : _Safe_tuple_element_impl<__i, _Tuple,
925 (__i >= 0 && __i < tuple_size<_Tuple>::value)>
929 * Maps an argument to bind() into an actual argument to the bound
930 * function object [TR1 3.6.3/5]. Only the first parameter should
931 * be specified: the rest are used to determine among the various
932 * implementations. Note that, although this class is a function
933 * object, it isn't entirely normal because it takes only two
934 * parameters regardless of the number of parameters passed to the
935 * bind expression. The first parameter is the bound argument and
936 * the second parameter is a tuple containing references to the
937 * rest of the arguments.
939 template<typename _Arg,
940 bool _IsBindExp = is_bind_expression<_Arg>::value,
941 bool _IsPlaceholder = (is_placeholder<_Arg>::value > 0)>
945 * If the argument is reference_wrapper<_Tp>, returns the
946 * underlying reference. [TR1 3.6.3/5 bullet 1]
948 template<typename _Tp>
949 class _Mu<reference_wrapper<_Tp>, false, false>
952 typedef _Tp& result_type;
954 /* Note: This won't actually work for const volatile
955 * reference_wrappers, because reference_wrapper::get() is const
956 * but not volatile-qualified. This might be a defect in the TR.
958 template<typename _CVRef, typename _Tuple>
960 operator()(_CVRef& __arg, _Tuple&) const volatile
961 { return __arg.get(); }
965 * If the argument is a bind expression, we invoke the underlying
966 * function object with the same cv-qualifiers as we are given and
967 * pass along all of our arguments (unwrapped). [TR1 3.6.3/5 bullet 2]
969 template<typename _Arg>
970 class _Mu<_Arg, true, false>
973 template<typename _CVArg, typename... _Args>
975 operator()(_CVArg& __arg,
976 tuple<_Args...>& __tuple) const volatile
977 -> decltype(__arg(declval<_Args>()...))
979 // Construct an index tuple and forward to __call
980 typedef typename _Build_index_tuple<sizeof...(_Args)>::__type
982 return this->__call(__arg, __tuple, _Indexes());
986 // Invokes the underlying function object __arg by unpacking all
987 // of the arguments in the tuple.
988 template<typename _CVArg, typename... _Args, int... _Indexes>
990 __call(_CVArg& __arg, tuple<_Args...>& __tuple,
991 const _Index_tuple<_Indexes...>&) const volatile
992 -> decltype(__arg(declval<_Args>()...))
994 return __arg(std::forward<_Args>(get<_Indexes>(__tuple))...);
999 * If the argument is a placeholder for the Nth argument, returns
1000 * a reference to the Nth argument to the bind function object.
1001 * [TR1 3.6.3/5 bullet 3]
1003 template<typename _Arg>
1004 class _Mu<_Arg, false, true>
1007 template<typename _Signature> class result;
1009 template<typename _CVMu, typename _CVArg, typename _Tuple>
1010 class result<_CVMu(_CVArg, _Tuple)>
1012 // Add a reference, if it hasn't already been done for us.
1013 // This allows us to be a little bit sloppy in constructing
1014 // the tuple that we pass to result_of<...>.
1015 typedef typename _Safe_tuple_element<(is_placeholder<_Arg>::value
1020 typedef typename add_rvalue_reference<__base_type>::type type;
1023 template<typename _Tuple>
1024 typename result<_Mu(_Arg, _Tuple)>::type
1025 operator()(const volatile _Arg&, _Tuple& __tuple) const volatile
1027 return std::forward<typename result<_Mu(_Arg, _Tuple)>::type>(
1028 ::std::get<(is_placeholder<_Arg>::value - 1)>(__tuple));
1033 * If the argument is just a value, returns a reference to that
1034 * value. The cv-qualifiers on the reference are the same as the
1035 * cv-qualifiers on the _Mu object. [TR1 3.6.3/5 bullet 4]
1037 template<typename _Arg>
1038 class _Mu<_Arg, false, false>
1041 template<typename _Signature> struct result;
1043 template<typename _CVMu, typename _CVArg, typename _Tuple>
1044 struct result<_CVMu(_CVArg, _Tuple)>
1046 typedef typename add_lvalue_reference<_CVArg>::type type;
1049 // Pick up the cv-qualifiers of the argument
1050 template<typename _CVArg, typename _Tuple>
1052 operator()(_CVArg&& __arg, _Tuple&) const volatile
1053 { return std::forward<_CVArg>(__arg); }
1057 * Maps member pointers into instances of _Mem_fn but leaves all
1058 * other function objects untouched. Used by tr1::bind(). The
1059 * primary template handles the non--member-pointer case.
1061 template<typename _Tp>
1062 struct _Maybe_wrap_member_pointer
1067 __do_wrap(const _Tp& __x)
1071 __do_wrap(_Tp&& __x)
1072 { return static_cast<_Tp&&>(__x); }
1076 * Maps member pointers into instances of _Mem_fn but leaves all
1077 * other function objects untouched. Used by tr1::bind(). This
1078 * partial specialization handles the member pointer case.
1080 template<typename _Tp, typename _Class>
1081 struct _Maybe_wrap_member_pointer<_Tp _Class::*>
1083 typedef _Mem_fn<_Tp _Class::*> type;
1086 __do_wrap(_Tp _Class::* __pm)
1087 { return type(__pm); }
1090 // Specialization needed to prevent "forming reference to void" errors when
1091 // bind<void>() is called, because argument deduction instantiates
1092 // _Maybe_wrap_member_pointer<void> outside the immediate context where
1095 struct _Maybe_wrap_member_pointer<void>
1100 // std::get<I> for volatile-qualified tuples
1101 template<size_t _Ind, typename... _Tp>
1103 __volget(volatile tuple<_Tp...>& __tuple)
1104 -> typename tuple_element<_Ind, tuple<_Tp...>>::type volatile&
1105 { return std::get<_Ind>(const_cast<tuple<_Tp...>&>(__tuple)); }
1107 // std::get<I> for const-volatile-qualified tuples
1108 template<size_t _Ind, typename... _Tp>
1110 __volget(const volatile tuple<_Tp...>& __tuple)
1111 -> typename tuple_element<_Ind, tuple<_Tp...>>::type const volatile&
1112 { return std::get<_Ind>(const_cast<const tuple<_Tp...>&>(__tuple)); }
1114 /// Type of the function object returned from bind().
1115 template<typename _Signature>
1118 template<typename _Functor, typename... _Bound_args>
1119 class _Bind<_Functor(_Bound_args...)>
1120 : public _Weak_result_type<_Functor>
1122 typedef _Bind __self_type;
1123 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
1127 tuple<_Bound_args...> _M_bound_args;
1130 template<typename _Result, typename... _Args, int... _Indexes>
1132 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>)
1134 return _M_f(_Mu<_Bound_args>()
1135 (get<_Indexes>(_M_bound_args), __args)...);
1139 template<typename _Result, typename... _Args, int... _Indexes>
1141 __call_c(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>) const
1143 return _M_f(_Mu<_Bound_args>()
1144 (get<_Indexes>(_M_bound_args), __args)...);
1148 template<typename _Result, typename... _Args, int... _Indexes>
1150 __call_v(tuple<_Args...>&& __args,
1151 _Index_tuple<_Indexes...>) volatile
1153 return _M_f(_Mu<_Bound_args>()
1154 (__volget<_Indexes>(_M_bound_args), __args)...);
1157 // Call as const volatile
1158 template<typename _Result, typename... _Args, int... _Indexes>
1160 __call_c_v(tuple<_Args...>&& __args,
1161 _Index_tuple<_Indexes...>) const volatile
1163 return _M_f(_Mu<_Bound_args>()
1164 (__volget<_Indexes>(_M_bound_args), __args)...);
1168 template<typename... _Args>
1169 explicit _Bind(const _Functor& __f, _Args&&... __args)
1170 : _M_f(__f), _M_bound_args(std::forward<_Args>(__args)...)
1173 template<typename... _Args>
1174 explicit _Bind(_Functor&& __f, _Args&&... __args)
1175 : _M_f(std::move(__f)), _M_bound_args(std::forward<_Args>(__args)...)
1178 _Bind(const _Bind&) = default;
1181 : _M_f(std::move(__b._M_f)), _M_bound_args(std::move(__b._M_bound_args))
1185 template<typename... _Args, typename _Result
1186 = decltype( std::declval<_Functor>()(
1187 _Mu<_Bound_args>()( std::declval<_Bound_args&>(),
1188 std::declval<tuple<_Args...>&>() )... ) )>
1190 operator()(_Args&&... __args)
1192 return this->__call<_Result>(
1193 std::forward_as_tuple(std::forward<_Args>(__args)...),
1198 template<typename... _Args, typename _Result
1199 = decltype( std::declval<const _Functor>()(
1200 _Mu<_Bound_args>()( std::declval<const _Bound_args&>(),
1201 std::declval<tuple<_Args...>&>() )... ) )>
1203 operator()(_Args&&... __args) const
1205 return this->__call_c<_Result>(
1206 std::forward_as_tuple(std::forward<_Args>(__args)...),
1211 template<typename... _Args, typename _Result
1212 = decltype( std::declval<volatile _Functor>()(
1213 _Mu<_Bound_args>()( std::declval<volatile _Bound_args&>(),
1214 std::declval<tuple<_Args...>&>() )... ) )>
1216 operator()(_Args&&... __args) volatile
1218 return this->__call_v<_Result>(
1219 std::forward_as_tuple(std::forward<_Args>(__args)...),
1223 // Call as const volatile
1224 template<typename... _Args, typename _Result
1225 = decltype( std::declval<const volatile _Functor>()(
1226 _Mu<_Bound_args>()( std::declval<const volatile _Bound_args&>(),
1227 std::declval<tuple<_Args...>&>() )... ) )>
1229 operator()(_Args&&... __args) const volatile
1231 return this->__call_c_v<_Result>(
1232 std::forward_as_tuple(std::forward<_Args>(__args)...),
1237 /// Type of the function object returned from bind<R>().
1238 template<typename _Result, typename _Signature>
1239 struct _Bind_result;
1241 template<typename _Result, typename _Functor, typename... _Bound_args>
1242 class _Bind_result<_Result, _Functor(_Bound_args...)>
1244 typedef _Bind_result __self_type;
1245 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
1249 tuple<_Bound_args...> _M_bound_args;
1252 template<typename _Res>
1253 struct __enable_if_void : enable_if<is_void<_Res>::value, int> { };
1254 template<typename _Res>
1255 struct __disable_if_void : enable_if<!is_void<_Res>::value, int> { };
1258 template<typename _Res, typename... _Args, int... _Indexes>
1260 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1261 typename __disable_if_void<_Res>::type = 0)
1263 return _M_f(_Mu<_Bound_args>()
1264 (get<_Indexes>(_M_bound_args), __args)...);
1267 // Call unqualified, return void
1268 template<typename _Res, typename... _Args, int... _Indexes>
1270 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1271 typename __enable_if_void<_Res>::type = 0)
1273 _M_f(_Mu<_Bound_args>()
1274 (get<_Indexes>(_M_bound_args), __args)...);
1278 template<typename _Res, typename... _Args, int... _Indexes>
1280 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1281 typename __disable_if_void<_Res>::type = 0) const
1283 return _M_f(_Mu<_Bound_args>()
1284 (get<_Indexes>(_M_bound_args), __args)...);
1287 // Call as const, return void
1288 template<typename _Res, typename... _Args, int... _Indexes>
1290 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1291 typename __enable_if_void<_Res>::type = 0) const
1293 _M_f(_Mu<_Bound_args>()
1294 (get<_Indexes>(_M_bound_args), __args)...);
1298 template<typename _Res, typename... _Args, int... _Indexes>
1300 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1301 typename __disable_if_void<_Res>::type = 0) volatile
1303 return _M_f(_Mu<_Bound_args>()
1304 (__volget<_Indexes>(_M_bound_args), __args)...);
1307 // Call as volatile, return void
1308 template<typename _Res, typename... _Args, int... _Indexes>
1310 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1311 typename __enable_if_void<_Res>::type = 0) volatile
1313 _M_f(_Mu<_Bound_args>()
1314 (__volget<_Indexes>(_M_bound_args), __args)...);
1317 // Call as const volatile
1318 template<typename _Res, typename... _Args, int... _Indexes>
1320 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1321 typename __disable_if_void<_Res>::type = 0) const volatile
1323 return _M_f(_Mu<_Bound_args>()
1324 (__volget<_Indexes>(_M_bound_args), __args)...);
1327 // Call as const volatile, return void
1328 template<typename _Res, typename... _Args, int... _Indexes>
1330 __call(tuple<_Args...>&& __args,
1331 _Index_tuple<_Indexes...>,
1332 typename __enable_if_void<_Res>::type = 0) const volatile
1334 _M_f(_Mu<_Bound_args>()
1335 (__volget<_Indexes>(_M_bound_args), __args)...);
1339 typedef _Result result_type;
1341 template<typename... _Args>
1342 explicit _Bind_result(const _Functor& __f, _Args&&... __args)
1343 : _M_f(__f), _M_bound_args(std::forward<_Args>(__args)...)
1346 template<typename... _Args>
1347 explicit _Bind_result(_Functor&& __f, _Args&&... __args)
1348 : _M_f(std::move(__f)), _M_bound_args(std::forward<_Args>(__args)...)
1351 _Bind_result(const _Bind_result&) = default;
1353 _Bind_result(_Bind_result&& __b)
1354 : _M_f(std::move(__b._M_f)), _M_bound_args(std::move(__b._M_bound_args))
1358 template<typename... _Args>
1360 operator()(_Args&&... __args)
1362 return this->__call<_Result>(
1363 std::forward_as_tuple(std::forward<_Args>(__args)...),
1368 template<typename... _Args>
1370 operator()(_Args&&... __args) const
1372 return this->__call<_Result>(
1373 std::forward_as_tuple(std::forward<_Args>(__args)...),
1378 template<typename... _Args>
1380 operator()(_Args&&... __args) volatile
1382 return this->__call<_Result>(
1383 std::forward_as_tuple(std::forward<_Args>(__args)...),
1387 // Call as const volatile
1388 template<typename... _Args>
1390 operator()(_Args&&... __args) const volatile
1392 return this->__call<_Result>(
1393 std::forward_as_tuple(std::forward<_Args>(__args)...),
1399 * @brief Class template _Bind is always a bind expression.
1402 template<typename _Signature>
1403 struct is_bind_expression<_Bind<_Signature> >
1404 : public true_type { };
1407 * @brief Class template _Bind is always a bind expression.
1410 template<typename _Result, typename _Signature>
1411 struct is_bind_expression<_Bind_result<_Result, _Signature> >
1412 : public true_type { };
1414 template<typename _Functor, typename... _ArgTypes>
1417 typedef _Maybe_wrap_member_pointer<typename decay<_Functor>::type>
1419 typedef typename __maybe_type::type __functor_type;
1420 typedef _Bind<__functor_type(typename decay<_ArgTypes>::type...)> type;
1424 * @brief Function template for std::bind.
1427 template<typename _Functor, typename... _ArgTypes>
1429 typename _Bind_helper<_Functor, _ArgTypes...>::type
1430 bind(_Functor&& __f, _ArgTypes&&... __args)
1432 typedef _Bind_helper<_Functor, _ArgTypes...> __helper_type;
1433 typedef typename __helper_type::__maybe_type __maybe_type;
1434 typedef typename __helper_type::type __result_type;
1435 return __result_type(__maybe_type::__do_wrap(std::forward<_Functor>(__f)),
1436 std::forward<_ArgTypes>(__args)...);
1439 template<typename _Result, typename _Functor, typename... _ArgTypes>
1440 struct _Bindres_helper
1442 typedef _Maybe_wrap_member_pointer<typename decay<_Functor>::type>
1444 typedef typename __maybe_type::type __functor_type;
1445 typedef _Bind_result<_Result,
1446 __functor_type(typename decay<_ArgTypes>::type...)>
1451 * @brief Function template for std::bind<R>.
1454 template<typename _Result, typename _Functor, typename... _ArgTypes>
1456 typename _Bindres_helper<_Result, _Functor, _ArgTypes...>::type
1457 bind(_Functor&& __f, _ArgTypes&&... __args)
1459 typedef _Bindres_helper<_Result, _Functor, _ArgTypes...> __helper_type;
1460 typedef typename __helper_type::__maybe_type __maybe_type;
1461 typedef typename __helper_type::type __result_type;
1462 return __result_type(__maybe_type::__do_wrap(std::forward<_Functor>(__f)),
1463 std::forward<_ArgTypes>(__args)...);
1467 * @brief Exception class thrown when class template function's
1468 * operator() is called with an empty target.
1469 * @ingroup exceptions
1471 class bad_function_call : public std::exception { };
1474 * Trait identifying "location-invariant" types, meaning that the
1475 * address of the object (or any of its members) will not escape.
1476 * Also implies a trivial copy constructor and assignment operator.
1478 template<typename _Tp>
1479 struct __is_location_invariant
1480 : integral_constant<bool, (is_pointer<_Tp>::value
1481 || is_member_pointer<_Tp>::value)>
1484 class _Undefined_class;
1489 const void* _M_const_object;
1490 void (*_M_function_pointer)();
1491 void (_Undefined_class::*_M_member_pointer)();
1496 void* _M_access() { return &_M_pod_data[0]; }
1497 const void* _M_access() const { return &_M_pod_data[0]; }
1499 template<typename _Tp>
1502 { return *static_cast<_Tp*>(_M_access()); }
1504 template<typename _Tp>
1507 { return *static_cast<const _Tp*>(_M_access()); }
1509 _Nocopy_types _M_unused;
1510 char _M_pod_data[sizeof(_Nocopy_types)];
1513 enum _Manager_operation
1521 // Simple type wrapper that helps avoid annoying const problems
1522 // when casting between void pointers and pointers-to-pointers.
1523 template<typename _Tp>
1524 struct _Simple_type_wrapper
1526 _Simple_type_wrapper(_Tp __value) : __value(__value) { }
1531 template<typename _Tp>
1532 struct __is_location_invariant<_Simple_type_wrapper<_Tp> >
1533 : __is_location_invariant<_Tp>
1536 // Converts a reference to a function object into a callable
1538 template<typename _Functor>
1540 __callable_functor(_Functor& __f)
1543 template<typename _Member, typename _Class>
1544 inline _Mem_fn<_Member _Class::*>
1545 __callable_functor(_Member _Class::* &__p)
1546 { return mem_fn(__p); }
1548 template<typename _Member, typename _Class>
1549 inline _Mem_fn<_Member _Class::*>
1550 __callable_functor(_Member _Class::* const &__p)
1551 { return mem_fn(__p); }
1553 template<typename _Signature>
1556 /// Base class of all polymorphic function object wrappers.
1557 class _Function_base
1560 static const std::size_t _M_max_size = sizeof(_Nocopy_types);
1561 static const std::size_t _M_max_align = __alignof__(_Nocopy_types);
1563 template<typename _Functor>
1567 static const bool __stored_locally =
1568 (__is_location_invariant<_Functor>::value
1569 && sizeof(_Functor) <= _M_max_size
1570 && __alignof__(_Functor) <= _M_max_align
1571 && (_M_max_align % __alignof__(_Functor) == 0));
1573 typedef integral_constant<bool, __stored_locally> _Local_storage;
1575 // Retrieve a pointer to the function object
1577 _M_get_pointer(const _Any_data& __source)
1579 const _Functor* __ptr =
1580 __stored_locally? &__source._M_access<_Functor>()
1581 /* have stored a pointer */ : __source._M_access<_Functor*>();
1582 return const_cast<_Functor*>(__ptr);
1585 // Clone a location-invariant function object that fits within
1586 // an _Any_data structure.
1588 _M_clone(_Any_data& __dest, const _Any_data& __source, true_type)
1590 new (__dest._M_access()) _Functor(__source._M_access<_Functor>());
1593 // Clone a function object that is not location-invariant or
1594 // that cannot fit into an _Any_data structure.
1596 _M_clone(_Any_data& __dest, const _Any_data& __source, false_type)
1598 __dest._M_access<_Functor*>() =
1599 new _Functor(*__source._M_access<_Functor*>());
1602 // Destroying a location-invariant object may still require
1605 _M_destroy(_Any_data& __victim, true_type)
1607 __victim._M_access<_Functor>().~_Functor();
1610 // Destroying an object located on the heap.
1612 _M_destroy(_Any_data& __victim, false_type)
1614 delete __victim._M_access<_Functor*>();
1619 _M_manager(_Any_data& __dest, const _Any_data& __source,
1620 _Manager_operation __op)
1625 case __get_type_info:
1626 __dest._M_access<const type_info*>() = &typeid(_Functor);
1629 case __get_functor_ptr:
1630 __dest._M_access<_Functor*>() = _M_get_pointer(__source);
1633 case __clone_functor:
1634 _M_clone(__dest, __source, _Local_storage());
1637 case __destroy_functor:
1638 _M_destroy(__dest, _Local_storage());
1645 _M_init_functor(_Any_data& __functor, _Functor&& __f)
1646 { _M_init_functor(__functor, std::move(__f), _Local_storage()); }
1648 template<typename _Signature>
1650 _M_not_empty_function(const function<_Signature>& __f)
1651 { return static_cast<bool>(__f); }
1653 template<typename _Tp>
1655 _M_not_empty_function(const _Tp*& __fp)
1658 template<typename _Class, typename _Tp>
1660 _M_not_empty_function(_Tp _Class::* const& __mp)
1663 template<typename _Tp>
1665 _M_not_empty_function(const _Tp&)
1670 _M_init_functor(_Any_data& __functor, _Functor&& __f, true_type)
1671 { new (__functor._M_access()) _Functor(std::move(__f)); }
1674 _M_init_functor(_Any_data& __functor, _Functor&& __f, false_type)
1675 { __functor._M_access<_Functor*>() = new _Functor(std::move(__f)); }
1678 template<typename _Functor>
1679 class _Ref_manager : public _Base_manager<_Functor*>
1681 typedef _Function_base::_Base_manager<_Functor*> _Base;
1685 _M_manager(_Any_data& __dest, const _Any_data& __source,
1686 _Manager_operation __op)
1691 case __get_type_info:
1692 __dest._M_access<const type_info*>() = &typeid(_Functor);
1695 case __get_functor_ptr:
1696 __dest._M_access<_Functor*>() = *_Base::_M_get_pointer(__source);
1697 return is_const<_Functor>::value;
1701 _Base::_M_manager(__dest, __source, __op);
1707 _M_init_functor(_Any_data& __functor, reference_wrapper<_Functor> __f)
1709 // TBD: Use address_of function instead.
1710 _Base::_M_init_functor(__functor, &__f.get());
1714 _Function_base() : _M_manager(0) { }
1719 _M_manager(_M_functor, _M_functor, __destroy_functor);
1723 bool _M_empty() const { return !_M_manager; }
1725 typedef bool (*_Manager_type)(_Any_data&, const _Any_data&,
1726 _Manager_operation);
1728 _Any_data _M_functor;
1729 _Manager_type _M_manager;
1732 template<typename _Signature, typename _Functor>
1733 class _Function_handler;
1735 template<typename _Res, typename _Functor, typename... _ArgTypes>
1736 class _Function_handler<_Res(_ArgTypes...), _Functor>
1737 : public _Function_base::_Base_manager<_Functor>
1739 typedef _Function_base::_Base_manager<_Functor> _Base;
1743 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1745 return (*_Base::_M_get_pointer(__functor))(
1746 std::forward<_ArgTypes>(__args)...);
1750 template<typename _Functor, typename... _ArgTypes>
1751 class _Function_handler<void(_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 (*_Base::_M_get_pointer(__functor))(
1761 std::forward<_ArgTypes>(__args)...);
1765 template<typename _Res, typename _Functor, typename... _ArgTypes>
1766 class _Function_handler<_Res(_ArgTypes...), reference_wrapper<_Functor> >
1767 : public _Function_base::_Ref_manager<_Functor>
1769 typedef _Function_base::_Ref_manager<_Functor> _Base;
1773 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1775 return __callable_functor(**_Base::_M_get_pointer(__functor))(
1776 std::forward<_ArgTypes>(__args)...);
1780 template<typename _Functor, typename... _ArgTypes>
1781 class _Function_handler<void(_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 __callable_functor(**_Base::_M_get_pointer(__functor))(
1791 std::forward<_ArgTypes>(__args)...);
1795 template<typename _Class, typename _Member, typename _Res,
1796 typename... _ArgTypes>
1797 class _Function_handler<_Res(_ArgTypes...), _Member _Class::*>
1798 : public _Function_handler<void(_ArgTypes...), _Member _Class::*>
1800 typedef _Function_handler<void(_ArgTypes...), _Member _Class::*>
1805 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1807 return mem_fn(_Base::_M_get_pointer(__functor)->__value)(
1808 std::forward<_ArgTypes>(__args)...);
1812 template<typename _Class, typename _Member, typename... _ArgTypes>
1813 class _Function_handler<void(_ArgTypes...), _Member _Class::*>
1814 : public _Function_base::_Base_manager<
1815 _Simple_type_wrapper< _Member _Class::* > >
1817 typedef _Member _Class::* _Functor;
1818 typedef _Simple_type_wrapper<_Functor> _Wrapper;
1819 typedef _Function_base::_Base_manager<_Wrapper> _Base;
1823 _M_manager(_Any_data& __dest, const _Any_data& __source,
1824 _Manager_operation __op)
1829 case __get_type_info:
1830 __dest._M_access<const type_info*>() = &typeid(_Functor);
1833 case __get_functor_ptr:
1834 __dest._M_access<_Functor*>() =
1835 &_Base::_M_get_pointer(__source)->__value;
1839 _Base::_M_manager(__dest, __source, __op);
1845 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1847 mem_fn(_Base::_M_get_pointer(__functor)->__value)(
1848 std::forward<_ArgTypes>(__args)...);
1853 * @brief Primary class template for std::function.
1856 * Polymorphic function wrapper.
1858 template<typename _Res, typename... _ArgTypes>
1859 class function<_Res(_ArgTypes...)>
1860 : public _Maybe_unary_or_binary_function<_Res, _ArgTypes...>,
1861 private _Function_base
1863 typedef _Res _Signature_type(_ArgTypes...);
1865 struct _Useless { };
1868 typedef _Res result_type;
1870 // [3.7.2.1] construct/copy/destroy
1873 * @brief Default construct creates an empty function call wrapper.
1874 * @post @c !(bool)*this
1876 function() : _Function_base() { }
1879 * @brief Creates an empty function call wrapper.
1880 * @post @c !(bool)*this
1882 function(nullptr_t) : _Function_base() { }
1885 * @brief %Function copy constructor.
1886 * @param x A %function object with identical call signature.
1887 * @post @c (bool)*this == (bool)x
1889 * The newly-created %function contains a copy of the target of @a
1890 * x (if it has one).
1892 function(const function& __x);
1895 * @brief %Function move constructor.
1896 * @param x A %function object rvalue with identical call signature.
1898 * The newly-created %function contains the target of @a x
1901 function(function&& __x) : _Function_base()
1906 // TODO: needs allocator_arg_t
1909 * @brief Builds a %function that targets a copy of the incoming
1911 * @param f A %function object that is callable with parameters of
1912 * type @c T1, @c T2, ..., @c TN and returns a value convertible
1915 * The newly-created %function object will target a copy of @a
1916 * f. If @a f is @c reference_wrapper<F>, then this function
1917 * object will contain a reference to the function object @c
1918 * f.get(). If @a f is a NULL function pointer or NULL
1919 * pointer-to-member, the newly-created object will be empty.
1921 * If @a f is a non-NULL function pointer or an object of type @c
1922 * reference_wrapper<F>, this function will not throw.
1924 template<typename _Functor>
1925 function(_Functor __f,
1927 !is_integral<_Functor>::value, _Useless>::type
1931 * @brief %Function assignment operator.
1932 * @param x A %function with identical call signature.
1933 * @post @c (bool)*this == (bool)x
1936 * The target of @a x is copied to @c *this. If @a x has no
1937 * target, then @c *this will be empty.
1939 * If @a x targets a function pointer or a reference to a function
1940 * object, then this operation will not throw an %exception.
1943 operator=(const function& __x)
1945 function(__x).swap(*this);
1950 * @brief %Function move-assignment operator.
1951 * @param x A %function rvalue with identical call signature.
1954 * The target of @a x is moved to @c *this. If @a x has no
1955 * target, then @c *this will be empty.
1957 * If @a x targets a function pointer or a reference to a function
1958 * object, then this operation will not throw an %exception.
1961 operator=(function&& __x)
1963 function(std::move(__x)).swap(*this);
1968 * @brief %Function assignment to zero.
1969 * @post @c !(bool)*this
1972 * The target of @c *this is deallocated, leaving it empty.
1975 operator=(nullptr_t)
1979 _M_manager(_M_functor, _M_functor, __destroy_functor);
1987 * @brief %Function assignment to a new target.
1988 * @param f A %function object that is callable with parameters of
1989 * type @c T1, @c T2, ..., @c TN and returns a value convertible
1993 * This %function object wrapper will target a copy of @a
1994 * f. If @a f is @c reference_wrapper<F>, then this function
1995 * object will contain a reference to the function object @c
1996 * f.get(). If @a f is a NULL function pointer or NULL
1997 * pointer-to-member, @c this object will be empty.
1999 * If @a f is a non-NULL function pointer or an object of type @c
2000 * reference_wrapper<F>, this function will not throw.
2002 template<typename _Functor>
2003 typename enable_if<!is_integral<_Functor>::value, function&>::type
2004 operator=(_Functor&& __f)
2006 function(std::forward<_Functor>(__f)).swap(*this);
2011 template<typename _Functor>
2012 typename enable_if<!is_integral<_Functor>::value, function&>::type
2013 operator=(reference_wrapper<_Functor> __f)
2015 function(__f).swap(*this);
2019 // [3.7.2.2] function modifiers
2022 * @brief Swap the targets of two %function objects.
2023 * @param f A %function with identical call signature.
2025 * Swap the targets of @c this function object and @a f. This
2026 * function will not throw an %exception.
2028 void swap(function& __x)
2030 std::swap(_M_functor, __x._M_functor);
2031 std::swap(_M_manager, __x._M_manager);
2032 std::swap(_M_invoker, __x._M_invoker);
2035 // TODO: needs allocator_arg_t
2037 template<typename _Functor, typename _Alloc>
2039 assign(_Functor&& __f, const _Alloc& __a)
2041 function(allocator_arg, __a,
2042 std::forward<_Functor>(__f)).swap(*this);
2046 // [3.7.2.3] function capacity
2049 * @brief Determine if the %function wrapper has a target.
2051 * @return @c true when this %function object contains a target,
2052 * or @c false when it is empty.
2054 * This function will not throw an %exception.
2056 explicit operator bool() const
2057 { return !_M_empty(); }
2059 // [3.7.2.4] function invocation
2062 * @brief Invokes the function targeted by @c *this.
2063 * @returns the result of the target.
2064 * @throws bad_function_call when @c !(bool)*this
2066 * The function call operator invokes the target function object
2067 * stored by @c this.
2069 _Res operator()(_ArgTypes... __args) const;
2072 // [3.7.2.5] function target access
2074 * @brief Determine the type of the target of this function object
2077 * @returns the type identifier of the target function object, or
2078 * @c typeid(void) if @c !(bool)*this.
2080 * This function will not throw an %exception.
2082 const type_info& target_type() const;
2085 * @brief Access the stored target function object.
2087 * @return Returns a pointer to the stored target function object,
2088 * if @c typeid(Functor).equals(target_type()); otherwise, a NULL
2091 * This function will not throw an %exception.
2093 template<typename _Functor> _Functor* target();
2096 template<typename _Functor> const _Functor* target() const;
2099 // deleted overloads
2100 template<typename _Res2, typename... _ArgTypes2>
2101 void operator==(const function<_Res2(_ArgTypes2...)>&) const = delete;
2102 template<typename _Res2, typename... _ArgTypes2>
2103 void operator!=(const function<_Res2(_ArgTypes2...)>&) const = delete;
2106 typedef _Res (*_Invoker_type)(const _Any_data&, _ArgTypes...);
2107 _Invoker_type _M_invoker;
2110 // Out-of-line member definitions.
2111 template<typename _Res, typename... _ArgTypes>
2112 function<_Res(_ArgTypes...)>::
2113 function(const function& __x)
2116 if (static_cast<bool>(__x))
2118 _M_invoker = __x._M_invoker;
2119 _M_manager = __x._M_manager;
2120 __x._M_manager(_M_functor, __x._M_functor, __clone_functor);
2124 template<typename _Res, typename... _ArgTypes>
2125 template<typename _Functor>
2126 function<_Res(_ArgTypes...)>::
2127 function(_Functor __f,
2129 !is_integral<_Functor>::value, _Useless>::type)
2132 typedef _Function_handler<_Signature_type, _Functor> _My_handler;
2134 if (_My_handler::_M_not_empty_function(__f))
2136 _M_invoker = &_My_handler::_M_invoke;
2137 _M_manager = &_My_handler::_M_manager;
2138 _My_handler::_M_init_functor(_M_functor, std::move(__f));
2142 template<typename _Res, typename... _ArgTypes>
2144 function<_Res(_ArgTypes...)>::
2145 operator()(_ArgTypes... __args) const
2148 __throw_bad_function_call();
2149 return _M_invoker(_M_functor, std::forward<_ArgTypes>(__args)...);
2153 template<typename _Res, typename... _ArgTypes>
2155 function<_Res(_ArgTypes...)>::
2160 _Any_data __typeinfo_result;
2161 _M_manager(__typeinfo_result, _M_functor, __get_type_info);
2162 return *__typeinfo_result._M_access<const type_info*>();
2165 return typeid(void);
2168 template<typename _Res, typename... _ArgTypes>
2169 template<typename _Functor>
2171 function<_Res(_ArgTypes...)>::
2174 if (typeid(_Functor) == target_type() && _M_manager)
2177 if (_M_manager(__ptr, _M_functor, __get_functor_ptr)
2178 && !is_const<_Functor>::value)
2181 return __ptr._M_access<_Functor*>();
2187 template<typename _Res, typename... _ArgTypes>
2188 template<typename _Functor>
2190 function<_Res(_ArgTypes...)>::
2193 if (typeid(_Functor) == target_type() && _M_manager)
2196 _M_manager(__ptr, _M_functor, __get_functor_ptr);
2197 return __ptr._M_access<const _Functor*>();
2204 // [20.7.15.2.6] null pointer comparisons
2207 * @brief Compares a polymorphic function object wrapper against 0
2208 * (the NULL pointer).
2209 * @returns @c true if the wrapper has no target, @c false otherwise
2211 * This function will not throw an %exception.
2213 template<typename _Res, typename... _Args>
2215 operator==(const function<_Res(_Args...)>& __f, nullptr_t)
2216 { return !static_cast<bool>(__f); }
2219 template<typename _Res, typename... _Args>
2221 operator==(nullptr_t, const function<_Res(_Args...)>& __f)
2222 { return !static_cast<bool>(__f); }
2225 * @brief Compares a polymorphic function object wrapper against 0
2226 * (the NULL pointer).
2227 * @returns @c false if the wrapper has no target, @c true otherwise
2229 * This function will not throw an %exception.
2231 template<typename _Res, typename... _Args>
2233 operator!=(const function<_Res(_Args...)>& __f, nullptr_t)
2234 { return static_cast<bool>(__f); }
2237 template<typename _Res, typename... _Args>
2239 operator!=(nullptr_t, const function<_Res(_Args...)>& __f)
2240 { return static_cast<bool>(__f); }
2242 // [20.7.15.2.7] specialized algorithms
2245 * @brief Swap the targets of two polymorphic function object wrappers.
2247 * This function will not throw an %exception.
2249 template<typename _Res, typename... _Args>
2251 swap(function<_Res(_Args...)>& __x, function<_Res(_Args...)>& __y)
2255 #endif // __GXX_EXPERIMENTAL_CXX0X__
2257 #endif // _GLIBCXX_FUNCTIONAL