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 template<typename _MemberPointer>
67 * Actual implementation of _Has_result_type, which uses SFINAE to
68 * determine if the type _Tp has a publicly-accessible member type
71 template<typename _Tp>
72 class _Has_result_type_helper : __sfinae_types
74 template<typename _Up>
78 template<typename _Up>
79 static __one __test(_Wrap_type<typename _Up::result_type>*);
81 template<typename _Up>
82 static __two __test(...);
85 static const bool value = sizeof(__test<_Tp>(0)) == 1;
88 template<typename _Tp>
89 struct _Has_result_type
90 : integral_constant<bool,
91 _Has_result_type_helper<typename remove_cv<_Tp>::type>::value>
94 /// If we have found a result_type, extract it.
95 template<bool _Has_result_type, typename _Functor>
96 struct _Maybe_get_result_type
99 template<typename _Functor>
100 struct _Maybe_get_result_type<true, _Functor>
102 typedef typename _Functor::result_type result_type;
106 * Base class for any function object that has a weak result type, as
107 * defined in 3.3/3 of TR1.
109 template<typename _Functor>
110 struct _Weak_result_type_impl
111 : _Maybe_get_result_type<_Has_result_type<_Functor>::value, _Functor>
114 /// Retrieve the result type for a function type.
115 template<typename _Res, typename... _ArgTypes>
116 struct _Weak_result_type_impl<_Res(_ArgTypes...)>
118 typedef _Res result_type;
121 /// Retrieve the result type for a function reference.
122 template<typename _Res, typename... _ArgTypes>
123 struct _Weak_result_type_impl<_Res(&)(_ArgTypes...)>
125 typedef _Res result_type;
128 /// Retrieve the result type for a function pointer.
129 template<typename _Res, typename... _ArgTypes>
130 struct _Weak_result_type_impl<_Res(*)(_ArgTypes...)>
132 typedef _Res result_type;
135 /// Retrieve result type for a member function pointer.
136 template<typename _Res, typename _Class, typename... _ArgTypes>
137 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...)>
139 typedef _Res result_type;
142 /// Retrieve result type for a const member function pointer.
143 template<typename _Res, typename _Class, typename... _ArgTypes>
144 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) const>
146 typedef _Res result_type;
149 /// Retrieve result type for a volatile member function pointer.
150 template<typename _Res, typename _Class, typename... _ArgTypes>
151 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) volatile>
153 typedef _Res result_type;
156 /// Retrieve result type for a const volatile member function pointer.
157 template<typename _Res, typename _Class, typename... _ArgTypes>
158 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...)const volatile>
160 typedef _Res result_type;
164 * Strip top-level cv-qualifiers from the function object and let
165 * _Weak_result_type_impl perform the real work.
167 template<typename _Functor>
168 struct _Weak_result_type
169 : _Weak_result_type_impl<typename remove_cv<_Functor>::type>
172 template<typename _Signature>
175 template<typename _Functor, typename... _ArgTypes>
176 struct result_of<_Functor(_ArgTypes...)>
179 decltype( std::declval<_Functor>()(std::declval<_ArgTypes>()...) )
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 // - a function type (binary)
321 template<typename _Res, typename _T1, typename _T2>
322 struct _Reference_wrapper_base<_Res(_T1, _T2)>
323 : binary_function<_T1, _T2, _Res>
326 // - a function pointer type (unary)
327 template<typename _Res, typename _T1>
328 struct _Reference_wrapper_base<_Res(*)(_T1)>
329 : unary_function<_T1, _Res>
332 // - a function pointer type (binary)
333 template<typename _Res, typename _T1, typename _T2>
334 struct _Reference_wrapper_base<_Res(*)(_T1, _T2)>
335 : binary_function<_T1, _T2, _Res>
338 // - a pointer to member function type (unary, no qualifiers)
339 template<typename _Res, typename _T1>
340 struct _Reference_wrapper_base<_Res (_T1::*)()>
341 : unary_function<_T1*, _Res>
344 // - a pointer to member function type (binary, no qualifiers)
345 template<typename _Res, typename _T1, typename _T2>
346 struct _Reference_wrapper_base<_Res (_T1::*)(_T2)>
347 : binary_function<_T1*, _T2, _Res>
350 // - a pointer to member function type (unary, const)
351 template<typename _Res, typename _T1>
352 struct _Reference_wrapper_base<_Res (_T1::*)() const>
353 : unary_function<const _T1*, _Res>
356 // - a pointer to member function type (binary, const)
357 template<typename _Res, typename _T1, typename _T2>
358 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const>
359 : binary_function<const _T1*, _T2, _Res>
362 // - a pointer to member function type (unary, volatile)
363 template<typename _Res, typename _T1>
364 struct _Reference_wrapper_base<_Res (_T1::*)() volatile>
365 : unary_function<volatile _T1*, _Res>
368 // - a pointer to member function type (binary, volatile)
369 template<typename _Res, typename _T1, typename _T2>
370 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) volatile>
371 : binary_function<volatile _T1*, _T2, _Res>
374 // - a pointer to member function type (unary, const volatile)
375 template<typename _Res, typename _T1>
376 struct _Reference_wrapper_base<_Res (_T1::*)() const volatile>
377 : unary_function<const volatile _T1*, _Res>
380 // - a pointer to member function type (binary, const volatile)
381 template<typename _Res, typename _T1, typename _T2>
382 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const volatile>
383 : binary_function<const volatile _T1*, _T2, _Res>
387 * @brief Primary class template for reference_wrapper.
391 template<typename _Tp>
392 class reference_wrapper
393 : public _Reference_wrapper_base<typename remove_cv<_Tp>::type>
395 // If _Tp is a function type, we can't form result_of<_Tp(...)>,
396 // so turn it into a function pointer type.
397 typedef typename _Function_to_function_pointer<_Tp>::type
404 reference_wrapper(_Tp& __indata): _M_data(&__indata)
407 reference_wrapper(_Tp&&) = delete;
409 reference_wrapper(const reference_wrapper<_Tp>& __inref):
410 _M_data(__inref._M_data)
414 operator=(const reference_wrapper<_Tp>& __inref)
416 _M_data = __inref._M_data;
420 operator _Tp&() const
421 { return this->get(); }
427 template<typename... _Args>
428 typename result_of<_M_func_type(_Args...)>::type
429 operator()(_Args&&... __args) const
431 return __invoke(get(), std::forward<_Args>(__args)...);
436 /// Denotes a reference should be taken to a variable.
437 template<typename _Tp>
438 inline reference_wrapper<_Tp>
440 { return reference_wrapper<_Tp>(__t); }
442 /// Denotes a const reference should be taken to a variable.
443 template<typename _Tp>
444 inline reference_wrapper<const _Tp>
446 { return reference_wrapper<const _Tp>(__t); }
448 /// Partial specialization.
449 template<typename _Tp>
450 inline reference_wrapper<_Tp>
451 ref(reference_wrapper<_Tp> __t)
452 { return ref(__t.get()); }
454 /// Partial specialization.
455 template<typename _Tp>
456 inline reference_wrapper<const _Tp>
457 cref(reference_wrapper<_Tp> __t)
458 { return cref(__t.get()); }
462 template<typename _Tp, bool>
463 struct _Mem_fn_const_or_non
465 typedef const _Tp& type;
468 template<typename _Tp>
469 struct _Mem_fn_const_or_non<_Tp, false>
475 * Derives from @c unary_function or @c binary_function, or perhaps
476 * nothing, depending on the number of arguments provided. The
477 * primary template is the basis case, which derives nothing.
479 template<typename _Res, typename... _ArgTypes>
480 struct _Maybe_unary_or_binary_function { };
482 /// Derives from @c unary_function, as appropriate.
483 template<typename _Res, typename _T1>
484 struct _Maybe_unary_or_binary_function<_Res, _T1>
485 : std::unary_function<_T1, _Res> { };
487 /// Derives from @c binary_function, as appropriate.
488 template<typename _Res, typename _T1, typename _T2>
489 struct _Maybe_unary_or_binary_function<_Res, _T1, _T2>
490 : std::binary_function<_T1, _T2, _Res> { };
492 /// Implementation of @c mem_fn for member function pointers.
493 template<typename _Res, typename _Class, typename... _ArgTypes>
494 class _Mem_fn<_Res (_Class::*)(_ArgTypes...)>
495 : public _Maybe_unary_or_binary_function<_Res, _Class*, _ArgTypes...>
497 typedef _Res (_Class::*_Functor)(_ArgTypes...);
499 template<typename _Tp>
501 _M_call(_Tp& __object, const volatile _Class *,
502 _ArgTypes... __args) const
503 { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); }
505 template<typename _Tp>
507 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
508 { return ((*__ptr).*__pmf)(std::forward<_ArgTypes>(__args)...); }
511 typedef _Res result_type;
513 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
517 operator()(_Class& __object, _ArgTypes... __args) const
518 { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); }
522 operator()(_Class* __object, _ArgTypes... __args) const
523 { return (__object->*__pmf)(std::forward<_ArgTypes>(__args)...); }
525 // Handle smart pointers, references and pointers to derived
526 template<typename _Tp>
528 operator()(_Tp& __object, _ArgTypes... __args) const
530 return _M_call(__object, &__object,
531 std::forward<_ArgTypes>(__args)...);
538 /// Implementation of @c mem_fn for const member function pointers.
539 template<typename _Res, typename _Class, typename... _ArgTypes>
540 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) const>
541 : public _Maybe_unary_or_binary_function<_Res, const _Class*,
544 typedef _Res (_Class::*_Functor)(_ArgTypes...) const;
546 template<typename _Tp>
548 _M_call(_Tp& __object, const volatile _Class *,
549 _ArgTypes... __args) const
550 { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); }
552 template<typename _Tp>
554 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
555 { return ((*__ptr).*__pmf)(std::forward<_ArgTypes>(__args)...); }
558 typedef _Res result_type;
560 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
564 operator()(const _Class& __object, _ArgTypes... __args) const
565 { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); }
569 operator()(const _Class* __object, _ArgTypes... __args) const
570 { return (__object->*__pmf)(std::forward<_ArgTypes>(__args)...); }
572 // Handle smart pointers, references and pointers to derived
573 template<typename _Tp>
574 _Res operator()(_Tp& __object, _ArgTypes... __args) const
576 return _M_call(__object, &__object,
577 std::forward<_ArgTypes>(__args)...);
584 /// Implementation of @c mem_fn for volatile member function pointers.
585 template<typename _Res, typename _Class, typename... _ArgTypes>
586 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) volatile>
587 : public _Maybe_unary_or_binary_function<_Res, volatile _Class*,
590 typedef _Res (_Class::*_Functor)(_ArgTypes...) volatile;
592 template<typename _Tp>
594 _M_call(_Tp& __object, const volatile _Class *,
595 _ArgTypes... __args) const
596 { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); }
598 template<typename _Tp>
600 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
601 { return ((*__ptr).*__pmf)(std::forward<_ArgTypes>(__args)...); }
604 typedef _Res result_type;
606 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
610 operator()(volatile _Class& __object, _ArgTypes... __args) const
611 { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); }
615 operator()(volatile _Class* __object, _ArgTypes... __args) const
616 { return (__object->*__pmf)(std::forward<_ArgTypes>(__args)...); }
618 // Handle smart pointers, references and pointers to derived
619 template<typename _Tp>
621 operator()(_Tp& __object, _ArgTypes... __args) const
623 return _M_call(__object, &__object,
624 std::forward<_ArgTypes>(__args)...);
631 /// Implementation of @c mem_fn for const volatile member function pointers.
632 template<typename _Res, typename _Class, typename... _ArgTypes>
633 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) const volatile>
634 : public _Maybe_unary_or_binary_function<_Res, const volatile _Class*,
637 typedef _Res (_Class::*_Functor)(_ArgTypes...) const volatile;
639 template<typename _Tp>
641 _M_call(_Tp& __object, const volatile _Class *,
642 _ArgTypes... __args) const
643 { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); }
645 template<typename _Tp>
647 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
648 { return ((*__ptr).*__pmf)(std::forward<_ArgTypes>(__args)...); }
651 typedef _Res result_type;
653 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
657 operator()(const volatile _Class& __object, _ArgTypes... __args) const
658 { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); }
662 operator()(const volatile _Class* __object, _ArgTypes... __args) const
663 { return (__object->*__pmf)(std::forward<_ArgTypes>(__args)...); }
665 // Handle smart pointers, references and pointers to derived
666 template<typename _Tp>
667 _Res operator()(_Tp& __object, _ArgTypes... __args) const
669 return _M_call(__object, &__object,
670 std::forward<_ArgTypes>(__args)...);
678 template<typename _Res, typename _Class>
679 class _Mem_fn<_Res _Class::*>
681 // This bit of genius is due to Peter Dimov, improved slightly by
683 template<typename _Tp>
685 _M_call(_Tp& __object, _Class *) const
686 { return __object.*__pm; }
688 template<typename _Tp, typename _Up>
690 _M_call(_Tp& __object, _Up * const *) const
691 { return (*__object).*__pm; }
693 template<typename _Tp, typename _Up>
695 _M_call(_Tp& __object, const _Up * const *) const
696 { return (*__object).*__pm; }
698 template<typename _Tp>
700 _M_call(_Tp& __object, const _Class *) const
701 { return __object.*__pm; }
703 template<typename _Tp>
705 _M_call(_Tp& __ptr, const volatile void*) const
706 { return (*__ptr).*__pm; }
708 template<typename _Tp> static _Tp& __get_ref();
710 template<typename _Tp>
711 static __sfinae_types::__one __check_const(_Tp&, _Class*);
712 template<typename _Tp, typename _Up>
713 static __sfinae_types::__one __check_const(_Tp&, _Up * const *);
714 template<typename _Tp, typename _Up>
715 static __sfinae_types::__two __check_const(_Tp&, const _Up * const *);
716 template<typename _Tp>
717 static __sfinae_types::__two __check_const(_Tp&, const _Class*);
718 template<typename _Tp>
719 static __sfinae_types::__two __check_const(_Tp&, const volatile void*);
722 template<typename _Tp>
724 : _Mem_fn_const_or_non<_Res,
725 (sizeof(__sfinae_types::__two)
726 == sizeof(__check_const<_Tp>(__get_ref<_Tp>(), (_Tp*)0)))>
729 template<typename _Signature>
732 template<typename _CVMem, typename _Tp>
733 struct result<_CVMem(_Tp)>
734 : public _Result_type<_Tp> { };
736 template<typename _CVMem, typename _Tp>
737 struct result<_CVMem(_Tp&)>
738 : public _Result_type<_Tp> { };
741 _Mem_fn(_Res _Class::*__pm) : __pm(__pm) { }
745 operator()(_Class& __object) const
746 { return __object.*__pm; }
749 operator()(const _Class& __object) const
750 { return __object.*__pm; }
754 operator()(_Class* __object) const
755 { return __object->*__pm; }
758 operator()(const _Class* __object) const
759 { return __object->*__pm; }
761 // Handle smart pointers and derived
762 template<typename _Tp>
763 typename _Result_type<_Tp>::type
764 operator()(_Tp& __unknown) const
765 { return _M_call(__unknown, &__unknown); }
772 * @brief Returns a function object that forwards to the member
776 template<typename _Tp, typename _Class>
777 inline _Mem_fn<_Tp _Class::*>
778 mem_fn(_Tp _Class::* __pm)
780 return _Mem_fn<_Tp _Class::*>(__pm);
784 * @brief Determines if the given type _Tp is a function object
785 * should be treated as a subexpression when evaluating calls to
786 * function objects returned by bind(). [TR1 3.6.1]
789 template<typename _Tp>
790 struct is_bind_expression
791 : public false_type { };
794 * @brief Determines if the given type _Tp is a placeholder in a
795 * bind() expression and, if so, which placeholder it is. [TR1 3.6.2]
798 template<typename _Tp>
799 struct is_placeholder
800 : public integral_constant<int, 0>
803 /// The type of placeholder objects defined by libstdc++.
804 template<int _Num> struct _Placeholder { };
806 /** @namespace std::placeholders
807 * @brief ISO C++ 0x entities sub namespace for functional.
810 * Define a large number of placeholders. There is no way to
811 * simplify this with variadic templates, because we're introducing
812 * unique names for each.
814 namespace placeholders
827 _Placeholder<10> _10;
828 _Placeholder<11> _11;
829 _Placeholder<12> _12;
830 _Placeholder<13> _13;
831 _Placeholder<14> _14;
832 _Placeholder<15> _15;
833 _Placeholder<16> _16;
834 _Placeholder<17> _17;
835 _Placeholder<18> _18;
836 _Placeholder<19> _19;
837 _Placeholder<20> _20;
838 _Placeholder<21> _21;
839 _Placeholder<22> _22;
840 _Placeholder<23> _23;
841 _Placeholder<24> _24;
842 _Placeholder<25> _25;
843 _Placeholder<26> _26;
844 _Placeholder<27> _27;
845 _Placeholder<28> _28;
846 _Placeholder<29> _29;
851 * Partial specialization of is_placeholder that provides the placeholder
852 * number for the placeholder objects defined by libstdc++.
856 struct is_placeholder<_Placeholder<_Num> >
857 : public integral_constant<int, _Num>
861 * Stores a tuple of indices. Used by bind() to extract the elements
864 template<int... _Indexes>
867 typedef _Index_tuple<_Indexes..., sizeof...(_Indexes)> __next;
870 /// Builds an _Index_tuple<0, 1, 2, ..., _Num-1>.
871 template<std::size_t _Num>
872 struct _Build_index_tuple
874 typedef typename _Build_index_tuple<_Num-1>::__type::__next __type;
878 struct _Build_index_tuple<0>
880 typedef _Index_tuple<> __type;
884 * Used by _Safe_tuple_element to indicate that there is no tuple
885 * element at this position.
887 struct _No_tuple_element;
890 * Implementation helper for _Safe_tuple_element. This primary
891 * template handles the case where it is safe to use @c
894 template<int __i, typename _Tuple, bool _IsSafe>
895 struct _Safe_tuple_element_impl
896 : tuple_element<__i, _Tuple> { };
899 * Implementation helper for _Safe_tuple_element. This partial
900 * specialization handles the case where it is not safe to use @c
901 * tuple_element. We just return @c _No_tuple_element.
903 template<int __i, typename _Tuple>
904 struct _Safe_tuple_element_impl<__i, _Tuple, false>
906 typedef _No_tuple_element type;
910 * Like tuple_element, but returns @c _No_tuple_element when
911 * tuple_element would return an error.
913 template<int __i, typename _Tuple>
914 struct _Safe_tuple_element
915 : _Safe_tuple_element_impl<__i, _Tuple,
916 (__i >= 0 && __i < tuple_size<_Tuple>::value)>
920 * Maps an argument to bind() into an actual argument to the bound
921 * function object [TR1 3.6.3/5]. Only the first parameter should
922 * be specified: the rest are used to determine among the various
923 * implementations. Note that, although this class is a function
924 * object, it isn't entirely normal because it takes only two
925 * parameters regardless of the number of parameters passed to the
926 * bind expression. The first parameter is the bound argument and
927 * the second parameter is a tuple containing references to the
928 * rest of the arguments.
930 template<typename _Arg,
931 bool _IsBindExp = is_bind_expression<_Arg>::value,
932 bool _IsPlaceholder = (is_placeholder<_Arg>::value > 0)>
936 * If the argument is reference_wrapper<_Tp>, returns the
937 * underlying reference. [TR1 3.6.3/5 bullet 1]
939 template<typename _Tp>
940 class _Mu<reference_wrapper<_Tp>, false, false>
943 typedef _Tp& result_type;
945 /* Note: This won't actually work for const volatile
946 * reference_wrappers, because reference_wrapper::get() is const
947 * but not volatile-qualified. This might be a defect in the TR.
949 template<typename _CVRef, typename _Tuple>
951 operator()(_CVRef& __arg, _Tuple&&) const volatile
952 { return __arg.get(); }
956 * If the argument is a bind expression, we invoke the underlying
957 * function object with the same cv-qualifiers as we are given and
958 * pass along all of our arguments (unwrapped). [TR1 3.6.3/5 bullet 2]
960 template<typename _Arg>
961 class _Mu<_Arg, true, false>
964 template<typename _Signature> class result;
966 // Determine the result type when we pass the arguments along. This
967 // involves passing along the cv-qualifiers placed on _Mu and
968 // unwrapping the argument bundle.
969 template<typename _CVMu, typename _CVArg, typename... _Args>
970 class result<_CVMu(_CVArg, tuple<_Args...>)>
971 : public result_of<_CVArg(_Args...)> { };
973 template<typename _CVArg, typename... _Args>
974 typename result_of<_CVArg(_Args...)>::type
975 operator()(_CVArg& __arg,
976 tuple<_Args...>&& __tuple) const volatile
978 // Construct an index tuple and forward to __call
979 typedef typename _Build_index_tuple<sizeof...(_Args)>::__type
981 return this->__call(__arg, std::move(__tuple), _Indexes());
985 // Invokes the underlying function object __arg by unpacking all
986 // of the arguments in the tuple.
987 template<typename _CVArg, typename... _Args, int... _Indexes>
988 typename result_of<_CVArg(_Args...)>::type
989 __call(_CVArg& __arg, tuple<_Args...>&& __tuple,
990 const _Index_tuple<_Indexes...>&) const volatile
992 return __arg(std::forward<_Args>(get<_Indexes>(__tuple))...);
997 * If the argument is a placeholder for the Nth argument, returns
998 * a reference to the Nth argument to the bind function object.
999 * [TR1 3.6.3/5 bullet 3]
1001 template<typename _Arg>
1002 class _Mu<_Arg, false, true>
1005 template<typename _Signature> class result;
1007 template<typename _CVMu, typename _CVArg, typename _Tuple>
1008 class result<_CVMu(_CVArg, _Tuple)>
1010 // Add a reference, if it hasn't already been done for us.
1011 // This allows us to be a little bit sloppy in constructing
1012 // the tuple that we pass to result_of<...>.
1013 typedef typename _Safe_tuple_element<(is_placeholder<_Arg>::value
1018 typedef typename add_rvalue_reference<__base_type>::type type;
1021 template<typename _Tuple>
1022 typename result<_Mu(_Arg, _Tuple)>::type
1023 operator()(const volatile _Arg&, _Tuple&& __tuple) const volatile
1025 return std::forward<typename result<_Mu(_Arg, _Tuple)>::type>(
1026 ::std::get<(is_placeholder<_Arg>::value - 1)>(__tuple));
1031 * If the argument is just a value, returns a reference to that
1032 * value. The cv-qualifiers on the reference are the same as the
1033 * cv-qualifiers on the _Mu object. [TR1 3.6.3/5 bullet 4]
1035 template<typename _Arg>
1036 class _Mu<_Arg, false, false>
1039 template<typename _Signature> struct result;
1041 template<typename _CVMu, typename _CVArg, typename _Tuple>
1042 struct result<_CVMu(_CVArg, _Tuple)>
1044 typedef typename add_lvalue_reference<_CVArg>::type type;
1047 // Pick up the cv-qualifiers of the argument
1048 template<typename _CVArg, typename _Tuple>
1050 operator()(_CVArg&& __arg, _Tuple&&) const volatile
1051 { return std::forward<_CVArg>(__arg); }
1055 * Maps member pointers into instances of _Mem_fn but leaves all
1056 * other function objects untouched. Used by tr1::bind(). The
1057 * primary template handles the non--member-pointer case.
1059 template<typename _Tp>
1060 struct _Maybe_wrap_member_pointer
1065 __do_wrap(const _Tp& __x)
1070 * Maps member pointers into instances of _Mem_fn but leaves all
1071 * other function objects untouched. Used by tr1::bind(). This
1072 * partial specialization handles the member pointer case.
1074 template<typename _Tp, typename _Class>
1075 struct _Maybe_wrap_member_pointer<_Tp _Class::*>
1077 typedef _Mem_fn<_Tp _Class::*> type;
1080 __do_wrap(_Tp _Class::* __pm)
1081 { return type(__pm); }
1084 // Specialization needed to prevent "forming reference to void" errors when
1085 // bind<void>() is called, because argument deduction instantiates
1086 // _Maybe_wrap_member_pointer<void> outside the immediate context where
1089 struct _Maybe_wrap_member_pointer<void>
1094 /// Type of the function object returned from bind().
1095 template<typename _Signature>
1098 template<typename _Functor, typename... _Bound_args>
1099 class _Bind<_Functor(_Bound_args...)>
1100 : public _Weak_result_type<_Functor>
1102 typedef _Bind __self_type;
1103 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
1107 tuple<_Bound_args...> _M_bound_args;
1110 template<typename _Result, typename... _Args, int... _Indexes>
1112 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>)
1114 return _M_f(_Mu<_Bound_args>()
1115 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1119 template<typename _Result, typename... _Args, int... _Indexes>
1121 __call_c(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>) const
1123 return _M_f(_Mu<_Bound_args>()
1124 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1129 template<typename _Result, typename... _Args, int... _Indexes>
1131 __call_v(tuple<_Args...>&& __args,
1132 _Index_tuple<_Indexes...>) volatile
1134 return _M_f(_Mu<_Bound_args>()
1135 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1138 // Call as const volatile
1139 template<typename _Result, typename... _Args, int... _Indexes>
1141 __call_c_v(tuple<_Args...>&& __args,
1142 _Index_tuple<_Indexes...>) const volatile
1144 return _M_f(_Mu<_Bound_args>()
1145 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1150 explicit _Bind(_Functor __f, _Bound_args... __bound_args)
1151 : _M_f(std::forward<_Functor>(__f)),
1152 _M_bound_args(std::forward<_Bound_args>(__bound_args)...)
1156 template<typename... _Args, typename _Result
1157 = decltype( std::declval<_Functor>()(
1158 _Mu<_Bound_args>()( std::declval<_Bound_args&>(),
1159 std::declval<tuple<_Args...>&&>() )... ) )>
1161 operator()(_Args&&... __args)
1163 return this->__call<_Result>(tuple<_Args...>
1164 (std::forward<_Args>(__args)...),
1169 template<typename... _Args, typename _Result
1170 = decltype( std::declval<const _Functor>()(
1171 _Mu<_Bound_args>()( std::declval<const _Bound_args&>(),
1172 std::declval<tuple<_Args...>&&>() )... ) )>
1174 operator()(_Args&&... __args) const
1176 return this->__call_c<_Result>(tuple<_Args...>
1177 (std::forward<_Args>(__args)...),
1183 template<typename... _Args, typename _Result
1184 = decltype( std::declval<volatile _Functor>()(
1185 _Mu<_Bound_args>()( std::declval<volatile _Bound_args&>(),
1186 std::declval<tuple<_Args...>&&>() )... ) )>
1188 operator()(_Args&&... __args) volatile
1190 return this->__call_v<_Result>(tuple<_Args...>
1191 (std::forward<_Args>(__args)...),
1195 // Call as const volatile
1196 template<typename... _Args, typename _Result
1197 = decltype( std::declval<const volatile _Functor>()(
1198 _Mu<_Bound_args>()( std::declval<const volatile _Bound_args&>(),
1199 std::declval<tuple<_Args...>&&>() )... ) )>
1201 operator()(_Args&&... __args) const volatile
1203 return this->__call_c_v<_Result>(tuple<_Args...>
1204 (std::forward<_Args>(__args)...),
1210 /// Type of the function object returned from bind<R>().
1211 template<typename _Result, typename _Signature>
1212 struct _Bind_result;
1214 template<typename _Result, typename _Functor, typename... _Bound_args>
1215 class _Bind_result<_Result, _Functor(_Bound_args...)>
1217 typedef _Bind_result __self_type;
1218 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
1222 tuple<_Bound_args...> _M_bound_args;
1225 template<typename _Res>
1226 struct __enable_if_void : enable_if<is_void<_Res>::value, int> { };
1227 template<typename _Res>
1228 struct __disable_if_void : enable_if<!is_void<_Res>::value, int> { };
1231 template<typename _Res, typename... _Args, int... _Indexes>
1233 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1234 typename __disable_if_void<_Res>::type = 0)
1236 return _M_f(_Mu<_Bound_args>()
1237 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1240 // Call unqualified, return void
1241 template<typename _Res, typename... _Args, int... _Indexes>
1243 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1244 typename __enable_if_void<_Res>::type = 0)
1246 _M_f(_Mu<_Bound_args>()
1247 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1251 template<typename _Res, typename... _Args, int... _Indexes>
1253 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1254 typename __disable_if_void<_Res>::type = 0) const
1256 return _M_f(_Mu<_Bound_args>()
1257 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1260 // Call as const, return void
1261 template<typename _Res, typename... _Args, int... _Indexes>
1263 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1264 typename __enable_if_void<_Res>::type = 0) const
1266 _M_f(_Mu<_Bound_args>()
1267 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1271 template<typename _Res, typename... _Args, int... _Indexes>
1273 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1274 typename __disable_if_void<_Res>::type = 0) volatile
1276 return _M_f(_Mu<_Bound_args>()
1277 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1280 // Call as volatile, return void
1281 template<typename _Res, typename... _Args, int... _Indexes>
1283 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1284 typename __enable_if_void<_Res>::type = 0) volatile
1286 _M_f(_Mu<_Bound_args>()
1287 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1290 // Call as const volatile
1291 template<typename _Res, typename... _Args, int... _Indexes>
1293 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1294 typename __disable_if_void<_Res>::type = 0) const volatile
1296 return _M_f(_Mu<_Bound_args>()
1297 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1300 // Call as const volatile, return void
1301 template<typename _Res, typename... _Args, int... _Indexes>
1303 __call(tuple<_Args...>&& __args,
1304 _Index_tuple<_Indexes...>,
1305 typename __enable_if_void<_Res>::type = 0) const volatile
1307 _M_f(_Mu<_Bound_args>()
1308 (get<_Indexes>(_M_bound_args), std::move(__args))...);
1312 typedef _Result result_type;
1315 _Bind_result(_Functor __f, _Bound_args... __bound_args)
1316 : _M_f(std::forward<_Functor>(__f)),
1317 _M_bound_args(std::forward<_Bound_args>(__bound_args)...)
1321 template<typename... _Args>
1323 operator()(_Args&&... __args)
1325 return this->__call<_Result>(
1326 tuple<_Args...>(std::forward<_Args>(__args)...),
1331 template<typename... _Args>
1333 operator()(_Args&&... __args) const
1335 return this->__call<_Result>(
1336 tuple<_Args...>(std::forward<_Args>(__args)...),
1341 template<typename... _Args>
1343 operator()(_Args&&... __args) volatile
1345 return this->__call<_Result>(
1346 tuple<_Args...>(std::forward<_Args>(__args)...),
1350 // Call as const volatile
1351 template<typename... _Args>
1353 operator()(_Args&&... __args) const volatile
1355 return this->__call<_Result>(
1356 tuple<_Args...>(std::forward<_Args>(__args)...),
1362 * @brief Class template _Bind is always a bind expression.
1365 template<typename _Signature>
1366 struct is_bind_expression<_Bind<_Signature> >
1367 : public true_type { };
1370 * @brief Class template _Bind is always a bind expression.
1373 template<typename _Result, typename _Signature>
1374 struct is_bind_expression<_Bind_result<_Result, _Signature> >
1375 : public true_type { };
1378 * @brief Function template for std::bind.
1381 template<typename _Functor, typename... _ArgTypes>
1383 _Bind<typename _Maybe_wrap_member_pointer<_Functor>::type(_ArgTypes...)>
1384 bind(_Functor __f, _ArgTypes... __args)
1386 typedef _Maybe_wrap_member_pointer<_Functor> __maybe_type;
1387 typedef typename __maybe_type::type __functor_type;
1388 typedef _Bind<__functor_type(_ArgTypes...)> __result_type;
1389 return __result_type(__maybe_type::__do_wrap(__f),
1390 std::forward<_ArgTypes>(__args)...);
1394 * @brief Function template for std::bind.
1397 template<typename _Result, typename _Functor, typename... _ArgTypes>
1399 _Bind_result<_Result,
1400 typename _Maybe_wrap_member_pointer<_Functor>::type
1402 bind(_Functor __f, _ArgTypes... __args)
1404 typedef _Maybe_wrap_member_pointer<_Functor> __maybe_type;
1405 typedef typename __maybe_type::type __functor_type;
1406 typedef _Bind_result<_Result, __functor_type(_ArgTypes...)>
1408 return __result_type(__maybe_type::__do_wrap(__f),
1409 std::forward<_ArgTypes>(__args)...);
1413 * @brief Exception class thrown when class template function's
1414 * operator() is called with an empty target.
1415 * @ingroup exceptions
1417 class bad_function_call : public std::exception { };
1420 * The integral constant expression 0 can be converted into a
1421 * pointer to this type. It is used by the function template to
1422 * accept NULL pointers.
1424 struct _M_clear_type;
1427 * Trait identifying "location-invariant" types, meaning that the
1428 * address of the object (or any of its members) will not escape.
1429 * Also implies a trivial copy constructor and assignment operator.
1431 template<typename _Tp>
1432 struct __is_location_invariant
1433 : integral_constant<bool, (is_pointer<_Tp>::value
1434 || is_member_pointer<_Tp>::value)>
1437 class _Undefined_class;
1442 const void* _M_const_object;
1443 void (*_M_function_pointer)();
1444 void (_Undefined_class::*_M_member_pointer)();
1449 void* _M_access() { return &_M_pod_data[0]; }
1450 const void* _M_access() const { return &_M_pod_data[0]; }
1452 template<typename _Tp>
1455 { return *static_cast<_Tp*>(_M_access()); }
1457 template<typename _Tp>
1460 { return *static_cast<const _Tp*>(_M_access()); }
1462 _Nocopy_types _M_unused;
1463 char _M_pod_data[sizeof(_Nocopy_types)];
1466 enum _Manager_operation
1474 // Simple type wrapper that helps avoid annoying const problems
1475 // when casting between void pointers and pointers-to-pointers.
1476 template<typename _Tp>
1477 struct _Simple_type_wrapper
1479 _Simple_type_wrapper(_Tp __value) : __value(__value) { }
1484 template<typename _Tp>
1485 struct __is_location_invariant<_Simple_type_wrapper<_Tp> >
1486 : __is_location_invariant<_Tp>
1489 // Converts a reference to a function object into a callable
1491 template<typename _Functor>
1493 __callable_functor(_Functor& __f)
1496 template<typename _Member, typename _Class>
1497 inline _Mem_fn<_Member _Class::*>
1498 __callable_functor(_Member _Class::* &__p)
1499 { return mem_fn(__p); }
1501 template<typename _Member, typename _Class>
1502 inline _Mem_fn<_Member _Class::*>
1503 __callable_functor(_Member _Class::* const &__p)
1504 { return mem_fn(__p); }
1506 template<typename _Signature>
1509 /// Base class of all polymorphic function object wrappers.
1510 class _Function_base
1513 static const std::size_t _M_max_size = sizeof(_Nocopy_types);
1514 static const std::size_t _M_max_align = __alignof__(_Nocopy_types);
1516 template<typename _Functor>
1520 static const bool __stored_locally =
1521 (__is_location_invariant<_Functor>::value
1522 && sizeof(_Functor) <= _M_max_size
1523 && __alignof__(_Functor) <= _M_max_align
1524 && (_M_max_align % __alignof__(_Functor) == 0));
1526 typedef integral_constant<bool, __stored_locally> _Local_storage;
1528 // Retrieve a pointer to the function object
1530 _M_get_pointer(const _Any_data& __source)
1532 const _Functor* __ptr =
1533 __stored_locally? &__source._M_access<_Functor>()
1534 /* have stored a pointer */ : __source._M_access<_Functor*>();
1535 return const_cast<_Functor*>(__ptr);
1538 // Clone a location-invariant function object that fits within
1539 // an _Any_data structure.
1541 _M_clone(_Any_data& __dest, const _Any_data& __source, true_type)
1543 new (__dest._M_access()) _Functor(__source._M_access<_Functor>());
1546 // Clone a function object that is not location-invariant or
1547 // that cannot fit into an _Any_data structure.
1549 _M_clone(_Any_data& __dest, const _Any_data& __source, false_type)
1551 __dest._M_access<_Functor*>() =
1552 new _Functor(*__source._M_access<_Functor*>());
1555 // Destroying a location-invariant object may still require
1558 _M_destroy(_Any_data& __victim, true_type)
1560 __victim._M_access<_Functor>().~_Functor();
1563 // Destroying an object located on the heap.
1565 _M_destroy(_Any_data& __victim, false_type)
1567 delete __victim._M_access<_Functor*>();
1572 _M_manager(_Any_data& __dest, const _Any_data& __source,
1573 _Manager_operation __op)
1578 case __get_type_info:
1579 __dest._M_access<const type_info*>() = &typeid(_Functor);
1582 case __get_functor_ptr:
1583 __dest._M_access<_Functor*>() = _M_get_pointer(__source);
1586 case __clone_functor:
1587 _M_clone(__dest, __source, _Local_storage());
1590 case __destroy_functor:
1591 _M_destroy(__dest, _Local_storage());
1598 _M_init_functor(_Any_data& __functor, _Functor&& __f)
1599 { _M_init_functor(__functor, std::move(__f), _Local_storage()); }
1601 template<typename _Signature>
1603 _M_not_empty_function(const function<_Signature>& __f)
1604 { return static_cast<bool>(__f); }
1606 template<typename _Tp>
1608 _M_not_empty_function(const _Tp*& __fp)
1611 template<typename _Class, typename _Tp>
1613 _M_not_empty_function(_Tp _Class::* const& __mp)
1616 template<typename _Tp>
1618 _M_not_empty_function(const _Tp&)
1623 _M_init_functor(_Any_data& __functor, _Functor&& __f, true_type)
1624 { new (__functor._M_access()) _Functor(std::move(__f)); }
1627 _M_init_functor(_Any_data& __functor, _Functor&& __f, false_type)
1628 { __functor._M_access<_Functor*>() = new _Functor(std::move(__f)); }
1631 template<typename _Functor>
1632 class _Ref_manager : public _Base_manager<_Functor*>
1634 typedef _Function_base::_Base_manager<_Functor*> _Base;
1638 _M_manager(_Any_data& __dest, const _Any_data& __source,
1639 _Manager_operation __op)
1644 case __get_type_info:
1645 __dest._M_access<const type_info*>() = &typeid(_Functor);
1648 case __get_functor_ptr:
1649 __dest._M_access<_Functor*>() = *_Base::_M_get_pointer(__source);
1650 return is_const<_Functor>::value;
1654 _Base::_M_manager(__dest, __source, __op);
1660 _M_init_functor(_Any_data& __functor, reference_wrapper<_Functor> __f)
1662 // TBD: Use address_of function instead.
1663 _Base::_M_init_functor(__functor, &__f.get());
1667 _Function_base() : _M_manager(0) { }
1672 _M_manager(_M_functor, _M_functor, __destroy_functor);
1676 bool _M_empty() const { return !_M_manager; }
1678 typedef bool (*_Manager_type)(_Any_data&, const _Any_data&,
1679 _Manager_operation);
1681 _Any_data _M_functor;
1682 _Manager_type _M_manager;
1685 template<typename _Signature, typename _Functor>
1686 class _Function_handler;
1688 template<typename _Res, typename _Functor, typename... _ArgTypes>
1689 class _Function_handler<_Res(_ArgTypes...), _Functor>
1690 : public _Function_base::_Base_manager<_Functor>
1692 typedef _Function_base::_Base_manager<_Functor> _Base;
1696 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1698 return (*_Base::_M_get_pointer(__functor))(
1699 std::forward<_ArgTypes>(__args)...);
1703 template<typename _Functor, typename... _ArgTypes>
1704 class _Function_handler<void(_ArgTypes...), _Functor>
1705 : public _Function_base::_Base_manager<_Functor>
1707 typedef _Function_base::_Base_manager<_Functor> _Base;
1711 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1713 (*_Base::_M_get_pointer(__functor))(
1714 std::forward<_ArgTypes>(__args)...);
1718 template<typename _Res, typename _Functor, typename... _ArgTypes>
1719 class _Function_handler<_Res(_ArgTypes...), reference_wrapper<_Functor> >
1720 : public _Function_base::_Ref_manager<_Functor>
1722 typedef _Function_base::_Ref_manager<_Functor> _Base;
1726 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1728 return __callable_functor(**_Base::_M_get_pointer(__functor))(
1729 std::forward<_ArgTypes>(__args)...);
1733 template<typename _Functor, typename... _ArgTypes>
1734 class _Function_handler<void(_ArgTypes...), reference_wrapper<_Functor> >
1735 : public _Function_base::_Ref_manager<_Functor>
1737 typedef _Function_base::_Ref_manager<_Functor> _Base;
1741 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1743 __callable_functor(**_Base::_M_get_pointer(__functor))(
1744 std::forward<_ArgTypes>(__args)...);
1748 template<typename _Class, typename _Member, typename _Res,
1749 typename... _ArgTypes>
1750 class _Function_handler<_Res(_ArgTypes...), _Member _Class::*>
1751 : public _Function_handler<void(_ArgTypes...), _Member _Class::*>
1753 typedef _Function_handler<void(_ArgTypes...), _Member _Class::*>
1758 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1760 return mem_fn(_Base::_M_get_pointer(__functor)->__value)(
1761 std::forward<_ArgTypes>(__args)...);
1765 template<typename _Class, typename _Member, typename... _ArgTypes>
1766 class _Function_handler<void(_ArgTypes...), _Member _Class::*>
1767 : public _Function_base::_Base_manager<
1768 _Simple_type_wrapper< _Member _Class::* > >
1770 typedef _Member _Class::* _Functor;
1771 typedef _Simple_type_wrapper<_Functor> _Wrapper;
1772 typedef _Function_base::_Base_manager<_Wrapper> _Base;
1776 _M_manager(_Any_data& __dest, const _Any_data& __source,
1777 _Manager_operation __op)
1782 case __get_type_info:
1783 __dest._M_access<const type_info*>() = &typeid(_Functor);
1786 case __get_functor_ptr:
1787 __dest._M_access<_Functor*>() =
1788 &_Base::_M_get_pointer(__source)->__value;
1792 _Base::_M_manager(__dest, __source, __op);
1798 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1800 mem_fn(_Base::_M_get_pointer(__functor)->__value)(
1801 std::forward<_ArgTypes>(__args)...);
1806 * @brief Primary class template for std::function.
1809 * Polymorphic function wrapper.
1811 template<typename _Res, typename... _ArgTypes>
1812 class function<_Res(_ArgTypes...)>
1813 : public _Maybe_unary_or_binary_function<_Res, _ArgTypes...>,
1814 private _Function_base
1816 typedef _Res _Signature_type(_ArgTypes...);
1818 struct _Useless { };
1821 typedef _Res result_type;
1823 // [3.7.2.1] construct/copy/destroy
1826 * @brief Default construct creates an empty function call wrapper.
1827 * @post @c !(bool)*this
1830 function() : _Function_base() { }
1833 * @brief Default construct creates an empty function call wrapper.
1834 * @post @c !(bool)*this
1836 function(_M_clear_type*) : _Function_base() { }
1839 * @brief %Function copy constructor.
1840 * @param x A %function object with identical call signature.
1841 * @post @c (bool)*this == (bool)x
1843 * The newly-created %function contains a copy of the target of @a
1844 * x (if it has one).
1846 function(const function& __x);
1849 * @brief %Function move constructor.
1850 * @param x A %function object rvalue with identical call signature.
1852 * The newly-created %function contains the target of @a x
1855 function(function&& __x) : _Function_base()
1860 // TODO: needs allocator_arg_t
1863 * @brief Builds a %function that targets a copy of the incoming
1865 * @param f A %function object that is callable with parameters of
1866 * type @c T1, @c T2, ..., @c TN and returns a value convertible
1869 * The newly-created %function object will target a copy of @a
1870 * f. If @a f is @c reference_wrapper<F>, then this function
1871 * object will contain a reference to the function object @c
1872 * f.get(). If @a f is a NULL function pointer or NULL
1873 * pointer-to-member, the newly-created object will be empty.
1875 * If @a f is a non-NULL function pointer or an object of type @c
1876 * reference_wrapper<F>, this function will not throw.
1878 template<typename _Functor>
1879 function(_Functor __f,
1881 !is_integral<_Functor>::value, _Useless>::type
1885 * @brief %Function assignment operator.
1886 * @param x A %function with identical call signature.
1887 * @post @c (bool)*this == (bool)x
1890 * The target of @a x is copied to @c *this. If @a x has no
1891 * target, then @c *this will be empty.
1893 * If @a x targets a function pointer or a reference to a function
1894 * object, then this operation will not throw an %exception.
1897 operator=(const function& __x)
1899 function(__x).swap(*this);
1904 * @brief %Function move-assignment operator.
1905 * @param x A %function rvalue with identical call signature.
1908 * The target of @a x is moved to @c *this. If @a x has no
1909 * target, then @c *this will be empty.
1911 * If @a x targets a function pointer or a reference to a function
1912 * object, then this operation will not throw an %exception.
1915 operator=(function&& __x)
1917 function(std::move(__x)).swap(*this);
1922 * @brief %Function assignment to zero.
1923 * @post @c !(bool)*this
1926 * The target of @c *this is deallocated, leaving it empty.
1929 operator=(_M_clear_type*)
1933 _M_manager(_M_functor, _M_functor, __destroy_functor);
1941 * @brief %Function assignment to a new target.
1942 * @param f A %function object that is callable with parameters of
1943 * type @c T1, @c T2, ..., @c TN and returns a value convertible
1947 * This %function object wrapper will target a copy of @a
1948 * f. If @a f is @c reference_wrapper<F>, then this function
1949 * object will contain a reference to the function object @c
1950 * f.get(). If @a f is a NULL function pointer or NULL
1951 * pointer-to-member, @c this object will be empty.
1953 * If @a f is a non-NULL function pointer or an object of type @c
1954 * reference_wrapper<F>, this function will not throw.
1956 template<typename _Functor>
1957 typename enable_if<!is_integral<_Functor>::value, function&>::type
1958 operator=(_Functor&& __f)
1960 function(std::forward<_Functor>(__f)).swap(*this);
1965 template<typename _Functor>
1966 typename enable_if<!is_integral<_Functor>::value, function&>::type
1967 operator=(reference_wrapper<_Functor> __f)
1969 function(__f).swap(*this);
1973 // [3.7.2.2] function modifiers
1976 * @brief Swap the targets of two %function objects.
1977 * @param f A %function with identical call signature.
1979 * Swap the targets of @c this function object and @a f. This
1980 * function will not throw an %exception.
1982 void swap(function& __x)
1984 /* We cannot perform direct assignments of the _M_functor
1985 parts as they are of type _Any_data and have a different
1986 dynamic type. Doing so would violate type-based aliasing
1987 rules and lead to spurious miscompilations.
1988 Instead perform a bytewise exchange of the memory of
1990 ??? A wordwise exchange honoring alignment of _M_functor
1991 would be more efficient. See PR42845. */
1992 for (unsigned i = 0; i < sizeof (_M_functor._M_pod_data); ++i)
1993 std::swap (_M_functor._M_pod_data[i], __x._M_functor._M_pod_data[i]);
1994 _Manager_type __old_manager = _M_manager;
1995 _M_manager = __x._M_manager;
1996 __x._M_manager = __old_manager;
1997 _Invoker_type __old_invoker = _M_invoker;
1998 _M_invoker = __x._M_invoker;
1999 __x._M_invoker = __old_invoker;
2002 // TODO: needs allocator_arg_t
2004 template<typename _Functor, typename _Alloc>
2006 assign(_Functor&& __f, const _Alloc& __a)
2008 function(allocator_arg, __a,
2009 std::forward<_Functor>(__f)).swap(*this);
2013 // [3.7.2.3] function capacity
2016 * @brief Determine if the %function wrapper has a target.
2018 * @return @c true when this %function object contains a target,
2019 * or @c false when it is empty.
2021 * This function will not throw an %exception.
2023 explicit operator bool() const
2024 { return !_M_empty(); }
2026 // [3.7.2.4] function invocation
2029 * @brief Invokes the function targeted by @c *this.
2030 * @returns the result of the target.
2031 * @throws bad_function_call when @c !(bool)*this
2033 * The function call operator invokes the target function object
2034 * stored by @c this.
2036 _Res operator()(_ArgTypes... __args) const;
2039 // [3.7.2.5] function target access
2041 * @brief Determine the type of the target of this function object
2044 * @returns the type identifier of the target function object, or
2045 * @c typeid(void) if @c !(bool)*this.
2047 * This function will not throw an %exception.
2049 const type_info& target_type() const;
2052 * @brief Access the stored target function object.
2054 * @return Returns a pointer to the stored target function object,
2055 * if @c typeid(Functor).equals(target_type()); otherwise, a NULL
2058 * This function will not throw an %exception.
2060 template<typename _Functor> _Functor* target();
2063 template<typename _Functor> const _Functor* target() const;
2066 // deleted overloads
2067 template<typename _Res2, typename... _ArgTypes2>
2068 void operator==(const function<_Res2(_ArgTypes2...)>&) const = delete;
2069 template<typename _Res2, typename... _ArgTypes2>
2070 void operator!=(const function<_Res2(_ArgTypes2...)>&) const = delete;
2073 typedef _Res (*_Invoker_type)(const _Any_data&, _ArgTypes...);
2074 _Invoker_type _M_invoker;
2077 // Out-of-line member definitions.
2078 template<typename _Res, typename... _ArgTypes>
2079 function<_Res(_ArgTypes...)>::
2080 function(const function& __x)
2083 if (static_cast<bool>(__x))
2085 _M_invoker = __x._M_invoker;
2086 _M_manager = __x._M_manager;
2087 __x._M_manager(_M_functor, __x._M_functor, __clone_functor);
2091 template<typename _Res, typename... _ArgTypes>
2092 template<typename _Functor>
2093 function<_Res(_ArgTypes...)>::
2094 function(_Functor __f,
2096 !is_integral<_Functor>::value, _Useless>::type)
2099 typedef _Function_handler<_Signature_type, _Functor> _My_handler;
2101 if (_My_handler::_M_not_empty_function(__f))
2103 _M_invoker = &_My_handler::_M_invoke;
2104 _M_manager = &_My_handler::_M_manager;
2105 _My_handler::_M_init_functor(_M_functor, std::move(__f));
2109 template<typename _Res, typename... _ArgTypes>
2111 function<_Res(_ArgTypes...)>::
2112 operator()(_ArgTypes... __args) const
2115 __throw_bad_function_call();
2116 return _M_invoker(_M_functor, std::forward<_ArgTypes>(__args)...);
2120 template<typename _Res, typename... _ArgTypes>
2122 function<_Res(_ArgTypes...)>::
2127 _Any_data __typeinfo_result;
2128 _M_manager(__typeinfo_result, _M_functor, __get_type_info);
2129 return *__typeinfo_result._M_access<const type_info*>();
2132 return typeid(void);
2135 template<typename _Res, typename... _ArgTypes>
2136 template<typename _Functor>
2138 function<_Res(_ArgTypes...)>::
2141 if (typeid(_Functor) == target_type() && _M_manager)
2144 if (_M_manager(__ptr, _M_functor, __get_functor_ptr)
2145 && !is_const<_Functor>::value)
2148 return __ptr._M_access<_Functor*>();
2154 template<typename _Res, typename... _ArgTypes>
2155 template<typename _Functor>
2157 function<_Res(_ArgTypes...)>::
2160 if (typeid(_Functor) == target_type() && _M_manager)
2163 _M_manager(__ptr, _M_functor, __get_functor_ptr);
2164 return __ptr._M_access<const _Functor*>();
2171 // [20.7.15.2.6] null pointer comparisons
2174 * @brief Compares a polymorphic function object wrapper against 0
2175 * (the NULL pointer).
2176 * @returns @c true if the wrapper has no target, @c false otherwise
2178 * This function will not throw an %exception.
2180 template<typename _Res, typename... _Args>
2182 operator==(const function<_Res(_Args...)>& __f, _M_clear_type*)
2183 { return !static_cast<bool>(__f); }
2186 template<typename _Res, typename... _Args>
2188 operator==(_M_clear_type*, const function<_Res(_Args...)>& __f)
2189 { return !static_cast<bool>(__f); }
2192 * @brief Compares a polymorphic function object wrapper against 0
2193 * (the NULL pointer).
2194 * @returns @c false if the wrapper has no target, @c true otherwise
2196 * This function will not throw an %exception.
2198 template<typename _Res, typename... _Args>
2200 operator!=(const function<_Res(_Args...)>& __f, _M_clear_type*)
2201 { return static_cast<bool>(__f); }
2204 template<typename _Res, typename... _Args>
2206 operator!=(_M_clear_type*, const function<_Res(_Args...)>& __f)
2207 { return static_cast<bool>(__f); }
2209 // [20.7.15.2.7] specialized algorithms
2212 * @brief Swap the targets of two polymorphic function object wrappers.
2214 * This function will not throw an %exception.
2216 template<typename _Res, typename... _Args>
2218 swap(function<_Res(_Args...)>& __x, function<_Res(_Args...)>& __y)
2222 #endif // __GXX_EXPERIMENTAL_CXX0X__
2224 #endif // _GLIBCXX_FUNCTIONAL