1 // <functional> -*- C++ -*-
3 // Copyright (C) 2001-2013 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
27 * Silicon Graphics Computer Systems, Inc.
29 * Permission to use, copy, modify, distribute and sell this software
30 * and its documentation for any purpose is hereby granted without fee,
31 * provided that the above copyright notice appear in all copies and
32 * that both that copyright notice and this permission notice appear
33 * in supporting documentation. Silicon Graphics makes no
34 * representations about the suitability of this software for any
35 * purpose. It is provided "as is" without express or implied warranty.
39 /** @file include/functional
40 * This is a Standard C++ Library header.
43 #ifndef _GLIBCXX_FUNCTIONAL
44 #define _GLIBCXX_FUNCTIONAL 1
46 #pragma GCC system_header
48 #include <bits/c++config.h>
49 #include <bits/stl_function.h>
51 #if __cplusplus >= 201103L
56 #include <type_traits>
57 #include <bits/functexcept.h>
58 #include <bits/functional_hash.h>
60 namespace std _GLIBCXX_VISIBILITY(default)
62 _GLIBCXX_BEGIN_NAMESPACE_VERSION
64 template<typename _MemberPointer>
66 template<typename _Tp, typename _Class>
67 _Mem_fn<_Tp _Class::*>
68 mem_fn(_Tp _Class::*) noexcept;
70 _GLIBCXX_HAS_NESTED_TYPE(result_type)
72 /// If we have found a result_type, extract it.
73 template<bool _Has_result_type, typename _Functor>
74 struct _Maybe_get_result_type
77 template<typename _Functor>
78 struct _Maybe_get_result_type<true, _Functor>
79 { typedef typename _Functor::result_type result_type; };
82 * Base class for any function object that has a weak result type, as
83 * defined in 3.3/3 of TR1.
85 template<typename _Functor>
86 struct _Weak_result_type_impl
87 : _Maybe_get_result_type<__has_result_type<_Functor>::value, _Functor>
90 /// Retrieve the result type for a function type.
91 template<typename _Res, typename... _ArgTypes>
92 struct _Weak_result_type_impl<_Res(_ArgTypes...)>
93 { typedef _Res result_type; };
95 template<typename _Res, typename... _ArgTypes>
96 struct _Weak_result_type_impl<_Res(_ArgTypes......)>
97 { typedef _Res result_type; };
99 template<typename _Res, typename... _ArgTypes>
100 struct _Weak_result_type_impl<_Res(_ArgTypes...) const>
101 { typedef _Res result_type; };
103 template<typename _Res, typename... _ArgTypes>
104 struct _Weak_result_type_impl<_Res(_ArgTypes......) const>
105 { typedef _Res result_type; };
107 template<typename _Res, typename... _ArgTypes>
108 struct _Weak_result_type_impl<_Res(_ArgTypes...) volatile>
109 { typedef _Res result_type; };
111 template<typename _Res, typename... _ArgTypes>
112 struct _Weak_result_type_impl<_Res(_ArgTypes......) volatile>
113 { typedef _Res result_type; };
115 template<typename _Res, typename... _ArgTypes>
116 struct _Weak_result_type_impl<_Res(_ArgTypes...) const volatile>
117 { typedef _Res result_type; };
119 template<typename _Res, typename... _ArgTypes>
120 struct _Weak_result_type_impl<_Res(_ArgTypes......) const volatile>
121 { typedef _Res result_type; };
123 /// Retrieve the result type for a function reference.
124 template<typename _Res, typename... _ArgTypes>
125 struct _Weak_result_type_impl<_Res(&)(_ArgTypes...)>
126 { typedef _Res result_type; };
128 template<typename _Res, typename... _ArgTypes>
129 struct _Weak_result_type_impl<_Res(&)(_ArgTypes......)>
130 { typedef _Res result_type; };
132 /// Retrieve the result type for a function pointer.
133 template<typename _Res, typename... _ArgTypes>
134 struct _Weak_result_type_impl<_Res(*)(_ArgTypes...)>
135 { typedef _Res result_type; };
137 template<typename _Res, typename... _ArgTypes>
138 struct _Weak_result_type_impl<_Res(*)(_ArgTypes......)>
139 { typedef _Res result_type; };
141 /// Retrieve result type for a member function pointer.
142 template<typename _Res, typename _Class, typename... _ArgTypes>
143 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...)>
144 { typedef _Res result_type; };
146 template<typename _Res, typename _Class, typename... _ArgTypes>
147 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes......)>
148 { typedef _Res result_type; };
150 /// Retrieve result type for a const member function pointer.
151 template<typename _Res, typename _Class, typename... _ArgTypes>
152 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) const>
153 { typedef _Res result_type; };
155 template<typename _Res, typename _Class, typename... _ArgTypes>
156 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes......) const>
157 { typedef _Res result_type; };
159 /// Retrieve result type for a volatile member function pointer.
160 template<typename _Res, typename _Class, typename... _ArgTypes>
161 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) volatile>
162 { typedef _Res result_type; };
164 template<typename _Res, typename _Class, typename... _ArgTypes>
165 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes......) volatile>
166 { typedef _Res result_type; };
168 /// Retrieve result type for a const volatile member function pointer.
169 template<typename _Res, typename _Class, typename... _ArgTypes>
170 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...)
172 { typedef _Res result_type; };
174 template<typename _Res, typename _Class, typename... _ArgTypes>
175 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes......)
177 { typedef _Res result_type; };
180 * Strip top-level cv-qualifiers from the function object and let
181 * _Weak_result_type_impl perform the real work.
183 template<typename _Functor>
184 struct _Weak_result_type
185 : _Weak_result_type_impl<typename remove_cv<_Functor>::type>
188 /// Determines if the type _Tp derives from unary_function.
189 template<typename _Tp>
190 struct _Derives_from_unary_function : __sfinae_types
193 template<typename _T1, typename _Res>
194 static __one __test(const volatile unary_function<_T1, _Res>*);
196 // It's tempting to change "..." to const volatile void*, but
197 // that fails when _Tp is a function type.
198 static __two __test(...);
201 static const bool value = sizeof(__test((_Tp*)0)) == 1;
204 /// Determines if the type _Tp derives from binary_function.
205 template<typename _Tp>
206 struct _Derives_from_binary_function : __sfinae_types
209 template<typename _T1, typename _T2, typename _Res>
210 static __one __test(const volatile binary_function<_T1, _T2, _Res>*);
212 // It's tempting to change "..." to const volatile void*, but
213 // that fails when _Tp is a function type.
214 static __two __test(...);
217 static const bool value = sizeof(__test((_Tp*)0)) == 1;
221 * Invoke a function object, which may be either a member pointer or a
222 * function object. The first parameter will tell which.
224 template<typename _Functor, typename... _Args>
227 (!is_member_pointer<_Functor>::value
228 && !is_function<_Functor>::value
229 && !is_function<typename remove_pointer<_Functor>::type>::value),
230 typename result_of<_Functor&(_Args&&...)>::type
232 __invoke(_Functor& __f, _Args&&... __args)
234 return __f(std::forward<_Args>(__args)...);
237 template<typename _Functor, typename... _Args>
240 (is_member_pointer<_Functor>::value
241 && !is_function<_Functor>::value
242 && !is_function<typename remove_pointer<_Functor>::type>::value),
243 typename result_of<_Functor(_Args&&...)>::type
245 __invoke(_Functor& __f, _Args&&... __args)
247 return std::mem_fn(__f)(std::forward<_Args>(__args)...);
250 // To pick up function references (that will become function pointers)
251 template<typename _Functor, typename... _Args>
254 (is_pointer<_Functor>::value
255 && is_function<typename remove_pointer<_Functor>::type>::value),
256 typename result_of<_Functor(_Args&&...)>::type
258 __invoke(_Functor __f, _Args&&... __args)
260 return __f(std::forward<_Args>(__args)...);
264 * Knowing which of unary_function and binary_function _Tp derives
265 * from, derives from the same and ensures that reference_wrapper
266 * will have a weak result type. See cases below.
268 template<bool _Unary, bool _Binary, typename _Tp>
269 struct _Reference_wrapper_base_impl;
271 // None of the nested argument types.
272 template<typename _Tp>
273 struct _Reference_wrapper_base_impl<false, false, _Tp>
274 : _Weak_result_type<_Tp>
277 // Nested argument_type only.
278 template<typename _Tp>
279 struct _Reference_wrapper_base_impl<true, false, _Tp>
280 : _Weak_result_type<_Tp>
282 typedef typename _Tp::argument_type argument_type;
285 // Nested first_argument_type and second_argument_type only.
286 template<typename _Tp>
287 struct _Reference_wrapper_base_impl<false, true, _Tp>
288 : _Weak_result_type<_Tp>
290 typedef typename _Tp::first_argument_type first_argument_type;
291 typedef typename _Tp::second_argument_type second_argument_type;
294 // All the nested argument types.
295 template<typename _Tp>
296 struct _Reference_wrapper_base_impl<true, true, _Tp>
297 : _Weak_result_type<_Tp>
299 typedef typename _Tp::argument_type argument_type;
300 typedef typename _Tp::first_argument_type first_argument_type;
301 typedef typename _Tp::second_argument_type second_argument_type;
304 _GLIBCXX_HAS_NESTED_TYPE(argument_type)
305 _GLIBCXX_HAS_NESTED_TYPE(first_argument_type)
306 _GLIBCXX_HAS_NESTED_TYPE(second_argument_type)
309 * Derives from unary_function or binary_function when it
310 * can. Specializations handle all of the easy cases. The primary
311 * template determines what to do with a class type, which may
312 * derive from both unary_function and binary_function.
314 template<typename _Tp>
315 struct _Reference_wrapper_base
316 : _Reference_wrapper_base_impl<
317 __has_argument_type<_Tp>::value,
318 __has_first_argument_type<_Tp>::value
319 && __has_second_argument_type<_Tp>::value,
323 // - a function type (unary)
324 template<typename _Res, typename _T1>
325 struct _Reference_wrapper_base<_Res(_T1)>
326 : unary_function<_T1, _Res>
329 template<typename _Res, typename _T1>
330 struct _Reference_wrapper_base<_Res(_T1) const>
331 : unary_function<_T1, _Res>
334 template<typename _Res, typename _T1>
335 struct _Reference_wrapper_base<_Res(_T1) volatile>
336 : unary_function<_T1, _Res>
339 template<typename _Res, typename _T1>
340 struct _Reference_wrapper_base<_Res(_T1) const volatile>
341 : unary_function<_T1, _Res>
344 // - a function type (binary)
345 template<typename _Res, typename _T1, typename _T2>
346 struct _Reference_wrapper_base<_Res(_T1, _T2)>
347 : binary_function<_T1, _T2, _Res>
350 template<typename _Res, typename _T1, typename _T2>
351 struct _Reference_wrapper_base<_Res(_T1, _T2) const>
352 : binary_function<_T1, _T2, _Res>
355 template<typename _Res, typename _T1, typename _T2>
356 struct _Reference_wrapper_base<_Res(_T1, _T2) volatile>
357 : binary_function<_T1, _T2, _Res>
360 template<typename _Res, typename _T1, typename _T2>
361 struct _Reference_wrapper_base<_Res(_T1, _T2) const volatile>
362 : binary_function<_T1, _T2, _Res>
365 // - a function pointer type (unary)
366 template<typename _Res, typename _T1>
367 struct _Reference_wrapper_base<_Res(*)(_T1)>
368 : unary_function<_T1, _Res>
371 // - a function pointer type (binary)
372 template<typename _Res, typename _T1, typename _T2>
373 struct _Reference_wrapper_base<_Res(*)(_T1, _T2)>
374 : binary_function<_T1, _T2, _Res>
377 // - a pointer to member function type (unary, no qualifiers)
378 template<typename _Res, typename _T1>
379 struct _Reference_wrapper_base<_Res (_T1::*)()>
380 : unary_function<_T1*, _Res>
383 // - a pointer to member function type (binary, no qualifiers)
384 template<typename _Res, typename _T1, typename _T2>
385 struct _Reference_wrapper_base<_Res (_T1::*)(_T2)>
386 : binary_function<_T1*, _T2, _Res>
389 // - a pointer to member function type (unary, const)
390 template<typename _Res, typename _T1>
391 struct _Reference_wrapper_base<_Res (_T1::*)() const>
392 : unary_function<const _T1*, _Res>
395 // - a pointer to member function type (binary, const)
396 template<typename _Res, typename _T1, typename _T2>
397 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const>
398 : binary_function<const _T1*, _T2, _Res>
401 // - a pointer to member function type (unary, volatile)
402 template<typename _Res, typename _T1>
403 struct _Reference_wrapper_base<_Res (_T1::*)() volatile>
404 : unary_function<volatile _T1*, _Res>
407 // - a pointer to member function type (binary, volatile)
408 template<typename _Res, typename _T1, typename _T2>
409 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) volatile>
410 : binary_function<volatile _T1*, _T2, _Res>
413 // - a pointer to member function type (unary, const volatile)
414 template<typename _Res, typename _T1>
415 struct _Reference_wrapper_base<_Res (_T1::*)() const volatile>
416 : unary_function<const volatile _T1*, _Res>
419 // - a pointer to member function type (binary, const volatile)
420 template<typename _Res, typename _T1, typename _T2>
421 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const volatile>
422 : binary_function<const volatile _T1*, _T2, _Res>
426 * @brief Primary class template for reference_wrapper.
430 template<typename _Tp>
431 class reference_wrapper
432 : public _Reference_wrapper_base<typename remove_cv<_Tp>::type>
439 reference_wrapper(_Tp& __indata) noexcept
440 : _M_data(std::__addressof(__indata))
443 reference_wrapper(_Tp&&) = delete;
445 reference_wrapper(const reference_wrapper<_Tp>& __inref) noexcept
446 : _M_data(__inref._M_data)
450 operator=(const reference_wrapper<_Tp>& __inref) noexcept
452 _M_data = __inref._M_data;
456 operator _Tp&() const noexcept
457 { return this->get(); }
463 template<typename... _Args>
464 typename result_of<_Tp&(_Args&&...)>::type
465 operator()(_Args&&... __args) const
467 return __invoke(get(), std::forward<_Args>(__args)...);
472 /// Denotes a reference should be taken to a variable.
473 template<typename _Tp>
474 inline reference_wrapper<_Tp>
475 ref(_Tp& __t) noexcept
476 { return reference_wrapper<_Tp>(__t); }
478 /// Denotes a const reference should be taken to a variable.
479 template<typename _Tp>
480 inline reference_wrapper<const _Tp>
481 cref(const _Tp& __t) noexcept
482 { return reference_wrapper<const _Tp>(__t); }
484 template<typename _Tp>
485 void ref(const _Tp&&) = delete;
487 template<typename _Tp>
488 void cref(const _Tp&&) = delete;
490 /// Partial specialization.
491 template<typename _Tp>
492 inline reference_wrapper<_Tp>
493 ref(reference_wrapper<_Tp> __t) noexcept
494 { return ref(__t.get()); }
496 /// Partial specialization.
497 template<typename _Tp>
498 inline reference_wrapper<const _Tp>
499 cref(reference_wrapper<_Tp> __t) noexcept
500 { return cref(__t.get()); }
504 template<typename... _Types>
505 struct _Pack : integral_constant<size_t, sizeof...(_Types)>
508 template<typename _From, typename _To, bool = _From::value == _To::value>
509 struct _AllConvertible : false_type
512 template<typename... _From, typename... _To>
513 struct _AllConvertible<_Pack<_From...>, _Pack<_To...>, true>
514 : __and_<is_convertible<_From, _To>...>
517 template<typename _Tp1, typename _Tp2>
518 using _NotSame = __not_<is_same<typename std::decay<_Tp1>::type,
519 typename std::decay<_Tp2>::type>>;
522 * Derives from @c unary_function or @c binary_function, or perhaps
523 * nothing, depending on the number of arguments provided. The
524 * primary template is the basis case, which derives nothing.
526 template<typename _Res, typename... _ArgTypes>
527 struct _Maybe_unary_or_binary_function { };
529 /// Derives from @c unary_function, as appropriate.
530 template<typename _Res, typename _T1>
531 struct _Maybe_unary_or_binary_function<_Res, _T1>
532 : std::unary_function<_T1, _Res> { };
534 /// Derives from @c binary_function, as appropriate.
535 template<typename _Res, typename _T1, typename _T2>
536 struct _Maybe_unary_or_binary_function<_Res, _T1, _T2>
537 : std::binary_function<_T1, _T2, _Res> { };
539 /// Implementation of @c mem_fn for member function pointers.
540 template<typename _Res, typename _Class, typename... _ArgTypes>
541 class _Mem_fn<_Res (_Class::*)(_ArgTypes...)>
542 : public _Maybe_unary_or_binary_function<_Res, _Class*, _ArgTypes...>
544 typedef _Res (_Class::*_Functor)(_ArgTypes...);
546 template<typename _Tp, typename... _Args>
548 _M_call(_Tp&& __object, const volatile _Class *,
549 _Args&&... __args) const
551 return (std::forward<_Tp>(__object).*__pmf)
552 (std::forward<_Args>(__args)...);
555 template<typename _Tp, typename... _Args>
557 _M_call(_Tp&& __ptr, const volatile void *, _Args&&... __args) const
558 { return ((*__ptr).*__pmf)(std::forward<_Args>(__args)...); }
560 // Require each _Args to be convertible to corresponding _ArgTypes
561 template<typename... _Args>
562 using _RequireValidArgs
563 = _Require<_AllConvertible<_Pack<_Args...>, _Pack<_ArgTypes...>>>;
565 // Require each _Args to be convertible to corresponding _ArgTypes
566 // and require _Tp is not _Class, _Class& or _Class*
567 template<typename _Tp, typename... _Args>
568 using _RequireValidArgs2
569 = _Require<_NotSame<_Class, _Tp>, _NotSame<_Class*, _Tp>,
570 _AllConvertible<_Pack<_Args...>, _Pack<_ArgTypes...>>>;
572 // Require each _Args to be convertible to corresponding _ArgTypes
573 // and require _Tp is _Class or derived from _Class
574 template<typename _Tp, typename... _Args>
575 using _RequireValidArgs3
576 = _Require<is_base_of<_Class, _Tp>,
577 _AllConvertible<_Pack<_Args...>, _Pack<_ArgTypes...>>>;
580 typedef _Res result_type;
582 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
585 template<typename... _Args, typename _Req = _RequireValidArgs<_Args...>>
587 operator()(_Class& __object, _Args&&... __args) const
588 { return (__object.*__pmf)(std::forward<_Args>(__args)...); }
590 template<typename... _Args, typename _Req = _RequireValidArgs<_Args...>>
592 operator()(_Class&& __object, _Args&&... __args) const
594 return (std::move(__object).*__pmf)(std::forward<_Args>(__args)...);
598 template<typename... _Args, typename _Req = _RequireValidArgs<_Args...>>
600 operator()(_Class* __object, _Args&&... __args) const
601 { return (__object->*__pmf)(std::forward<_Args>(__args)...); }
603 // Handle smart pointers, references and pointers to derived
604 template<typename _Tp, typename... _Args,
605 typename _Req = _RequireValidArgs2<_Tp, _Args...>>
607 operator()(_Tp&& __object, _Args&&... __args) const
609 return _M_call(std::forward<_Tp>(__object), &__object,
610 std::forward<_Args>(__args)...);
613 template<typename _Tp, typename... _Args,
614 typename _Req = _RequireValidArgs3<_Tp, _Args...>>
616 operator()(reference_wrapper<_Tp> __ref, _Args&&... __args) const
617 { return operator()(__ref.get(), std::forward<_Args>(__args)...); }
623 /// Implementation of @c mem_fn for const member function pointers.
624 template<typename _Res, typename _Class, typename... _ArgTypes>
625 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) const>
626 : public _Maybe_unary_or_binary_function<_Res, const _Class*,
629 typedef _Res (_Class::*_Functor)(_ArgTypes...) const;
631 template<typename _Tp, typename... _Args>
633 _M_call(_Tp&& __object, const volatile _Class *,
634 _Args&&... __args) const
636 return (std::forward<_Tp>(__object).*__pmf)
637 (std::forward<_Args>(__args)...);
640 template<typename _Tp, typename... _Args>
642 _M_call(_Tp&& __ptr, const volatile void *, _Args&&... __args) const
643 { return ((*__ptr).*__pmf)(std::forward<_Args>(__args)...); }
645 template<typename... _Args>
646 using _RequireValidArgs
647 = _Require<_AllConvertible<_Pack<_Args...>, _Pack<_ArgTypes...>>>;
649 template<typename _Tp, typename... _Args>
650 using _RequireValidArgs2
651 = _Require<_NotSame<_Class, _Tp>, _NotSame<const _Class*, _Tp>,
652 _AllConvertible<_Pack<_Args...>, _Pack<_ArgTypes...>>>;
654 template<typename _Tp, typename... _Args>
655 using _RequireValidArgs3
656 = _Require<is_base_of<_Class, _Tp>,
657 _AllConvertible<_Pack<_Args...>, _Pack<_ArgTypes...>>>;
660 typedef _Res result_type;
662 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
665 template<typename... _Args, typename _Req = _RequireValidArgs<_Args...>>
667 operator()(const _Class& __object, _Args&&... __args) const
668 { return (__object.*__pmf)(std::forward<_Args>(__args)...); }
670 template<typename... _Args, typename _Req = _RequireValidArgs<_Args...>>
672 operator()(const _Class&& __object, _Args&&... __args) const
674 return (std::move(__object).*__pmf)(std::forward<_Args>(__args)...);
678 template<typename... _Args, typename _Req = _RequireValidArgs<_Args...>>
680 operator()(const _Class* __object, _Args&&... __args) const
681 { return (__object->*__pmf)(std::forward<_Args>(__args)...); }
683 // Handle smart pointers, references and pointers to derived
684 template<typename _Tp, typename... _Args,
685 typename _Req = _RequireValidArgs2<_Tp, _Args...>>
686 _Res operator()(_Tp&& __object, _Args&&... __args) const
688 return _M_call(std::forward<_Tp>(__object), &__object,
689 std::forward<_Args>(__args)...);
692 template<typename _Tp, typename... _Args,
693 typename _Req = _RequireValidArgs3<_Tp, _Args...>>
695 operator()(reference_wrapper<_Tp> __ref, _Args&&... __args) const
696 { return operator()(__ref.get(), std::forward<_Args>(__args)...); }
702 /// Implementation of @c mem_fn for volatile member function pointers.
703 template<typename _Res, typename _Class, typename... _ArgTypes>
704 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) volatile>
705 : public _Maybe_unary_or_binary_function<_Res, volatile _Class*,
708 typedef _Res (_Class::*_Functor)(_ArgTypes...) volatile;
710 template<typename _Tp, typename... _Args>
712 _M_call(_Tp&& __object, const volatile _Class *,
713 _Args&&... __args) const
715 return (std::forward<_Tp>(__object).*__pmf)
716 (std::forward<_Args>(__args)...);
719 template<typename _Tp, typename... _Args>
721 _M_call(_Tp&& __ptr, const volatile void *, _Args&&... __args) const
722 { return ((*__ptr).*__pmf)(std::forward<_Args>(__args)...); }
724 template<typename... _Args>
725 using _RequireValidArgs
726 = _Require<_AllConvertible<_Pack<_Args...>, _Pack<_ArgTypes...>>>;
728 template<typename _Tp, typename... _Args>
729 using _RequireValidArgs2
730 = _Require<_NotSame<_Class, _Tp>, _NotSame<volatile _Class*, _Tp>,
731 _AllConvertible<_Pack<_Args...>, _Pack<_ArgTypes...>>>;
733 template<typename _Tp, typename... _Args>
734 using _RequireValidArgs3
735 = _Require<is_base_of<_Class, _Tp>,
736 _AllConvertible<_Pack<_Args...>, _Pack<_ArgTypes...>>>;
739 typedef _Res result_type;
741 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
744 template<typename... _Args, typename _Req = _RequireValidArgs<_Args...>>
746 operator()(volatile _Class& __object, _Args&&... __args) const
747 { return (__object.*__pmf)(std::forward<_Args>(__args)...); }
749 template<typename... _Args, typename _Req = _RequireValidArgs<_Args...>>
751 operator()(volatile _Class&& __object, _Args&&... __args) const
753 return (std::move(__object).*__pmf)(std::forward<_Args>(__args)...);
757 template<typename... _Args, typename _Req = _RequireValidArgs<_Args...>>
759 operator()(volatile _Class* __object, _Args&&... __args) const
760 { return (__object->*__pmf)(std::forward<_Args>(__args)...); }
762 // Handle smart pointers, references and pointers to derived
763 template<typename _Tp, typename... _Args,
764 typename _Req = _RequireValidArgs2<_Tp, _Args...>>
766 operator()(_Tp&& __object, _Args&&... __args) const
768 return _M_call(std::forward<_Tp>(__object), &__object,
769 std::forward<_Args>(__args)...);
772 template<typename _Tp, typename... _Args,
773 typename _Req = _RequireValidArgs3<_Tp, _Args...>>
775 operator()(reference_wrapper<_Tp> __ref, _Args&&... __args) const
776 { return operator()(__ref.get(), std::forward<_Args>(__args)...); }
782 /// Implementation of @c mem_fn for const volatile member function pointers.
783 template<typename _Res, typename _Class, typename... _ArgTypes>
784 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) const volatile>
785 : public _Maybe_unary_or_binary_function<_Res, const volatile _Class*,
788 typedef _Res (_Class::*_Functor)(_ArgTypes...) const volatile;
790 template<typename _Tp, typename... _Args>
792 _M_call(_Tp&& __object, const volatile _Class *,
793 _Args&&... __args) const
795 return (std::forward<_Tp>(__object).*__pmf)
796 (std::forward<_Args>(__args)...);
799 template<typename _Tp, typename... _Args>
801 _M_call(_Tp&& __ptr, const volatile void *, _Args&&... __args) const
802 { return ((*__ptr).*__pmf)(std::forward<_Args>(__args)...); }
804 template<typename... _Args>
805 using _RequireValidArgs
806 = _Require<_AllConvertible<_Pack<_Args...>, _Pack<_ArgTypes...>>>;
808 template<typename _Tp, typename... _Args>
809 using _RequireValidArgs2
810 = _Require<_NotSame<_Class, _Tp>,
811 _NotSame<const volatile _Class*, _Tp>,
812 _AllConvertible<_Pack<_Args...>, _Pack<_ArgTypes...>>>;
814 template<typename _Tp, typename... _Args>
815 using _RequireValidArgs3
816 = _Require<is_base_of<_Class, _Tp>,
817 _AllConvertible<_Pack<_Args...>, _Pack<_ArgTypes...>>>;
820 typedef _Res result_type;
822 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
825 template<typename... _Args, typename _Req = _RequireValidArgs<_Args...>>
827 operator()(const volatile _Class& __object, _Args&&... __args) const
828 { return (__object.*__pmf)(std::forward<_Args>(__args)...); }
830 template<typename... _Args, typename _Req = _RequireValidArgs<_Args...>>
832 operator()(const volatile _Class&& __object, _Args&&... __args) const
834 return (std::move(__object).*__pmf)(std::forward<_Args>(__args)...);
838 template<typename... _Args, typename _Req = _RequireValidArgs<_Args...>>
840 operator()(const volatile _Class* __object, _Args&&... __args) const
841 { return (__object->*__pmf)(std::forward<_Args>(__args)...); }
843 // Handle smart pointers, references and pointers to derived
844 template<typename _Tp, typename... _Args,
845 typename _Req = _RequireValidArgs2<_Tp, _Args...>>
846 _Res operator()(_Tp&& __object, _Args&&... __args) const
848 return _M_call(std::forward<_Tp>(__object), &__object,
849 std::forward<_Args>(__args)...);
852 template<typename _Tp, typename... _Args,
853 typename _Req = _RequireValidArgs3<_Tp, _Args...>>
855 operator()(reference_wrapper<_Tp> __ref, _Args&&... __args) const
856 { return operator()(__ref.get(), std::forward<_Args>(__args)...); }
863 template<typename _Tp, bool>
864 struct _Mem_fn_const_or_non
866 typedef const _Tp& type;
869 template<typename _Tp>
870 struct _Mem_fn_const_or_non<_Tp, false>
875 template<typename _Res, typename _Class>
876 class _Mem_fn<_Res _Class::*>
878 using __pm_type = _Res _Class::*;
880 // This bit of genius is due to Peter Dimov, improved slightly by
882 // Made less elegant to support perfect forwarding and noexcept.
883 template<typename _Tp>
885 _M_call(_Tp&& __object, const _Class *) const noexcept
886 -> decltype(std::forward<_Tp>(__object).*std::declval<__pm_type&>())
887 { return std::forward<_Tp>(__object).*__pm; }
889 template<typename _Tp, typename _Up>
891 _M_call(_Tp&& __object, _Up * const *) const noexcept
892 -> decltype((*std::forward<_Tp>(__object)).*std::declval<__pm_type&>())
893 { return (*std::forward<_Tp>(__object)).*__pm; }
895 template<typename _Tp>
897 _M_call(_Tp&& __ptr, const volatile void*) const
898 noexcept(noexcept((*__ptr).*std::declval<__pm_type&>()))
899 -> decltype((*__ptr).*std::declval<__pm_type&>())
900 { return (*__ptr).*__pm; }
904 _Mem_fn(_Res _Class::*__pm) noexcept : __pm(__pm) { }
908 operator()(_Class& __object) const noexcept
909 { return __object.*__pm; }
912 operator()(const _Class& __object) const noexcept
913 { return __object.*__pm; }
916 operator()(_Class&& __object) const noexcept
917 { return std::forward<_Class>(__object).*__pm; }
920 operator()(const _Class&& __object) const noexcept
921 { return std::forward<const _Class>(__object).*__pm; }
925 operator()(_Class* __object) const noexcept
926 { return __object->*__pm; }
929 operator()(const _Class* __object) const noexcept
930 { return __object->*__pm; }
932 // Handle smart pointers and derived
933 template<typename _Tp, typename _Req = _Require<_NotSame<_Class*, _Tp>>>
935 operator()(_Tp&& __unknown) const
936 noexcept(noexcept(std::declval<_Mem_fn*>()->_M_call
937 (std::forward<_Tp>(__unknown), &__unknown)))
938 -> decltype(this->_M_call(std::forward<_Tp>(__unknown), &__unknown))
939 { return _M_call(std::forward<_Tp>(__unknown), &__unknown); }
941 template<typename _Tp, typename _Req = _Require<is_base_of<_Class, _Tp>>>
943 operator()(reference_wrapper<_Tp> __ref) const
944 noexcept(noexcept(std::declval<_Mem_fn&>()(__ref.get())))
945 -> decltype((*this)(__ref.get()))
946 { return (*this)(__ref.get()); }
952 // _GLIBCXX_RESOLVE_LIB_DEFECTS
953 // 2048. Unnecessary mem_fn overloads
955 * @brief Returns a function object that forwards to the member
959 template<typename _Tp, typename _Class>
960 inline _Mem_fn<_Tp _Class::*>
961 mem_fn(_Tp _Class::* __pm) noexcept
963 return _Mem_fn<_Tp _Class::*>(__pm);
967 * @brief Determines if the given type _Tp is a function object
968 * should be treated as a subexpression when evaluating calls to
969 * function objects returned by bind(). [TR1 3.6.1]
972 template<typename _Tp>
973 struct is_bind_expression
974 : public false_type { };
977 * @brief Determines if the given type _Tp is a placeholder in a
978 * bind() expression and, if so, which placeholder it is. [TR1 3.6.2]
981 template<typename _Tp>
982 struct is_placeholder
983 : public integral_constant<int, 0>
986 /** @brief The type of placeholder objects defined by libstdc++.
989 template<int _Num> struct _Placeholder { };
991 _GLIBCXX_END_NAMESPACE_VERSION
993 /** @namespace std::placeholders
994 * @brief ISO C++11 entities sub-namespace for functional.
997 namespace placeholders
999 _GLIBCXX_BEGIN_NAMESPACE_VERSION
1000 /* Define a large number of placeholders. There is no way to
1001 * simplify this with variadic templates, because we're introducing
1002 * unique names for each.
1004 extern const _Placeholder<1> _1;
1005 extern const _Placeholder<2> _2;
1006 extern const _Placeholder<3> _3;
1007 extern const _Placeholder<4> _4;
1008 extern const _Placeholder<5> _5;
1009 extern const _Placeholder<6> _6;
1010 extern const _Placeholder<7> _7;
1011 extern const _Placeholder<8> _8;
1012 extern const _Placeholder<9> _9;
1013 extern const _Placeholder<10> _10;
1014 extern const _Placeholder<11> _11;
1015 extern const _Placeholder<12> _12;
1016 extern const _Placeholder<13> _13;
1017 extern const _Placeholder<14> _14;
1018 extern const _Placeholder<15> _15;
1019 extern const _Placeholder<16> _16;
1020 extern const _Placeholder<17> _17;
1021 extern const _Placeholder<18> _18;
1022 extern const _Placeholder<19> _19;
1023 extern const _Placeholder<20> _20;
1024 extern const _Placeholder<21> _21;
1025 extern const _Placeholder<22> _22;
1026 extern const _Placeholder<23> _23;
1027 extern const _Placeholder<24> _24;
1028 extern const _Placeholder<25> _25;
1029 extern const _Placeholder<26> _26;
1030 extern const _Placeholder<27> _27;
1031 extern const _Placeholder<28> _28;
1032 extern const _Placeholder<29> _29;
1033 _GLIBCXX_END_NAMESPACE_VERSION
1036 _GLIBCXX_BEGIN_NAMESPACE_VERSION
1039 * Partial specialization of is_placeholder that provides the placeholder
1040 * number for the placeholder objects defined by libstdc++.
1044 struct is_placeholder<_Placeholder<_Num> >
1045 : public integral_constant<int, _Num>
1049 struct is_placeholder<const _Placeholder<_Num> >
1050 : public integral_constant<int, _Num>
1054 * Used by _Safe_tuple_element to indicate that there is no tuple
1055 * element at this position.
1057 struct _No_tuple_element;
1060 * Implementation helper for _Safe_tuple_element. This primary
1061 * template handles the case where it is safe to use @c
1064 template<std::size_t __i, typename _Tuple, bool _IsSafe>
1065 struct _Safe_tuple_element_impl
1066 : tuple_element<__i, _Tuple> { };
1069 * Implementation helper for _Safe_tuple_element. This partial
1070 * specialization handles the case where it is not safe to use @c
1071 * tuple_element. We just return @c _No_tuple_element.
1073 template<std::size_t __i, typename _Tuple>
1074 struct _Safe_tuple_element_impl<__i, _Tuple, false>
1076 typedef _No_tuple_element type;
1080 * Like tuple_element, but returns @c _No_tuple_element when
1081 * tuple_element would return an error.
1083 template<std::size_t __i, typename _Tuple>
1084 struct _Safe_tuple_element
1085 : _Safe_tuple_element_impl<__i, _Tuple,
1086 (__i < tuple_size<_Tuple>::value)>
1090 * Maps an argument to bind() into an actual argument to the bound
1091 * function object [TR1 3.6.3/5]. Only the first parameter should
1092 * be specified: the rest are used to determine among the various
1093 * implementations. Note that, although this class is a function
1094 * object, it isn't entirely normal because it takes only two
1095 * parameters regardless of the number of parameters passed to the
1096 * bind expression. The first parameter is the bound argument and
1097 * the second parameter is a tuple containing references to the
1098 * rest of the arguments.
1100 template<typename _Arg,
1101 bool _IsBindExp = is_bind_expression<_Arg>::value,
1102 bool _IsPlaceholder = (is_placeholder<_Arg>::value > 0)>
1106 * If the argument is reference_wrapper<_Tp>, returns the
1107 * underlying reference. [TR1 3.6.3/5 bullet 1]
1109 template<typename _Tp>
1110 class _Mu<reference_wrapper<_Tp>, false, false>
1113 typedef _Tp& result_type;
1115 /* Note: This won't actually work for const volatile
1116 * reference_wrappers, because reference_wrapper::get() is const
1117 * but not volatile-qualified. This might be a defect in the TR.
1119 template<typename _CVRef, typename _Tuple>
1121 operator()(_CVRef& __arg, _Tuple&) const volatile
1122 { return __arg.get(); }
1126 * If the argument is a bind expression, we invoke the underlying
1127 * function object with the same cv-qualifiers as we are given and
1128 * pass along all of our arguments (unwrapped). [TR1 3.6.3/5 bullet 2]
1130 template<typename _Arg>
1131 class _Mu<_Arg, true, false>
1134 template<typename _CVArg, typename... _Args>
1136 operator()(_CVArg& __arg,
1137 tuple<_Args...>& __tuple) const volatile
1138 -> decltype(__arg(declval<_Args>()...))
1140 // Construct an index tuple and forward to __call
1141 typedef typename _Build_index_tuple<sizeof...(_Args)>::__type
1143 return this->__call(__arg, __tuple, _Indexes());
1147 // Invokes the underlying function object __arg by unpacking all
1148 // of the arguments in the tuple.
1149 template<typename _CVArg, typename... _Args, std::size_t... _Indexes>
1151 __call(_CVArg& __arg, tuple<_Args...>& __tuple,
1152 const _Index_tuple<_Indexes...>&) const volatile
1153 -> decltype(__arg(declval<_Args>()...))
1155 return __arg(std::forward<_Args>(get<_Indexes>(__tuple))...);
1160 * If the argument is a placeholder for the Nth argument, returns
1161 * a reference to the Nth argument to the bind function object.
1162 * [TR1 3.6.3/5 bullet 3]
1164 template<typename _Arg>
1165 class _Mu<_Arg, false, true>
1168 template<typename _Signature> class result;
1170 template<typename _CVMu, typename _CVArg, typename _Tuple>
1171 class result<_CVMu(_CVArg, _Tuple)>
1173 // Add a reference, if it hasn't already been done for us.
1174 // This allows us to be a little bit sloppy in constructing
1175 // the tuple that we pass to result_of<...>.
1176 typedef typename _Safe_tuple_element<(is_placeholder<_Arg>::value
1181 typedef typename add_rvalue_reference<__base_type>::type type;
1184 template<typename _Tuple>
1185 typename result<_Mu(_Arg, _Tuple)>::type
1186 operator()(const volatile _Arg&, _Tuple& __tuple) const volatile
1188 return std::forward<typename result<_Mu(_Arg, _Tuple)>::type>(
1189 ::std::get<(is_placeholder<_Arg>::value - 1)>(__tuple));
1194 * If the argument is just a value, returns a reference to that
1195 * value. The cv-qualifiers on the reference are the same as the
1196 * cv-qualifiers on the _Mu object. [TR1 3.6.3/5 bullet 4]
1198 template<typename _Arg>
1199 class _Mu<_Arg, false, false>
1202 template<typename _Signature> struct result;
1204 template<typename _CVMu, typename _CVArg, typename _Tuple>
1205 struct result<_CVMu(_CVArg, _Tuple)>
1207 typedef typename add_lvalue_reference<_CVArg>::type type;
1210 // Pick up the cv-qualifiers of the argument
1211 template<typename _CVArg, typename _Tuple>
1213 operator()(_CVArg&& __arg, _Tuple&) const volatile
1214 { return std::forward<_CVArg>(__arg); }
1218 * Maps member pointers into instances of _Mem_fn but leaves all
1219 * other function objects untouched. Used by tr1::bind(). The
1220 * primary template handles the non--member-pointer case.
1222 template<typename _Tp>
1223 struct _Maybe_wrap_member_pointer
1228 __do_wrap(const _Tp& __x)
1232 __do_wrap(_Tp&& __x)
1233 { return static_cast<_Tp&&>(__x); }
1237 * Maps member pointers into instances of _Mem_fn but leaves all
1238 * other function objects untouched. Used by tr1::bind(). This
1239 * partial specialization handles the member pointer case.
1241 template<typename _Tp, typename _Class>
1242 struct _Maybe_wrap_member_pointer<_Tp _Class::*>
1244 typedef _Mem_fn<_Tp _Class::*> type;
1247 __do_wrap(_Tp _Class::* __pm)
1248 { return type(__pm); }
1251 // Specialization needed to prevent "forming reference to void" errors when
1252 // bind<void>() is called, because argument deduction instantiates
1253 // _Maybe_wrap_member_pointer<void> outside the immediate context where
1256 struct _Maybe_wrap_member_pointer<void>
1261 // std::get<I> for volatile-qualified tuples
1262 template<std::size_t _Ind, typename... _Tp>
1264 __volget(volatile tuple<_Tp...>& __tuple)
1265 -> typename tuple_element<_Ind, tuple<_Tp...>>::type volatile&
1266 { return std::get<_Ind>(const_cast<tuple<_Tp...>&>(__tuple)); }
1268 // std::get<I> for const-volatile-qualified tuples
1269 template<std::size_t _Ind, typename... _Tp>
1271 __volget(const volatile tuple<_Tp...>& __tuple)
1272 -> typename tuple_element<_Ind, tuple<_Tp...>>::type const volatile&
1273 { return std::get<_Ind>(const_cast<const tuple<_Tp...>&>(__tuple)); }
1275 /// Type of the function object returned from bind().
1276 template<typename _Signature>
1279 template<typename _Functor, typename... _Bound_args>
1280 class _Bind<_Functor(_Bound_args...)>
1281 : public _Weak_result_type<_Functor>
1283 typedef _Bind __self_type;
1284 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
1288 tuple<_Bound_args...> _M_bound_args;
1291 template<typename _Result, typename... _Args, std::size_t... _Indexes>
1293 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>)
1295 return _M_f(_Mu<_Bound_args>()
1296 (get<_Indexes>(_M_bound_args), __args)...);
1300 template<typename _Result, typename... _Args, std::size_t... _Indexes>
1302 __call_c(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>) const
1304 return _M_f(_Mu<_Bound_args>()
1305 (get<_Indexes>(_M_bound_args), __args)...);
1309 template<typename _Result, typename... _Args, std::size_t... _Indexes>
1311 __call_v(tuple<_Args...>&& __args,
1312 _Index_tuple<_Indexes...>) volatile
1314 return _M_f(_Mu<_Bound_args>()
1315 (__volget<_Indexes>(_M_bound_args), __args)...);
1318 // Call as const volatile
1319 template<typename _Result, typename... _Args, std::size_t... _Indexes>
1321 __call_c_v(tuple<_Args...>&& __args,
1322 _Index_tuple<_Indexes...>) const volatile
1324 return _M_f(_Mu<_Bound_args>()
1325 (__volget<_Indexes>(_M_bound_args), __args)...);
1329 template<typename... _Args>
1330 explicit _Bind(const _Functor& __f, _Args&&... __args)
1331 : _M_f(__f), _M_bound_args(std::forward<_Args>(__args)...)
1334 template<typename... _Args>
1335 explicit _Bind(_Functor&& __f, _Args&&... __args)
1336 : _M_f(std::move(__f)), _M_bound_args(std::forward<_Args>(__args)...)
1339 _Bind(const _Bind&) = default;
1342 : _M_f(std::move(__b._M_f)), _M_bound_args(std::move(__b._M_bound_args))
1346 template<typename... _Args, typename _Result
1347 = decltype( std::declval<_Functor>()(
1348 _Mu<_Bound_args>()( std::declval<_Bound_args&>(),
1349 std::declval<tuple<_Args...>&>() )... ) )>
1351 operator()(_Args&&... __args)
1353 return this->__call<_Result>(
1354 std::forward_as_tuple(std::forward<_Args>(__args)...),
1359 template<typename... _Args, typename _Result
1360 = decltype( std::declval<typename enable_if<(sizeof...(_Args) >= 0),
1361 typename add_const<_Functor>::type>::type>()(
1362 _Mu<_Bound_args>()( std::declval<const _Bound_args&>(),
1363 std::declval<tuple<_Args...>&>() )... ) )>
1365 operator()(_Args&&... __args) const
1367 return this->__call_c<_Result>(
1368 std::forward_as_tuple(std::forward<_Args>(__args)...),
1373 template<typename... _Args, typename _Result
1374 = decltype( std::declval<typename enable_if<(sizeof...(_Args) >= 0),
1375 typename add_volatile<_Functor>::type>::type>()(
1376 _Mu<_Bound_args>()( std::declval<volatile _Bound_args&>(),
1377 std::declval<tuple<_Args...>&>() )... ) )>
1379 operator()(_Args&&... __args) volatile
1381 return this->__call_v<_Result>(
1382 std::forward_as_tuple(std::forward<_Args>(__args)...),
1386 // Call as const volatile
1387 template<typename... _Args, typename _Result
1388 = decltype( std::declval<typename enable_if<(sizeof...(_Args) >= 0),
1389 typename add_cv<_Functor>::type>::type>()(
1390 _Mu<_Bound_args>()( std::declval<const volatile _Bound_args&>(),
1391 std::declval<tuple<_Args...>&>() )... ) )>
1393 operator()(_Args&&... __args) const volatile
1395 return this->__call_c_v<_Result>(
1396 std::forward_as_tuple(std::forward<_Args>(__args)...),
1401 /// Type of the function object returned from bind<R>().
1402 template<typename _Result, typename _Signature>
1403 struct _Bind_result;
1405 template<typename _Result, typename _Functor, typename... _Bound_args>
1406 class _Bind_result<_Result, _Functor(_Bound_args...)>
1408 typedef _Bind_result __self_type;
1409 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
1413 tuple<_Bound_args...> _M_bound_args;
1416 template<typename _Res>
1417 struct __enable_if_void : enable_if<is_void<_Res>::value, int> { };
1418 template<typename _Res>
1419 struct __disable_if_void : enable_if<!is_void<_Res>::value, int> { };
1422 template<typename _Res, typename... _Args, std::size_t... _Indexes>
1424 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1425 typename __disable_if_void<_Res>::type = 0)
1427 return _M_f(_Mu<_Bound_args>()
1428 (get<_Indexes>(_M_bound_args), __args)...);
1431 // Call unqualified, return void
1432 template<typename _Res, typename... _Args, std::size_t... _Indexes>
1434 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1435 typename __enable_if_void<_Res>::type = 0)
1437 _M_f(_Mu<_Bound_args>()
1438 (get<_Indexes>(_M_bound_args), __args)...);
1442 template<typename _Res, typename... _Args, std::size_t... _Indexes>
1444 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1445 typename __disable_if_void<_Res>::type = 0) const
1447 return _M_f(_Mu<_Bound_args>()
1448 (get<_Indexes>(_M_bound_args), __args)...);
1451 // Call as const, return void
1452 template<typename _Res, typename... _Args, std::size_t... _Indexes>
1454 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1455 typename __enable_if_void<_Res>::type = 0) const
1457 _M_f(_Mu<_Bound_args>()
1458 (get<_Indexes>(_M_bound_args), __args)...);
1462 template<typename _Res, typename... _Args, std::size_t... _Indexes>
1464 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1465 typename __disable_if_void<_Res>::type = 0) volatile
1467 return _M_f(_Mu<_Bound_args>()
1468 (__volget<_Indexes>(_M_bound_args), __args)...);
1471 // Call as volatile, return void
1472 template<typename _Res, typename... _Args, std::size_t... _Indexes>
1474 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1475 typename __enable_if_void<_Res>::type = 0) volatile
1477 _M_f(_Mu<_Bound_args>()
1478 (__volget<_Indexes>(_M_bound_args), __args)...);
1481 // Call as const volatile
1482 template<typename _Res, typename... _Args, std::size_t... _Indexes>
1484 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1485 typename __disable_if_void<_Res>::type = 0) const volatile
1487 return _M_f(_Mu<_Bound_args>()
1488 (__volget<_Indexes>(_M_bound_args), __args)...);
1491 // Call as const volatile, return void
1492 template<typename _Res, typename... _Args, std::size_t... _Indexes>
1494 __call(tuple<_Args...>&& __args,
1495 _Index_tuple<_Indexes...>,
1496 typename __enable_if_void<_Res>::type = 0) const volatile
1498 _M_f(_Mu<_Bound_args>()
1499 (__volget<_Indexes>(_M_bound_args), __args)...);
1503 typedef _Result result_type;
1505 template<typename... _Args>
1506 explicit _Bind_result(const _Functor& __f, _Args&&... __args)
1507 : _M_f(__f), _M_bound_args(std::forward<_Args>(__args)...)
1510 template<typename... _Args>
1511 explicit _Bind_result(_Functor&& __f, _Args&&... __args)
1512 : _M_f(std::move(__f)), _M_bound_args(std::forward<_Args>(__args)...)
1515 _Bind_result(const _Bind_result&) = default;
1517 _Bind_result(_Bind_result&& __b)
1518 : _M_f(std::move(__b._M_f)), _M_bound_args(std::move(__b._M_bound_args))
1522 template<typename... _Args>
1524 operator()(_Args&&... __args)
1526 return this->__call<_Result>(
1527 std::forward_as_tuple(std::forward<_Args>(__args)...),
1532 template<typename... _Args>
1534 operator()(_Args&&... __args) const
1536 return this->__call<_Result>(
1537 std::forward_as_tuple(std::forward<_Args>(__args)...),
1542 template<typename... _Args>
1544 operator()(_Args&&... __args) volatile
1546 return this->__call<_Result>(
1547 std::forward_as_tuple(std::forward<_Args>(__args)...),
1551 // Call as const volatile
1552 template<typename... _Args>
1554 operator()(_Args&&... __args) const volatile
1556 return this->__call<_Result>(
1557 std::forward_as_tuple(std::forward<_Args>(__args)...),
1563 * @brief Class template _Bind is always a bind expression.
1566 template<typename _Signature>
1567 struct is_bind_expression<_Bind<_Signature> >
1568 : public true_type { };
1571 * @brief Class template _Bind is always a bind expression.
1574 template<typename _Signature>
1575 struct is_bind_expression<const _Bind<_Signature> >
1576 : public true_type { };
1579 * @brief Class template _Bind is always a bind expression.
1582 template<typename _Signature>
1583 struct is_bind_expression<volatile _Bind<_Signature> >
1584 : public true_type { };
1587 * @brief Class template _Bind is always a bind expression.
1590 template<typename _Signature>
1591 struct is_bind_expression<const volatile _Bind<_Signature>>
1592 : public true_type { };
1595 * @brief Class template _Bind_result is always a bind expression.
1598 template<typename _Result, typename _Signature>
1599 struct is_bind_expression<_Bind_result<_Result, _Signature>>
1600 : public true_type { };
1603 * @brief Class template _Bind_result is always a bind expression.
1606 template<typename _Result, typename _Signature>
1607 struct is_bind_expression<const _Bind_result<_Result, _Signature>>
1608 : public true_type { };
1611 * @brief Class template _Bind_result is always a bind expression.
1614 template<typename _Result, typename _Signature>
1615 struct is_bind_expression<volatile _Bind_result<_Result, _Signature>>
1616 : public true_type { };
1619 * @brief Class template _Bind_result is always a bind expression.
1622 template<typename _Result, typename _Signature>
1623 struct is_bind_expression<const volatile _Bind_result<_Result, _Signature>>
1624 : public true_type { };
1626 // Trait type used to remove std::bind() from overload set via SFINAE
1627 // when first argument has integer type, so that std::bind() will
1628 // not be a better match than ::bind() from the BSD Sockets API.
1629 template<typename _Tp, typename _Tp2 = typename decay<_Tp>::type>
1630 using __is_socketlike = __or_<is_integral<_Tp2>, is_enum<_Tp2>>;
1632 template<bool _SocketLike, typename _Func, typename... _BoundArgs>
1635 typedef _Maybe_wrap_member_pointer<typename decay<_Func>::type>
1637 typedef typename __maybe_type::type __func_type;
1638 typedef _Bind<__func_type(typename decay<_BoundArgs>::type...)> type;
1641 // Partial specialization for is_socketlike == true, does not define
1642 // nested type so std::bind() will not participate in overload resolution
1643 // when the first argument might be a socket file descriptor.
1644 template<typename _Func, typename... _BoundArgs>
1645 struct _Bind_helper<true, _Func, _BoundArgs...>
1649 * @brief Function template for std::bind.
1652 template<typename _Func, typename... _BoundArgs>
1654 _Bind_helper<__is_socketlike<_Func>::value, _Func, _BoundArgs...>::type
1655 bind(_Func&& __f, _BoundArgs&&... __args)
1657 typedef _Bind_helper<false, _Func, _BoundArgs...> __helper_type;
1658 typedef typename __helper_type::__maybe_type __maybe_type;
1659 typedef typename __helper_type::type __result_type;
1660 return __result_type(__maybe_type::__do_wrap(std::forward<_Func>(__f)),
1661 std::forward<_BoundArgs>(__args)...);
1664 template<typename _Result, typename _Func, typename... _BoundArgs>
1665 struct _Bindres_helper
1667 typedef _Maybe_wrap_member_pointer<typename decay<_Func>::type>
1669 typedef typename __maybe_type::type __functor_type;
1670 typedef _Bind_result<_Result,
1671 __functor_type(typename decay<_BoundArgs>::type...)>
1676 * @brief Function template for std::bind<R>.
1679 template<typename _Result, typename _Func, typename... _BoundArgs>
1681 typename _Bindres_helper<_Result, _Func, _BoundArgs...>::type
1682 bind(_Func&& __f, _BoundArgs&&... __args)
1684 typedef _Bindres_helper<_Result, _Func, _BoundArgs...> __helper_type;
1685 typedef typename __helper_type::__maybe_type __maybe_type;
1686 typedef typename __helper_type::type __result_type;
1687 return __result_type(__maybe_type::__do_wrap(std::forward<_Func>(__f)),
1688 std::forward<_BoundArgs>(__args)...);
1691 template<typename _Signature>
1692 struct _Bind_simple;
1694 template<typename _Callable, typename... _Args>
1695 struct _Bind_simple<_Callable(_Args...)>
1697 typedef typename result_of<_Callable(_Args...)>::type result_type;
1699 template<typename... _Args2, typename = typename
1700 enable_if< sizeof...(_Args) == sizeof...(_Args2)>::type>
1702 _Bind_simple(const _Callable& __callable, _Args2&&... __args)
1703 : _M_bound(__callable, std::forward<_Args2>(__args)...)
1706 template<typename... _Args2, typename = typename
1707 enable_if< sizeof...(_Args) == sizeof...(_Args2)>::type>
1709 _Bind_simple(_Callable&& __callable, _Args2&&... __args)
1710 : _M_bound(std::move(__callable), std::forward<_Args2>(__args)...)
1713 _Bind_simple(const _Bind_simple&) = default;
1714 _Bind_simple(_Bind_simple&&) = default;
1719 typedef typename _Build_index_tuple<sizeof...(_Args)>::__type _Indices;
1720 return _M_invoke(_Indices());
1725 template<std::size_t... _Indices>
1726 typename result_of<_Callable(_Args...)>::type
1727 _M_invoke(_Index_tuple<_Indices...>)
1729 // std::bind always forwards bound arguments as lvalues,
1730 // but this type can call functions which only accept rvalues.
1731 return std::forward<_Callable>(std::get<0>(_M_bound))(
1732 std::forward<_Args>(std::get<_Indices+1>(_M_bound))...);
1735 std::tuple<_Callable, _Args...> _M_bound;
1738 template<typename _Func, typename... _BoundArgs>
1739 struct _Bind_simple_helper
1741 typedef _Maybe_wrap_member_pointer<typename decay<_Func>::type>
1743 typedef typename __maybe_type::type __func_type;
1744 typedef _Bind_simple<__func_type(typename decay<_BoundArgs>::type...)>
1748 // Simplified version of std::bind for internal use, without support for
1749 // unbound arguments, placeholders or nested bind expressions.
1750 template<typename _Callable, typename... _Args>
1751 typename _Bind_simple_helper<_Callable, _Args...>::__type
1752 __bind_simple(_Callable&& __callable, _Args&&... __args)
1754 typedef _Bind_simple_helper<_Callable, _Args...> __helper_type;
1755 typedef typename __helper_type::__maybe_type __maybe_type;
1756 typedef typename __helper_type::__type __result_type;
1757 return __result_type(
1758 __maybe_type::__do_wrap( std::forward<_Callable>(__callable)),
1759 std::forward<_Args>(__args)...);
1763 * @brief Exception class thrown when class template function's
1764 * operator() is called with an empty target.
1765 * @ingroup exceptions
1767 class bad_function_call : public std::exception
1770 virtual ~bad_function_call() noexcept;
1772 const char* what() const noexcept;
1776 * Trait identifying "location-invariant" types, meaning that the
1777 * address of the object (or any of its members) will not escape.
1778 * Also implies a trivial copy constructor and assignment operator.
1780 template<typename _Tp>
1781 struct __is_location_invariant
1782 : integral_constant<bool, (is_pointer<_Tp>::value
1783 || is_member_pointer<_Tp>::value)>
1786 class _Undefined_class;
1791 const void* _M_const_object;
1792 void (*_M_function_pointer)();
1793 void (_Undefined_class::*_M_member_pointer)();
1798 void* _M_access() { return &_M_pod_data[0]; }
1799 const void* _M_access() const { return &_M_pod_data[0]; }
1801 template<typename _Tp>
1804 { return *static_cast<_Tp*>(_M_access()); }
1806 template<typename _Tp>
1809 { return *static_cast<const _Tp*>(_M_access()); }
1811 _Nocopy_types _M_unused;
1812 char _M_pod_data[sizeof(_Nocopy_types)];
1815 enum _Manager_operation
1823 // Simple type wrapper that helps avoid annoying const problems
1824 // when casting between void pointers and pointers-to-pointers.
1825 template<typename _Tp>
1826 struct _Simple_type_wrapper
1828 _Simple_type_wrapper(_Tp __value) : __value(__value) { }
1833 template<typename _Tp>
1834 struct __is_location_invariant<_Simple_type_wrapper<_Tp> >
1835 : __is_location_invariant<_Tp>
1838 // Converts a reference to a function object into a callable
1840 template<typename _Functor>
1842 __callable_functor(_Functor& __f)
1845 template<typename _Member, typename _Class>
1846 inline _Mem_fn<_Member _Class::*>
1847 __callable_functor(_Member _Class::* &__p)
1848 { return std::mem_fn(__p); }
1850 template<typename _Member, typename _Class>
1851 inline _Mem_fn<_Member _Class::*>
1852 __callable_functor(_Member _Class::* const &__p)
1853 { return std::mem_fn(__p); }
1855 template<typename _Member, typename _Class>
1856 inline _Mem_fn<_Member _Class::*>
1857 __callable_functor(_Member _Class::* volatile &__p)
1858 { return std::mem_fn(__p); }
1860 template<typename _Member, typename _Class>
1861 inline _Mem_fn<_Member _Class::*>
1862 __callable_functor(_Member _Class::* const volatile &__p)
1863 { return std::mem_fn(__p); }
1865 template<typename _Signature>
1868 /// Base class of all polymorphic function object wrappers.
1869 class _Function_base
1872 static const std::size_t _M_max_size = sizeof(_Nocopy_types);
1873 static const std::size_t _M_max_align = __alignof__(_Nocopy_types);
1875 template<typename _Functor>
1879 static const bool __stored_locally =
1880 (__is_location_invariant<_Functor>::value
1881 && sizeof(_Functor) <= _M_max_size
1882 && __alignof__(_Functor) <= _M_max_align
1883 && (_M_max_align % __alignof__(_Functor) == 0));
1885 typedef integral_constant<bool, __stored_locally> _Local_storage;
1887 // Retrieve a pointer to the function object
1889 _M_get_pointer(const _Any_data& __source)
1891 const _Functor* __ptr =
1892 __stored_locally? std::__addressof(__source._M_access<_Functor>())
1893 /* have stored a pointer */ : __source._M_access<_Functor*>();
1894 return const_cast<_Functor*>(__ptr);
1897 // Clone a location-invariant function object that fits within
1898 // an _Any_data structure.
1900 _M_clone(_Any_data& __dest, const _Any_data& __source, true_type)
1902 new (__dest._M_access()) _Functor(__source._M_access<_Functor>());
1905 // Clone a function object that is not location-invariant or
1906 // that cannot fit into an _Any_data structure.
1908 _M_clone(_Any_data& __dest, const _Any_data& __source, false_type)
1910 __dest._M_access<_Functor*>() =
1911 new _Functor(*__source._M_access<_Functor*>());
1914 // Destroying a location-invariant object may still require
1917 _M_destroy(_Any_data& __victim, true_type)
1919 __victim._M_access<_Functor>().~_Functor();
1922 // Destroying an object located on the heap.
1924 _M_destroy(_Any_data& __victim, false_type)
1926 delete __victim._M_access<_Functor*>();
1931 _M_manager(_Any_data& __dest, const _Any_data& __source,
1932 _Manager_operation __op)
1937 case __get_type_info:
1938 __dest._M_access<const type_info*>() = &typeid(_Functor);
1941 case __get_functor_ptr:
1942 __dest._M_access<_Functor*>() = _M_get_pointer(__source);
1945 case __clone_functor:
1946 _M_clone(__dest, __source, _Local_storage());
1949 case __destroy_functor:
1950 _M_destroy(__dest, _Local_storage());
1957 _M_init_functor(_Any_data& __functor, _Functor&& __f)
1958 { _M_init_functor(__functor, std::move(__f), _Local_storage()); }
1960 template<typename _Signature>
1962 _M_not_empty_function(const function<_Signature>& __f)
1963 { return static_cast<bool>(__f); }
1965 template<typename _Tp>
1967 _M_not_empty_function(_Tp* const& __fp)
1970 template<typename _Class, typename _Tp>
1972 _M_not_empty_function(_Tp _Class::* const& __mp)
1975 template<typename _Tp>
1977 _M_not_empty_function(const _Tp&)
1982 _M_init_functor(_Any_data& __functor, _Functor&& __f, true_type)
1983 { new (__functor._M_access()) _Functor(std::move(__f)); }
1986 _M_init_functor(_Any_data& __functor, _Functor&& __f, false_type)
1987 { __functor._M_access<_Functor*>() = new _Functor(std::move(__f)); }
1990 template<typename _Functor>
1991 class _Ref_manager : public _Base_manager<_Functor*>
1993 typedef _Function_base::_Base_manager<_Functor*> _Base;
1997 _M_manager(_Any_data& __dest, const _Any_data& __source,
1998 _Manager_operation __op)
2003 case __get_type_info:
2004 __dest._M_access<const type_info*>() = &typeid(_Functor);
2007 case __get_functor_ptr:
2008 __dest._M_access<_Functor*>() = *_Base::_M_get_pointer(__source);
2009 return is_const<_Functor>::value;
2013 _Base::_M_manager(__dest, __source, __op);
2019 _M_init_functor(_Any_data& __functor, reference_wrapper<_Functor> __f)
2021 _Base::_M_init_functor(__functor, std::__addressof(__f.get()));
2025 _Function_base() : _M_manager(0) { }
2030 _M_manager(_M_functor, _M_functor, __destroy_functor);
2034 bool _M_empty() const { return !_M_manager; }
2036 typedef bool (*_Manager_type)(_Any_data&, const _Any_data&,
2037 _Manager_operation);
2039 _Any_data _M_functor;
2040 _Manager_type _M_manager;
2043 template<typename _Signature, typename _Functor>
2044 class _Function_handler;
2046 template<typename _Res, typename _Functor, typename... _ArgTypes>
2047 class _Function_handler<_Res(_ArgTypes...), _Functor>
2048 : public _Function_base::_Base_manager<_Functor>
2050 typedef _Function_base::_Base_manager<_Functor> _Base;
2054 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
2056 return (*_Base::_M_get_pointer(__functor))(
2057 std::forward<_ArgTypes>(__args)...);
2061 template<typename _Functor, typename... _ArgTypes>
2062 class _Function_handler<void(_ArgTypes...), _Functor>
2063 : public _Function_base::_Base_manager<_Functor>
2065 typedef _Function_base::_Base_manager<_Functor> _Base;
2069 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
2071 (*_Base::_M_get_pointer(__functor))(
2072 std::forward<_ArgTypes>(__args)...);
2076 template<typename _Res, typename _Functor, typename... _ArgTypes>
2077 class _Function_handler<_Res(_ArgTypes...), reference_wrapper<_Functor> >
2078 : public _Function_base::_Ref_manager<_Functor>
2080 typedef _Function_base::_Ref_manager<_Functor> _Base;
2084 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
2086 return __callable_functor(**_Base::_M_get_pointer(__functor))(
2087 std::forward<_ArgTypes>(__args)...);
2091 template<typename _Functor, typename... _ArgTypes>
2092 class _Function_handler<void(_ArgTypes...), reference_wrapper<_Functor> >
2093 : public _Function_base::_Ref_manager<_Functor>
2095 typedef _Function_base::_Ref_manager<_Functor> _Base;
2099 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
2101 __callable_functor(**_Base::_M_get_pointer(__functor))(
2102 std::forward<_ArgTypes>(__args)...);
2106 template<typename _Class, typename _Member, typename _Res,
2107 typename... _ArgTypes>
2108 class _Function_handler<_Res(_ArgTypes...), _Member _Class::*>
2109 : public _Function_handler<void(_ArgTypes...), _Member _Class::*>
2111 typedef _Function_handler<void(_ArgTypes...), _Member _Class::*>
2116 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
2118 return std::mem_fn(_Base::_M_get_pointer(__functor)->__value)(
2119 std::forward<_ArgTypes>(__args)...);
2123 template<typename _Class, typename _Member, typename... _ArgTypes>
2124 class _Function_handler<void(_ArgTypes...), _Member _Class::*>
2125 : public _Function_base::_Base_manager<
2126 _Simple_type_wrapper< _Member _Class::* > >
2128 typedef _Member _Class::* _Functor;
2129 typedef _Simple_type_wrapper<_Functor> _Wrapper;
2130 typedef _Function_base::_Base_manager<_Wrapper> _Base;
2134 _M_manager(_Any_data& __dest, const _Any_data& __source,
2135 _Manager_operation __op)
2140 case __get_type_info:
2141 __dest._M_access<const type_info*>() = &typeid(_Functor);
2144 case __get_functor_ptr:
2145 __dest._M_access<_Functor*>() =
2146 &_Base::_M_get_pointer(__source)->__value;
2150 _Base::_M_manager(__dest, __source, __op);
2156 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
2158 std::mem_fn(_Base::_M_get_pointer(__functor)->__value)(
2159 std::forward<_ArgTypes>(__args)...);
2163 template<typename _From, typename _To>
2164 using __check_func_return_type
2165 = __or_<is_void<_To>, is_convertible<_From, _To>>;
2168 * @brief Primary class template for std::function.
2171 * Polymorphic function wrapper.
2173 template<typename _Res, typename... _ArgTypes>
2174 class function<_Res(_ArgTypes...)>
2175 : public _Maybe_unary_or_binary_function<_Res, _ArgTypes...>,
2176 private _Function_base
2178 typedef _Res _Signature_type(_ArgTypes...);
2180 template<typename _Functor>
2181 using _Invoke = decltype(__callable_functor(std::declval<_Functor&>())
2182 (std::declval<_ArgTypes>()...) );
2184 // Used so the return type convertibility checks aren't done when
2185 // performing overload resolution for copy construction/assignment.
2186 template<typename _Tp>
2187 using _NotSelf = __not_<is_same<_Tp, function>>;
2189 template<typename _Functor>
2191 = __and_<_NotSelf<_Functor>,
2192 __check_func_return_type<_Invoke<_Functor>, _Res>>;
2194 template<typename _Cond, typename _Tp>
2195 using _Requires = typename enable_if<_Cond::value, _Tp>::type;
2198 typedef _Res result_type;
2200 // [3.7.2.1] construct/copy/destroy
2203 * @brief Default construct creates an empty function call wrapper.
2204 * @post @c !(bool)*this
2207 : _Function_base() { }
2210 * @brief Creates an empty function call wrapper.
2211 * @post @c !(bool)*this
2213 function(nullptr_t) noexcept
2214 : _Function_base() { }
2217 * @brief %Function copy constructor.
2218 * @param __x A %function object with identical call signature.
2219 * @post @c bool(*this) == bool(__x)
2221 * The newly-created %function contains a copy of the target of @a
2222 * __x (if it has one).
2224 function(const function& __x);
2227 * @brief %Function move constructor.
2228 * @param __x A %function object rvalue with identical call signature.
2230 * The newly-created %function contains the target of @a __x
2233 function(function&& __x) : _Function_base()
2238 // TODO: needs allocator_arg_t
2241 * @brief Builds a %function that targets a copy of the incoming
2243 * @param __f A %function object that is callable with parameters of
2244 * type @c T1, @c T2, ..., @c TN and returns a value convertible
2247 * The newly-created %function object will target a copy of
2248 * @a __f. If @a __f is @c reference_wrapper<F>, then this function
2249 * object will contain a reference to the function object @c
2250 * __f.get(). If @a __f is a NULL function pointer or NULL
2251 * pointer-to-member, the newly-created object will be empty.
2253 * If @a __f is a non-NULL function pointer or an object of type @c
2254 * reference_wrapper<F>, this function will not throw.
2256 template<typename _Functor,
2257 typename = _Requires<_Callable<_Functor>, void>>
2261 * @brief %Function assignment operator.
2262 * @param __x A %function with identical call signature.
2263 * @post @c (bool)*this == (bool)x
2266 * The target of @a __x is copied to @c *this. If @a __x has no
2267 * target, then @c *this will be empty.
2269 * If @a __x targets a function pointer or a reference to a function
2270 * object, then this operation will not throw an %exception.
2273 operator=(const function& __x)
2275 function(__x).swap(*this);
2280 * @brief %Function move-assignment operator.
2281 * @param __x A %function rvalue with identical call signature.
2284 * The target of @a __x is moved to @c *this. If @a __x has no
2285 * target, then @c *this will be empty.
2287 * If @a __x targets a function pointer or a reference to a function
2288 * object, then this operation will not throw an %exception.
2291 operator=(function&& __x)
2293 function(std::move(__x)).swap(*this);
2298 * @brief %Function assignment to zero.
2299 * @post @c !(bool)*this
2302 * The target of @c *this is deallocated, leaving it empty.
2305 operator=(nullptr_t)
2309 _M_manager(_M_functor, _M_functor, __destroy_functor);
2317 * @brief %Function assignment to a new target.
2318 * @param __f A %function object that is callable with parameters of
2319 * type @c T1, @c T2, ..., @c TN and returns a value convertible
2323 * This %function object wrapper will target a copy of @a
2324 * __f. If @a __f is @c reference_wrapper<F>, then this function
2325 * object will contain a reference to the function object @c
2326 * __f.get(). If @a __f is a NULL function pointer or NULL
2327 * pointer-to-member, @c this object will be empty.
2329 * If @a __f is a non-NULL function pointer or an object of type @c
2330 * reference_wrapper<F>, this function will not throw.
2332 template<typename _Functor>
2333 _Requires<_Callable<typename decay<_Functor>::type>, function&>
2334 operator=(_Functor&& __f)
2336 function(std::forward<_Functor>(__f)).swap(*this);
2341 template<typename _Functor>
2343 operator=(reference_wrapper<_Functor> __f) noexcept
2345 function(__f).swap(*this);
2349 // [3.7.2.2] function modifiers
2352 * @brief Swap the targets of two %function objects.
2353 * @param __x A %function with identical call signature.
2355 * Swap the targets of @c this function object and @a __f. This
2356 * function will not throw an %exception.
2358 void swap(function& __x)
2360 std::swap(_M_functor, __x._M_functor);
2361 std::swap(_M_manager, __x._M_manager);
2362 std::swap(_M_invoker, __x._M_invoker);
2365 // TODO: needs allocator_arg_t
2367 template<typename _Functor, typename _Alloc>
2369 assign(_Functor&& __f, const _Alloc& __a)
2371 function(allocator_arg, __a,
2372 std::forward<_Functor>(__f)).swap(*this);
2376 // [3.7.2.3] function capacity
2379 * @brief Determine if the %function wrapper has a target.
2381 * @return @c true when this %function object contains a target,
2382 * or @c false when it is empty.
2384 * This function will not throw an %exception.
2386 explicit operator bool() const noexcept
2387 { return !_M_empty(); }
2389 // [3.7.2.4] function invocation
2392 * @brief Invokes the function targeted by @c *this.
2393 * @returns the result of the target.
2394 * @throws bad_function_call when @c !(bool)*this
2396 * The function call operator invokes the target function object
2397 * stored by @c this.
2399 _Res operator()(_ArgTypes... __args) const;
2402 // [3.7.2.5] function target access
2404 * @brief Determine the type of the target of this function object
2407 * @returns the type identifier of the target function object, or
2408 * @c typeid(void) if @c !(bool)*this.
2410 * This function will not throw an %exception.
2412 const type_info& target_type() const noexcept;
2415 * @brief Access the stored target function object.
2417 * @return Returns a pointer to the stored target function object,
2418 * if @c typeid(Functor).equals(target_type()); otherwise, a NULL
2421 * This function will not throw an %exception.
2423 template<typename _Functor> _Functor* target() noexcept;
2426 template<typename _Functor> const _Functor* target() const noexcept;
2430 typedef _Res (*_Invoker_type)(const _Any_data&, _ArgTypes...);
2431 _Invoker_type _M_invoker;
2434 // Out-of-line member definitions.
2435 template<typename _Res, typename... _ArgTypes>
2436 function<_Res(_ArgTypes...)>::
2437 function(const function& __x)
2440 if (static_cast<bool>(__x))
2442 __x._M_manager(_M_functor, __x._M_functor, __clone_functor);
2443 _M_invoker = __x._M_invoker;
2444 _M_manager = __x._M_manager;
2448 template<typename _Res, typename... _ArgTypes>
2449 template<typename _Functor, typename>
2450 function<_Res(_ArgTypes...)>::
2451 function(_Functor __f)
2454 typedef _Function_handler<_Signature_type, _Functor> _My_handler;
2456 if (_My_handler::_M_not_empty_function(__f))
2458 _My_handler::_M_init_functor(_M_functor, std::move(__f));
2459 _M_invoker = &_My_handler::_M_invoke;
2460 _M_manager = &_My_handler::_M_manager;
2464 template<typename _Res, typename... _ArgTypes>
2466 function<_Res(_ArgTypes...)>::
2467 operator()(_ArgTypes... __args) const
2470 __throw_bad_function_call();
2471 return _M_invoker(_M_functor, std::forward<_ArgTypes>(__args)...);
2475 template<typename _Res, typename... _ArgTypes>
2477 function<_Res(_ArgTypes...)>::
2478 target_type() const noexcept
2482 _Any_data __typeinfo_result;
2483 _M_manager(__typeinfo_result, _M_functor, __get_type_info);
2484 return *__typeinfo_result._M_access<const type_info*>();
2487 return typeid(void);
2490 template<typename _Res, typename... _ArgTypes>
2491 template<typename _Functor>
2493 function<_Res(_ArgTypes...)>::
2496 if (typeid(_Functor) == target_type() && _M_manager)
2499 if (_M_manager(__ptr, _M_functor, __get_functor_ptr)
2500 && !is_const<_Functor>::value)
2503 return __ptr._M_access<_Functor*>();
2509 template<typename _Res, typename... _ArgTypes>
2510 template<typename _Functor>
2512 function<_Res(_ArgTypes...)>::
2513 target() const noexcept
2515 if (typeid(_Functor) == target_type() && _M_manager)
2518 _M_manager(__ptr, _M_functor, __get_functor_ptr);
2519 return __ptr._M_access<const _Functor*>();
2526 // [20.7.15.2.6] null pointer comparisons
2529 * @brief Compares a polymorphic function object wrapper against 0
2530 * (the NULL pointer).
2531 * @returns @c true if the wrapper has no target, @c false otherwise
2533 * This function will not throw an %exception.
2535 template<typename _Res, typename... _Args>
2537 operator==(const function<_Res(_Args...)>& __f, nullptr_t) noexcept
2538 { return !static_cast<bool>(__f); }
2541 template<typename _Res, typename... _Args>
2543 operator==(nullptr_t, const function<_Res(_Args...)>& __f) noexcept
2544 { return !static_cast<bool>(__f); }
2547 * @brief Compares a polymorphic function object wrapper against 0
2548 * (the NULL pointer).
2549 * @returns @c false if the wrapper has no target, @c true otherwise
2551 * This function will not throw an %exception.
2553 template<typename _Res, typename... _Args>
2555 operator!=(const function<_Res(_Args...)>& __f, nullptr_t) noexcept
2556 { return static_cast<bool>(__f); }
2559 template<typename _Res, typename... _Args>
2561 operator!=(nullptr_t, const function<_Res(_Args...)>& __f) noexcept
2562 { return static_cast<bool>(__f); }
2564 // [20.7.15.2.7] specialized algorithms
2567 * @brief Swap the targets of two polymorphic function object wrappers.
2569 * This function will not throw an %exception.
2571 template<typename _Res, typename... _Args>
2573 swap(function<_Res(_Args...)>& __x, function<_Res(_Args...)>& __y)
2576 _GLIBCXX_END_NAMESPACE_VERSION
2581 #endif // _GLIBCXX_FUNCTIONAL