1 // <functional> -*- C++ -*-
3 // Copyright (C) 2001-2016 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 /// If we have found a result_type, extract it.
71 template<typename _Functor, typename = __void_t<>>
72 struct _Maybe_get_result_type
75 template<typename _Functor>
76 struct _Maybe_get_result_type<_Functor,
77 __void_t<typename _Functor::result_type>>
78 { typedef typename _Functor::result_type result_type; };
81 * Base class for any function object that has a weak result type, as
82 * defined in 20.8.2 [func.require] of C++11.
84 template<typename _Functor>
85 struct _Weak_result_type_impl
86 : _Maybe_get_result_type<_Functor>
89 /// Retrieve the result type for a function type.
90 template<typename _Res, typename... _ArgTypes>
91 struct _Weak_result_type_impl<_Res(_ArgTypes...)>
92 { typedef _Res result_type; };
94 template<typename _Res, typename... _ArgTypes>
95 struct _Weak_result_type_impl<_Res(_ArgTypes......)>
96 { typedef _Res result_type; };
98 template<typename _Res, typename... _ArgTypes>
99 struct _Weak_result_type_impl<_Res(_ArgTypes...) const>
100 { typedef _Res result_type; };
102 template<typename _Res, typename... _ArgTypes>
103 struct _Weak_result_type_impl<_Res(_ArgTypes......) const>
104 { typedef _Res result_type; };
106 template<typename _Res, typename... _ArgTypes>
107 struct _Weak_result_type_impl<_Res(_ArgTypes...) volatile>
108 { typedef _Res result_type; };
110 template<typename _Res, typename... _ArgTypes>
111 struct _Weak_result_type_impl<_Res(_ArgTypes......) volatile>
112 { typedef _Res result_type; };
114 template<typename _Res, typename... _ArgTypes>
115 struct _Weak_result_type_impl<_Res(_ArgTypes...) const volatile>
116 { typedef _Res result_type; };
118 template<typename _Res, typename... _ArgTypes>
119 struct _Weak_result_type_impl<_Res(_ArgTypes......) const volatile>
120 { typedef _Res result_type; };
122 /// Retrieve the result type for a function reference.
123 template<typename _Res, typename... _ArgTypes>
124 struct _Weak_result_type_impl<_Res(&)(_ArgTypes...)>
125 { typedef _Res result_type; };
127 template<typename _Res, typename... _ArgTypes>
128 struct _Weak_result_type_impl<_Res(&)(_ArgTypes......)>
129 { typedef _Res result_type; };
131 /// Retrieve the result type for a function pointer.
132 template<typename _Res, typename... _ArgTypes>
133 struct _Weak_result_type_impl<_Res(*)(_ArgTypes...)>
134 { typedef _Res result_type; };
136 template<typename _Res, typename... _ArgTypes>
137 struct _Weak_result_type_impl<_Res(*)(_ArgTypes......)>
138 { typedef _Res result_type; };
140 /// Retrieve result type for a member function pointer.
141 template<typename _Res, typename _Class, typename... _ArgTypes>
142 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...)>
143 { typedef _Res result_type; };
145 template<typename _Res, typename _Class, typename... _ArgTypes>
146 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes......)>
147 { typedef _Res result_type; };
149 /// Retrieve result type for a const member function pointer.
150 template<typename _Res, typename _Class, typename... _ArgTypes>
151 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) const>
152 { typedef _Res result_type; };
154 template<typename _Res, typename _Class, typename... _ArgTypes>
155 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes......) const>
156 { typedef _Res result_type; };
158 /// Retrieve result type for a volatile member function pointer.
159 template<typename _Res, typename _Class, typename... _ArgTypes>
160 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) volatile>
161 { typedef _Res result_type; };
163 template<typename _Res, typename _Class, typename... _ArgTypes>
164 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes......) volatile>
165 { typedef _Res result_type; };
167 /// Retrieve result type for a const volatile member function pointer.
168 template<typename _Res, typename _Class, typename... _ArgTypes>
169 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...)
171 { typedef _Res result_type; };
173 template<typename _Res, typename _Class, typename... _ArgTypes>
174 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes......)
176 { typedef _Res result_type; };
179 * Strip top-level cv-qualifiers from the function object and let
180 * _Weak_result_type_impl perform the real work.
182 template<typename _Functor>
183 struct _Weak_result_type
184 : _Weak_result_type_impl<typename remove_cv<_Functor>::type>
187 template<typename _Tp, typename _Up = typename decay<_Tp>::type>
193 template<typename _Tp, typename _Up>
194 struct __inv_unwrap<_Tp, reference_wrapper<_Up>>
199 // Used by __invoke_impl instead of std::forward<_Tp> so that a
200 // reference_wrapper is converted to an lvalue-reference.
201 template<typename _Tp, typename _Up = typename __inv_unwrap<_Tp>::type>
203 __invfwd(typename remove_reference<_Tp>::type& __t) noexcept
204 { return static_cast<_Up&&>(__t); }
206 template<typename _Res, typename _Fn, typename... _Args>
208 __invoke_impl(__invoke_other, _Fn&& __f, _Args&&... __args)
209 noexcept(noexcept(std::forward<_Fn>(__f)(std::forward<_Args>(__args)...)))
210 { return std::forward<_Fn>(__f)(std::forward<_Args>(__args)...); }
212 template<typename _Res, typename _MemFun, typename _Tp, typename... _Args>
214 __invoke_impl(__invoke_memfun_ref, _MemFun&& __f, _Tp&& __t,
217 (__invfwd<_Tp>(__t).*__f)(std::forward<_Args>(__args)...)))
218 { return (__invfwd<_Tp>(__t).*__f)(std::forward<_Args>(__args)...); }
220 template<typename _Res, typename _MemFun, typename _Tp, typename... _Args>
222 __invoke_impl(__invoke_memfun_deref, _MemFun&& __f, _Tp&& __t,
225 ((*std::forward<_Tp>(__t)).*__f)(std::forward<_Args>(__args)...)))
227 return ((*std::forward<_Tp>(__t)).*__f)(std::forward<_Args>(__args)...);
230 template<typename _Res, typename _MemPtr, typename _Tp>
232 __invoke_impl(__invoke_memobj_ref, _MemPtr&& __f, _Tp&& __t)
233 noexcept(noexcept(__invfwd<_Tp>(__t).*__f))
234 { return __invfwd<_Tp>(__t).*__f; }
236 template<typename _Res, typename _MemPtr, typename _Tp>
238 __invoke_impl(__invoke_memobj_deref, _MemPtr&& __f, _Tp&& __t)
239 noexcept(noexcept((*std::forward<_Tp>(__t)).*__f))
240 { return (*std::forward<_Tp>(__t)).*__f; }
242 /// Invoke a callable object.
243 template<typename _Callable, typename... _Args>
244 inline typename result_of<_Callable&&(_Args&&...)>::type
245 __invoke(_Callable&& __fn, _Args&&... __args)
247 using __result_of = result_of<_Callable&&(_Args&&...)>;
248 using __type = typename __result_of::type;
249 using __tag = typename __result_of::__invoke_type;
250 return std::__invoke_impl<__type>(__tag{}, std::forward<_Callable>(__fn),
251 std::forward<_Args>(__args)...);
254 #if __cplusplus > 201402L
255 # define __cpp_lib_invoke 201411
257 /// Invoke a callable object.
258 template<typename _Callable, typename... _Args>
259 inline result_of_t<_Callable&&(_Args&&...)>
260 invoke(_Callable&& __fn, _Args&&... __args)
262 return std::__invoke(std::forward<_Callable>(__fn),
263 std::forward<_Args>(__args)...);
268 * Knowing which of unary_function and binary_function _Tp derives
269 * from, derives from the same and ensures that reference_wrapper
270 * will have a weak result type. See cases below.
272 template<bool _Unary, bool _Binary, typename _Tp>
273 struct _Reference_wrapper_base_impl;
275 // None of the nested argument types.
276 template<typename _Tp>
277 struct _Reference_wrapper_base_impl<false, false, _Tp>
278 : _Weak_result_type<_Tp>
281 // Nested argument_type only.
282 template<typename _Tp>
283 struct _Reference_wrapper_base_impl<true, false, _Tp>
284 : _Weak_result_type<_Tp>
286 typedef typename _Tp::argument_type argument_type;
289 // Nested first_argument_type and second_argument_type only.
290 template<typename _Tp>
291 struct _Reference_wrapper_base_impl<false, true, _Tp>
292 : _Weak_result_type<_Tp>
294 typedef typename _Tp::first_argument_type first_argument_type;
295 typedef typename _Tp::second_argument_type second_argument_type;
298 // All the nested argument types.
299 template<typename _Tp>
300 struct _Reference_wrapper_base_impl<true, true, _Tp>
301 : _Weak_result_type<_Tp>
303 typedef typename _Tp::argument_type argument_type;
304 typedef typename _Tp::first_argument_type first_argument_type;
305 typedef typename _Tp::second_argument_type second_argument_type;
308 _GLIBCXX_HAS_NESTED_TYPE(argument_type)
309 _GLIBCXX_HAS_NESTED_TYPE(first_argument_type)
310 _GLIBCXX_HAS_NESTED_TYPE(second_argument_type)
313 * Derives from unary_function or binary_function when it
314 * can. Specializations handle all of the easy cases. The primary
315 * template determines what to do with a class type, which may
316 * derive from both unary_function and binary_function.
318 template<typename _Tp>
319 struct _Reference_wrapper_base
320 : _Reference_wrapper_base_impl<
321 __has_argument_type<_Tp>::value,
322 __has_first_argument_type<_Tp>::value
323 && __has_second_argument_type<_Tp>::value,
327 // - a function type (unary)
328 template<typename _Res, typename _T1>
329 struct _Reference_wrapper_base<_Res(_T1)>
330 : unary_function<_T1, _Res>
333 template<typename _Res, typename _T1>
334 struct _Reference_wrapper_base<_Res(_T1) const>
335 : unary_function<_T1, _Res>
338 template<typename _Res, typename _T1>
339 struct _Reference_wrapper_base<_Res(_T1) volatile>
340 : unary_function<_T1, _Res>
343 template<typename _Res, typename _T1>
344 struct _Reference_wrapper_base<_Res(_T1) const volatile>
345 : unary_function<_T1, _Res>
348 // - a function type (binary)
349 template<typename _Res, typename _T1, typename _T2>
350 struct _Reference_wrapper_base<_Res(_T1, _T2)>
351 : binary_function<_T1, _T2, _Res>
354 template<typename _Res, typename _T1, typename _T2>
355 struct _Reference_wrapper_base<_Res(_T1, _T2) const>
356 : binary_function<_T1, _T2, _Res>
359 template<typename _Res, typename _T1, typename _T2>
360 struct _Reference_wrapper_base<_Res(_T1, _T2) volatile>
361 : binary_function<_T1, _T2, _Res>
364 template<typename _Res, typename _T1, typename _T2>
365 struct _Reference_wrapper_base<_Res(_T1, _T2) const volatile>
366 : binary_function<_T1, _T2, _Res>
369 // - a function pointer type (unary)
370 template<typename _Res, typename _T1>
371 struct _Reference_wrapper_base<_Res(*)(_T1)>
372 : unary_function<_T1, _Res>
375 // - a function pointer type (binary)
376 template<typename _Res, typename _T1, typename _T2>
377 struct _Reference_wrapper_base<_Res(*)(_T1, _T2)>
378 : binary_function<_T1, _T2, _Res>
381 // - a pointer to member function type (unary, no qualifiers)
382 template<typename _Res, typename _T1>
383 struct _Reference_wrapper_base<_Res (_T1::*)()>
384 : unary_function<_T1*, _Res>
387 // - a pointer to member function type (binary, no qualifiers)
388 template<typename _Res, typename _T1, typename _T2>
389 struct _Reference_wrapper_base<_Res (_T1::*)(_T2)>
390 : binary_function<_T1*, _T2, _Res>
393 // - a pointer to member function type (unary, const)
394 template<typename _Res, typename _T1>
395 struct _Reference_wrapper_base<_Res (_T1::*)() const>
396 : unary_function<const _T1*, _Res>
399 // - a pointer to member function type (binary, const)
400 template<typename _Res, typename _T1, typename _T2>
401 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const>
402 : binary_function<const _T1*, _T2, _Res>
405 // - a pointer to member function type (unary, volatile)
406 template<typename _Res, typename _T1>
407 struct _Reference_wrapper_base<_Res (_T1::*)() volatile>
408 : unary_function<volatile _T1*, _Res>
411 // - a pointer to member function type (binary, volatile)
412 template<typename _Res, typename _T1, typename _T2>
413 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) volatile>
414 : binary_function<volatile _T1*, _T2, _Res>
417 // - a pointer to member function type (unary, const volatile)
418 template<typename _Res, typename _T1>
419 struct _Reference_wrapper_base<_Res (_T1::*)() const volatile>
420 : unary_function<const volatile _T1*, _Res>
423 // - a pointer to member function type (binary, const volatile)
424 template<typename _Res, typename _T1, typename _T2>
425 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const volatile>
426 : binary_function<const volatile _T1*, _T2, _Res>
430 * @brief Primary class template for reference_wrapper.
434 template<typename _Tp>
435 class reference_wrapper
436 : public _Reference_wrapper_base<typename remove_cv<_Tp>::type>
443 reference_wrapper(_Tp& __indata) noexcept
444 : _M_data(std::__addressof(__indata))
447 reference_wrapper(_Tp&&) = delete;
449 reference_wrapper(const reference_wrapper&) = default;
452 operator=(const reference_wrapper&) = default;
454 operator _Tp&() const noexcept
455 { return this->get(); }
461 template<typename... _Args>
462 typename result_of<_Tp&(_Args&&...)>::type
463 operator()(_Args&&... __args) const
465 return std::__invoke(get(), std::forward<_Args>(__args)...);
470 /// Denotes a reference should be taken to a variable.
471 template<typename _Tp>
472 inline reference_wrapper<_Tp>
473 ref(_Tp& __t) noexcept
474 { return reference_wrapper<_Tp>(__t); }
476 /// Denotes a const reference should be taken to a variable.
477 template<typename _Tp>
478 inline reference_wrapper<const _Tp>
479 cref(const _Tp& __t) noexcept
480 { return reference_wrapper<const _Tp>(__t); }
482 template<typename _Tp>
483 void ref(const _Tp&&) = delete;
485 template<typename _Tp>
486 void cref(const _Tp&&) = delete;
488 /// Partial specialization.
489 template<typename _Tp>
490 inline reference_wrapper<_Tp>
491 ref(reference_wrapper<_Tp> __t) noexcept
492 { return ref(__t.get()); }
494 /// Partial specialization.
495 template<typename _Tp>
496 inline reference_wrapper<const _Tp>
497 cref(reference_wrapper<_Tp> __t) noexcept
498 { return cref(__t.get()); }
502 template<typename... _Types>
503 struct _Pack : integral_constant<size_t, sizeof...(_Types)>
506 template<typename _From, typename _To, bool = _From::value == _To::value>
507 struct _AllConvertible : false_type
510 template<typename... _From, typename... _To>
511 struct _AllConvertible<_Pack<_From...>, _Pack<_To...>, true>
512 : __and_<is_convertible<_From, _To>...>
515 template<typename _Tp1, typename _Tp2>
516 using _NotSame = __not_<is_same<typename std::decay<_Tp1>::type,
517 typename std::decay<_Tp2>::type>>;
520 * Derives from @c unary_function or @c binary_function, or perhaps
521 * nothing, depending on the number of arguments provided. The
522 * primary template is the basis case, which derives nothing.
524 template<typename _Res, typename... _ArgTypes>
525 struct _Maybe_unary_or_binary_function { };
527 /// Derives from @c unary_function, as appropriate.
528 template<typename _Res, typename _T1>
529 struct _Maybe_unary_or_binary_function<_Res, _T1>
530 : std::unary_function<_T1, _Res> { };
532 /// Derives from @c binary_function, as appropriate.
533 template<typename _Res, typename _T1, typename _T2>
534 struct _Maybe_unary_or_binary_function<_Res, _T1, _T2>
535 : std::binary_function<_T1, _T2, _Res> { };
537 template<typename _Signature>
538 struct _Mem_fn_traits;
540 template<typename _Res, typename _Class, typename... _ArgTypes>
541 struct _Mem_fn_traits_base
543 using __result_type = _Res;
545 = _Maybe_unary_or_binary_function<_Res, _Class*, _ArgTypes...>;
546 using __arity = integral_constant<size_t, sizeof...(_ArgTypes)>;
549 #define _GLIBCXX_MEM_FN_TRAITS2(_CV, _REF, _LVAL, _RVAL) \
550 template<typename _Res, typename _Class, typename... _ArgTypes> \
551 struct _Mem_fn_traits<_Res (_Class::*)(_ArgTypes...) _CV _REF> \
552 : _Mem_fn_traits_base<_Res, _CV _Class, _ArgTypes...> \
554 using __vararg = false_type; \
556 template<typename _Res, typename _Class, typename... _ArgTypes> \
557 struct _Mem_fn_traits<_Res (_Class::*)(_ArgTypes... ...) _CV _REF> \
558 : _Mem_fn_traits_base<_Res, _CV _Class, _ArgTypes...> \
560 using __vararg = true_type; \
563 #define _GLIBCXX_MEM_FN_TRAITS(_REF, _LVAL, _RVAL) \
564 _GLIBCXX_MEM_FN_TRAITS2( , _REF, _LVAL, _RVAL) \
565 _GLIBCXX_MEM_FN_TRAITS2(const , _REF, _LVAL, _RVAL) \
566 _GLIBCXX_MEM_FN_TRAITS2(volatile , _REF, _LVAL, _RVAL) \
567 _GLIBCXX_MEM_FN_TRAITS2(const volatile, _REF, _LVAL, _RVAL)
569 _GLIBCXX_MEM_FN_TRAITS( , true_type, true_type)
570 _GLIBCXX_MEM_FN_TRAITS(&, true_type, false_type)
571 _GLIBCXX_MEM_FN_TRAITS(&&, false_type, true_type)
573 #undef _GLIBCXX_MEM_FN_TRAITS
574 #undef _GLIBCXX_MEM_FN_TRAITS2
576 template<typename _MemFunPtr,
577 bool __is_mem_fn = is_member_function_pointer<_MemFunPtr>::value>
579 : public _Mem_fn_traits<_MemFunPtr>::__maybe_type
581 using _Traits = _Mem_fn_traits<_MemFunPtr>;
583 using _Arity = typename _Traits::__arity;
584 using _Varargs = typename _Traits::__vararg;
586 template<typename _Func, typename... _BoundArgs>
587 friend struct _Bind_check_arity;
593 using result_type = typename _Traits::__result_type;
596 _Mem_fn_base(_MemFunPtr __pmf) noexcept : _M_pmf(__pmf) { }
598 template<typename... _Args>
600 operator()(_Args&&... __args) const
602 std::__invoke(_M_pmf, std::forward<_Args>(__args)...)))
603 -> decltype(std::__invoke(_M_pmf, std::forward<_Args>(__args)...))
604 { return std::__invoke(_M_pmf, std::forward<_Args>(__args)...); }
607 // Partial specialization for member object pointers.
608 template<typename _MemObjPtr>
609 class _Mem_fn_base<_MemObjPtr, false>
611 using _Arity = integral_constant<size_t, 0>;
612 using _Varargs = false_type;
614 template<typename _Func, typename... _BoundArgs>
615 friend struct _Bind_check_arity;
621 _Mem_fn_base(_MemObjPtr __pm) noexcept : _M_pm(__pm) { }
623 template<typename _Tp>
625 operator()(_Tp&& __obj) const
626 noexcept(noexcept(std::__invoke(_M_pm, std::forward<_Tp>(__obj))))
627 -> decltype(std::__invoke(_M_pm, std::forward<_Tp>(__obj)))
628 { return std::__invoke(_M_pm, std::forward<_Tp>(__obj)); }
631 template<typename _Res, typename _Class>
632 struct _Mem_fn<_Res _Class::*>
633 : _Mem_fn_base<_Res _Class::*>
635 using _Mem_fn_base<_Res _Class::*>::_Mem_fn_base;
638 // _GLIBCXX_RESOLVE_LIB_DEFECTS
639 // 2048. Unnecessary mem_fn overloads
641 * @brief Returns a function object that forwards to the member
645 template<typename _Tp, typename _Class>
646 inline _Mem_fn<_Tp _Class::*>
647 mem_fn(_Tp _Class::* __pm) noexcept
649 return _Mem_fn<_Tp _Class::*>(__pm);
653 * @brief Determines if the given type _Tp is a function object that
654 * should be treated as a subexpression when evaluating calls to
655 * function objects returned by bind().
657 * C++11 [func.bind.isbind].
660 template<typename _Tp>
661 struct is_bind_expression
662 : public false_type { };
665 * @brief Determines if the given type _Tp is a placeholder in a
666 * bind() expression and, if so, which placeholder it is.
668 * C++11 [func.bind.isplace].
671 template<typename _Tp>
672 struct is_placeholder
673 : public integral_constant<int, 0>
676 /** @brief The type of placeholder objects defined by libstdc++.
679 template<int _Num> struct _Placeholder { };
681 _GLIBCXX_END_NAMESPACE_VERSION
683 /** @namespace std::placeholders
684 * @brief ISO C++11 entities sub-namespace for functional.
687 namespace placeholders
689 _GLIBCXX_BEGIN_NAMESPACE_VERSION
690 /* Define a large number of placeholders. There is no way to
691 * simplify this with variadic templates, because we're introducing
692 * unique names for each.
694 extern const _Placeholder<1> _1;
695 extern const _Placeholder<2> _2;
696 extern const _Placeholder<3> _3;
697 extern const _Placeholder<4> _4;
698 extern const _Placeholder<5> _5;
699 extern const _Placeholder<6> _6;
700 extern const _Placeholder<7> _7;
701 extern const _Placeholder<8> _8;
702 extern const _Placeholder<9> _9;
703 extern const _Placeholder<10> _10;
704 extern const _Placeholder<11> _11;
705 extern const _Placeholder<12> _12;
706 extern const _Placeholder<13> _13;
707 extern const _Placeholder<14> _14;
708 extern const _Placeholder<15> _15;
709 extern const _Placeholder<16> _16;
710 extern const _Placeholder<17> _17;
711 extern const _Placeholder<18> _18;
712 extern const _Placeholder<19> _19;
713 extern const _Placeholder<20> _20;
714 extern const _Placeholder<21> _21;
715 extern const _Placeholder<22> _22;
716 extern const _Placeholder<23> _23;
717 extern const _Placeholder<24> _24;
718 extern const _Placeholder<25> _25;
719 extern const _Placeholder<26> _26;
720 extern const _Placeholder<27> _27;
721 extern const _Placeholder<28> _28;
722 extern const _Placeholder<29> _29;
723 _GLIBCXX_END_NAMESPACE_VERSION
726 _GLIBCXX_BEGIN_NAMESPACE_VERSION
729 * Partial specialization of is_placeholder that provides the placeholder
730 * number for the placeholder objects defined by libstdc++.
734 struct is_placeholder<_Placeholder<_Num> >
735 : public integral_constant<int, _Num>
739 struct is_placeholder<const _Placeholder<_Num> >
740 : public integral_constant<int, _Num>
744 // Like tuple_element_t but SFINAE-friendly.
745 template<std::size_t __i, typename _Tuple>
746 using _Safe_tuple_element_t
747 = typename enable_if<(__i < tuple_size<_Tuple>::value),
748 tuple_element<__i, _Tuple>>::type::type;
751 * Maps an argument to bind() into an actual argument to the bound
752 * function object [func.bind.bind]/10. Only the first parameter should
753 * be specified: the rest are used to determine among the various
754 * implementations. Note that, although this class is a function
755 * object, it isn't entirely normal because it takes only two
756 * parameters regardless of the number of parameters passed to the
757 * bind expression. The first parameter is the bound argument and
758 * the second parameter is a tuple containing references to the
759 * rest of the arguments.
761 template<typename _Arg,
762 bool _IsBindExp = is_bind_expression<_Arg>::value,
763 bool _IsPlaceholder = (is_placeholder<_Arg>::value > 0)>
767 * If the argument is reference_wrapper<_Tp>, returns the
768 * underlying reference.
769 * C++11 [func.bind.bind] p10 bullet 1.
771 template<typename _Tp>
772 class _Mu<reference_wrapper<_Tp>, false, false>
775 /* Note: This won't actually work for const volatile
776 * reference_wrappers, because reference_wrapper::get() is const
777 * but not volatile-qualified. This might be a defect in the TR.
779 template<typename _CVRef, typename _Tuple>
781 operator()(_CVRef& __arg, _Tuple&) const volatile
782 { return __arg.get(); }
786 * If the argument is a bind expression, we invoke the underlying
787 * function object with the same cv-qualifiers as we are given and
788 * pass along all of our arguments (unwrapped).
789 * C++11 [func.bind.bind] p10 bullet 2.
791 template<typename _Arg>
792 class _Mu<_Arg, true, false>
795 template<typename _CVArg, typename... _Args>
797 operator()(_CVArg& __arg,
798 tuple<_Args...>& __tuple) const volatile
799 -> decltype(__arg(declval<_Args>()...))
801 // Construct an index tuple and forward to __call
802 typedef typename _Build_index_tuple<sizeof...(_Args)>::__type
804 return this->__call(__arg, __tuple, _Indexes());
808 // Invokes the underlying function object __arg by unpacking all
809 // of the arguments in the tuple.
810 template<typename _CVArg, typename... _Args, std::size_t... _Indexes>
812 __call(_CVArg& __arg, tuple<_Args...>& __tuple,
813 const _Index_tuple<_Indexes...>&) const volatile
814 -> decltype(__arg(declval<_Args>()...))
816 return __arg(std::forward<_Args>(std::get<_Indexes>(__tuple))...);
821 * If the argument is a placeholder for the Nth argument, returns
822 * a reference to the Nth argument to the bind function object.
823 * C++11 [func.bind.bind] p10 bullet 3.
825 template<typename _Arg>
826 class _Mu<_Arg, false, true>
829 template<typename _Tuple>
830 _Safe_tuple_element_t<(is_placeholder<_Arg>::value - 1), _Tuple>&&
831 operator()(const volatile _Arg&, _Tuple& __tuple) const volatile
834 = __tuple_element_t<(is_placeholder<_Arg>::value - 1), _Tuple>;
835 return std::forward<__type>(
836 ::std::get<(is_placeholder<_Arg>::value - 1)>(__tuple));
841 * If the argument is just a value, returns a reference to that
842 * value. The cv-qualifiers on the reference are determined by the caller.
843 * C++11 [func.bind.bind] p10 bullet 4.
845 template<typename _Arg>
846 class _Mu<_Arg, false, false>
849 template<typename _CVArg, typename _Tuple>
851 operator()(_CVArg&& __arg, _Tuple&) const volatile
852 { return std::forward<_CVArg>(__arg); }
856 * Maps member pointers into instances of _Mem_fn but leaves all
857 * other function objects untouched. Used by std::bind(). The
858 * primary template handles the non-member-pointer case.
860 template<typename _Tp>
861 struct _Maybe_wrap_member_pointer
865 static constexpr const _Tp&
866 __do_wrap(const _Tp& __x)
869 static constexpr _Tp&&
871 { return static_cast<_Tp&&>(__x); }
875 * Maps member pointers into instances of _Mem_fn but leaves all
876 * other function objects untouched. Used by std::bind(). This
877 * partial specialization handles the member pointer case.
879 template<typename _Tp, typename _Class>
880 struct _Maybe_wrap_member_pointer<_Tp _Class::*>
882 typedef _Mem_fn<_Tp _Class::*> type;
884 static constexpr type
885 __do_wrap(_Tp _Class::* __pm)
886 { return type(__pm); }
889 // Specialization needed to prevent "forming reference to void" errors when
890 // bind<void>() is called, because argument deduction instantiates
891 // _Maybe_wrap_member_pointer<void> outside the immediate context where
894 struct _Maybe_wrap_member_pointer<void>
899 // std::get<I> for volatile-qualified tuples
900 template<std::size_t _Ind, typename... _Tp>
902 __volget(volatile tuple<_Tp...>& __tuple)
903 -> __tuple_element_t<_Ind, tuple<_Tp...>> volatile&
904 { return std::get<_Ind>(const_cast<tuple<_Tp...>&>(__tuple)); }
906 // std::get<I> for const-volatile-qualified tuples
907 template<std::size_t _Ind, typename... _Tp>
909 __volget(const volatile tuple<_Tp...>& __tuple)
910 -> __tuple_element_t<_Ind, tuple<_Tp...>> const volatile&
911 { return std::get<_Ind>(const_cast<const tuple<_Tp...>&>(__tuple)); }
913 /// Type of the function object returned from bind().
914 template<typename _Signature>
917 template<typename _Functor, typename... _Bound_args>
918 class _Bind<_Functor(_Bound_args...)>
919 : public _Weak_result_type<_Functor>
921 typedef _Bind __self_type;
922 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
926 tuple<_Bound_args...> _M_bound_args;
929 template<typename _Result, typename... _Args, std::size_t... _Indexes>
931 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>)
933 return _M_f(_Mu<_Bound_args>()
934 (std::get<_Indexes>(_M_bound_args), __args)...);
938 template<typename _Result, typename... _Args, std::size_t... _Indexes>
940 __call_c(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>) const
942 return _M_f(_Mu<_Bound_args>()
943 (std::get<_Indexes>(_M_bound_args), __args)...);
947 template<typename _Result, typename... _Args, std::size_t... _Indexes>
949 __call_v(tuple<_Args...>&& __args,
950 _Index_tuple<_Indexes...>) volatile
952 return _M_f(_Mu<_Bound_args>()
953 (__volget<_Indexes>(_M_bound_args), __args)...);
956 // Call as const volatile
957 template<typename _Result, typename... _Args, std::size_t... _Indexes>
959 __call_c_v(tuple<_Args...>&& __args,
960 _Index_tuple<_Indexes...>) const volatile
962 return _M_f(_Mu<_Bound_args>()
963 (__volget<_Indexes>(_M_bound_args), __args)...);
967 template<typename... _Args>
968 explicit _Bind(const _Functor& __f, _Args&&... __args)
969 : _M_f(__f), _M_bound_args(std::forward<_Args>(__args)...)
972 template<typename... _Args>
973 explicit _Bind(_Functor&& __f, _Args&&... __args)
974 : _M_f(std::move(__f)), _M_bound_args(std::forward<_Args>(__args)...)
977 _Bind(const _Bind&) = default;
980 : _M_f(std::move(__b._M_f)), _M_bound_args(std::move(__b._M_bound_args))
984 template<typename... _Args, typename _Result
985 = decltype( std::declval<_Functor&>()(
986 _Mu<_Bound_args>()( std::declval<_Bound_args&>(),
987 std::declval<tuple<_Args...>&>() )... ) )>
989 operator()(_Args&&... __args)
991 return this->__call<_Result>(
992 std::forward_as_tuple(std::forward<_Args>(__args)...),
997 template<typename... _Args, typename _Result
998 = decltype( std::declval<typename enable_if<(sizeof...(_Args) >= 0),
999 typename add_const<_Functor>::type&>::type>()(
1000 _Mu<_Bound_args>()( std::declval<const _Bound_args&>(),
1001 std::declval<tuple<_Args...>&>() )... ) )>
1003 operator()(_Args&&... __args) const
1005 return this->__call_c<_Result>(
1006 std::forward_as_tuple(std::forward<_Args>(__args)...),
1011 template<typename... _Args, typename _Result
1012 = decltype( std::declval<typename enable_if<(sizeof...(_Args) >= 0),
1013 typename add_volatile<_Functor>::type&>::type>()(
1014 _Mu<_Bound_args>()( std::declval<volatile _Bound_args&>(),
1015 std::declval<tuple<_Args...>&>() )... ) )>
1017 operator()(_Args&&... __args) volatile
1019 return this->__call_v<_Result>(
1020 std::forward_as_tuple(std::forward<_Args>(__args)...),
1024 // Call as const volatile
1025 template<typename... _Args, typename _Result
1026 = decltype( std::declval<typename enable_if<(sizeof...(_Args) >= 0),
1027 typename add_cv<_Functor>::type&>::type>()(
1028 _Mu<_Bound_args>()( std::declval<const volatile _Bound_args&>(),
1029 std::declval<tuple<_Args...>&>() )... ) )>
1031 operator()(_Args&&... __args) const volatile
1033 return this->__call_c_v<_Result>(
1034 std::forward_as_tuple(std::forward<_Args>(__args)...),
1039 /// Type of the function object returned from bind<R>().
1040 template<typename _Result, typename _Signature>
1041 struct _Bind_result;
1043 template<typename _Result, typename _Functor, typename... _Bound_args>
1044 class _Bind_result<_Result, _Functor(_Bound_args...)>
1046 typedef _Bind_result __self_type;
1047 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
1051 tuple<_Bound_args...> _M_bound_args;
1054 template<typename _Res>
1055 struct __enable_if_void : enable_if<is_void<_Res>::value, int> { };
1056 template<typename _Res>
1057 struct __disable_if_void : enable_if<!is_void<_Res>::value, int> { };
1060 template<typename _Res, typename... _Args, std::size_t... _Indexes>
1062 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1063 typename __disable_if_void<_Res>::type = 0)
1065 return _M_f(_Mu<_Bound_args>()
1066 (std::get<_Indexes>(_M_bound_args), __args)...);
1069 // Call unqualified, return void
1070 template<typename _Res, typename... _Args, std::size_t... _Indexes>
1072 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1073 typename __enable_if_void<_Res>::type = 0)
1075 _M_f(_Mu<_Bound_args>()
1076 (std::get<_Indexes>(_M_bound_args), __args)...);
1080 template<typename _Res, typename... _Args, std::size_t... _Indexes>
1082 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1083 typename __disable_if_void<_Res>::type = 0) const
1085 return _M_f(_Mu<_Bound_args>()
1086 (std::get<_Indexes>(_M_bound_args), __args)...);
1089 // Call as const, return void
1090 template<typename _Res, typename... _Args, std::size_t... _Indexes>
1092 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1093 typename __enable_if_void<_Res>::type = 0) const
1095 _M_f(_Mu<_Bound_args>()
1096 (std::get<_Indexes>(_M_bound_args), __args)...);
1100 template<typename _Res, typename... _Args, std::size_t... _Indexes>
1102 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1103 typename __disable_if_void<_Res>::type = 0) volatile
1105 return _M_f(_Mu<_Bound_args>()
1106 (__volget<_Indexes>(_M_bound_args), __args)...);
1109 // Call as volatile, return void
1110 template<typename _Res, typename... _Args, std::size_t... _Indexes>
1112 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1113 typename __enable_if_void<_Res>::type = 0) volatile
1115 _M_f(_Mu<_Bound_args>()
1116 (__volget<_Indexes>(_M_bound_args), __args)...);
1119 // Call as const volatile
1120 template<typename _Res, typename... _Args, std::size_t... _Indexes>
1122 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1123 typename __disable_if_void<_Res>::type = 0) const volatile
1125 return _M_f(_Mu<_Bound_args>()
1126 (__volget<_Indexes>(_M_bound_args), __args)...);
1129 // Call as const volatile, return void
1130 template<typename _Res, typename... _Args, std::size_t... _Indexes>
1132 __call(tuple<_Args...>&& __args,
1133 _Index_tuple<_Indexes...>,
1134 typename __enable_if_void<_Res>::type = 0) const volatile
1136 _M_f(_Mu<_Bound_args>()
1137 (__volget<_Indexes>(_M_bound_args), __args)...);
1141 typedef _Result result_type;
1143 template<typename... _Args>
1144 explicit _Bind_result(const _Functor& __f, _Args&&... __args)
1145 : _M_f(__f), _M_bound_args(std::forward<_Args>(__args)...)
1148 template<typename... _Args>
1149 explicit _Bind_result(_Functor&& __f, _Args&&... __args)
1150 : _M_f(std::move(__f)), _M_bound_args(std::forward<_Args>(__args)...)
1153 _Bind_result(const _Bind_result&) = default;
1155 _Bind_result(_Bind_result&& __b)
1156 : _M_f(std::move(__b._M_f)), _M_bound_args(std::move(__b._M_bound_args))
1160 template<typename... _Args>
1162 operator()(_Args&&... __args)
1164 return this->__call<_Result>(
1165 std::forward_as_tuple(std::forward<_Args>(__args)...),
1170 template<typename... _Args>
1172 operator()(_Args&&... __args) const
1174 return this->__call<_Result>(
1175 std::forward_as_tuple(std::forward<_Args>(__args)...),
1180 template<typename... _Args>
1182 operator()(_Args&&... __args) volatile
1184 return this->__call<_Result>(
1185 std::forward_as_tuple(std::forward<_Args>(__args)...),
1189 // Call as const volatile
1190 template<typename... _Args>
1192 operator()(_Args&&... __args) const volatile
1194 return this->__call<_Result>(
1195 std::forward_as_tuple(std::forward<_Args>(__args)...),
1201 * @brief Class template _Bind is always a bind expression.
1204 template<typename _Signature>
1205 struct is_bind_expression<_Bind<_Signature> >
1206 : public true_type { };
1209 * @brief Class template _Bind is always a bind expression.
1212 template<typename _Signature>
1213 struct is_bind_expression<const _Bind<_Signature> >
1214 : public true_type { };
1217 * @brief Class template _Bind is always a bind expression.
1220 template<typename _Signature>
1221 struct is_bind_expression<volatile _Bind<_Signature> >
1222 : public true_type { };
1225 * @brief Class template _Bind is always a bind expression.
1228 template<typename _Signature>
1229 struct is_bind_expression<const volatile _Bind<_Signature>>
1230 : public true_type { };
1233 * @brief Class template _Bind_result is always a bind expression.
1236 template<typename _Result, typename _Signature>
1237 struct is_bind_expression<_Bind_result<_Result, _Signature>>
1238 : public true_type { };
1241 * @brief Class template _Bind_result is always a bind expression.
1244 template<typename _Result, typename _Signature>
1245 struct is_bind_expression<const _Bind_result<_Result, _Signature>>
1246 : public true_type { };
1249 * @brief Class template _Bind_result is always a bind expression.
1252 template<typename _Result, typename _Signature>
1253 struct is_bind_expression<volatile _Bind_result<_Result, _Signature>>
1254 : public true_type { };
1257 * @brief Class template _Bind_result is always a bind expression.
1260 template<typename _Result, typename _Signature>
1261 struct is_bind_expression<const volatile _Bind_result<_Result, _Signature>>
1262 : public true_type { };
1264 template<typename _Func, typename... _BoundArgs>
1265 struct _Bind_check_arity { };
1267 template<typename _Ret, typename... _Args, typename... _BoundArgs>
1268 struct _Bind_check_arity<_Ret (*)(_Args...), _BoundArgs...>
1270 static_assert(sizeof...(_BoundArgs) == sizeof...(_Args),
1271 "Wrong number of arguments for function");
1274 template<typename _Ret, typename... _Args, typename... _BoundArgs>
1275 struct _Bind_check_arity<_Ret (*)(_Args......), _BoundArgs...>
1277 static_assert(sizeof...(_BoundArgs) >= sizeof...(_Args),
1278 "Wrong number of arguments for function");
1281 template<typename _Tp, typename _Class, typename... _BoundArgs>
1282 struct _Bind_check_arity<_Tp _Class::*, _BoundArgs...>
1284 using _Arity = typename _Mem_fn<_Tp _Class::*>::_Arity;
1285 using _Varargs = typename _Mem_fn<_Tp _Class::*>::_Varargs;
1286 static_assert(_Varargs::value
1287 ? sizeof...(_BoundArgs) >= _Arity::value + 1
1288 : sizeof...(_BoundArgs) == _Arity::value + 1,
1289 "Wrong number of arguments for pointer-to-member");
1292 // Trait type used to remove std::bind() from overload set via SFINAE
1293 // when first argument has integer type, so that std::bind() will
1294 // not be a better match than ::bind() from the BSD Sockets API.
1295 template<typename _Tp, typename _Tp2 = typename decay<_Tp>::type>
1296 using __is_socketlike = __or_<is_integral<_Tp2>, is_enum<_Tp2>>;
1298 template<bool _SocketLike, typename _Func, typename... _BoundArgs>
1300 : _Bind_check_arity<typename decay<_Func>::type, _BoundArgs...>
1302 typedef _Maybe_wrap_member_pointer<typename decay<_Func>::type>
1304 typedef typename __maybe_type::type __func_type;
1305 typedef _Bind<__func_type(typename decay<_BoundArgs>::type...)> type;
1308 // Partial specialization for is_socketlike == true, does not define
1309 // nested type so std::bind() will not participate in overload resolution
1310 // when the first argument might be a socket file descriptor.
1311 template<typename _Func, typename... _BoundArgs>
1312 struct _Bind_helper<true, _Func, _BoundArgs...>
1316 * @brief Function template for std::bind.
1319 template<typename _Func, typename... _BoundArgs>
1321 _Bind_helper<__is_socketlike<_Func>::value, _Func, _BoundArgs...>::type
1322 bind(_Func&& __f, _BoundArgs&&... __args)
1324 typedef _Bind_helper<false, _Func, _BoundArgs...> __helper_type;
1325 typedef typename __helper_type::__maybe_type __maybe_type;
1326 typedef typename __helper_type::type __result_type;
1327 return __result_type(__maybe_type::__do_wrap(std::forward<_Func>(__f)),
1328 std::forward<_BoundArgs>(__args)...);
1331 template<typename _Result, typename _Func, typename... _BoundArgs>
1332 struct _Bindres_helper
1333 : _Bind_check_arity<typename decay<_Func>::type, _BoundArgs...>
1335 typedef _Maybe_wrap_member_pointer<typename decay<_Func>::type>
1337 typedef typename __maybe_type::type __functor_type;
1338 typedef _Bind_result<_Result,
1339 __functor_type(typename decay<_BoundArgs>::type...)>
1344 * @brief Function template for std::bind<R>.
1347 template<typename _Result, typename _Func, typename... _BoundArgs>
1349 typename _Bindres_helper<_Result, _Func, _BoundArgs...>::type
1350 bind(_Func&& __f, _BoundArgs&&... __args)
1352 typedef _Bindres_helper<_Result, _Func, _BoundArgs...> __helper_type;
1353 typedef typename __helper_type::__maybe_type __maybe_type;
1354 typedef typename __helper_type::type __result_type;
1355 return __result_type(__maybe_type::__do_wrap(std::forward<_Func>(__f)),
1356 std::forward<_BoundArgs>(__args)...);
1359 template<typename _Signature>
1360 struct _Bind_simple;
1362 template<typename _Callable, typename... _Args>
1363 struct _Bind_simple<_Callable(_Args...)>
1365 typedef typename result_of<_Callable(_Args...)>::type result_type;
1367 template<typename _Tp, typename... _Up>
1369 _Bind_simple(_Tp&& __f, _Up&&... __args)
1370 : _M_bound(std::forward<_Tp>(__f), std::forward<_Up>(__args)...)
1373 _Bind_simple(const _Bind_simple&) = default;
1374 _Bind_simple(_Bind_simple&&) = default;
1379 typedef typename _Build_index_tuple<sizeof...(_Args)>::__type _Indices;
1380 return _M_invoke(_Indices());
1384 template<std::size_t... _Indices>
1385 typename result_of<_Callable(_Args...)>::type
1386 _M_invoke(_Index_tuple<_Indices...>)
1388 // std::bind always forwards bound arguments as lvalues,
1389 // but this type can call functions which only accept rvalues.
1390 return std::forward<_Callable>(std::get<0>(_M_bound))(
1391 std::forward<_Args>(std::get<_Indices+1>(_M_bound))...);
1394 std::tuple<_Callable, _Args...> _M_bound;
1397 template<typename _Func, typename... _BoundArgs>
1398 struct _Bind_simple_helper
1399 : _Bind_check_arity<typename decay<_Func>::type, _BoundArgs...>
1401 typedef _Maybe_wrap_member_pointer<typename decay<_Func>::type>
1403 typedef typename __maybe_type::type __func_type;
1404 typedef _Bind_simple<__func_type(typename decay<_BoundArgs>::type...)>
1408 // Simplified version of std::bind for internal use, without support for
1409 // unbound arguments, placeholders or nested bind expressions.
1410 template<typename _Callable, typename... _Args>
1411 typename _Bind_simple_helper<_Callable, _Args...>::__type
1412 __bind_simple(_Callable&& __callable, _Args&&... __args)
1414 typedef _Bind_simple_helper<_Callable, _Args...> __helper_type;
1415 typedef typename __helper_type::__maybe_type __maybe_type;
1416 typedef typename __helper_type::__type __result_type;
1417 return __result_type(
1418 __maybe_type::__do_wrap( std::forward<_Callable>(__callable)),
1419 std::forward<_Args>(__args)...);
1423 * @brief Exception class thrown when class template function's
1424 * operator() is called with an empty target.
1425 * @ingroup exceptions
1427 class bad_function_call : public std::exception
1430 virtual ~bad_function_call() noexcept;
1432 const char* what() const noexcept;
1436 * Trait identifying "location-invariant" types, meaning that the
1437 * address of the object (or any of its members) will not escape.
1438 * Trivially copyable types are location-invariant and users can
1439 * specialize this trait for other types.
1441 template<typename _Tp>
1442 struct __is_location_invariant
1443 : is_trivially_copyable<_Tp>::type
1446 class _Undefined_class;
1451 const void* _M_const_object;
1452 void (*_M_function_pointer)();
1453 void (_Undefined_class::*_M_member_pointer)();
1456 union [[gnu::may_alias]] _Any_data
1458 void* _M_access() { return &_M_pod_data[0]; }
1459 const void* _M_access() const { return &_M_pod_data[0]; }
1461 template<typename _Tp>
1464 { return *static_cast<_Tp*>(_M_access()); }
1466 template<typename _Tp>
1469 { return *static_cast<const _Tp*>(_M_access()); }
1471 _Nocopy_types _M_unused;
1472 char _M_pod_data[sizeof(_Nocopy_types)];
1475 enum _Manager_operation
1483 // Simple type wrapper that helps avoid annoying const problems
1484 // when casting between void pointers and pointers-to-pointers.
1485 template<typename _Tp>
1486 struct _Simple_type_wrapper
1488 _Simple_type_wrapper(_Tp __value) : __value(__value) { }
1493 template<typename _Tp>
1494 struct __is_location_invariant<_Simple_type_wrapper<_Tp> >
1495 : __is_location_invariant<_Tp>
1498 // Converts a reference to a function object into a callable
1500 template<typename _Functor>
1502 __callable_functor(_Functor& __f)
1505 template<typename _Member, typename _Class>
1506 inline _Mem_fn<_Member _Class::*>
1507 __callable_functor(_Member _Class::* &__p)
1508 { return std::mem_fn(__p); }
1510 template<typename _Member, typename _Class>
1511 inline _Mem_fn<_Member _Class::*>
1512 __callable_functor(_Member _Class::* const &__p)
1513 { return std::mem_fn(__p); }
1515 template<typename _Member, typename _Class>
1516 inline _Mem_fn<_Member _Class::*>
1517 __callable_functor(_Member _Class::* volatile &__p)
1518 { return std::mem_fn(__p); }
1520 template<typename _Member, typename _Class>
1521 inline _Mem_fn<_Member _Class::*>
1522 __callable_functor(_Member _Class::* const volatile &__p)
1523 { return std::mem_fn(__p); }
1525 template<typename _Signature>
1528 /// Base class of all polymorphic function object wrappers.
1529 class _Function_base
1532 static const std::size_t _M_max_size = sizeof(_Nocopy_types);
1533 static const std::size_t _M_max_align = __alignof__(_Nocopy_types);
1535 template<typename _Functor>
1539 static const bool __stored_locally =
1540 (__is_location_invariant<_Functor>::value
1541 && sizeof(_Functor) <= _M_max_size
1542 && __alignof__(_Functor) <= _M_max_align
1543 && (_M_max_align % __alignof__(_Functor) == 0));
1545 typedef integral_constant<bool, __stored_locally> _Local_storage;
1547 // Retrieve a pointer to the function object
1549 _M_get_pointer(const _Any_data& __source)
1551 const _Functor* __ptr =
1552 __stored_locally? std::__addressof(__source._M_access<_Functor>())
1553 /* have stored a pointer */ : __source._M_access<_Functor*>();
1554 return const_cast<_Functor*>(__ptr);
1557 // Clone a location-invariant function object that fits within
1558 // an _Any_data structure.
1560 _M_clone(_Any_data& __dest, const _Any_data& __source, true_type)
1562 ::new (__dest._M_access()) _Functor(__source._M_access<_Functor>());
1565 // Clone a function object that is not location-invariant or
1566 // that cannot fit into an _Any_data structure.
1568 _M_clone(_Any_data& __dest, const _Any_data& __source, false_type)
1570 __dest._M_access<_Functor*>() =
1571 new _Functor(*__source._M_access<_Functor*>());
1574 // Destroying a location-invariant object may still require
1577 _M_destroy(_Any_data& __victim, true_type)
1579 __victim._M_access<_Functor>().~_Functor();
1582 // Destroying an object located on the heap.
1584 _M_destroy(_Any_data& __victim, false_type)
1586 delete __victim._M_access<_Functor*>();
1591 _M_manager(_Any_data& __dest, const _Any_data& __source,
1592 _Manager_operation __op)
1597 case __get_type_info:
1598 __dest._M_access<const type_info*>() = &typeid(_Functor);
1601 case __get_functor_ptr:
1602 __dest._M_access<_Functor*>() = _M_get_pointer(__source);
1605 case __clone_functor:
1606 _M_clone(__dest, __source, _Local_storage());
1609 case __destroy_functor:
1610 _M_destroy(__dest, _Local_storage());
1617 _M_init_functor(_Any_data& __functor, _Functor&& __f)
1618 { _M_init_functor(__functor, std::move(__f), _Local_storage()); }
1620 template<typename _Signature>
1622 _M_not_empty_function(const function<_Signature>& __f)
1623 { return static_cast<bool>(__f); }
1625 template<typename _Tp>
1627 _M_not_empty_function(_Tp* __fp)
1628 { return __fp != nullptr; }
1630 template<typename _Class, typename _Tp>
1632 _M_not_empty_function(_Tp _Class::* __mp)
1633 { return __mp != nullptr; }
1635 template<typename _Tp>
1637 _M_not_empty_function(const _Tp&)
1642 _M_init_functor(_Any_data& __functor, _Functor&& __f, true_type)
1643 { ::new (__functor._M_access()) _Functor(std::move(__f)); }
1646 _M_init_functor(_Any_data& __functor, _Functor&& __f, false_type)
1647 { __functor._M_access<_Functor*>() = new _Functor(std::move(__f)); }
1650 template<typename _Functor>
1651 class _Ref_manager : public _Base_manager<_Functor*>
1653 typedef _Function_base::_Base_manager<_Functor*> _Base;
1657 _M_manager(_Any_data& __dest, const _Any_data& __source,
1658 _Manager_operation __op)
1663 case __get_type_info:
1664 __dest._M_access<const type_info*>() = &typeid(_Functor);
1667 case __get_functor_ptr:
1668 __dest._M_access<_Functor*>() = *_Base::_M_get_pointer(__source);
1669 return is_const<_Functor>::value;
1673 _Base::_M_manager(__dest, __source, __op);
1679 _M_init_functor(_Any_data& __functor, reference_wrapper<_Functor> __f)
1681 _Base::_M_init_functor(__functor, std::__addressof(__f.get()));
1685 _Function_base() : _M_manager(nullptr) { }
1690 _M_manager(_M_functor, _M_functor, __destroy_functor);
1694 bool _M_empty() const { return !_M_manager; }
1696 typedef bool (*_Manager_type)(_Any_data&, const _Any_data&,
1697 _Manager_operation);
1699 _Any_data _M_functor;
1700 _Manager_type _M_manager;
1703 template<typename _Signature, typename _Functor>
1704 class _Function_handler;
1706 template<typename _Res, typename _Functor, typename... _ArgTypes>
1707 class _Function_handler<_Res(_ArgTypes...), _Functor>
1708 : public _Function_base::_Base_manager<_Functor>
1710 typedef _Function_base::_Base_manager<_Functor> _Base;
1714 _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args)
1716 return (*_Base::_M_get_pointer(__functor))(
1717 std::forward<_ArgTypes>(__args)...);
1721 template<typename _Functor, typename... _ArgTypes>
1722 class _Function_handler<void(_ArgTypes...), _Functor>
1723 : public _Function_base::_Base_manager<_Functor>
1725 typedef _Function_base::_Base_manager<_Functor> _Base;
1729 _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args)
1731 (*_Base::_M_get_pointer(__functor))(
1732 std::forward<_ArgTypes>(__args)...);
1736 template<typename _Res, typename _Functor, typename... _ArgTypes>
1737 class _Function_handler<_Res(_ArgTypes...), reference_wrapper<_Functor> >
1738 : public _Function_base::_Ref_manager<_Functor>
1740 typedef _Function_base::_Ref_manager<_Functor> _Base;
1744 _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args)
1746 return std::__callable_functor(**_Base::_M_get_pointer(__functor))(
1747 std::forward<_ArgTypes>(__args)...);
1751 template<typename _Functor, typename... _ArgTypes>
1752 class _Function_handler<void(_ArgTypes...), reference_wrapper<_Functor> >
1753 : public _Function_base::_Ref_manager<_Functor>
1755 typedef _Function_base::_Ref_manager<_Functor> _Base;
1759 _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args)
1761 std::__callable_functor(**_Base::_M_get_pointer(__functor))(
1762 std::forward<_ArgTypes>(__args)...);
1766 template<typename _Class, typename _Member, typename _Res,
1767 typename... _ArgTypes>
1768 class _Function_handler<_Res(_ArgTypes...), _Member _Class::*>
1769 : public _Function_handler<void(_ArgTypes...), _Member _Class::*>
1771 typedef _Function_handler<void(_ArgTypes...), _Member _Class::*>
1776 _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args)
1778 return std::mem_fn(_Base::_M_get_pointer(__functor)->__value)(
1779 std::forward<_ArgTypes>(__args)...);
1783 template<typename _Class, typename _Member, typename... _ArgTypes>
1784 class _Function_handler<void(_ArgTypes...), _Member _Class::*>
1785 : public _Function_base::_Base_manager<
1786 _Simple_type_wrapper< _Member _Class::* > >
1788 typedef _Member _Class::* _Functor;
1789 typedef _Simple_type_wrapper<_Functor> _Wrapper;
1790 typedef _Function_base::_Base_manager<_Wrapper> _Base;
1794 _M_manager(_Any_data& __dest, const _Any_data& __source,
1795 _Manager_operation __op)
1800 case __get_type_info:
1801 __dest._M_access<const type_info*>() = &typeid(_Functor);
1804 case __get_functor_ptr:
1805 __dest._M_access<_Functor*>() =
1806 &_Base::_M_get_pointer(__source)->__value;
1810 _Base::_M_manager(__dest, __source, __op);
1816 _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args)
1818 std::mem_fn(_Base::_M_get_pointer(__functor)->__value)(
1819 std::forward<_ArgTypes>(__args)...);
1823 template<typename _From, typename _To>
1824 using __check_func_return_type
1825 = __or_<is_void<_To>, is_same<_From, _To>, is_convertible<_From, _To>>;
1828 * @brief Primary class template for std::function.
1831 * Polymorphic function wrapper.
1833 template<typename _Res, typename... _ArgTypes>
1834 class function<_Res(_ArgTypes...)>
1835 : public _Maybe_unary_or_binary_function<_Res, _ArgTypes...>,
1836 private _Function_base
1838 typedef _Res _Signature_type(_ArgTypes...);
1840 template<typename _Func,
1841 typename _Res2 = typename result_of<_Func&(_ArgTypes...)>::type>
1842 struct _Callable : __check_func_return_type<_Res2, _Res> { };
1844 // Used so the return type convertibility checks aren't done when
1845 // performing overload resolution for copy construction/assignment.
1846 template<typename _Tp>
1847 struct _Callable<function, _Tp> : false_type { };
1849 template<typename _Cond, typename _Tp>
1850 using _Requires = typename enable_if<_Cond::value, _Tp>::type;
1853 typedef _Res result_type;
1855 // [3.7.2.1] construct/copy/destroy
1858 * @brief Default construct creates an empty function call wrapper.
1859 * @post @c !(bool)*this
1862 : _Function_base() { }
1865 * @brief Creates an empty function call wrapper.
1866 * @post @c !(bool)*this
1868 function(nullptr_t) noexcept
1869 : _Function_base() { }
1872 * @brief %Function copy constructor.
1873 * @param __x A %function object with identical call signature.
1874 * @post @c bool(*this) == bool(__x)
1876 * The newly-created %function contains a copy of the target of @a
1877 * __x (if it has one).
1879 function(const function& __x);
1882 * @brief %Function move constructor.
1883 * @param __x A %function object rvalue with identical call signature.
1885 * The newly-created %function contains the target of @a __x
1888 function(function&& __x) : _Function_base()
1893 // TODO: needs allocator_arg_t
1896 * @brief Builds a %function that targets a copy of the incoming
1898 * @param __f A %function object that is callable with parameters of
1899 * type @c T1, @c T2, ..., @c TN and returns a value convertible
1902 * The newly-created %function object will target a copy of
1903 * @a __f. If @a __f is @c reference_wrapper<F>, then this function
1904 * object will contain a reference to the function object @c
1905 * __f.get(). If @a __f is a NULL function pointer or NULL
1906 * pointer-to-member, the newly-created object will be empty.
1908 * If @a __f is a non-NULL function pointer or an object of type @c
1909 * reference_wrapper<F>, this function will not throw.
1911 template<typename _Functor,
1912 typename = _Requires<__not_<is_same<_Functor, function>>, void>,
1913 typename = _Requires<_Callable<_Functor>, void>>
1917 * @brief %Function assignment operator.
1918 * @param __x A %function with identical call signature.
1919 * @post @c (bool)*this == (bool)x
1922 * The target of @a __x is copied to @c *this. If @a __x has no
1923 * target, then @c *this will be empty.
1925 * If @a __x targets a function pointer or a reference to a function
1926 * object, then this operation will not throw an %exception.
1929 operator=(const function& __x)
1931 function(__x).swap(*this);
1936 * @brief %Function move-assignment operator.
1937 * @param __x A %function rvalue with identical call signature.
1940 * The target of @a __x is moved to @c *this. If @a __x has no
1941 * target, then @c *this will be empty.
1943 * If @a __x targets a function pointer or a reference to a function
1944 * object, then this operation will not throw an %exception.
1947 operator=(function&& __x)
1949 function(std::move(__x)).swap(*this);
1954 * @brief %Function assignment to zero.
1955 * @post @c !(bool)*this
1958 * The target of @c *this is deallocated, leaving it empty.
1961 operator=(nullptr_t) noexcept
1965 _M_manager(_M_functor, _M_functor, __destroy_functor);
1966 _M_manager = nullptr;
1967 _M_invoker = nullptr;
1973 * @brief %Function assignment to a new target.
1974 * @param __f A %function object that is callable with parameters of
1975 * type @c T1, @c T2, ..., @c TN and returns a value convertible
1979 * This %function object wrapper will target a copy of @a
1980 * __f. If @a __f is @c reference_wrapper<F>, then this function
1981 * object will contain a reference to the function object @c
1982 * __f.get(). If @a __f is a NULL function pointer or NULL
1983 * pointer-to-member, @c this object will be empty.
1985 * If @a __f is a non-NULL function pointer or an object of type @c
1986 * reference_wrapper<F>, this function will not throw.
1988 template<typename _Functor>
1989 _Requires<_Callable<typename decay<_Functor>::type>, function&>
1990 operator=(_Functor&& __f)
1992 function(std::forward<_Functor>(__f)).swap(*this);
1997 template<typename _Functor>
1999 operator=(reference_wrapper<_Functor> __f) noexcept
2001 function(__f).swap(*this);
2005 // [3.7.2.2] function modifiers
2008 * @brief Swap the targets of two %function objects.
2009 * @param __x A %function with identical call signature.
2011 * Swap the targets of @c this function object and @a __f. This
2012 * function will not throw an %exception.
2014 void swap(function& __x) noexcept
2016 std::swap(_M_functor, __x._M_functor);
2017 std::swap(_M_manager, __x._M_manager);
2018 std::swap(_M_invoker, __x._M_invoker);
2021 // TODO: needs allocator_arg_t
2023 template<typename _Functor, typename _Alloc>
2025 assign(_Functor&& __f, const _Alloc& __a)
2027 function(allocator_arg, __a,
2028 std::forward<_Functor>(__f)).swap(*this);
2032 // [3.7.2.3] function capacity
2035 * @brief Determine if the %function wrapper has a target.
2037 * @return @c true when this %function object contains a target,
2038 * or @c false when it is empty.
2040 * This function will not throw an %exception.
2042 explicit operator bool() const noexcept
2043 { return !_M_empty(); }
2045 // [3.7.2.4] function invocation
2048 * @brief Invokes the function targeted by @c *this.
2049 * @returns the result of the target.
2050 * @throws bad_function_call when @c !(bool)*this
2052 * The function call operator invokes the target function object
2053 * stored by @c this.
2055 _Res operator()(_ArgTypes... __args) const;
2058 // [3.7.2.5] function target access
2060 * @brief Determine the type of the target of this function object
2063 * @returns the type identifier of the target function object, or
2064 * @c typeid(void) if @c !(bool)*this.
2066 * This function will not throw an %exception.
2068 const type_info& target_type() const noexcept;
2071 * @brief Access the stored target function object.
2073 * @return Returns a pointer to the stored target function object,
2074 * if @c typeid(Functor).equals(target_type()); otherwise, a NULL
2077 * This function will not throw an %exception.
2079 template<typename _Functor> _Functor* target() noexcept;
2082 template<typename _Functor> const _Functor* target() const noexcept;
2086 using _Invoker_type = _Res (*)(const _Any_data&, _ArgTypes&&...);
2087 _Invoker_type _M_invoker;
2090 // Out-of-line member definitions.
2091 template<typename _Res, typename... _ArgTypes>
2092 function<_Res(_ArgTypes...)>::
2093 function(const function& __x)
2096 if (static_cast<bool>(__x))
2098 __x._M_manager(_M_functor, __x._M_functor, __clone_functor);
2099 _M_invoker = __x._M_invoker;
2100 _M_manager = __x._M_manager;
2104 template<typename _Res, typename... _ArgTypes>
2105 template<typename _Functor, typename, typename>
2106 function<_Res(_ArgTypes...)>::
2107 function(_Functor __f)
2110 typedef _Function_handler<_Signature_type, _Functor> _My_handler;
2112 if (_My_handler::_M_not_empty_function(__f))
2114 _My_handler::_M_init_functor(_M_functor, std::move(__f));
2115 _M_invoker = &_My_handler::_M_invoke;
2116 _M_manager = &_My_handler::_M_manager;
2120 template<typename _Res, typename... _ArgTypes>
2122 function<_Res(_ArgTypes...)>::
2123 operator()(_ArgTypes... __args) const
2126 __throw_bad_function_call();
2127 return _M_invoker(_M_functor, std::forward<_ArgTypes>(__args)...);
2131 template<typename _Res, typename... _ArgTypes>
2133 function<_Res(_ArgTypes...)>::
2134 target_type() const noexcept
2138 _Any_data __typeinfo_result;
2139 _M_manager(__typeinfo_result, _M_functor, __get_type_info);
2140 return *__typeinfo_result._M_access<const type_info*>();
2143 return typeid(void);
2146 template<typename _Res, typename... _ArgTypes>
2147 template<typename _Functor>
2149 function<_Res(_ArgTypes...)>::
2152 if (typeid(_Functor) == target_type() && _M_manager)
2155 if (_M_manager(__ptr, _M_functor, __get_functor_ptr)
2156 && !is_const<_Functor>::value)
2159 return __ptr._M_access<_Functor*>();
2165 template<typename _Res, typename... _ArgTypes>
2166 template<typename _Functor>
2168 function<_Res(_ArgTypes...)>::
2169 target() const noexcept
2171 if (typeid(_Functor) == target_type() && _M_manager)
2174 _M_manager(__ptr, _M_functor, __get_functor_ptr);
2175 return __ptr._M_access<const _Functor*>();
2182 // [20.7.15.2.6] null pointer comparisons
2185 * @brief Compares a polymorphic function object wrapper against 0
2186 * (the NULL pointer).
2187 * @returns @c true if the wrapper has no target, @c false otherwise
2189 * This function will not throw an %exception.
2191 template<typename _Res, typename... _Args>
2193 operator==(const function<_Res(_Args...)>& __f, nullptr_t) noexcept
2194 { return !static_cast<bool>(__f); }
2197 template<typename _Res, typename... _Args>
2199 operator==(nullptr_t, const function<_Res(_Args...)>& __f) noexcept
2200 { return !static_cast<bool>(__f); }
2203 * @brief Compares a polymorphic function object wrapper against 0
2204 * (the NULL pointer).
2205 * @returns @c false if the wrapper has no target, @c true otherwise
2207 * This function will not throw an %exception.
2209 template<typename _Res, typename... _Args>
2211 operator!=(const function<_Res(_Args...)>& __f, nullptr_t) noexcept
2212 { return static_cast<bool>(__f); }
2215 template<typename _Res, typename... _Args>
2217 operator!=(nullptr_t, const function<_Res(_Args...)>& __f) noexcept
2218 { return static_cast<bool>(__f); }
2220 // [20.7.15.2.7] specialized algorithms
2223 * @brief Swap the targets of two polymorphic function object wrappers.
2225 * This function will not throw an %exception.
2227 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2228 // 2062. Effect contradictions w/o no-throw guarantee of std::function swaps
2229 template<typename _Res, typename... _Args>
2231 swap(function<_Res(_Args...)>& __x, function<_Res(_Args...)>& __y) noexcept
2234 _GLIBCXX_END_NAMESPACE_VERSION
2239 #endif // _GLIBCXX_FUNCTIONAL