1 // <functional> -*- C++ -*-
3 // Copyright (C) 2001-2014 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>
189 * Invoke a function object, which may be either a member pointer or a
190 * function object. The first parameter will tell which.
192 template<typename _Functor, typename... _Args>
195 (!is_member_pointer<_Functor>::value
196 && !is_function<_Functor>::value
197 && !is_function<typename remove_pointer<_Functor>::type>::value),
198 typename result_of<_Functor&(_Args&&...)>::type
200 __invoke(_Functor& __f, _Args&&... __args)
202 return __f(std::forward<_Args>(__args)...);
205 template<typename _Functor, typename... _Args>
208 (is_member_pointer<_Functor>::value
209 && !is_function<_Functor>::value
210 && !is_function<typename remove_pointer<_Functor>::type>::value),
211 typename result_of<_Functor(_Args&&...)>::type
213 __invoke(_Functor& __f, _Args&&... __args)
215 return std::mem_fn(__f)(std::forward<_Args>(__args)...);
218 // To pick up function references (that will become function pointers)
219 template<typename _Functor, typename... _Args>
222 (is_pointer<_Functor>::value
223 && is_function<typename remove_pointer<_Functor>::type>::value),
224 typename result_of<_Functor(_Args&&...)>::type
226 __invoke(_Functor __f, _Args&&... __args)
228 return __f(std::forward<_Args>(__args)...);
232 * Knowing which of unary_function and binary_function _Tp derives
233 * from, derives from the same and ensures that reference_wrapper
234 * will have a weak result type. See cases below.
236 template<bool _Unary, bool _Binary, typename _Tp>
237 struct _Reference_wrapper_base_impl;
239 // None of the nested argument types.
240 template<typename _Tp>
241 struct _Reference_wrapper_base_impl<false, false, _Tp>
242 : _Weak_result_type<_Tp>
245 // Nested argument_type only.
246 template<typename _Tp>
247 struct _Reference_wrapper_base_impl<true, false, _Tp>
248 : _Weak_result_type<_Tp>
250 typedef typename _Tp::argument_type argument_type;
253 // Nested first_argument_type and second_argument_type only.
254 template<typename _Tp>
255 struct _Reference_wrapper_base_impl<false, true, _Tp>
256 : _Weak_result_type<_Tp>
258 typedef typename _Tp::first_argument_type first_argument_type;
259 typedef typename _Tp::second_argument_type second_argument_type;
262 // All the nested argument types.
263 template<typename _Tp>
264 struct _Reference_wrapper_base_impl<true, true, _Tp>
265 : _Weak_result_type<_Tp>
267 typedef typename _Tp::argument_type argument_type;
268 typedef typename _Tp::first_argument_type first_argument_type;
269 typedef typename _Tp::second_argument_type second_argument_type;
272 _GLIBCXX_HAS_NESTED_TYPE(argument_type)
273 _GLIBCXX_HAS_NESTED_TYPE(first_argument_type)
274 _GLIBCXX_HAS_NESTED_TYPE(second_argument_type)
277 * Derives from unary_function or binary_function when it
278 * can. Specializations handle all of the easy cases. The primary
279 * template determines what to do with a class type, which may
280 * derive from both unary_function and binary_function.
282 template<typename _Tp>
283 struct _Reference_wrapper_base
284 : _Reference_wrapper_base_impl<
285 __has_argument_type<_Tp>::value,
286 __has_first_argument_type<_Tp>::value
287 && __has_second_argument_type<_Tp>::value,
291 // - a function type (unary)
292 template<typename _Res, typename _T1>
293 struct _Reference_wrapper_base<_Res(_T1)>
294 : unary_function<_T1, _Res>
297 template<typename _Res, typename _T1>
298 struct _Reference_wrapper_base<_Res(_T1) const>
299 : unary_function<_T1, _Res>
302 template<typename _Res, typename _T1>
303 struct _Reference_wrapper_base<_Res(_T1) volatile>
304 : unary_function<_T1, _Res>
307 template<typename _Res, typename _T1>
308 struct _Reference_wrapper_base<_Res(_T1) const volatile>
309 : unary_function<_T1, _Res>
312 // - a function type (binary)
313 template<typename _Res, typename _T1, typename _T2>
314 struct _Reference_wrapper_base<_Res(_T1, _T2)>
315 : binary_function<_T1, _T2, _Res>
318 template<typename _Res, typename _T1, typename _T2>
319 struct _Reference_wrapper_base<_Res(_T1, _T2) const>
320 : binary_function<_T1, _T2, _Res>
323 template<typename _Res, typename _T1, typename _T2>
324 struct _Reference_wrapper_base<_Res(_T1, _T2) volatile>
325 : binary_function<_T1, _T2, _Res>
328 template<typename _Res, typename _T1, typename _T2>
329 struct _Reference_wrapper_base<_Res(_T1, _T2) const volatile>
330 : binary_function<_T1, _T2, _Res>
333 // - a function pointer type (unary)
334 template<typename _Res, typename _T1>
335 struct _Reference_wrapper_base<_Res(*)(_T1)>
336 : unary_function<_T1, _Res>
339 // - a function pointer type (binary)
340 template<typename _Res, typename _T1, typename _T2>
341 struct _Reference_wrapper_base<_Res(*)(_T1, _T2)>
342 : binary_function<_T1, _T2, _Res>
345 // - a pointer to member function type (unary, no qualifiers)
346 template<typename _Res, typename _T1>
347 struct _Reference_wrapper_base<_Res (_T1::*)()>
348 : unary_function<_T1*, _Res>
351 // - a pointer to member function type (binary, no qualifiers)
352 template<typename _Res, typename _T1, typename _T2>
353 struct _Reference_wrapper_base<_Res (_T1::*)(_T2)>
354 : binary_function<_T1*, _T2, _Res>
357 // - a pointer to member function type (unary, const)
358 template<typename _Res, typename _T1>
359 struct _Reference_wrapper_base<_Res (_T1::*)() const>
360 : unary_function<const _T1*, _Res>
363 // - a pointer to member function type (binary, const)
364 template<typename _Res, typename _T1, typename _T2>
365 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const>
366 : binary_function<const _T1*, _T2, _Res>
369 // - a pointer to member function type (unary, volatile)
370 template<typename _Res, typename _T1>
371 struct _Reference_wrapper_base<_Res (_T1::*)() volatile>
372 : unary_function<volatile _T1*, _Res>
375 // - a pointer to member function type (binary, volatile)
376 template<typename _Res, typename _T1, typename _T2>
377 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) volatile>
378 : binary_function<volatile _T1*, _T2, _Res>
381 // - a pointer to member function type (unary, const volatile)
382 template<typename _Res, typename _T1>
383 struct _Reference_wrapper_base<_Res (_T1::*)() const volatile>
384 : unary_function<const volatile _T1*, _Res>
387 // - a pointer to member function type (binary, const volatile)
388 template<typename _Res, typename _T1, typename _T2>
389 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const volatile>
390 : binary_function<const volatile _T1*, _T2, _Res>
394 * @brief Primary class template for reference_wrapper.
398 template<typename _Tp>
399 class reference_wrapper
400 : public _Reference_wrapper_base<typename remove_cv<_Tp>::type>
407 reference_wrapper(_Tp& __indata) noexcept
408 : _M_data(std::__addressof(__indata))
411 reference_wrapper(_Tp&&) = delete;
413 reference_wrapper(const reference_wrapper<_Tp>& __inref) noexcept
414 : _M_data(__inref._M_data)
418 operator=(const reference_wrapper<_Tp>& __inref) noexcept
420 _M_data = __inref._M_data;
424 operator _Tp&() const noexcept
425 { return this->get(); }
431 template<typename... _Args>
432 typename result_of<_Tp&(_Args&&...)>::type
433 operator()(_Args&&... __args) const
435 return __invoke(get(), std::forward<_Args>(__args)...);
440 /// Denotes a reference should be taken to a variable.
441 template<typename _Tp>
442 inline reference_wrapper<_Tp>
443 ref(_Tp& __t) noexcept
444 { return reference_wrapper<_Tp>(__t); }
446 /// Denotes a const reference should be taken to a variable.
447 template<typename _Tp>
448 inline reference_wrapper<const _Tp>
449 cref(const _Tp& __t) noexcept
450 { return reference_wrapper<const _Tp>(__t); }
452 template<typename _Tp>
453 void ref(const _Tp&&) = delete;
455 template<typename _Tp>
456 void cref(const _Tp&&) = delete;
458 /// Partial specialization.
459 template<typename _Tp>
460 inline reference_wrapper<_Tp>
461 ref(reference_wrapper<_Tp> __t) noexcept
462 { return ref(__t.get()); }
464 /// Partial specialization.
465 template<typename _Tp>
466 inline reference_wrapper<const _Tp>
467 cref(reference_wrapper<_Tp> __t) noexcept
468 { return cref(__t.get()); }
472 template<typename... _Types>
473 struct _Pack : integral_constant<size_t, sizeof...(_Types)>
476 template<typename _From, typename _To, bool = _From::value == _To::value>
477 struct _AllConvertible : false_type
480 template<typename... _From, typename... _To>
481 struct _AllConvertible<_Pack<_From...>, _Pack<_To...>, true>
482 : __and_<is_convertible<_From, _To>...>
485 template<typename _Tp1, typename _Tp2>
486 using _NotSame = __not_<is_same<typename std::decay<_Tp1>::type,
487 typename std::decay<_Tp2>::type>>;
490 * Derives from @c unary_function or @c binary_function, or perhaps
491 * nothing, depending on the number of arguments provided. The
492 * primary template is the basis case, which derives nothing.
494 template<typename _Res, typename... _ArgTypes>
495 struct _Maybe_unary_or_binary_function { };
497 /// Derives from @c unary_function, as appropriate.
498 template<typename _Res, typename _T1>
499 struct _Maybe_unary_or_binary_function<_Res, _T1>
500 : std::unary_function<_T1, _Res> { };
502 /// Derives from @c binary_function, as appropriate.
503 template<typename _Res, typename _T1, typename _T2>
504 struct _Maybe_unary_or_binary_function<_Res, _T1, _T2>
505 : std::binary_function<_T1, _T2, _Res> { };
507 /// Implementation of @c mem_fn for member function pointers.
508 template<typename _Res, typename _Class, typename... _ArgTypes>
509 class _Mem_fn<_Res (_Class::*)(_ArgTypes...)>
510 : public _Maybe_unary_or_binary_function<_Res, _Class*, _ArgTypes...>
512 typedef _Res (_Class::*_Functor)(_ArgTypes...);
514 template<typename _Tp, typename... _Args>
516 _M_call(_Tp&& __object, const volatile _Class *,
517 _Args&&... __args) const
519 return (std::forward<_Tp>(__object).*__pmf)
520 (std::forward<_Args>(__args)...);
523 template<typename _Tp, typename... _Args>
525 _M_call(_Tp&& __ptr, const volatile void *, _Args&&... __args) const
526 { return ((*__ptr).*__pmf)(std::forward<_Args>(__args)...); }
528 // Require each _Args to be convertible to corresponding _ArgTypes
529 template<typename... _Args>
530 using _RequireValidArgs
531 = _Require<_AllConvertible<_Pack<_Args...>, _Pack<_ArgTypes...>>>;
533 // Require each _Args to be convertible to corresponding _ArgTypes
534 // and require _Tp is not _Class, _Class& or _Class*
535 template<typename _Tp, typename... _Args>
536 using _RequireValidArgs2
537 = _Require<_NotSame<_Class, _Tp>, _NotSame<_Class*, _Tp>,
538 _AllConvertible<_Pack<_Args...>, _Pack<_ArgTypes...>>>;
540 // Require each _Args to be convertible to corresponding _ArgTypes
541 // and require _Tp is _Class or derived from _Class
542 template<typename _Tp, typename... _Args>
543 using _RequireValidArgs3
544 = _Require<is_base_of<_Class, _Tp>,
545 _AllConvertible<_Pack<_Args...>, _Pack<_ArgTypes...>>>;
548 typedef _Res result_type;
550 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
553 template<typename... _Args, typename _Req = _RequireValidArgs<_Args...>>
555 operator()(_Class& __object, _Args&&... __args) const
556 { return (__object.*__pmf)(std::forward<_Args>(__args)...); }
558 template<typename... _Args, typename _Req = _RequireValidArgs<_Args...>>
560 operator()(_Class&& __object, _Args&&... __args) const
562 return (std::move(__object).*__pmf)(std::forward<_Args>(__args)...);
566 template<typename... _Args, typename _Req = _RequireValidArgs<_Args...>>
568 operator()(_Class* __object, _Args&&... __args) const
569 { return (__object->*__pmf)(std::forward<_Args>(__args)...); }
571 // Handle smart pointers, references and pointers to derived
572 template<typename _Tp, typename... _Args,
573 typename _Req = _RequireValidArgs2<_Tp, _Args...>>
575 operator()(_Tp&& __object, _Args&&... __args) const
577 return _M_call(std::forward<_Tp>(__object), &__object,
578 std::forward<_Args>(__args)...);
581 template<typename _Tp, typename... _Args,
582 typename _Req = _RequireValidArgs3<_Tp, _Args...>>
584 operator()(reference_wrapper<_Tp> __ref, _Args&&... __args) const
585 { return operator()(__ref.get(), std::forward<_Args>(__args)...); }
591 /// Implementation of @c mem_fn for const member function pointers.
592 template<typename _Res, typename _Class, typename... _ArgTypes>
593 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) const>
594 : public _Maybe_unary_or_binary_function<_Res, const _Class*,
597 typedef _Res (_Class::*_Functor)(_ArgTypes...) const;
599 template<typename _Tp, typename... _Args>
601 _M_call(_Tp&& __object, const volatile _Class *,
602 _Args&&... __args) const
604 return (std::forward<_Tp>(__object).*__pmf)
605 (std::forward<_Args>(__args)...);
608 template<typename _Tp, typename... _Args>
610 _M_call(_Tp&& __ptr, const volatile void *, _Args&&... __args) const
611 { return ((*__ptr).*__pmf)(std::forward<_Args>(__args)...); }
613 template<typename... _Args>
614 using _RequireValidArgs
615 = _Require<_AllConvertible<_Pack<_Args...>, _Pack<_ArgTypes...>>>;
617 template<typename _Tp, typename... _Args>
618 using _RequireValidArgs2
619 = _Require<_NotSame<_Class, _Tp>, _NotSame<const _Class*, _Tp>,
620 _AllConvertible<_Pack<_Args...>, _Pack<_ArgTypes...>>>;
622 template<typename _Tp, typename... _Args>
623 using _RequireValidArgs3
624 = _Require<is_base_of<_Class, _Tp>,
625 _AllConvertible<_Pack<_Args...>, _Pack<_ArgTypes...>>>;
628 typedef _Res result_type;
630 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
633 template<typename... _Args, typename _Req = _RequireValidArgs<_Args...>>
635 operator()(const _Class& __object, _Args&&... __args) const
636 { return (__object.*__pmf)(std::forward<_Args>(__args)...); }
638 template<typename... _Args, typename _Req = _RequireValidArgs<_Args...>>
640 operator()(const _Class&& __object, _Args&&... __args) const
642 return (std::move(__object).*__pmf)(std::forward<_Args>(__args)...);
646 template<typename... _Args, typename _Req = _RequireValidArgs<_Args...>>
648 operator()(const _Class* __object, _Args&&... __args) const
649 { return (__object->*__pmf)(std::forward<_Args>(__args)...); }
651 // Handle smart pointers, references and pointers to derived
652 template<typename _Tp, typename... _Args,
653 typename _Req = _RequireValidArgs2<_Tp, _Args...>>
654 _Res operator()(_Tp&& __object, _Args&&... __args) const
656 return _M_call(std::forward<_Tp>(__object), &__object,
657 std::forward<_Args>(__args)...);
660 template<typename _Tp, typename... _Args,
661 typename _Req = _RequireValidArgs3<_Tp, _Args...>>
663 operator()(reference_wrapper<_Tp> __ref, _Args&&... __args) const
664 { return operator()(__ref.get(), std::forward<_Args>(__args)...); }
670 /// Implementation of @c mem_fn for volatile member function pointers.
671 template<typename _Res, typename _Class, typename... _ArgTypes>
672 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) volatile>
673 : public _Maybe_unary_or_binary_function<_Res, volatile _Class*,
676 typedef _Res (_Class::*_Functor)(_ArgTypes...) volatile;
678 template<typename _Tp, typename... _Args>
680 _M_call(_Tp&& __object, const volatile _Class *,
681 _Args&&... __args) const
683 return (std::forward<_Tp>(__object).*__pmf)
684 (std::forward<_Args>(__args)...);
687 template<typename _Tp, typename... _Args>
689 _M_call(_Tp&& __ptr, const volatile void *, _Args&&... __args) const
690 { return ((*__ptr).*__pmf)(std::forward<_Args>(__args)...); }
692 template<typename... _Args>
693 using _RequireValidArgs
694 = _Require<_AllConvertible<_Pack<_Args...>, _Pack<_ArgTypes...>>>;
696 template<typename _Tp, typename... _Args>
697 using _RequireValidArgs2
698 = _Require<_NotSame<_Class, _Tp>, _NotSame<volatile _Class*, _Tp>,
699 _AllConvertible<_Pack<_Args...>, _Pack<_ArgTypes...>>>;
701 template<typename _Tp, typename... _Args>
702 using _RequireValidArgs3
703 = _Require<is_base_of<_Class, _Tp>,
704 _AllConvertible<_Pack<_Args...>, _Pack<_ArgTypes...>>>;
707 typedef _Res result_type;
709 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
712 template<typename... _Args, typename _Req = _RequireValidArgs<_Args...>>
714 operator()(volatile _Class& __object, _Args&&... __args) const
715 { return (__object.*__pmf)(std::forward<_Args>(__args)...); }
717 template<typename... _Args, typename _Req = _RequireValidArgs<_Args...>>
719 operator()(volatile _Class&& __object, _Args&&... __args) const
721 return (std::move(__object).*__pmf)(std::forward<_Args>(__args)...);
725 template<typename... _Args, typename _Req = _RequireValidArgs<_Args...>>
727 operator()(volatile _Class* __object, _Args&&... __args) const
728 { return (__object->*__pmf)(std::forward<_Args>(__args)...); }
730 // Handle smart pointers, references and pointers to derived
731 template<typename _Tp, typename... _Args,
732 typename _Req = _RequireValidArgs2<_Tp, _Args...>>
734 operator()(_Tp&& __object, _Args&&... __args) const
736 return _M_call(std::forward<_Tp>(__object), &__object,
737 std::forward<_Args>(__args)...);
740 template<typename _Tp, typename... _Args,
741 typename _Req = _RequireValidArgs3<_Tp, _Args...>>
743 operator()(reference_wrapper<_Tp> __ref, _Args&&... __args) const
744 { return operator()(__ref.get(), std::forward<_Args>(__args)...); }
750 /// Implementation of @c mem_fn for const volatile member function pointers.
751 template<typename _Res, typename _Class, typename... _ArgTypes>
752 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) const volatile>
753 : public _Maybe_unary_or_binary_function<_Res, const volatile _Class*,
756 typedef _Res (_Class::*_Functor)(_ArgTypes...) const volatile;
758 template<typename _Tp, typename... _Args>
760 _M_call(_Tp&& __object, const volatile _Class *,
761 _Args&&... __args) const
763 return (std::forward<_Tp>(__object).*__pmf)
764 (std::forward<_Args>(__args)...);
767 template<typename _Tp, typename... _Args>
769 _M_call(_Tp&& __ptr, const volatile void *, _Args&&... __args) const
770 { return ((*__ptr).*__pmf)(std::forward<_Args>(__args)...); }
772 template<typename... _Args>
773 using _RequireValidArgs
774 = _Require<_AllConvertible<_Pack<_Args...>, _Pack<_ArgTypes...>>>;
776 template<typename _Tp, typename... _Args>
777 using _RequireValidArgs2
778 = _Require<_NotSame<_Class, _Tp>,
779 _NotSame<const volatile _Class*, _Tp>,
780 _AllConvertible<_Pack<_Args...>, _Pack<_ArgTypes...>>>;
782 template<typename _Tp, typename... _Args>
783 using _RequireValidArgs3
784 = _Require<is_base_of<_Class, _Tp>,
785 _AllConvertible<_Pack<_Args...>, _Pack<_ArgTypes...>>>;
788 typedef _Res result_type;
790 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
793 template<typename... _Args, typename _Req = _RequireValidArgs<_Args...>>
795 operator()(const volatile _Class& __object, _Args&&... __args) const
796 { return (__object.*__pmf)(std::forward<_Args>(__args)...); }
798 template<typename... _Args, typename _Req = _RequireValidArgs<_Args...>>
800 operator()(const volatile _Class&& __object, _Args&&... __args) const
802 return (std::move(__object).*__pmf)(std::forward<_Args>(__args)...);
806 template<typename... _Args, typename _Req = _RequireValidArgs<_Args...>>
808 operator()(const volatile _Class* __object, _Args&&... __args) const
809 { return (__object->*__pmf)(std::forward<_Args>(__args)...); }
811 // Handle smart pointers, references and pointers to derived
812 template<typename _Tp, typename... _Args,
813 typename _Req = _RequireValidArgs2<_Tp, _Args...>>
814 _Res operator()(_Tp&& __object, _Args&&... __args) const
816 return _M_call(std::forward<_Tp>(__object), &__object,
817 std::forward<_Args>(__args)...);
820 template<typename _Tp, typename... _Args,
821 typename _Req = _RequireValidArgs3<_Tp, _Args...>>
823 operator()(reference_wrapper<_Tp> __ref, _Args&&... __args) const
824 { return operator()(__ref.get(), std::forward<_Args>(__args)...); }
831 template<typename _Tp, bool>
832 struct _Mem_fn_const_or_non
834 typedef const _Tp& type;
837 template<typename _Tp>
838 struct _Mem_fn_const_or_non<_Tp, false>
843 template<typename _Res, typename _Class>
844 class _Mem_fn<_Res _Class::*>
846 using __pm_type = _Res _Class::*;
848 // This bit of genius is due to Peter Dimov, improved slightly by
850 // Made less elegant to support perfect forwarding and noexcept.
851 template<typename _Tp>
853 _M_call(_Tp&& __object, const _Class *) const noexcept
854 -> decltype(std::forward<_Tp>(__object).*std::declval<__pm_type&>())
855 { return std::forward<_Tp>(__object).*__pm; }
857 template<typename _Tp, typename _Up>
859 _M_call(_Tp&& __object, _Up * const *) const noexcept
860 -> decltype((*std::forward<_Tp>(__object)).*std::declval<__pm_type&>())
861 { return (*std::forward<_Tp>(__object)).*__pm; }
863 template<typename _Tp>
865 _M_call(_Tp&& __ptr, const volatile void*) const
866 noexcept(noexcept((*__ptr).*std::declval<__pm_type&>()))
867 -> decltype((*__ptr).*std::declval<__pm_type&>())
868 { return (*__ptr).*__pm; }
872 _Mem_fn(_Res _Class::*__pm) noexcept : __pm(__pm) { }
876 operator()(_Class& __object) const noexcept
877 { return __object.*__pm; }
880 operator()(const _Class& __object) const noexcept
881 { return __object.*__pm; }
884 operator()(_Class&& __object) const noexcept
885 { return std::forward<_Class>(__object).*__pm; }
888 operator()(const _Class&& __object) const noexcept
889 { return std::forward<const _Class>(__object).*__pm; }
893 operator()(_Class* __object) const noexcept
894 { return __object->*__pm; }
897 operator()(const _Class* __object) const noexcept
898 { return __object->*__pm; }
900 // Handle smart pointers and derived
901 template<typename _Tp, typename _Req = _Require<_NotSame<_Class*, _Tp>>>
903 operator()(_Tp&& __unknown) const
904 noexcept(noexcept(std::declval<_Mem_fn*>()->_M_call
905 (std::forward<_Tp>(__unknown), &__unknown)))
906 -> decltype(this->_M_call(std::forward<_Tp>(__unknown), &__unknown))
907 { return _M_call(std::forward<_Tp>(__unknown), &__unknown); }
909 template<typename _Tp, typename _Req = _Require<is_base_of<_Class, _Tp>>>
911 operator()(reference_wrapper<_Tp> __ref) const
912 noexcept(noexcept(std::declval<_Mem_fn&>()(__ref.get())))
913 -> decltype((*this)(__ref.get()))
914 { return (*this)(__ref.get()); }
920 // _GLIBCXX_RESOLVE_LIB_DEFECTS
921 // 2048. Unnecessary mem_fn overloads
923 * @brief Returns a function object that forwards to the member
927 template<typename _Tp, typename _Class>
928 inline _Mem_fn<_Tp _Class::*>
929 mem_fn(_Tp _Class::* __pm) noexcept
931 return _Mem_fn<_Tp _Class::*>(__pm);
935 * @brief Determines if the given type _Tp is a function object
936 * should be treated as a subexpression when evaluating calls to
937 * function objects returned by bind(). [TR1 3.6.1]
940 template<typename _Tp>
941 struct is_bind_expression
942 : public false_type { };
945 * @brief Determines if the given type _Tp is a placeholder in a
946 * bind() expression and, if so, which placeholder it is. [TR1 3.6.2]
949 template<typename _Tp>
950 struct is_placeholder
951 : public integral_constant<int, 0>
954 /** @brief The type of placeholder objects defined by libstdc++.
957 template<int _Num> struct _Placeholder { };
959 _GLIBCXX_END_NAMESPACE_VERSION
961 /** @namespace std::placeholders
962 * @brief ISO C++11 entities sub-namespace for functional.
965 namespace placeholders
967 _GLIBCXX_BEGIN_NAMESPACE_VERSION
968 /* Define a large number of placeholders. There is no way to
969 * simplify this with variadic templates, because we're introducing
970 * unique names for each.
972 extern const _Placeholder<1> _1;
973 extern const _Placeholder<2> _2;
974 extern const _Placeholder<3> _3;
975 extern const _Placeholder<4> _4;
976 extern const _Placeholder<5> _5;
977 extern const _Placeholder<6> _6;
978 extern const _Placeholder<7> _7;
979 extern const _Placeholder<8> _8;
980 extern const _Placeholder<9> _9;
981 extern const _Placeholder<10> _10;
982 extern const _Placeholder<11> _11;
983 extern const _Placeholder<12> _12;
984 extern const _Placeholder<13> _13;
985 extern const _Placeholder<14> _14;
986 extern const _Placeholder<15> _15;
987 extern const _Placeholder<16> _16;
988 extern const _Placeholder<17> _17;
989 extern const _Placeholder<18> _18;
990 extern const _Placeholder<19> _19;
991 extern const _Placeholder<20> _20;
992 extern const _Placeholder<21> _21;
993 extern const _Placeholder<22> _22;
994 extern const _Placeholder<23> _23;
995 extern const _Placeholder<24> _24;
996 extern const _Placeholder<25> _25;
997 extern const _Placeholder<26> _26;
998 extern const _Placeholder<27> _27;
999 extern const _Placeholder<28> _28;
1000 extern const _Placeholder<29> _29;
1001 _GLIBCXX_END_NAMESPACE_VERSION
1004 _GLIBCXX_BEGIN_NAMESPACE_VERSION
1007 * Partial specialization of is_placeholder that provides the placeholder
1008 * number for the placeholder objects defined by libstdc++.
1012 struct is_placeholder<_Placeholder<_Num> >
1013 : public integral_constant<int, _Num>
1017 struct is_placeholder<const _Placeholder<_Num> >
1018 : public integral_constant<int, _Num>
1022 * Used by _Safe_tuple_element to indicate that there is no tuple
1023 * element at this position.
1025 struct _No_tuple_element;
1028 * Implementation helper for _Safe_tuple_element. This primary
1029 * template handles the case where it is safe to use @c
1032 template<std::size_t __i, typename _Tuple, bool _IsSafe>
1033 struct _Safe_tuple_element_impl
1034 : tuple_element<__i, _Tuple> { };
1037 * Implementation helper for _Safe_tuple_element. This partial
1038 * specialization handles the case where it is not safe to use @c
1039 * tuple_element. We just return @c _No_tuple_element.
1041 template<std::size_t __i, typename _Tuple>
1042 struct _Safe_tuple_element_impl<__i, _Tuple, false>
1044 typedef _No_tuple_element type;
1048 * Like tuple_element, but returns @c _No_tuple_element when
1049 * tuple_element would return an error.
1051 template<std::size_t __i, typename _Tuple>
1052 struct _Safe_tuple_element
1053 : _Safe_tuple_element_impl<__i, _Tuple,
1054 (__i < tuple_size<_Tuple>::value)>
1058 * Maps an argument to bind() into an actual argument to the bound
1059 * function object [TR1 3.6.3/5]. Only the first parameter should
1060 * be specified: the rest are used to determine among the various
1061 * implementations. Note that, although this class is a function
1062 * object, it isn't entirely normal because it takes only two
1063 * parameters regardless of the number of parameters passed to the
1064 * bind expression. The first parameter is the bound argument and
1065 * the second parameter is a tuple containing references to the
1066 * rest of the arguments.
1068 template<typename _Arg,
1069 bool _IsBindExp = is_bind_expression<_Arg>::value,
1070 bool _IsPlaceholder = (is_placeholder<_Arg>::value > 0)>
1074 * If the argument is reference_wrapper<_Tp>, returns the
1075 * underlying reference. [TR1 3.6.3/5 bullet 1]
1077 template<typename _Tp>
1078 class _Mu<reference_wrapper<_Tp>, false, false>
1081 typedef _Tp& result_type;
1083 /* Note: This won't actually work for const volatile
1084 * reference_wrappers, because reference_wrapper::get() is const
1085 * but not volatile-qualified. This might be a defect in the TR.
1087 template<typename _CVRef, typename _Tuple>
1089 operator()(_CVRef& __arg, _Tuple&) const volatile
1090 { return __arg.get(); }
1094 * If the argument is a bind expression, we invoke the underlying
1095 * function object with the same cv-qualifiers as we are given and
1096 * pass along all of our arguments (unwrapped). [TR1 3.6.3/5 bullet 2]
1098 template<typename _Arg>
1099 class _Mu<_Arg, true, false>
1102 template<typename _CVArg, typename... _Args>
1104 operator()(_CVArg& __arg,
1105 tuple<_Args...>& __tuple) const volatile
1106 -> decltype(__arg(declval<_Args>()...))
1108 // Construct an index tuple and forward to __call
1109 typedef typename _Build_index_tuple<sizeof...(_Args)>::__type
1111 return this->__call(__arg, __tuple, _Indexes());
1115 // Invokes the underlying function object __arg by unpacking all
1116 // of the arguments in the tuple.
1117 template<typename _CVArg, typename... _Args, std::size_t... _Indexes>
1119 __call(_CVArg& __arg, tuple<_Args...>& __tuple,
1120 const _Index_tuple<_Indexes...>&) const volatile
1121 -> decltype(__arg(declval<_Args>()...))
1123 return __arg(std::forward<_Args>(std::get<_Indexes>(__tuple))...);
1128 * If the argument is a placeholder for the Nth argument, returns
1129 * a reference to the Nth argument to the bind function object.
1130 * [TR1 3.6.3/5 bullet 3]
1132 template<typename _Arg>
1133 class _Mu<_Arg, false, true>
1136 template<typename _Signature> class result;
1138 template<typename _CVMu, typename _CVArg, typename _Tuple>
1139 class result<_CVMu(_CVArg, _Tuple)>
1141 // Add a reference, if it hasn't already been done for us.
1142 // This allows us to be a little bit sloppy in constructing
1143 // the tuple that we pass to result_of<...>.
1144 typedef typename _Safe_tuple_element<(is_placeholder<_Arg>::value
1149 typedef typename add_rvalue_reference<__base_type>::type type;
1152 template<typename _Tuple>
1153 typename result<_Mu(_Arg, _Tuple)>::type
1154 operator()(const volatile _Arg&, _Tuple& __tuple) const volatile
1156 return std::forward<typename result<_Mu(_Arg, _Tuple)>::type>(
1157 ::std::get<(is_placeholder<_Arg>::value - 1)>(__tuple));
1162 * If the argument is just a value, returns a reference to that
1163 * value. The cv-qualifiers on the reference are the same as the
1164 * cv-qualifiers on the _Mu object. [TR1 3.6.3/5 bullet 4]
1166 template<typename _Arg>
1167 class _Mu<_Arg, false, false>
1170 template<typename _Signature> struct result;
1172 template<typename _CVMu, typename _CVArg, typename _Tuple>
1173 struct result<_CVMu(_CVArg, _Tuple)>
1175 typedef typename add_lvalue_reference<_CVArg>::type type;
1178 // Pick up the cv-qualifiers of the argument
1179 template<typename _CVArg, typename _Tuple>
1181 operator()(_CVArg&& __arg, _Tuple&) const volatile
1182 { return std::forward<_CVArg>(__arg); }
1186 * Maps member pointers into instances of _Mem_fn but leaves all
1187 * other function objects untouched. Used by std::bind(). The
1188 * primary template handles the non-member-pointer case.
1190 template<typename _Tp>
1191 struct _Maybe_wrap_member_pointer
1196 __do_wrap(const _Tp& __x)
1200 __do_wrap(_Tp&& __x)
1201 { return static_cast<_Tp&&>(__x); }
1205 * Maps member pointers into instances of _Mem_fn but leaves all
1206 * other function objects untouched. Used by std::bind(). This
1207 * partial specialization handles the member pointer case.
1209 template<typename _Tp, typename _Class>
1210 struct _Maybe_wrap_member_pointer<_Tp _Class::*>
1212 typedef _Mem_fn<_Tp _Class::*> type;
1215 __do_wrap(_Tp _Class::* __pm)
1216 { return type(__pm); }
1219 // Specialization needed to prevent "forming reference to void" errors when
1220 // bind<void>() is called, because argument deduction instantiates
1221 // _Maybe_wrap_member_pointer<void> outside the immediate context where
1224 struct _Maybe_wrap_member_pointer<void>
1229 // std::get<I> for volatile-qualified tuples
1230 template<std::size_t _Ind, typename... _Tp>
1232 __volget(volatile tuple<_Tp...>& __tuple)
1233 -> __tuple_element_t<_Ind, tuple<_Tp...>> volatile&
1234 { return std::get<_Ind>(const_cast<tuple<_Tp...>&>(__tuple)); }
1236 // std::get<I> for const-volatile-qualified tuples
1237 template<std::size_t _Ind, typename... _Tp>
1239 __volget(const volatile tuple<_Tp...>& __tuple)
1240 -> __tuple_element_t<_Ind, tuple<_Tp...>> const volatile&
1241 { return std::get<_Ind>(const_cast<const tuple<_Tp...>&>(__tuple)); }
1243 /// Type of the function object returned from bind().
1244 template<typename _Signature>
1247 template<typename _Functor, typename... _Bound_args>
1248 class _Bind<_Functor(_Bound_args...)>
1249 : public _Weak_result_type<_Functor>
1251 typedef _Bind __self_type;
1252 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
1256 tuple<_Bound_args...> _M_bound_args;
1259 template<typename _Result, typename... _Args, std::size_t... _Indexes>
1261 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>)
1263 return _M_f(_Mu<_Bound_args>()
1264 (std::get<_Indexes>(_M_bound_args), __args)...);
1268 template<typename _Result, typename... _Args, std::size_t... _Indexes>
1270 __call_c(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>) const
1272 return _M_f(_Mu<_Bound_args>()
1273 (std::get<_Indexes>(_M_bound_args), __args)...);
1277 template<typename _Result, typename... _Args, std::size_t... _Indexes>
1279 __call_v(tuple<_Args...>&& __args,
1280 _Index_tuple<_Indexes...>) volatile
1282 return _M_f(_Mu<_Bound_args>()
1283 (__volget<_Indexes>(_M_bound_args), __args)...);
1286 // Call as const volatile
1287 template<typename _Result, typename... _Args, std::size_t... _Indexes>
1289 __call_c_v(tuple<_Args...>&& __args,
1290 _Index_tuple<_Indexes...>) const volatile
1292 return _M_f(_Mu<_Bound_args>()
1293 (__volget<_Indexes>(_M_bound_args), __args)...);
1297 template<typename... _Args>
1298 explicit _Bind(const _Functor& __f, _Args&&... __args)
1299 : _M_f(__f), _M_bound_args(std::forward<_Args>(__args)...)
1302 template<typename... _Args>
1303 explicit _Bind(_Functor&& __f, _Args&&... __args)
1304 : _M_f(std::move(__f)), _M_bound_args(std::forward<_Args>(__args)...)
1307 _Bind(const _Bind&) = default;
1310 : _M_f(std::move(__b._M_f)), _M_bound_args(std::move(__b._M_bound_args))
1314 template<typename... _Args, typename _Result
1315 = decltype( std::declval<_Functor>()(
1316 _Mu<_Bound_args>()( std::declval<_Bound_args&>(),
1317 std::declval<tuple<_Args...>&>() )... ) )>
1319 operator()(_Args&&... __args)
1321 return this->__call<_Result>(
1322 std::forward_as_tuple(std::forward<_Args>(__args)...),
1327 template<typename... _Args, typename _Result
1328 = decltype( std::declval<typename enable_if<(sizeof...(_Args) >= 0),
1329 typename add_const<_Functor>::type>::type>()(
1330 _Mu<_Bound_args>()( std::declval<const _Bound_args&>(),
1331 std::declval<tuple<_Args...>&>() )... ) )>
1333 operator()(_Args&&... __args) const
1335 return this->__call_c<_Result>(
1336 std::forward_as_tuple(std::forward<_Args>(__args)...),
1341 template<typename... _Args, typename _Result
1342 = decltype( std::declval<typename enable_if<(sizeof...(_Args) >= 0),
1343 typename add_volatile<_Functor>::type>::type>()(
1344 _Mu<_Bound_args>()( std::declval<volatile _Bound_args&>(),
1345 std::declval<tuple<_Args...>&>() )... ) )>
1347 operator()(_Args&&... __args) volatile
1349 return this->__call_v<_Result>(
1350 std::forward_as_tuple(std::forward<_Args>(__args)...),
1354 // Call as const volatile
1355 template<typename... _Args, typename _Result
1356 = decltype( std::declval<typename enable_if<(sizeof...(_Args) >= 0),
1357 typename add_cv<_Functor>::type>::type>()(
1358 _Mu<_Bound_args>()( std::declval<const volatile _Bound_args&>(),
1359 std::declval<tuple<_Args...>&>() )... ) )>
1361 operator()(_Args&&... __args) const volatile
1363 return this->__call_c_v<_Result>(
1364 std::forward_as_tuple(std::forward<_Args>(__args)...),
1369 /// Type of the function object returned from bind<R>().
1370 template<typename _Result, typename _Signature>
1371 struct _Bind_result;
1373 template<typename _Result, typename _Functor, typename... _Bound_args>
1374 class _Bind_result<_Result, _Functor(_Bound_args...)>
1376 typedef _Bind_result __self_type;
1377 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
1381 tuple<_Bound_args...> _M_bound_args;
1384 template<typename _Res>
1385 struct __enable_if_void : enable_if<is_void<_Res>::value, int> { };
1386 template<typename _Res>
1387 struct __disable_if_void : enable_if<!is_void<_Res>::value, int> { };
1390 template<typename _Res, typename... _Args, std::size_t... _Indexes>
1392 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1393 typename __disable_if_void<_Res>::type = 0)
1395 return _M_f(_Mu<_Bound_args>()
1396 (std::get<_Indexes>(_M_bound_args), __args)...);
1399 // Call unqualified, return void
1400 template<typename _Res, typename... _Args, std::size_t... _Indexes>
1402 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1403 typename __enable_if_void<_Res>::type = 0)
1405 _M_f(_Mu<_Bound_args>()
1406 (std::get<_Indexes>(_M_bound_args), __args)...);
1410 template<typename _Res, typename... _Args, std::size_t... _Indexes>
1412 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1413 typename __disable_if_void<_Res>::type = 0) const
1415 return _M_f(_Mu<_Bound_args>()
1416 (std::get<_Indexes>(_M_bound_args), __args)...);
1419 // Call as const, return void
1420 template<typename _Res, typename... _Args, std::size_t... _Indexes>
1422 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1423 typename __enable_if_void<_Res>::type = 0) const
1425 _M_f(_Mu<_Bound_args>()
1426 (std::get<_Indexes>(_M_bound_args), __args)...);
1430 template<typename _Res, typename... _Args, std::size_t... _Indexes>
1432 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1433 typename __disable_if_void<_Res>::type = 0) volatile
1435 return _M_f(_Mu<_Bound_args>()
1436 (__volget<_Indexes>(_M_bound_args), __args)...);
1439 // Call as volatile, return void
1440 template<typename _Res, typename... _Args, std::size_t... _Indexes>
1442 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1443 typename __enable_if_void<_Res>::type = 0) volatile
1445 _M_f(_Mu<_Bound_args>()
1446 (__volget<_Indexes>(_M_bound_args), __args)...);
1449 // Call as const volatile
1450 template<typename _Res, typename... _Args, std::size_t... _Indexes>
1452 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1453 typename __disable_if_void<_Res>::type = 0) const volatile
1455 return _M_f(_Mu<_Bound_args>()
1456 (__volget<_Indexes>(_M_bound_args), __args)...);
1459 // Call as const volatile, return void
1460 template<typename _Res, typename... _Args, std::size_t... _Indexes>
1462 __call(tuple<_Args...>&& __args,
1463 _Index_tuple<_Indexes...>,
1464 typename __enable_if_void<_Res>::type = 0) const volatile
1466 _M_f(_Mu<_Bound_args>()
1467 (__volget<_Indexes>(_M_bound_args), __args)...);
1471 typedef _Result result_type;
1473 template<typename... _Args>
1474 explicit _Bind_result(const _Functor& __f, _Args&&... __args)
1475 : _M_f(__f), _M_bound_args(std::forward<_Args>(__args)...)
1478 template<typename... _Args>
1479 explicit _Bind_result(_Functor&& __f, _Args&&... __args)
1480 : _M_f(std::move(__f)), _M_bound_args(std::forward<_Args>(__args)...)
1483 _Bind_result(const _Bind_result&) = default;
1485 _Bind_result(_Bind_result&& __b)
1486 : _M_f(std::move(__b._M_f)), _M_bound_args(std::move(__b._M_bound_args))
1490 template<typename... _Args>
1492 operator()(_Args&&... __args)
1494 return this->__call<_Result>(
1495 std::forward_as_tuple(std::forward<_Args>(__args)...),
1500 template<typename... _Args>
1502 operator()(_Args&&... __args) const
1504 return this->__call<_Result>(
1505 std::forward_as_tuple(std::forward<_Args>(__args)...),
1510 template<typename... _Args>
1512 operator()(_Args&&... __args) volatile
1514 return this->__call<_Result>(
1515 std::forward_as_tuple(std::forward<_Args>(__args)...),
1519 // Call as const volatile
1520 template<typename... _Args>
1522 operator()(_Args&&... __args) const volatile
1524 return this->__call<_Result>(
1525 std::forward_as_tuple(std::forward<_Args>(__args)...),
1531 * @brief Class template _Bind is always a bind expression.
1534 template<typename _Signature>
1535 struct is_bind_expression<_Bind<_Signature> >
1536 : public true_type { };
1539 * @brief Class template _Bind is always a bind expression.
1542 template<typename _Signature>
1543 struct is_bind_expression<const _Bind<_Signature> >
1544 : public true_type { };
1547 * @brief Class template _Bind is always a bind expression.
1550 template<typename _Signature>
1551 struct is_bind_expression<volatile _Bind<_Signature> >
1552 : public true_type { };
1555 * @brief Class template _Bind is always a bind expression.
1558 template<typename _Signature>
1559 struct is_bind_expression<const volatile _Bind<_Signature>>
1560 : public true_type { };
1563 * @brief Class template _Bind_result is always a bind expression.
1566 template<typename _Result, typename _Signature>
1567 struct is_bind_expression<_Bind_result<_Result, _Signature>>
1568 : public true_type { };
1571 * @brief Class template _Bind_result is always a bind expression.
1574 template<typename _Result, typename _Signature>
1575 struct is_bind_expression<const _Bind_result<_Result, _Signature>>
1576 : public true_type { };
1579 * @brief Class template _Bind_result is always a bind expression.
1582 template<typename _Result, typename _Signature>
1583 struct is_bind_expression<volatile _Bind_result<_Result, _Signature>>
1584 : public true_type { };
1587 * @brief Class template _Bind_result is always a bind expression.
1590 template<typename _Result, typename _Signature>
1591 struct is_bind_expression<const volatile _Bind_result<_Result, _Signature>>
1592 : public true_type { };
1594 // Trait type used to remove std::bind() from overload set via SFINAE
1595 // when first argument has integer type, so that std::bind() will
1596 // not be a better match than ::bind() from the BSD Sockets API.
1597 template<typename _Tp, typename _Tp2 = typename decay<_Tp>::type>
1598 using __is_socketlike = __or_<is_integral<_Tp2>, is_enum<_Tp2>>;
1600 template<bool _SocketLike, typename _Func, typename... _BoundArgs>
1603 typedef _Maybe_wrap_member_pointer<typename decay<_Func>::type>
1605 typedef typename __maybe_type::type __func_type;
1606 typedef _Bind<__func_type(typename decay<_BoundArgs>::type...)> type;
1609 // Partial specialization for is_socketlike == true, does not define
1610 // nested type so std::bind() will not participate in overload resolution
1611 // when the first argument might be a socket file descriptor.
1612 template<typename _Func, typename... _BoundArgs>
1613 struct _Bind_helper<true, _Func, _BoundArgs...>
1617 * @brief Function template for std::bind.
1620 template<typename _Func, typename... _BoundArgs>
1622 _Bind_helper<__is_socketlike<_Func>::value, _Func, _BoundArgs...>::type
1623 bind(_Func&& __f, _BoundArgs&&... __args)
1625 typedef _Bind_helper<false, _Func, _BoundArgs...> __helper_type;
1626 typedef typename __helper_type::__maybe_type __maybe_type;
1627 typedef typename __helper_type::type __result_type;
1628 return __result_type(__maybe_type::__do_wrap(std::forward<_Func>(__f)),
1629 std::forward<_BoundArgs>(__args)...);
1632 template<typename _Result, typename _Func, typename... _BoundArgs>
1633 struct _Bindres_helper
1635 typedef _Maybe_wrap_member_pointer<typename decay<_Func>::type>
1637 typedef typename __maybe_type::type __functor_type;
1638 typedef _Bind_result<_Result,
1639 __functor_type(typename decay<_BoundArgs>::type...)>
1644 * @brief Function template for std::bind<R>.
1647 template<typename _Result, typename _Func, typename... _BoundArgs>
1649 typename _Bindres_helper<_Result, _Func, _BoundArgs...>::type
1650 bind(_Func&& __f, _BoundArgs&&... __args)
1652 typedef _Bindres_helper<_Result, _Func, _BoundArgs...> __helper_type;
1653 typedef typename __helper_type::__maybe_type __maybe_type;
1654 typedef typename __helper_type::type __result_type;
1655 return __result_type(__maybe_type::__do_wrap(std::forward<_Func>(__f)),
1656 std::forward<_BoundArgs>(__args)...);
1659 template<typename _Signature>
1660 struct _Bind_simple;
1662 template<typename _Callable, typename... _Args>
1663 struct _Bind_simple<_Callable(_Args...)>
1665 typedef typename result_of<_Callable(_Args...)>::type result_type;
1667 template<typename... _Args2, typename = typename
1668 enable_if< sizeof...(_Args) == sizeof...(_Args2)>::type>
1670 _Bind_simple(const _Callable& __callable, _Args2&&... __args)
1671 : _M_bound(__callable, std::forward<_Args2>(__args)...)
1674 template<typename... _Args2, typename = typename
1675 enable_if< sizeof...(_Args) == sizeof...(_Args2)>::type>
1677 _Bind_simple(_Callable&& __callable, _Args2&&... __args)
1678 : _M_bound(std::move(__callable), std::forward<_Args2>(__args)...)
1681 _Bind_simple(const _Bind_simple&) = default;
1682 _Bind_simple(_Bind_simple&&) = default;
1687 typedef typename _Build_index_tuple<sizeof...(_Args)>::__type _Indices;
1688 return _M_invoke(_Indices());
1693 template<std::size_t... _Indices>
1694 typename result_of<_Callable(_Args...)>::type
1695 _M_invoke(_Index_tuple<_Indices...>)
1697 // std::bind always forwards bound arguments as lvalues,
1698 // but this type can call functions which only accept rvalues.
1699 return std::forward<_Callable>(std::get<0>(_M_bound))(
1700 std::forward<_Args>(std::get<_Indices+1>(_M_bound))...);
1703 std::tuple<_Callable, _Args...> _M_bound;
1706 template<typename _Func, typename... _BoundArgs>
1707 struct _Bind_simple_helper
1709 typedef _Maybe_wrap_member_pointer<typename decay<_Func>::type>
1711 typedef typename __maybe_type::type __func_type;
1712 typedef _Bind_simple<__func_type(typename decay<_BoundArgs>::type...)>
1716 // Simplified version of std::bind for internal use, without support for
1717 // unbound arguments, placeholders or nested bind expressions.
1718 template<typename _Callable, typename... _Args>
1719 typename _Bind_simple_helper<_Callable, _Args...>::__type
1720 __bind_simple(_Callable&& __callable, _Args&&... __args)
1722 typedef _Bind_simple_helper<_Callable, _Args...> __helper_type;
1723 typedef typename __helper_type::__maybe_type __maybe_type;
1724 typedef typename __helper_type::__type __result_type;
1725 return __result_type(
1726 __maybe_type::__do_wrap( std::forward<_Callable>(__callable)),
1727 std::forward<_Args>(__args)...);
1731 * @brief Exception class thrown when class template function's
1732 * operator() is called with an empty target.
1733 * @ingroup exceptions
1735 class bad_function_call : public std::exception
1738 virtual ~bad_function_call() noexcept;
1740 const char* what() const noexcept;
1744 * Trait identifying "location-invariant" types, meaning that the
1745 * address of the object (or any of its members) will not escape.
1746 * Also implies a trivial copy constructor and assignment operator.
1748 template<typename _Tp>
1749 struct __is_location_invariant
1750 : __or_<is_pointer<_Tp>, is_member_pointer<_Tp>>::type
1753 class _Undefined_class;
1758 const void* _M_const_object;
1759 void (*_M_function_pointer)();
1760 void (_Undefined_class::*_M_member_pointer)();
1765 void* _M_access() { return &_M_pod_data[0]; }
1766 const void* _M_access() const { return &_M_pod_data[0]; }
1768 template<typename _Tp>
1771 { return *static_cast<_Tp*>(_M_access()); }
1773 template<typename _Tp>
1776 { return *static_cast<const _Tp*>(_M_access()); }
1778 _Nocopy_types _M_unused;
1779 char _M_pod_data[sizeof(_Nocopy_types)];
1782 enum _Manager_operation
1790 // Simple type wrapper that helps avoid annoying const problems
1791 // when casting between void pointers and pointers-to-pointers.
1792 template<typename _Tp>
1793 struct _Simple_type_wrapper
1795 _Simple_type_wrapper(_Tp __value) : __value(__value) { }
1800 template<typename _Tp>
1801 struct __is_location_invariant<_Simple_type_wrapper<_Tp> >
1802 : __is_location_invariant<_Tp>
1805 // Converts a reference to a function object into a callable
1807 template<typename _Functor>
1809 __callable_functor(_Functor& __f)
1812 template<typename _Member, typename _Class>
1813 inline _Mem_fn<_Member _Class::*>
1814 __callable_functor(_Member _Class::* &__p)
1815 { return std::mem_fn(__p); }
1817 template<typename _Member, typename _Class>
1818 inline _Mem_fn<_Member _Class::*>
1819 __callable_functor(_Member _Class::* const &__p)
1820 { return std::mem_fn(__p); }
1822 template<typename _Member, typename _Class>
1823 inline _Mem_fn<_Member _Class::*>
1824 __callable_functor(_Member _Class::* volatile &__p)
1825 { return std::mem_fn(__p); }
1827 template<typename _Member, typename _Class>
1828 inline _Mem_fn<_Member _Class::*>
1829 __callable_functor(_Member _Class::* const volatile &__p)
1830 { return std::mem_fn(__p); }
1832 template<typename _Signature>
1835 /// Base class of all polymorphic function object wrappers.
1836 class _Function_base
1839 static const std::size_t _M_max_size = sizeof(_Nocopy_types);
1840 static const std::size_t _M_max_align = __alignof__(_Nocopy_types);
1842 template<typename _Functor>
1846 static const bool __stored_locally =
1847 (__is_location_invariant<_Functor>::value
1848 && sizeof(_Functor) <= _M_max_size
1849 && __alignof__(_Functor) <= _M_max_align
1850 && (_M_max_align % __alignof__(_Functor) == 0));
1852 typedef integral_constant<bool, __stored_locally> _Local_storage;
1854 // Retrieve a pointer to the function object
1856 _M_get_pointer(const _Any_data& __source)
1858 const _Functor* __ptr =
1859 __stored_locally? std::__addressof(__source._M_access<_Functor>())
1860 /* have stored a pointer */ : __source._M_access<_Functor*>();
1861 return const_cast<_Functor*>(__ptr);
1864 // Clone a location-invariant function object that fits within
1865 // an _Any_data structure.
1867 _M_clone(_Any_data& __dest, const _Any_data& __source, true_type)
1869 new (__dest._M_access()) _Functor(__source._M_access<_Functor>());
1872 // Clone a function object that is not location-invariant or
1873 // that cannot fit into an _Any_data structure.
1875 _M_clone(_Any_data& __dest, const _Any_data& __source, false_type)
1877 __dest._M_access<_Functor*>() =
1878 new _Functor(*__source._M_access<_Functor*>());
1881 // Destroying a location-invariant object may still require
1884 _M_destroy(_Any_data& __victim, true_type)
1886 __victim._M_access<_Functor>().~_Functor();
1889 // Destroying an object located on the heap.
1891 _M_destroy(_Any_data& __victim, false_type)
1893 delete __victim._M_access<_Functor*>();
1898 _M_manager(_Any_data& __dest, const _Any_data& __source,
1899 _Manager_operation __op)
1904 case __get_type_info:
1905 __dest._M_access<const type_info*>() = &typeid(_Functor);
1908 case __get_functor_ptr:
1909 __dest._M_access<_Functor*>() = _M_get_pointer(__source);
1912 case __clone_functor:
1913 _M_clone(__dest, __source, _Local_storage());
1916 case __destroy_functor:
1917 _M_destroy(__dest, _Local_storage());
1924 _M_init_functor(_Any_data& __functor, _Functor&& __f)
1925 { _M_init_functor(__functor, std::move(__f), _Local_storage()); }
1927 template<typename _Signature>
1929 _M_not_empty_function(const function<_Signature>& __f)
1930 { return static_cast<bool>(__f); }
1932 template<typename _Tp>
1934 _M_not_empty_function(_Tp* const& __fp)
1937 template<typename _Class, typename _Tp>
1939 _M_not_empty_function(_Tp _Class::* const& __mp)
1942 template<typename _Tp>
1944 _M_not_empty_function(const _Tp&)
1949 _M_init_functor(_Any_data& __functor, _Functor&& __f, true_type)
1950 { new (__functor._M_access()) _Functor(std::move(__f)); }
1953 _M_init_functor(_Any_data& __functor, _Functor&& __f, false_type)
1954 { __functor._M_access<_Functor*>() = new _Functor(std::move(__f)); }
1957 template<typename _Functor>
1958 class _Ref_manager : public _Base_manager<_Functor*>
1960 typedef _Function_base::_Base_manager<_Functor*> _Base;
1964 _M_manager(_Any_data& __dest, const _Any_data& __source,
1965 _Manager_operation __op)
1970 case __get_type_info:
1971 __dest._M_access<const type_info*>() = &typeid(_Functor);
1974 case __get_functor_ptr:
1975 __dest._M_access<_Functor*>() = *_Base::_M_get_pointer(__source);
1976 return is_const<_Functor>::value;
1980 _Base::_M_manager(__dest, __source, __op);
1986 _M_init_functor(_Any_data& __functor, reference_wrapper<_Functor> __f)
1988 _Base::_M_init_functor(__functor, std::__addressof(__f.get()));
1992 _Function_base() : _M_manager(0) { }
1997 _M_manager(_M_functor, _M_functor, __destroy_functor);
2001 bool _M_empty() const { return !_M_manager; }
2003 typedef bool (*_Manager_type)(_Any_data&, const _Any_data&,
2004 _Manager_operation);
2006 _Any_data _M_functor;
2007 _Manager_type _M_manager;
2010 template<typename _Signature, typename _Functor>
2011 class _Function_handler;
2013 template<typename _Res, typename _Functor, typename... _ArgTypes>
2014 class _Function_handler<_Res(_ArgTypes...), _Functor>
2015 : public _Function_base::_Base_manager<_Functor>
2017 typedef _Function_base::_Base_manager<_Functor> _Base;
2021 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
2023 return (*_Base::_M_get_pointer(__functor))(
2024 std::forward<_ArgTypes>(__args)...);
2028 template<typename _Functor, typename... _ArgTypes>
2029 class _Function_handler<void(_ArgTypes...), _Functor>
2030 : public _Function_base::_Base_manager<_Functor>
2032 typedef _Function_base::_Base_manager<_Functor> _Base;
2036 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
2038 (*_Base::_M_get_pointer(__functor))(
2039 std::forward<_ArgTypes>(__args)...);
2043 template<typename _Res, typename _Functor, typename... _ArgTypes>
2044 class _Function_handler<_Res(_ArgTypes...), reference_wrapper<_Functor> >
2045 : public _Function_base::_Ref_manager<_Functor>
2047 typedef _Function_base::_Ref_manager<_Functor> _Base;
2051 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
2053 return __callable_functor(**_Base::_M_get_pointer(__functor))(
2054 std::forward<_ArgTypes>(__args)...);
2058 template<typename _Functor, typename... _ArgTypes>
2059 class _Function_handler<void(_ArgTypes...), reference_wrapper<_Functor> >
2060 : public _Function_base::_Ref_manager<_Functor>
2062 typedef _Function_base::_Ref_manager<_Functor> _Base;
2066 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
2068 __callable_functor(**_Base::_M_get_pointer(__functor))(
2069 std::forward<_ArgTypes>(__args)...);
2073 template<typename _Class, typename _Member, typename _Res,
2074 typename... _ArgTypes>
2075 class _Function_handler<_Res(_ArgTypes...), _Member _Class::*>
2076 : public _Function_handler<void(_ArgTypes...), _Member _Class::*>
2078 typedef _Function_handler<void(_ArgTypes...), _Member _Class::*>
2083 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
2085 return std::mem_fn(_Base::_M_get_pointer(__functor)->__value)(
2086 std::forward<_ArgTypes>(__args)...);
2090 template<typename _Class, typename _Member, typename... _ArgTypes>
2091 class _Function_handler<void(_ArgTypes...), _Member _Class::*>
2092 : public _Function_base::_Base_manager<
2093 _Simple_type_wrapper< _Member _Class::* > >
2095 typedef _Member _Class::* _Functor;
2096 typedef _Simple_type_wrapper<_Functor> _Wrapper;
2097 typedef _Function_base::_Base_manager<_Wrapper> _Base;
2101 _M_manager(_Any_data& __dest, const _Any_data& __source,
2102 _Manager_operation __op)
2107 case __get_type_info:
2108 __dest._M_access<const type_info*>() = &typeid(_Functor);
2111 case __get_functor_ptr:
2112 __dest._M_access<_Functor*>() =
2113 &_Base::_M_get_pointer(__source)->__value;
2117 _Base::_M_manager(__dest, __source, __op);
2123 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
2125 std::mem_fn(_Base::_M_get_pointer(__functor)->__value)(
2126 std::forward<_ArgTypes>(__args)...);
2130 template<typename _From, typename _To>
2131 using __check_func_return_type
2132 = __or_<is_void<_To>, is_convertible<_From, _To>>;
2135 * @brief Primary class template for std::function.
2138 * Polymorphic function wrapper.
2140 template<typename _Res, typename... _ArgTypes>
2141 class function<_Res(_ArgTypes...)>
2142 : public _Maybe_unary_or_binary_function<_Res, _ArgTypes...>,
2143 private _Function_base
2145 typedef _Res _Signature_type(_ArgTypes...);
2147 template<typename _Functor>
2148 using _Invoke = decltype(__callable_functor(std::declval<_Functor&>())
2149 (std::declval<_ArgTypes>()...) );
2151 // Used so the return type convertibility checks aren't done when
2152 // performing overload resolution for copy construction/assignment.
2153 template<typename _Tp>
2154 using _NotSelf = __not_<is_same<_Tp, function>>;
2156 template<typename _Functor>
2158 = __and_<_NotSelf<_Functor>,
2159 __check_func_return_type<_Invoke<_Functor>, _Res>>;
2161 template<typename _Cond, typename _Tp>
2162 using _Requires = typename enable_if<_Cond::value, _Tp>::type;
2165 typedef _Res result_type;
2167 // [3.7.2.1] construct/copy/destroy
2170 * @brief Default construct creates an empty function call wrapper.
2171 * @post @c !(bool)*this
2174 : _Function_base() { }
2177 * @brief Creates an empty function call wrapper.
2178 * @post @c !(bool)*this
2180 function(nullptr_t) noexcept
2181 : _Function_base() { }
2184 * @brief %Function copy constructor.
2185 * @param __x A %function object with identical call signature.
2186 * @post @c bool(*this) == bool(__x)
2188 * The newly-created %function contains a copy of the target of @a
2189 * __x (if it has one).
2191 function(const function& __x);
2194 * @brief %Function move constructor.
2195 * @param __x A %function object rvalue with identical call signature.
2197 * The newly-created %function contains the target of @a __x
2200 function(function&& __x) : _Function_base()
2205 // TODO: needs allocator_arg_t
2208 * @brief Builds a %function that targets a copy of the incoming
2210 * @param __f A %function object that is callable with parameters of
2211 * type @c T1, @c T2, ..., @c TN and returns a value convertible
2214 * The newly-created %function object will target a copy of
2215 * @a __f. If @a __f is @c reference_wrapper<F>, then this function
2216 * object will contain a reference to the function object @c
2217 * __f.get(). If @a __f is a NULL function pointer or NULL
2218 * pointer-to-member, the newly-created object will be empty.
2220 * If @a __f is a non-NULL function pointer or an object of type @c
2221 * reference_wrapper<F>, this function will not throw.
2223 template<typename _Functor,
2224 typename = _Requires<_Callable<_Functor>, void>>
2228 * @brief %Function assignment operator.
2229 * @param __x A %function with identical call signature.
2230 * @post @c (bool)*this == (bool)x
2233 * The target of @a __x is copied to @c *this. If @a __x has no
2234 * target, then @c *this will be empty.
2236 * If @a __x targets a function pointer or a reference to a function
2237 * object, then this operation will not throw an %exception.
2240 operator=(const function& __x)
2242 function(__x).swap(*this);
2247 * @brief %Function move-assignment operator.
2248 * @param __x A %function rvalue with identical call signature.
2251 * The target of @a __x is moved to @c *this. If @a __x has no
2252 * target, then @c *this will be empty.
2254 * If @a __x targets a function pointer or a reference to a function
2255 * object, then this operation will not throw an %exception.
2258 operator=(function&& __x)
2260 function(std::move(__x)).swap(*this);
2265 * @brief %Function assignment to zero.
2266 * @post @c !(bool)*this
2269 * The target of @c *this is deallocated, leaving it empty.
2272 operator=(nullptr_t)
2276 _M_manager(_M_functor, _M_functor, __destroy_functor);
2284 * @brief %Function assignment to a new target.
2285 * @param __f A %function object that is callable with parameters of
2286 * type @c T1, @c T2, ..., @c TN and returns a value convertible
2290 * This %function object wrapper will target a copy of @a
2291 * __f. If @a __f is @c reference_wrapper<F>, then this function
2292 * object will contain a reference to the function object @c
2293 * __f.get(). If @a __f is a NULL function pointer or NULL
2294 * pointer-to-member, @c this object will be empty.
2296 * If @a __f is a non-NULL function pointer or an object of type @c
2297 * reference_wrapper<F>, this function will not throw.
2299 template<typename _Functor>
2300 _Requires<_Callable<typename decay<_Functor>::type>, function&>
2301 operator=(_Functor&& __f)
2303 function(std::forward<_Functor>(__f)).swap(*this);
2308 template<typename _Functor>
2310 operator=(reference_wrapper<_Functor> __f) noexcept
2312 function(__f).swap(*this);
2316 // [3.7.2.2] function modifiers
2319 * @brief Swap the targets of two %function objects.
2320 * @param __x A %function with identical call signature.
2322 * Swap the targets of @c this function object and @a __f. This
2323 * function will not throw an %exception.
2325 void swap(function& __x)
2327 std::swap(_M_functor, __x._M_functor);
2328 std::swap(_M_manager, __x._M_manager);
2329 std::swap(_M_invoker, __x._M_invoker);
2332 // TODO: needs allocator_arg_t
2334 template<typename _Functor, typename _Alloc>
2336 assign(_Functor&& __f, const _Alloc& __a)
2338 function(allocator_arg, __a,
2339 std::forward<_Functor>(__f)).swap(*this);
2343 // [3.7.2.3] function capacity
2346 * @brief Determine if the %function wrapper has a target.
2348 * @return @c true when this %function object contains a target,
2349 * or @c false when it is empty.
2351 * This function will not throw an %exception.
2353 explicit operator bool() const noexcept
2354 { return !_M_empty(); }
2356 // [3.7.2.4] function invocation
2359 * @brief Invokes the function targeted by @c *this.
2360 * @returns the result of the target.
2361 * @throws bad_function_call when @c !(bool)*this
2363 * The function call operator invokes the target function object
2364 * stored by @c this.
2366 _Res operator()(_ArgTypes... __args) const;
2369 // [3.7.2.5] function target access
2371 * @brief Determine the type of the target of this function object
2374 * @returns the type identifier of the target function object, or
2375 * @c typeid(void) if @c !(bool)*this.
2377 * This function will not throw an %exception.
2379 const type_info& target_type() const noexcept;
2382 * @brief Access the stored target function object.
2384 * @return Returns a pointer to the stored target function object,
2385 * if @c typeid(Functor).equals(target_type()); otherwise, a NULL
2388 * This function will not throw an %exception.
2390 template<typename _Functor> _Functor* target() noexcept;
2393 template<typename _Functor> const _Functor* target() const noexcept;
2397 typedef _Res (*_Invoker_type)(const _Any_data&, _ArgTypes...);
2398 _Invoker_type _M_invoker;
2401 // Out-of-line member definitions.
2402 template<typename _Res, typename... _ArgTypes>
2403 function<_Res(_ArgTypes...)>::
2404 function(const function& __x)
2407 if (static_cast<bool>(__x))
2409 _M_invoker = __x._M_invoker;
2410 _M_manager = __x._M_manager;
2411 __x._M_manager(_M_functor, __x._M_functor, __clone_functor);
2415 template<typename _Res, typename... _ArgTypes>
2416 template<typename _Functor, typename>
2417 function<_Res(_ArgTypes...)>::
2418 function(_Functor __f)
2421 typedef _Function_handler<_Signature_type, _Functor> _My_handler;
2423 if (_My_handler::_M_not_empty_function(__f))
2425 _My_handler::_M_init_functor(_M_functor, std::move(__f));
2426 _M_invoker = &_My_handler::_M_invoke;
2427 _M_manager = &_My_handler::_M_manager;
2431 template<typename _Res, typename... _ArgTypes>
2433 function<_Res(_ArgTypes...)>::
2434 operator()(_ArgTypes... __args) const
2437 __throw_bad_function_call();
2438 return _M_invoker(_M_functor, std::forward<_ArgTypes>(__args)...);
2442 template<typename _Res, typename... _ArgTypes>
2444 function<_Res(_ArgTypes...)>::
2445 target_type() const noexcept
2449 _Any_data __typeinfo_result;
2450 _M_manager(__typeinfo_result, _M_functor, __get_type_info);
2451 return *__typeinfo_result._M_access<const type_info*>();
2454 return typeid(void);
2457 template<typename _Res, typename... _ArgTypes>
2458 template<typename _Functor>
2460 function<_Res(_ArgTypes...)>::
2463 if (typeid(_Functor) == target_type() && _M_manager)
2466 if (_M_manager(__ptr, _M_functor, __get_functor_ptr)
2467 && !is_const<_Functor>::value)
2470 return __ptr._M_access<_Functor*>();
2476 template<typename _Res, typename... _ArgTypes>
2477 template<typename _Functor>
2479 function<_Res(_ArgTypes...)>::
2480 target() const noexcept
2482 if (typeid(_Functor) == target_type() && _M_manager)
2485 _M_manager(__ptr, _M_functor, __get_functor_ptr);
2486 return __ptr._M_access<const _Functor*>();
2493 // [20.7.15.2.6] null pointer comparisons
2496 * @brief Compares a polymorphic function object wrapper against 0
2497 * (the NULL pointer).
2498 * @returns @c true if the wrapper has no target, @c false otherwise
2500 * This function will not throw an %exception.
2502 template<typename _Res, typename... _Args>
2504 operator==(const function<_Res(_Args...)>& __f, nullptr_t) noexcept
2505 { return !static_cast<bool>(__f); }
2508 template<typename _Res, typename... _Args>
2510 operator==(nullptr_t, const function<_Res(_Args...)>& __f) noexcept
2511 { return !static_cast<bool>(__f); }
2514 * @brief Compares a polymorphic function object wrapper against 0
2515 * (the NULL pointer).
2516 * @returns @c false if the wrapper has no target, @c true otherwise
2518 * This function will not throw an %exception.
2520 template<typename _Res, typename... _Args>
2522 operator!=(const function<_Res(_Args...)>& __f, nullptr_t) noexcept
2523 { return static_cast<bool>(__f); }
2526 template<typename _Res, typename... _Args>
2528 operator!=(nullptr_t, const function<_Res(_Args...)>& __f) noexcept
2529 { return static_cast<bool>(__f); }
2531 // [20.7.15.2.7] specialized algorithms
2534 * @brief Swap the targets of two polymorphic function object wrappers.
2536 * This function will not throw an %exception.
2538 template<typename _Res, typename... _Args>
2540 swap(function<_Res(_Args...)>& __x, function<_Res(_Args...)>& __y)
2543 _GLIBCXX_END_NAMESPACE_VERSION
2548 #endif // _GLIBCXX_FUNCTIONAL