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;
215 /// Turns a function type into a function pointer type
216 template<typename _Tp, bool _IsFunctionType = is_function<_Tp>::value>
217 struct _Function_to_function_pointer
222 template<typename _Tp>
223 struct _Function_to_function_pointer<_Tp, true>
229 * Invoke a function object, which may be either a member pointer or a
230 * function object. The first parameter will tell which.
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 __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 // Not a unary_function or binary_function, so try a weak result type.
267 template<typename _Tp>
268 struct _Reference_wrapper_base_impl<false, false, _Tp>
269 : _Weak_result_type<_Tp>
272 // unary_function but not binary_function
273 template<typename _Tp>
274 struct _Reference_wrapper_base_impl<true, false, _Tp>
275 : unary_function<typename _Tp::argument_type,
276 typename _Tp::result_type>
279 // binary_function but not unary_function
280 template<typename _Tp>
281 struct _Reference_wrapper_base_impl<false, true, _Tp>
282 : binary_function<typename _Tp::first_argument_type,
283 typename _Tp::second_argument_type,
284 typename _Tp::result_type>
287 // Both unary_function and binary_function. Import result_type to
289 template<typename _Tp>
290 struct _Reference_wrapper_base_impl<true, true, _Tp>
291 : unary_function<typename _Tp::argument_type,
292 typename _Tp::result_type>,
293 binary_function<typename _Tp::first_argument_type,
294 typename _Tp::second_argument_type,
295 typename _Tp::result_type>
297 typedef typename _Tp::result_type result_type;
301 * Derives from unary_function or binary_function when it
302 * can. Specializations handle all of the easy cases. The primary
303 * template determines what to do with a class type, which may
304 * derive from both unary_function and binary_function.
306 template<typename _Tp>
307 struct _Reference_wrapper_base
308 : _Reference_wrapper_base_impl<
309 _Derives_from_unary_function<_Tp>::value,
310 _Derives_from_binary_function<_Tp>::value,
314 // - a function type (unary)
315 template<typename _Res, typename _T1>
316 struct _Reference_wrapper_base<_Res(_T1)>
317 : unary_function<_T1, _Res>
320 template<typename _Res, typename _T1>
321 struct _Reference_wrapper_base<_Res(_T1) const>
322 : unary_function<_T1, _Res>
325 template<typename _Res, typename _T1>
326 struct _Reference_wrapper_base<_Res(_T1) volatile>
327 : unary_function<_T1, _Res>
330 template<typename _Res, typename _T1>
331 struct _Reference_wrapper_base<_Res(_T1) const volatile>
332 : unary_function<_T1, _Res>
335 // - a function type (binary)
336 template<typename _Res, typename _T1, typename _T2>
337 struct _Reference_wrapper_base<_Res(_T1, _T2)>
338 : binary_function<_T1, _T2, _Res>
341 template<typename _Res, typename _T1, typename _T2>
342 struct _Reference_wrapper_base<_Res(_T1, _T2) const>
343 : binary_function<_T1, _T2, _Res>
346 template<typename _Res, typename _T1, typename _T2>
347 struct _Reference_wrapper_base<_Res(_T1, _T2) volatile>
348 : binary_function<_T1, _T2, _Res>
351 template<typename _Res, typename _T1, typename _T2>
352 struct _Reference_wrapper_base<_Res(_T1, _T2) const volatile>
353 : binary_function<_T1, _T2, _Res>
356 // - a function pointer type (unary)
357 template<typename _Res, typename _T1>
358 struct _Reference_wrapper_base<_Res(*)(_T1)>
359 : unary_function<_T1, _Res>
362 // - a function pointer type (binary)
363 template<typename _Res, typename _T1, typename _T2>
364 struct _Reference_wrapper_base<_Res(*)(_T1, _T2)>
365 : binary_function<_T1, _T2, _Res>
368 // - a pointer to member function type (unary, no qualifiers)
369 template<typename _Res, typename _T1>
370 struct _Reference_wrapper_base<_Res (_T1::*)()>
371 : unary_function<_T1*, _Res>
374 // - a pointer to member function type (binary, no qualifiers)
375 template<typename _Res, typename _T1, typename _T2>
376 struct _Reference_wrapper_base<_Res (_T1::*)(_T2)>
377 : binary_function<_T1*, _T2, _Res>
380 // - a pointer to member function type (unary, const)
381 template<typename _Res, typename _T1>
382 struct _Reference_wrapper_base<_Res (_T1::*)() const>
383 : unary_function<const _T1*, _Res>
386 // - a pointer to member function type (binary, const)
387 template<typename _Res, typename _T1, typename _T2>
388 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const>
389 : binary_function<const _T1*, _T2, _Res>
392 // - a pointer to member function type (unary, volatile)
393 template<typename _Res, typename _T1>
394 struct _Reference_wrapper_base<_Res (_T1::*)() volatile>
395 : unary_function<volatile _T1*, _Res>
398 // - a pointer to member function type (binary, volatile)
399 template<typename _Res, typename _T1, typename _T2>
400 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) volatile>
401 : binary_function<volatile _T1*, _T2, _Res>
404 // - a pointer to member function type (unary, const volatile)
405 template<typename _Res, typename _T1>
406 struct _Reference_wrapper_base<_Res (_T1::*)() const volatile>
407 : unary_function<const volatile _T1*, _Res>
410 // - a pointer to member function type (binary, const volatile)
411 template<typename _Res, typename _T1, typename _T2>
412 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const volatile>
413 : binary_function<const volatile _T1*, _T2, _Res>
417 * @brief Primary class template for reference_wrapper.
421 template<typename _Tp>
422 class reference_wrapper
423 : public _Reference_wrapper_base<typename remove_cv<_Tp>::type>
425 // If _Tp is a function type, we can't form result_of<_Tp(...)>,
426 // so turn it into a function pointer type.
427 typedef typename _Function_to_function_pointer<_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<_M_func_type(_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 /// Partial specialization.
480 template<typename _Tp>
481 inline reference_wrapper<_Tp>
482 ref(reference_wrapper<_Tp> __t)
483 { return ref(__t.get()); }
485 /// Partial specialization.
486 template<typename _Tp>
487 inline reference_wrapper<const _Tp>
488 cref(reference_wrapper<_Tp> __t)
489 { return cref(__t.get()); }
493 template<typename _MemberPointer>
497 * Derives from @c unary_function or @c binary_function, or perhaps
498 * nothing, depending on the number of arguments provided. The
499 * primary template is the basis case, which derives nothing.
501 template<typename _Res, typename... _ArgTypes>
502 struct _Maybe_unary_or_binary_function { };
504 /// Derives from @c unary_function, as appropriate.
505 template<typename _Res, typename _T1>
506 struct _Maybe_unary_or_binary_function<_Res, _T1>
507 : std::unary_function<_T1, _Res> { };
509 /// Derives from @c binary_function, as appropriate.
510 template<typename _Res, typename _T1, typename _T2>
511 struct _Maybe_unary_or_binary_function<_Res, _T1, _T2>
512 : std::binary_function<_T1, _T2, _Res> { };
514 /// Implementation of @c mem_fn for member function pointers.
515 template<typename _Res, typename _Class, typename... _ArgTypes>
516 class _Mem_fn<_Res (_Class::*)(_ArgTypes...)>
517 : public _Maybe_unary_or_binary_function<_Res, _Class*, _ArgTypes...>
519 typedef _Res (_Class::*_Functor)(_ArgTypes...);
521 template<typename _Tp>
523 _M_call(_Tp& __object, const volatile _Class *,
524 _ArgTypes... __args) const
525 { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); }
527 template<typename _Tp>
529 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
530 { return ((*__ptr).*__pmf)(std::forward<_ArgTypes>(__args)...); }
533 typedef _Res result_type;
535 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
539 operator()(_Class& __object, _ArgTypes... __args) const
540 { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); }
544 operator()(_Class* __object, _ArgTypes... __args) const
545 { return (__object->*__pmf)(std::forward<_ArgTypes>(__args)...); }
547 // Handle smart pointers, references and pointers to derived
548 template<typename _Tp>
550 operator()(_Tp& __object, _ArgTypes... __args) const
552 return _M_call(__object, &__object,
553 std::forward<_ArgTypes>(__args)...);
560 /// Implementation of @c mem_fn for const member function pointers.
561 template<typename _Res, typename _Class, typename... _ArgTypes>
562 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) const>
563 : public _Maybe_unary_or_binary_function<_Res, const _Class*,
566 typedef _Res (_Class::*_Functor)(_ArgTypes...) const;
568 template<typename _Tp>
570 _M_call(_Tp& __object, const volatile _Class *,
571 _ArgTypes... __args) const
572 { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); }
574 template<typename _Tp>
576 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
577 { return ((*__ptr).*__pmf)(std::forward<_ArgTypes>(__args)...); }
580 typedef _Res result_type;
582 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
586 operator()(const _Class& __object, _ArgTypes... __args) const
587 { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); }
591 operator()(const _Class* __object, _ArgTypes... __args) const
592 { return (__object->*__pmf)(std::forward<_ArgTypes>(__args)...); }
594 // Handle smart pointers, references and pointers to derived
595 template<typename _Tp>
596 _Res operator()(_Tp& __object, _ArgTypes... __args) const
598 return _M_call(__object, &__object,
599 std::forward<_ArgTypes>(__args)...);
606 /// Implementation of @c mem_fn for volatile member function pointers.
607 template<typename _Res, typename _Class, typename... _ArgTypes>
608 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) volatile>
609 : public _Maybe_unary_or_binary_function<_Res, volatile _Class*,
612 typedef _Res (_Class::*_Functor)(_ArgTypes...) volatile;
614 template<typename _Tp>
616 _M_call(_Tp& __object, const volatile _Class *,
617 _ArgTypes... __args) const
618 { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); }
620 template<typename _Tp>
622 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
623 { return ((*__ptr).*__pmf)(std::forward<_ArgTypes>(__args)...); }
626 typedef _Res result_type;
628 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
632 operator()(volatile _Class& __object, _ArgTypes... __args) const
633 { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); }
637 operator()(volatile _Class* __object, _ArgTypes... __args) const
638 { return (__object->*__pmf)(std::forward<_ArgTypes>(__args)...); }
640 // Handle smart pointers, references and pointers to derived
641 template<typename _Tp>
643 operator()(_Tp& __object, _ArgTypes... __args) const
645 return _M_call(__object, &__object,
646 std::forward<_ArgTypes>(__args)...);
653 /// Implementation of @c mem_fn for const volatile member function pointers.
654 template<typename _Res, typename _Class, typename... _ArgTypes>
655 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) const volatile>
656 : public _Maybe_unary_or_binary_function<_Res, const volatile _Class*,
659 typedef _Res (_Class::*_Functor)(_ArgTypes...) const volatile;
661 template<typename _Tp>
663 _M_call(_Tp& __object, const volatile _Class *,
664 _ArgTypes... __args) const
665 { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); }
667 template<typename _Tp>
669 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
670 { return ((*__ptr).*__pmf)(std::forward<_ArgTypes>(__args)...); }
673 typedef _Res result_type;
675 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
679 operator()(const volatile _Class& __object, _ArgTypes... __args) const
680 { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); }
684 operator()(const volatile _Class* __object, _ArgTypes... __args) const
685 { return (__object->*__pmf)(std::forward<_ArgTypes>(__args)...); }
687 // Handle smart pointers, references and pointers to derived
688 template<typename _Tp>
689 _Res operator()(_Tp& __object, _ArgTypes... __args) const
691 return _M_call(__object, &__object,
692 std::forward<_ArgTypes>(__args)...);
700 template<typename _Tp, bool>
701 struct _Mem_fn_const_or_non
703 typedef const _Tp& type;
706 template<typename _Tp>
707 struct _Mem_fn_const_or_non<_Tp, false>
712 template<typename _Res, typename _Class>
713 class _Mem_fn<_Res _Class::*>
715 // This bit of genius is due to Peter Dimov, improved slightly by
717 template<typename _Tp>
719 _M_call(_Tp& __object, _Class *) const
720 { return __object.*__pm; }
722 template<typename _Tp, typename _Up>
724 _M_call(_Tp& __object, _Up * const *) const
725 { return (*__object).*__pm; }
727 template<typename _Tp, typename _Up>
729 _M_call(_Tp& __object, const _Up * const *) const
730 { return (*__object).*__pm; }
732 template<typename _Tp>
734 _M_call(_Tp& __object, const _Class *) const
735 { return __object.*__pm; }
737 template<typename _Tp>
739 _M_call(_Tp& __ptr, const volatile void*) const
740 { return (*__ptr).*__pm; }
742 template<typename _Tp> static _Tp& __get_ref();
744 template<typename _Tp>
745 static __sfinae_types::__one __check_const(_Tp&, _Class*);
746 template<typename _Tp, typename _Up>
747 static __sfinae_types::__one __check_const(_Tp&, _Up * const *);
748 template<typename _Tp, typename _Up>
749 static __sfinae_types::__two __check_const(_Tp&, const _Up * const *);
750 template<typename _Tp>
751 static __sfinae_types::__two __check_const(_Tp&, const _Class*);
752 template<typename _Tp>
753 static __sfinae_types::__two __check_const(_Tp&, const volatile void*);
756 template<typename _Tp>
758 : _Mem_fn_const_or_non<_Res,
759 (sizeof(__sfinae_types::__two)
760 == sizeof(__check_const<_Tp>(__get_ref<_Tp>(), (_Tp*)0)))>
763 template<typename _Signature>
766 template<typename _CVMem, typename _Tp>
767 struct result<_CVMem(_Tp)>
768 : public _Result_type<_Tp> { };
770 template<typename _CVMem, typename _Tp>
771 struct result<_CVMem(_Tp&)>
772 : public _Result_type<_Tp> { };
775 _Mem_fn(_Res _Class::*__pm) : __pm(__pm) { }
779 operator()(_Class& __object) const
780 { return __object.*__pm; }
783 operator()(const _Class& __object) const
784 { return __object.*__pm; }
788 operator()(_Class* __object) const
789 { return __object->*__pm; }
792 operator()(const _Class* __object) const
793 { return __object->*__pm; }
795 // Handle smart pointers and derived
796 template<typename _Tp>
797 typename _Result_type<_Tp>::type
798 operator()(_Tp& __unknown) const
799 { return _M_call(__unknown, &__unknown); }
806 * @brief Returns a function object that forwards to the member
810 template<typename _Tp, typename _Class>
811 inline _Mem_fn<_Tp _Class::*>
812 mem_fn(_Tp _Class::* __pm)
814 return _Mem_fn<_Tp _Class::*>(__pm);
818 * @brief Determines if the given type _Tp is a function object
819 * should be treated as a subexpression when evaluating calls to
820 * function objects returned by bind(). [TR1 3.6.1]
823 template<typename _Tp>
824 struct is_bind_expression
825 : public false_type { };
828 * @brief Determines if the given type _Tp is a placeholder in a
829 * bind() expression and, if so, which placeholder it is. [TR1 3.6.2]
832 template<typename _Tp>
833 struct is_placeholder
834 : public integral_constant<int, 0>
837 /// The type of placeholder objects defined by libstdc++.
838 template<int _Num> struct _Placeholder { };
840 _GLIBCXX_END_NAMESPACE_VERSION
842 /** @namespace std::placeholders
843 * @brief ISO C++ 0x entities sub namespace for functional.
846 * Define a large number of placeholders. There is no way to
847 * simplify this with variadic templates, because we're introducing
848 * unique names for each.
850 namespace placeholders
852 _GLIBCXX_BEGIN_NAMESPACE_VERSION
853 extern const _Placeholder<1> _1;
854 extern const _Placeholder<2> _2;
855 extern const _Placeholder<3> _3;
856 extern const _Placeholder<4> _4;
857 extern const _Placeholder<5> _5;
858 extern const _Placeholder<6> _6;
859 extern const _Placeholder<7> _7;
860 extern const _Placeholder<8> _8;
861 extern const _Placeholder<9> _9;
862 extern const _Placeholder<10> _10;
863 extern const _Placeholder<11> _11;
864 extern const _Placeholder<12> _12;
865 extern const _Placeholder<13> _13;
866 extern const _Placeholder<14> _14;
867 extern const _Placeholder<15> _15;
868 extern const _Placeholder<16> _16;
869 extern const _Placeholder<17> _17;
870 extern const _Placeholder<18> _18;
871 extern const _Placeholder<19> _19;
872 extern const _Placeholder<20> _20;
873 extern const _Placeholder<21> _21;
874 extern const _Placeholder<22> _22;
875 extern const _Placeholder<23> _23;
876 extern const _Placeholder<24> _24;
877 extern const _Placeholder<25> _25;
878 extern const _Placeholder<26> _26;
879 extern const _Placeholder<27> _27;
880 extern const _Placeholder<28> _28;
881 extern const _Placeholder<29> _29;
882 _GLIBCXX_END_NAMESPACE_VERSION
885 _GLIBCXX_BEGIN_NAMESPACE_VERSION
888 * Partial specialization of is_placeholder that provides the placeholder
889 * number for the placeholder objects defined by libstdc++.
893 struct is_placeholder<_Placeholder<_Num> >
894 : public integral_constant<int, _Num>
898 * Used by _Safe_tuple_element to indicate that there is no tuple
899 * element at this position.
901 struct _No_tuple_element;
904 * Implementation helper for _Safe_tuple_element. This primary
905 * template handles the case where it is safe to use @c
908 template<int __i, typename _Tuple, bool _IsSafe>
909 struct _Safe_tuple_element_impl
910 : tuple_element<__i, _Tuple> { };
913 * Implementation helper for _Safe_tuple_element. This partial
914 * specialization handles the case where it is not safe to use @c
915 * tuple_element. We just return @c _No_tuple_element.
917 template<int __i, typename _Tuple>
918 struct _Safe_tuple_element_impl<__i, _Tuple, false>
920 typedef _No_tuple_element type;
924 * Like tuple_element, but returns @c _No_tuple_element when
925 * tuple_element would return an error.
927 template<int __i, typename _Tuple>
928 struct _Safe_tuple_element
929 : _Safe_tuple_element_impl<__i, _Tuple,
930 (__i >= 0 && __i < tuple_size<_Tuple>::value)>
934 * Maps an argument to bind() into an actual argument to the bound
935 * function object [TR1 3.6.3/5]. Only the first parameter should
936 * be specified: the rest are used to determine among the various
937 * implementations. Note that, although this class is a function
938 * object, it isn't entirely normal because it takes only two
939 * parameters regardless of the number of parameters passed to the
940 * bind expression. The first parameter is the bound argument and
941 * the second parameter is a tuple containing references to the
942 * rest of the arguments.
944 template<typename _Arg,
945 bool _IsBindExp = is_bind_expression<_Arg>::value,
946 bool _IsPlaceholder = (is_placeholder<_Arg>::value > 0)>
950 * If the argument is reference_wrapper<_Tp>, returns the
951 * underlying reference. [TR1 3.6.3/5 bullet 1]
953 template<typename _Tp>
954 class _Mu<reference_wrapper<_Tp>, false, false>
957 typedef _Tp& result_type;
959 /* Note: This won't actually work for const volatile
960 * reference_wrappers, because reference_wrapper::get() is const
961 * but not volatile-qualified. This might be a defect in the TR.
963 template<typename _CVRef, typename _Tuple>
965 operator()(_CVRef& __arg, _Tuple&) const volatile
966 { return __arg.get(); }
970 * If the argument is a bind expression, we invoke the underlying
971 * function object with the same cv-qualifiers as we are given and
972 * pass along all of our arguments (unwrapped). [TR1 3.6.3/5 bullet 2]
974 template<typename _Arg>
975 class _Mu<_Arg, true, false>
978 template<typename _CVArg, typename... _Args>
980 operator()(_CVArg& __arg,
981 tuple<_Args...>& __tuple) const volatile
982 -> decltype(__arg(declval<_Args>()...))
984 // Construct an index tuple and forward to __call
985 typedef typename _Build_index_tuple<sizeof...(_Args)>::__type
987 return this->__call(__arg, __tuple, _Indexes());
991 // Invokes the underlying function object __arg by unpacking all
992 // of the arguments in the tuple.
993 template<typename _CVArg, typename... _Args, int... _Indexes>
995 __call(_CVArg& __arg, tuple<_Args...>& __tuple,
996 const _Index_tuple<_Indexes...>&) const volatile
997 -> decltype(__arg(declval<_Args>()...))
999 return __arg(std::forward<_Args>(get<_Indexes>(__tuple))...);
1004 * If the argument is a placeholder for the Nth argument, returns
1005 * a reference to the Nth argument to the bind function object.
1006 * [TR1 3.6.3/5 bullet 3]
1008 template<typename _Arg>
1009 class _Mu<_Arg, false, true>
1012 template<typename _Signature> class result;
1014 template<typename _CVMu, typename _CVArg, typename _Tuple>
1015 class result<_CVMu(_CVArg, _Tuple)>
1017 // Add a reference, if it hasn't already been done for us.
1018 // This allows us to be a little bit sloppy in constructing
1019 // the tuple that we pass to result_of<...>.
1020 typedef typename _Safe_tuple_element<(is_placeholder<_Arg>::value
1025 typedef typename add_rvalue_reference<__base_type>::type type;
1028 template<typename _Tuple>
1029 typename result<_Mu(_Arg, _Tuple)>::type
1030 operator()(const volatile _Arg&, _Tuple& __tuple) const volatile
1032 return std::forward<typename result<_Mu(_Arg, _Tuple)>::type>(
1033 ::std::get<(is_placeholder<_Arg>::value - 1)>(__tuple));
1038 * If the argument is just a value, returns a reference to that
1039 * value. The cv-qualifiers on the reference are the same as the
1040 * cv-qualifiers on the _Mu object. [TR1 3.6.3/5 bullet 4]
1042 template<typename _Arg>
1043 class _Mu<_Arg, false, false>
1046 template<typename _Signature> struct result;
1048 template<typename _CVMu, typename _CVArg, typename _Tuple>
1049 struct result<_CVMu(_CVArg, _Tuple)>
1051 typedef typename add_lvalue_reference<_CVArg>::type type;
1054 // Pick up the cv-qualifiers of the argument
1055 template<typename _CVArg, typename _Tuple>
1057 operator()(_CVArg&& __arg, _Tuple&) const volatile
1058 { return std::forward<_CVArg>(__arg); }
1062 * Maps member pointers into instances of _Mem_fn but leaves all
1063 * other function objects untouched. Used by tr1::bind(). The
1064 * primary template handles the non--member-pointer case.
1066 template<typename _Tp>
1067 struct _Maybe_wrap_member_pointer
1072 __do_wrap(const _Tp& __x)
1076 __do_wrap(_Tp&& __x)
1077 { return static_cast<_Tp&&>(__x); }
1081 * Maps member pointers into instances of _Mem_fn but leaves all
1082 * other function objects untouched. Used by tr1::bind(). This
1083 * partial specialization handles the member pointer case.
1085 template<typename _Tp, typename _Class>
1086 struct _Maybe_wrap_member_pointer<_Tp _Class::*>
1088 typedef _Mem_fn<_Tp _Class::*> type;
1091 __do_wrap(_Tp _Class::* __pm)
1092 { return type(__pm); }
1095 // Specialization needed to prevent "forming reference to void" errors when
1096 // bind<void>() is called, because argument deduction instantiates
1097 // _Maybe_wrap_member_pointer<void> outside the immediate context where
1100 struct _Maybe_wrap_member_pointer<void>
1105 // std::get<I> for volatile-qualified tuples
1106 template<size_t _Ind, typename... _Tp>
1108 __volget(volatile tuple<_Tp...>& __tuple)
1109 -> typename tuple_element<_Ind, tuple<_Tp...>>::type volatile&
1110 { return std::get<_Ind>(const_cast<tuple<_Tp...>&>(__tuple)); }
1112 // std::get<I> for const-volatile-qualified tuples
1113 template<size_t _Ind, typename... _Tp>
1115 __volget(const volatile tuple<_Tp...>& __tuple)
1116 -> typename tuple_element<_Ind, tuple<_Tp...>>::type const volatile&
1117 { return std::get<_Ind>(const_cast<const tuple<_Tp...>&>(__tuple)); }
1119 /// Type of the function object returned from bind().
1120 template<typename _Signature>
1123 template<typename _Functor, typename... _Bound_args>
1124 class _Bind<_Functor(_Bound_args...)>
1125 : public _Weak_result_type<_Functor>
1127 typedef _Bind __self_type;
1128 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
1132 tuple<_Bound_args...> _M_bound_args;
1135 template<typename _Result, typename... _Args, int... _Indexes>
1137 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>)
1139 return _M_f(_Mu<_Bound_args>()
1140 (get<_Indexes>(_M_bound_args), __args)...);
1144 template<typename _Result, typename... _Args, int... _Indexes>
1146 __call_c(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>) const
1148 return _M_f(_Mu<_Bound_args>()
1149 (get<_Indexes>(_M_bound_args), __args)...);
1153 template<typename _Result, typename... _Args, int... _Indexes>
1155 __call_v(tuple<_Args...>&& __args,
1156 _Index_tuple<_Indexes...>) volatile
1158 return _M_f(_Mu<_Bound_args>()
1159 (__volget<_Indexes>(_M_bound_args), __args)...);
1162 // Call as const volatile
1163 template<typename _Result, typename... _Args, int... _Indexes>
1165 __call_c_v(tuple<_Args...>&& __args,
1166 _Index_tuple<_Indexes...>) const volatile
1168 return _M_f(_Mu<_Bound_args>()
1169 (__volget<_Indexes>(_M_bound_args), __args)...);
1173 template<typename... _Args>
1174 explicit _Bind(const _Functor& __f, _Args&&... __args)
1175 : _M_f(__f), _M_bound_args(std::forward<_Args>(__args)...)
1178 template<typename... _Args>
1179 explicit _Bind(_Functor&& __f, _Args&&... __args)
1180 : _M_f(std::move(__f)), _M_bound_args(std::forward<_Args>(__args)...)
1183 _Bind(const _Bind&) = default;
1186 : _M_f(std::move(__b._M_f)), _M_bound_args(std::move(__b._M_bound_args))
1190 template<typename... _Args, typename _Result
1191 = decltype( std::declval<_Functor>()(
1192 _Mu<_Bound_args>()( std::declval<_Bound_args&>(),
1193 std::declval<tuple<_Args...>&>() )... ) )>
1195 operator()(_Args&&... __args)
1197 return this->__call<_Result>(
1198 std::forward_as_tuple(std::forward<_Args>(__args)...),
1203 template<typename... _Args, typename _Result
1204 = decltype( std::declval<const _Functor>()(
1205 _Mu<_Bound_args>()( std::declval<const _Bound_args&>(),
1206 std::declval<tuple<_Args...>&>() )... ) )>
1208 operator()(_Args&&... __args) const
1210 return this->__call_c<_Result>(
1211 std::forward_as_tuple(std::forward<_Args>(__args)...),
1216 template<typename... _Args, typename _Result
1217 = decltype( std::declval<volatile _Functor>()(
1218 _Mu<_Bound_args>()( std::declval<volatile _Bound_args&>(),
1219 std::declval<tuple<_Args...>&>() )... ) )>
1221 operator()(_Args&&... __args) volatile
1223 return this->__call_v<_Result>(
1224 std::forward_as_tuple(std::forward<_Args>(__args)...),
1228 // Call as const volatile
1229 template<typename... _Args, typename _Result
1230 = decltype( std::declval<const volatile _Functor>()(
1231 _Mu<_Bound_args>()( std::declval<const volatile _Bound_args&>(),
1232 std::declval<tuple<_Args...>&>() )... ) )>
1234 operator()(_Args&&... __args) const volatile
1236 return this->__call_c_v<_Result>(
1237 std::forward_as_tuple(std::forward<_Args>(__args)...),
1242 /// Type of the function object returned from bind<R>().
1243 template<typename _Result, typename _Signature>
1244 struct _Bind_result;
1246 template<typename _Result, typename _Functor, typename... _Bound_args>
1247 class _Bind_result<_Result, _Functor(_Bound_args...)>
1249 typedef _Bind_result __self_type;
1250 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
1254 tuple<_Bound_args...> _M_bound_args;
1257 template<typename _Res>
1258 struct __enable_if_void : enable_if<is_void<_Res>::value, int> { };
1259 template<typename _Res>
1260 struct __disable_if_void : enable_if<!is_void<_Res>::value, int> { };
1263 template<typename _Res, typename... _Args, int... _Indexes>
1265 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1266 typename __disable_if_void<_Res>::type = 0)
1268 return _M_f(_Mu<_Bound_args>()
1269 (get<_Indexes>(_M_bound_args), __args)...);
1272 // Call unqualified, return void
1273 template<typename _Res, typename... _Args, int... _Indexes>
1275 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1276 typename __enable_if_void<_Res>::type = 0)
1278 _M_f(_Mu<_Bound_args>()
1279 (get<_Indexes>(_M_bound_args), __args)...);
1283 template<typename _Res, typename... _Args, int... _Indexes>
1285 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1286 typename __disable_if_void<_Res>::type = 0) const
1288 return _M_f(_Mu<_Bound_args>()
1289 (get<_Indexes>(_M_bound_args), __args)...);
1292 // Call as const, return void
1293 template<typename _Res, typename... _Args, int... _Indexes>
1295 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1296 typename __enable_if_void<_Res>::type = 0) const
1298 _M_f(_Mu<_Bound_args>()
1299 (get<_Indexes>(_M_bound_args), __args)...);
1303 template<typename _Res, typename... _Args, int... _Indexes>
1305 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1306 typename __disable_if_void<_Res>::type = 0) volatile
1308 return _M_f(_Mu<_Bound_args>()
1309 (__volget<_Indexes>(_M_bound_args), __args)...);
1312 // Call as volatile, return void
1313 template<typename _Res, typename... _Args, int... _Indexes>
1315 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1316 typename __enable_if_void<_Res>::type = 0) volatile
1318 _M_f(_Mu<_Bound_args>()
1319 (__volget<_Indexes>(_M_bound_args), __args)...);
1322 // Call as const volatile
1323 template<typename _Res, typename... _Args, int... _Indexes>
1325 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1326 typename __disable_if_void<_Res>::type = 0) const volatile
1328 return _M_f(_Mu<_Bound_args>()
1329 (__volget<_Indexes>(_M_bound_args), __args)...);
1332 // Call as const volatile, return void
1333 template<typename _Res, typename... _Args, int... _Indexes>
1335 __call(tuple<_Args...>&& __args,
1336 _Index_tuple<_Indexes...>,
1337 typename __enable_if_void<_Res>::type = 0) const volatile
1339 _M_f(_Mu<_Bound_args>()
1340 (__volget<_Indexes>(_M_bound_args), __args)...);
1344 typedef _Result result_type;
1346 template<typename... _Args>
1347 explicit _Bind_result(const _Functor& __f, _Args&&... __args)
1348 : _M_f(__f), _M_bound_args(std::forward<_Args>(__args)...)
1351 template<typename... _Args>
1352 explicit _Bind_result(_Functor&& __f, _Args&&... __args)
1353 : _M_f(std::move(__f)), _M_bound_args(std::forward<_Args>(__args)...)
1356 _Bind_result(const _Bind_result&) = default;
1358 _Bind_result(_Bind_result&& __b)
1359 : _M_f(std::move(__b._M_f)), _M_bound_args(std::move(__b._M_bound_args))
1363 template<typename... _Args>
1365 operator()(_Args&&... __args)
1367 return this->__call<_Result>(
1368 std::forward_as_tuple(std::forward<_Args>(__args)...),
1373 template<typename... _Args>
1375 operator()(_Args&&... __args) const
1377 return this->__call<_Result>(
1378 std::forward_as_tuple(std::forward<_Args>(__args)...),
1383 template<typename... _Args>
1385 operator()(_Args&&... __args) volatile
1387 return this->__call<_Result>(
1388 std::forward_as_tuple(std::forward<_Args>(__args)...),
1392 // Call as const volatile
1393 template<typename... _Args>
1395 operator()(_Args&&... __args) const volatile
1397 return this->__call<_Result>(
1398 std::forward_as_tuple(std::forward<_Args>(__args)...),
1404 * @brief Class template _Bind is always a bind expression.
1407 template<typename _Signature>
1408 struct is_bind_expression<_Bind<_Signature> >
1409 : public true_type { };
1412 * @brief Class template _Bind is always a bind expression.
1415 template<typename _Result, typename _Signature>
1416 struct is_bind_expression<_Bind_result<_Result, _Signature> >
1417 : public true_type { };
1419 template<typename _Functor, typename... _ArgTypes>
1422 typedef _Maybe_wrap_member_pointer<typename decay<_Functor>::type>
1424 typedef typename __maybe_type::type __functor_type;
1425 typedef _Bind<__functor_type(typename decay<_ArgTypes>::type...)> type;
1429 * @brief Function template for std::bind.
1432 template<typename _Functor, typename... _ArgTypes>
1434 typename _Bind_helper<_Functor, _ArgTypes...>::type
1435 bind(_Functor&& __f, _ArgTypes&&... __args)
1437 typedef _Bind_helper<_Functor, _ArgTypes...> __helper_type;
1438 typedef typename __helper_type::__maybe_type __maybe_type;
1439 typedef typename __helper_type::type __result_type;
1440 return __result_type(__maybe_type::__do_wrap(std::forward<_Functor>(__f)),
1441 std::forward<_ArgTypes>(__args)...);
1444 template<typename _Result, typename _Functor, typename... _ArgTypes>
1445 struct _Bindres_helper
1447 typedef _Maybe_wrap_member_pointer<typename decay<_Functor>::type>
1449 typedef typename __maybe_type::type __functor_type;
1450 typedef _Bind_result<_Result,
1451 __functor_type(typename decay<_ArgTypes>::type...)>
1456 * @brief Function template for std::bind<R>.
1459 template<typename _Result, typename _Functor, typename... _ArgTypes>
1461 typename _Bindres_helper<_Result, _Functor, _ArgTypes...>::type
1462 bind(_Functor&& __f, _ArgTypes&&... __args)
1464 typedef _Bindres_helper<_Result, _Functor, _ArgTypes...> __helper_type;
1465 typedef typename __helper_type::__maybe_type __maybe_type;
1466 typedef typename __helper_type::type __result_type;
1467 return __result_type(__maybe_type::__do_wrap(std::forward<_Functor>(__f)),
1468 std::forward<_ArgTypes>(__args)...);
1472 * @brief Exception class thrown when class template function's
1473 * operator() is called with an empty target.
1474 * @ingroup exceptions
1476 class bad_function_call : public std::exception { };
1479 * Trait identifying "location-invariant" types, meaning that the
1480 * address of the object (or any of its members) will not escape.
1481 * Also implies a trivial copy constructor and assignment operator.
1483 template<typename _Tp>
1484 struct __is_location_invariant
1485 : integral_constant<bool, (is_pointer<_Tp>::value
1486 || is_member_pointer<_Tp>::value)>
1489 class _Undefined_class;
1494 const void* _M_const_object;
1495 void (*_M_function_pointer)();
1496 void (_Undefined_class::*_M_member_pointer)();
1501 void* _M_access() { return &_M_pod_data[0]; }
1502 const void* _M_access() const { return &_M_pod_data[0]; }
1504 template<typename _Tp>
1507 { return *static_cast<_Tp*>(_M_access()); }
1509 template<typename _Tp>
1512 { return *static_cast<const _Tp*>(_M_access()); }
1514 _Nocopy_types _M_unused;
1515 char _M_pod_data[sizeof(_Nocopy_types)];
1518 enum _Manager_operation
1526 // Simple type wrapper that helps avoid annoying const problems
1527 // when casting between void pointers and pointers-to-pointers.
1528 template<typename _Tp>
1529 struct _Simple_type_wrapper
1531 _Simple_type_wrapper(_Tp __value) : __value(__value) { }
1536 template<typename _Tp>
1537 struct __is_location_invariant<_Simple_type_wrapper<_Tp> >
1538 : __is_location_invariant<_Tp>
1541 // Converts a reference to a function object into a callable
1543 template<typename _Functor>
1545 __callable_functor(_Functor& __f)
1548 template<typename _Member, typename _Class>
1549 inline _Mem_fn<_Member _Class::*>
1550 __callable_functor(_Member _Class::* &__p)
1551 { return mem_fn(__p); }
1553 template<typename _Member, typename _Class>
1554 inline _Mem_fn<_Member _Class::*>
1555 __callable_functor(_Member _Class::* const &__p)
1556 { return mem_fn(__p); }
1558 template<typename _Signature>
1561 /// Base class of all polymorphic function object wrappers.
1562 class _Function_base
1565 static const std::size_t _M_max_size = sizeof(_Nocopy_types);
1566 static const std::size_t _M_max_align = __alignof__(_Nocopy_types);
1568 template<typename _Functor>
1572 static const bool __stored_locally =
1573 (__is_location_invariant<_Functor>::value
1574 && sizeof(_Functor) <= _M_max_size
1575 && __alignof__(_Functor) <= _M_max_align
1576 && (_M_max_align % __alignof__(_Functor) == 0));
1578 typedef integral_constant<bool, __stored_locally> _Local_storage;
1580 // Retrieve a pointer to the function object
1582 _M_get_pointer(const _Any_data& __source)
1584 const _Functor* __ptr =
1585 __stored_locally? &__source._M_access<_Functor>()
1586 /* have stored a pointer */ : __source._M_access<_Functor*>();
1587 return const_cast<_Functor*>(__ptr);
1590 // Clone a location-invariant function object that fits within
1591 // an _Any_data structure.
1593 _M_clone(_Any_data& __dest, const _Any_data& __source, true_type)
1595 new (__dest._M_access()) _Functor(__source._M_access<_Functor>());
1598 // Clone a function object that is not location-invariant or
1599 // that cannot fit into an _Any_data structure.
1601 _M_clone(_Any_data& __dest, const _Any_data& __source, false_type)
1603 __dest._M_access<_Functor*>() =
1604 new _Functor(*__source._M_access<_Functor*>());
1607 // Destroying a location-invariant object may still require
1610 _M_destroy(_Any_data& __victim, true_type)
1612 __victim._M_access<_Functor>().~_Functor();
1615 // Destroying an object located on the heap.
1617 _M_destroy(_Any_data& __victim, false_type)
1619 delete __victim._M_access<_Functor*>();
1624 _M_manager(_Any_data& __dest, const _Any_data& __source,
1625 _Manager_operation __op)
1630 case __get_type_info:
1631 __dest._M_access<const type_info*>() = &typeid(_Functor);
1634 case __get_functor_ptr:
1635 __dest._M_access<_Functor*>() = _M_get_pointer(__source);
1638 case __clone_functor:
1639 _M_clone(__dest, __source, _Local_storage());
1642 case __destroy_functor:
1643 _M_destroy(__dest, _Local_storage());
1650 _M_init_functor(_Any_data& __functor, _Functor&& __f)
1651 { _M_init_functor(__functor, std::move(__f), _Local_storage()); }
1653 template<typename _Signature>
1655 _M_not_empty_function(const function<_Signature>& __f)
1656 { return static_cast<bool>(__f); }
1658 template<typename _Tp>
1660 _M_not_empty_function(const _Tp*& __fp)
1663 template<typename _Class, typename _Tp>
1665 _M_not_empty_function(_Tp _Class::* const& __mp)
1668 template<typename _Tp>
1670 _M_not_empty_function(const _Tp&)
1675 _M_init_functor(_Any_data& __functor, _Functor&& __f, true_type)
1676 { new (__functor._M_access()) _Functor(std::move(__f)); }
1679 _M_init_functor(_Any_data& __functor, _Functor&& __f, false_type)
1680 { __functor._M_access<_Functor*>() = new _Functor(std::move(__f)); }
1683 template<typename _Functor>
1684 class _Ref_manager : public _Base_manager<_Functor*>
1686 typedef _Function_base::_Base_manager<_Functor*> _Base;
1690 _M_manager(_Any_data& __dest, const _Any_data& __source,
1691 _Manager_operation __op)
1696 case __get_type_info:
1697 __dest._M_access<const type_info*>() = &typeid(_Functor);
1700 case __get_functor_ptr:
1701 __dest._M_access<_Functor*>() = *_Base::_M_get_pointer(__source);
1702 return is_const<_Functor>::value;
1706 _Base::_M_manager(__dest, __source, __op);
1712 _M_init_functor(_Any_data& __functor, reference_wrapper<_Functor> __f)
1714 // TBD: Use address_of function instead.
1715 _Base::_M_init_functor(__functor, &__f.get());
1719 _Function_base() : _M_manager(0) { }
1724 _M_manager(_M_functor, _M_functor, __destroy_functor);
1728 bool _M_empty() const { return !_M_manager; }
1730 typedef bool (*_Manager_type)(_Any_data&, const _Any_data&,
1731 _Manager_operation);
1733 _Any_data _M_functor;
1734 _Manager_type _M_manager;
1737 template<typename _Signature, typename _Functor>
1738 class _Function_handler;
1740 template<typename _Res, typename _Functor, typename... _ArgTypes>
1741 class _Function_handler<_Res(_ArgTypes...), _Functor>
1742 : public _Function_base::_Base_manager<_Functor>
1744 typedef _Function_base::_Base_manager<_Functor> _Base;
1748 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1750 return (*_Base::_M_get_pointer(__functor))(
1751 std::forward<_ArgTypes>(__args)...);
1755 template<typename _Functor, typename... _ArgTypes>
1756 class _Function_handler<void(_ArgTypes...), _Functor>
1757 : public _Function_base::_Base_manager<_Functor>
1759 typedef _Function_base::_Base_manager<_Functor> _Base;
1763 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1765 (*_Base::_M_get_pointer(__functor))(
1766 std::forward<_ArgTypes>(__args)...);
1770 template<typename _Res, typename _Functor, typename... _ArgTypes>
1771 class _Function_handler<_Res(_ArgTypes...), reference_wrapper<_Functor> >
1772 : public _Function_base::_Ref_manager<_Functor>
1774 typedef _Function_base::_Ref_manager<_Functor> _Base;
1778 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1780 return __callable_functor(**_Base::_M_get_pointer(__functor))(
1781 std::forward<_ArgTypes>(__args)...);
1785 template<typename _Functor, typename... _ArgTypes>
1786 class _Function_handler<void(_ArgTypes...), reference_wrapper<_Functor> >
1787 : public _Function_base::_Ref_manager<_Functor>
1789 typedef _Function_base::_Ref_manager<_Functor> _Base;
1793 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1795 __callable_functor(**_Base::_M_get_pointer(__functor))(
1796 std::forward<_ArgTypes>(__args)...);
1800 template<typename _Class, typename _Member, typename _Res,
1801 typename... _ArgTypes>
1802 class _Function_handler<_Res(_ArgTypes...), _Member _Class::*>
1803 : public _Function_handler<void(_ArgTypes...), _Member _Class::*>
1805 typedef _Function_handler<void(_ArgTypes...), _Member _Class::*>
1810 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1812 return mem_fn(_Base::_M_get_pointer(__functor)->__value)(
1813 std::forward<_ArgTypes>(__args)...);
1817 template<typename _Class, typename _Member, typename... _ArgTypes>
1818 class _Function_handler<void(_ArgTypes...), _Member _Class::*>
1819 : public _Function_base::_Base_manager<
1820 _Simple_type_wrapper< _Member _Class::* > >
1822 typedef _Member _Class::* _Functor;
1823 typedef _Simple_type_wrapper<_Functor> _Wrapper;
1824 typedef _Function_base::_Base_manager<_Wrapper> _Base;
1828 _M_manager(_Any_data& __dest, const _Any_data& __source,
1829 _Manager_operation __op)
1834 case __get_type_info:
1835 __dest._M_access<const type_info*>() = &typeid(_Functor);
1838 case __get_functor_ptr:
1839 __dest._M_access<_Functor*>() =
1840 &_Base::_M_get_pointer(__source)->__value;
1844 _Base::_M_manager(__dest, __source, __op);
1850 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1852 mem_fn(_Base::_M_get_pointer(__functor)->__value)(
1853 std::forward<_ArgTypes>(__args)...);
1858 * @brief Primary class template for std::function.
1861 * Polymorphic function wrapper.
1863 template<typename _Res, typename... _ArgTypes>
1864 class function<_Res(_ArgTypes...)>
1865 : public _Maybe_unary_or_binary_function<_Res, _ArgTypes...>,
1866 private _Function_base
1868 typedef _Res _Signature_type(_ArgTypes...);
1870 struct _Useless { };
1873 typedef _Res result_type;
1875 // [3.7.2.1] construct/copy/destroy
1878 * @brief Default construct creates an empty function call wrapper.
1879 * @post @c !(bool)*this
1881 function() : _Function_base() { }
1884 * @brief Creates an empty function call wrapper.
1885 * @post @c !(bool)*this
1887 function(nullptr_t) : _Function_base() { }
1890 * @brief %Function copy constructor.
1891 * @param x A %function object with identical call signature.
1892 * @post @c (bool)*this == (bool)x
1894 * The newly-created %function contains a copy of the target of @a
1895 * x (if it has one).
1897 function(const function& __x);
1900 * @brief %Function move constructor.
1901 * @param x A %function object rvalue with identical call signature.
1903 * The newly-created %function contains the target of @a x
1906 function(function&& __x) : _Function_base()
1911 // TODO: needs allocator_arg_t
1914 * @brief Builds a %function that targets a copy of the incoming
1916 * @param f A %function object that is callable with parameters of
1917 * type @c T1, @c T2, ..., @c TN and returns a value convertible
1920 * The newly-created %function object will target a copy of @a
1921 * f. If @a f is @c reference_wrapper<F>, then this function
1922 * object will contain a reference to the function object @c
1923 * f.get(). If @a f is a NULL function pointer or NULL
1924 * pointer-to-member, the newly-created object will be empty.
1926 * If @a f is a non-NULL function pointer or an object of type @c
1927 * reference_wrapper<F>, this function will not throw.
1929 template<typename _Functor>
1930 function(_Functor __f,
1932 !is_integral<_Functor>::value, _Useless>::type
1936 * @brief %Function assignment operator.
1937 * @param x A %function with identical call signature.
1938 * @post @c (bool)*this == (bool)x
1941 * The target of @a x is copied to @c *this. If @a x has no
1942 * target, then @c *this will be empty.
1944 * If @a x targets a function pointer or a reference to a function
1945 * object, then this operation will not throw an %exception.
1948 operator=(const function& __x)
1950 function(__x).swap(*this);
1955 * @brief %Function move-assignment operator.
1956 * @param x A %function rvalue with identical call signature.
1959 * The target of @a x is moved to @c *this. If @a x has no
1960 * target, then @c *this will be empty.
1962 * If @a x targets a function pointer or a reference to a function
1963 * object, then this operation will not throw an %exception.
1966 operator=(function&& __x)
1968 function(std::move(__x)).swap(*this);
1973 * @brief %Function assignment to zero.
1974 * @post @c !(bool)*this
1977 * The target of @c *this is deallocated, leaving it empty.
1980 operator=(nullptr_t)
1984 _M_manager(_M_functor, _M_functor, __destroy_functor);
1992 * @brief %Function assignment to a new target.
1993 * @param f A %function object that is callable with parameters of
1994 * type @c T1, @c T2, ..., @c TN and returns a value convertible
1998 * This %function object wrapper will target a copy of @a
1999 * f. If @a f is @c reference_wrapper<F>, then this function
2000 * object will contain a reference to the function object @c
2001 * f.get(). If @a f is a NULL function pointer or NULL
2002 * pointer-to-member, @c this object will be empty.
2004 * If @a f is a non-NULL function pointer or an object of type @c
2005 * reference_wrapper<F>, this function will not throw.
2007 template<typename _Functor>
2008 typename enable_if<!is_integral<_Functor>::value, function&>::type
2009 operator=(_Functor&& __f)
2011 function(std::forward<_Functor>(__f)).swap(*this);
2016 template<typename _Functor>
2017 typename enable_if<!is_integral<_Functor>::value, function&>::type
2018 operator=(reference_wrapper<_Functor> __f)
2020 function(__f).swap(*this);
2024 // [3.7.2.2] function modifiers
2027 * @brief Swap the targets of two %function objects.
2028 * @param f A %function with identical call signature.
2030 * Swap the targets of @c this function object and @a f. This
2031 * function will not throw an %exception.
2033 void swap(function& __x)
2035 std::swap(_M_functor, __x._M_functor);
2036 std::swap(_M_manager, __x._M_manager);
2037 std::swap(_M_invoker, __x._M_invoker);
2040 // TODO: needs allocator_arg_t
2042 template<typename _Functor, typename _Alloc>
2044 assign(_Functor&& __f, const _Alloc& __a)
2046 function(allocator_arg, __a,
2047 std::forward<_Functor>(__f)).swap(*this);
2051 // [3.7.2.3] function capacity
2054 * @brief Determine if the %function wrapper has a target.
2056 * @return @c true when this %function object contains a target,
2057 * or @c false when it is empty.
2059 * This function will not throw an %exception.
2061 explicit operator bool() const
2062 { return !_M_empty(); }
2064 // [3.7.2.4] function invocation
2067 * @brief Invokes the function targeted by @c *this.
2068 * @returns the result of the target.
2069 * @throws bad_function_call when @c !(bool)*this
2071 * The function call operator invokes the target function object
2072 * stored by @c this.
2074 _Res operator()(_ArgTypes... __args) const;
2077 // [3.7.2.5] function target access
2079 * @brief Determine the type of the target of this function object
2082 * @returns the type identifier of the target function object, or
2083 * @c typeid(void) if @c !(bool)*this.
2085 * This function will not throw an %exception.
2087 const type_info& target_type() const;
2090 * @brief Access the stored target function object.
2092 * @return Returns a pointer to the stored target function object,
2093 * if @c typeid(Functor).equals(target_type()); otherwise, a NULL
2096 * This function will not throw an %exception.
2098 template<typename _Functor> _Functor* target();
2101 template<typename _Functor> const _Functor* target() const;
2105 typedef _Res (*_Invoker_type)(const _Any_data&, _ArgTypes...);
2106 _Invoker_type _M_invoker;
2109 // Out-of-line member definitions.
2110 template<typename _Res, typename... _ArgTypes>
2111 function<_Res(_ArgTypes...)>::
2112 function(const function& __x)
2115 if (static_cast<bool>(__x))
2117 _M_invoker = __x._M_invoker;
2118 _M_manager = __x._M_manager;
2119 __x._M_manager(_M_functor, __x._M_functor, __clone_functor);
2123 template<typename _Res, typename... _ArgTypes>
2124 template<typename _Functor>
2125 function<_Res(_ArgTypes...)>::
2126 function(_Functor __f,
2128 !is_integral<_Functor>::value, _Useless>::type)
2131 typedef _Function_handler<_Signature_type, _Functor> _My_handler;
2133 if (_My_handler::_M_not_empty_function(__f))
2135 _M_invoker = &_My_handler::_M_invoke;
2136 _M_manager = &_My_handler::_M_manager;
2137 _My_handler::_M_init_functor(_M_functor, std::move(__f));
2141 template<typename _Res, typename... _ArgTypes>
2143 function<_Res(_ArgTypes...)>::
2144 operator()(_ArgTypes... __args) const
2147 __throw_bad_function_call();
2148 return _M_invoker(_M_functor, std::forward<_ArgTypes>(__args)...);
2152 template<typename _Res, typename... _ArgTypes>
2154 function<_Res(_ArgTypes...)>::
2159 _Any_data __typeinfo_result;
2160 _M_manager(__typeinfo_result, _M_functor, __get_type_info);
2161 return *__typeinfo_result._M_access<const type_info*>();
2164 return typeid(void);
2167 template<typename _Res, typename... _ArgTypes>
2168 template<typename _Functor>
2170 function<_Res(_ArgTypes...)>::
2173 if (typeid(_Functor) == target_type() && _M_manager)
2176 if (_M_manager(__ptr, _M_functor, __get_functor_ptr)
2177 && !is_const<_Functor>::value)
2180 return __ptr._M_access<_Functor*>();
2186 template<typename _Res, typename... _ArgTypes>
2187 template<typename _Functor>
2189 function<_Res(_ArgTypes...)>::
2192 if (typeid(_Functor) == target_type() && _M_manager)
2195 _M_manager(__ptr, _M_functor, __get_functor_ptr);
2196 return __ptr._M_access<const _Functor*>();
2203 // [20.7.15.2.6] null pointer comparisons
2206 * @brief Compares a polymorphic function object wrapper against 0
2207 * (the NULL pointer).
2208 * @returns @c true if the wrapper has no target, @c false otherwise
2210 * This function will not throw an %exception.
2212 template<typename _Res, typename... _Args>
2214 operator==(const function<_Res(_Args...)>& __f, nullptr_t)
2215 { return !static_cast<bool>(__f); }
2218 template<typename _Res, typename... _Args>
2220 operator==(nullptr_t, const function<_Res(_Args...)>& __f)
2221 { return !static_cast<bool>(__f); }
2224 * @brief Compares a polymorphic function object wrapper against 0
2225 * (the NULL pointer).
2226 * @returns @c false if the wrapper has no target, @c true otherwise
2228 * This function will not throw an %exception.
2230 template<typename _Res, typename... _Args>
2232 operator!=(const function<_Res(_Args...)>& __f, nullptr_t)
2233 { return static_cast<bool>(__f); }
2236 template<typename _Res, typename... _Args>
2238 operator!=(nullptr_t, const function<_Res(_Args...)>& __f)
2239 { return static_cast<bool>(__f); }
2241 // [20.7.15.2.7] specialized algorithms
2244 * @brief Swap the targets of two polymorphic function object wrappers.
2246 * This function will not throw an %exception.
2248 template<typename _Res, typename... _Args>
2250 swap(function<_Res(_Args...)>& __x, function<_Res(_Args...)>& __y)
2253 _GLIBCXX_END_NAMESPACE_VERSION
2256 #endif // __GXX_EXPERIMENTAL_CXX0X__
2258 #endif // _GLIBCXX_FUNCTIONAL