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 // Used by __invoke_impl instead of std::forward<_Tp> so that a
188 // reference_wrapper is converted to an lvalue-reference.
189 template<typename _Tp, typename _Up = typename __inv_unwrap<_Tp>::type>
191 __invfwd(typename remove_reference<_Tp>::type& __t) noexcept
192 { return static_cast<_Up&&>(__t); }
194 template<typename _Res, typename _Fn, typename... _Args>
196 __invoke_impl(__invoke_other, _Fn&& __f, _Args&&... __args)
197 { return std::forward<_Fn>(__f)(std::forward<_Args>(__args)...); }
199 template<typename _Res, typename _MemFun, typename _Tp, typename... _Args>
201 __invoke_impl(__invoke_memfun_ref, _MemFun&& __f, _Tp&& __t,
203 { return (__invfwd<_Tp>(__t).*__f)(std::forward<_Args>(__args)...); }
205 template<typename _Res, typename _MemFun, typename _Tp, typename... _Args>
207 __invoke_impl(__invoke_memfun_deref, _MemFun&& __f, _Tp&& __t,
210 return ((*std::forward<_Tp>(__t)).*__f)(std::forward<_Args>(__args)...);
213 template<typename _Res, typename _MemPtr, typename _Tp>
215 __invoke_impl(__invoke_memobj_ref, _MemPtr&& __f, _Tp&& __t)
216 { return __invfwd<_Tp>(__t).*__f; }
218 template<typename _Res, typename _MemPtr, typename _Tp>
220 __invoke_impl(__invoke_memobj_deref, _MemPtr&& __f, _Tp&& __t)
221 { return (*std::forward<_Tp>(__t)).*__f; }
223 /// Invoke a callable object.
224 template<typename _Callable, typename... _Args>
225 inline typename result_of<_Callable&&(_Args&&...)>::type
226 __invoke(_Callable&& __fn, _Args&&... __args)
227 noexcept(__is_nothrow_callable<_Callable(_Args...)>::value)
229 using __result_of = result_of<_Callable&&(_Args&&...)>;
230 using __type = typename __result_of::type;
231 using __tag = typename __result_of::__invoke_type;
232 return std::__invoke_impl<__type>(__tag{}, std::forward<_Callable>(__fn),
233 std::forward<_Args>(__args)...);
236 #if __cplusplus > 201402L
237 # define __cpp_lib_invoke 201411
239 /// Invoke a callable object.
240 template<typename _Callable, typename... _Args>
241 inline result_of_t<_Callable&&(_Args&&...)>
242 invoke(_Callable&& __fn, _Args&&... __args)
243 noexcept(is_nothrow_callable_v<_Callable(_Args...)>)
245 return std::__invoke(std::forward<_Callable>(__fn),
246 std::forward<_Args>(__args)...);
251 * Knowing which of unary_function and binary_function _Tp derives
252 * from, derives from the same and ensures that reference_wrapper
253 * will have a weak result type. See cases below.
255 template<bool _Unary, bool _Binary, typename _Tp>
256 struct _Reference_wrapper_base_impl;
258 // None of the nested argument types.
259 template<typename _Tp>
260 struct _Reference_wrapper_base_impl<false, false, _Tp>
261 : _Weak_result_type<_Tp>
264 // Nested argument_type only.
265 template<typename _Tp>
266 struct _Reference_wrapper_base_impl<true, false, _Tp>
267 : _Weak_result_type<_Tp>
269 typedef typename _Tp::argument_type argument_type;
272 // Nested first_argument_type and second_argument_type only.
273 template<typename _Tp>
274 struct _Reference_wrapper_base_impl<false, true, _Tp>
275 : _Weak_result_type<_Tp>
277 typedef typename _Tp::first_argument_type first_argument_type;
278 typedef typename _Tp::second_argument_type second_argument_type;
281 // All the nested argument types.
282 template<typename _Tp>
283 struct _Reference_wrapper_base_impl<true, true, _Tp>
284 : _Weak_result_type<_Tp>
286 typedef typename _Tp::argument_type argument_type;
287 typedef typename _Tp::first_argument_type first_argument_type;
288 typedef typename _Tp::second_argument_type second_argument_type;
291 _GLIBCXX_HAS_NESTED_TYPE(argument_type)
292 _GLIBCXX_HAS_NESTED_TYPE(first_argument_type)
293 _GLIBCXX_HAS_NESTED_TYPE(second_argument_type)
296 * Derives from unary_function or binary_function when it
297 * can. Specializations handle all of the easy cases. The primary
298 * template determines what to do with a class type, which may
299 * derive from both unary_function and binary_function.
301 template<typename _Tp>
302 struct _Reference_wrapper_base
303 : _Reference_wrapper_base_impl<
304 __has_argument_type<_Tp>::value,
305 __has_first_argument_type<_Tp>::value
306 && __has_second_argument_type<_Tp>::value,
310 // - a function type (unary)
311 template<typename _Res, typename _T1>
312 struct _Reference_wrapper_base<_Res(_T1)>
313 : unary_function<_T1, _Res>
316 template<typename _Res, typename _T1>
317 struct _Reference_wrapper_base<_Res(_T1) const>
318 : unary_function<_T1, _Res>
321 template<typename _Res, typename _T1>
322 struct _Reference_wrapper_base<_Res(_T1) volatile>
323 : unary_function<_T1, _Res>
326 template<typename _Res, typename _T1>
327 struct _Reference_wrapper_base<_Res(_T1) const volatile>
328 : unary_function<_T1, _Res>
331 // - a function type (binary)
332 template<typename _Res, typename _T1, typename _T2>
333 struct _Reference_wrapper_base<_Res(_T1, _T2)>
334 : binary_function<_T1, _T2, _Res>
337 template<typename _Res, typename _T1, typename _T2>
338 struct _Reference_wrapper_base<_Res(_T1, _T2) const>
339 : binary_function<_T1, _T2, _Res>
342 template<typename _Res, typename _T1, typename _T2>
343 struct _Reference_wrapper_base<_Res(_T1, _T2) volatile>
344 : binary_function<_T1, _T2, _Res>
347 template<typename _Res, typename _T1, typename _T2>
348 struct _Reference_wrapper_base<_Res(_T1, _T2) const volatile>
349 : binary_function<_T1, _T2, _Res>
352 // - a function pointer type (unary)
353 template<typename _Res, typename _T1>
354 struct _Reference_wrapper_base<_Res(*)(_T1)>
355 : unary_function<_T1, _Res>
358 // - a function pointer type (binary)
359 template<typename _Res, typename _T1, typename _T2>
360 struct _Reference_wrapper_base<_Res(*)(_T1, _T2)>
361 : binary_function<_T1, _T2, _Res>
364 // - a pointer to member function type (unary, no qualifiers)
365 template<typename _Res, typename _T1>
366 struct _Reference_wrapper_base<_Res (_T1::*)()>
367 : unary_function<_T1*, _Res>
370 // - a pointer to member function type (binary, no qualifiers)
371 template<typename _Res, typename _T1, typename _T2>
372 struct _Reference_wrapper_base<_Res (_T1::*)(_T2)>
373 : binary_function<_T1*, _T2, _Res>
376 // - a pointer to member function type (unary, const)
377 template<typename _Res, typename _T1>
378 struct _Reference_wrapper_base<_Res (_T1::*)() const>
379 : unary_function<const _T1*, _Res>
382 // - a pointer to member function type (binary, const)
383 template<typename _Res, typename _T1, typename _T2>
384 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const>
385 : binary_function<const _T1*, _T2, _Res>
388 // - a pointer to member function type (unary, volatile)
389 template<typename _Res, typename _T1>
390 struct _Reference_wrapper_base<_Res (_T1::*)() volatile>
391 : unary_function<volatile _T1*, _Res>
394 // - a pointer to member function type (binary, volatile)
395 template<typename _Res, typename _T1, typename _T2>
396 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) volatile>
397 : binary_function<volatile _T1*, _T2, _Res>
400 // - a pointer to member function type (unary, const volatile)
401 template<typename _Res, typename _T1>
402 struct _Reference_wrapper_base<_Res (_T1::*)() const volatile>
403 : unary_function<const volatile _T1*, _Res>
406 // - a pointer to member function type (binary, const volatile)
407 template<typename _Res, typename _T1, typename _T2>
408 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const volatile>
409 : binary_function<const volatile _T1*, _T2, _Res>
413 * @brief Primary class template for reference_wrapper.
417 template<typename _Tp>
418 class reference_wrapper
419 : public _Reference_wrapper_base<typename remove_cv<_Tp>::type>
426 reference_wrapper(_Tp& __indata) noexcept
427 : _M_data(std::__addressof(__indata))
430 reference_wrapper(_Tp&&) = delete;
432 reference_wrapper(const reference_wrapper&) = default;
435 operator=(const reference_wrapper&) = default;
437 operator _Tp&() const noexcept
438 { return this->get(); }
444 template<typename... _Args>
445 typename result_of<_Tp&(_Args&&...)>::type
446 operator()(_Args&&... __args) const
448 return std::__invoke(get(), std::forward<_Args>(__args)...);
453 /// Denotes a reference should be taken to a variable.
454 template<typename _Tp>
455 inline reference_wrapper<_Tp>
456 ref(_Tp& __t) noexcept
457 { return reference_wrapper<_Tp>(__t); }
459 /// Denotes a const reference should be taken to a variable.
460 template<typename _Tp>
461 inline reference_wrapper<const _Tp>
462 cref(const _Tp& __t) noexcept
463 { return reference_wrapper<const _Tp>(__t); }
465 template<typename _Tp>
466 void ref(const _Tp&&) = delete;
468 template<typename _Tp>
469 void cref(const _Tp&&) = delete;
471 /// Partial specialization.
472 template<typename _Tp>
473 inline reference_wrapper<_Tp>
474 ref(reference_wrapper<_Tp> __t) noexcept
475 { return ref(__t.get()); }
477 /// Partial specialization.
478 template<typename _Tp>
479 inline reference_wrapper<const _Tp>
480 cref(reference_wrapper<_Tp> __t) noexcept
481 { return cref(__t.get()); }
485 template<typename... _Types>
486 struct _Pack : integral_constant<size_t, sizeof...(_Types)>
489 template<typename _From, typename _To, bool = _From::value == _To::value>
490 struct _AllConvertible : false_type
493 template<typename... _From, typename... _To>
494 struct _AllConvertible<_Pack<_From...>, _Pack<_To...>, true>
495 : __and_<is_convertible<_From, _To>...>
498 template<typename _Tp1, typename _Tp2>
499 using _NotSame = __not_<is_same<typename std::decay<_Tp1>::type,
500 typename std::decay<_Tp2>::type>>;
503 * Derives from @c unary_function or @c binary_function, or perhaps
504 * nothing, depending on the number of arguments provided. The
505 * primary template is the basis case, which derives nothing.
507 template<typename _Res, typename... _ArgTypes>
508 struct _Maybe_unary_or_binary_function { };
510 /// Derives from @c unary_function, as appropriate.
511 template<typename _Res, typename _T1>
512 struct _Maybe_unary_or_binary_function<_Res, _T1>
513 : std::unary_function<_T1, _Res> { };
515 /// Derives from @c binary_function, as appropriate.
516 template<typename _Res, typename _T1, typename _T2>
517 struct _Maybe_unary_or_binary_function<_Res, _T1, _T2>
518 : std::binary_function<_T1, _T2, _Res> { };
520 template<typename _Signature>
521 struct _Mem_fn_traits;
523 template<typename _Res, typename _Class, typename... _ArgTypes>
524 struct _Mem_fn_traits_base
526 using __result_type = _Res;
528 = _Maybe_unary_or_binary_function<_Res, _Class*, _ArgTypes...>;
529 using __arity = integral_constant<size_t, sizeof...(_ArgTypes)>;
532 #define _GLIBCXX_MEM_FN_TRAITS2(_CV, _REF, _LVAL, _RVAL) \
533 template<typename _Res, typename _Class, typename... _ArgTypes> \
534 struct _Mem_fn_traits<_Res (_Class::*)(_ArgTypes...) _CV _REF> \
535 : _Mem_fn_traits_base<_Res, _CV _Class, _ArgTypes...> \
537 using __vararg = false_type; \
539 template<typename _Res, typename _Class, typename... _ArgTypes> \
540 struct _Mem_fn_traits<_Res (_Class::*)(_ArgTypes... ...) _CV _REF> \
541 : _Mem_fn_traits_base<_Res, _CV _Class, _ArgTypes...> \
543 using __vararg = true_type; \
546 #define _GLIBCXX_MEM_FN_TRAITS(_REF, _LVAL, _RVAL) \
547 _GLIBCXX_MEM_FN_TRAITS2( , _REF, _LVAL, _RVAL) \
548 _GLIBCXX_MEM_FN_TRAITS2(const , _REF, _LVAL, _RVAL) \
549 _GLIBCXX_MEM_FN_TRAITS2(volatile , _REF, _LVAL, _RVAL) \
550 _GLIBCXX_MEM_FN_TRAITS2(const volatile, _REF, _LVAL, _RVAL)
552 _GLIBCXX_MEM_FN_TRAITS( , true_type, true_type)
553 _GLIBCXX_MEM_FN_TRAITS(&, true_type, false_type)
554 _GLIBCXX_MEM_FN_TRAITS(&&, false_type, true_type)
556 #undef _GLIBCXX_MEM_FN_TRAITS
557 #undef _GLIBCXX_MEM_FN_TRAITS2
559 template<typename _MemFunPtr,
560 bool __is_mem_fn = is_member_function_pointer<_MemFunPtr>::value>
562 : public _Mem_fn_traits<_MemFunPtr>::__maybe_type
564 using _Traits = _Mem_fn_traits<_MemFunPtr>;
566 using _Arity = typename _Traits::__arity;
567 using _Varargs = typename _Traits::__vararg;
569 template<typename _Func, typename... _BoundArgs>
570 friend struct _Bind_check_arity;
576 using result_type = typename _Traits::__result_type;
579 _Mem_fn_base(_MemFunPtr __pmf) noexcept : _M_pmf(__pmf) { }
581 template<typename... _Args>
583 operator()(_Args&&... __args) const
585 std::__invoke(_M_pmf, std::forward<_Args>(__args)...)))
586 -> decltype(std::__invoke(_M_pmf, std::forward<_Args>(__args)...))
587 { return std::__invoke(_M_pmf, std::forward<_Args>(__args)...); }
590 // Partial specialization for member object pointers.
591 template<typename _MemObjPtr>
592 class _Mem_fn_base<_MemObjPtr, false>
594 using _Arity = integral_constant<size_t, 0>;
595 using _Varargs = false_type;
597 template<typename _Func, typename... _BoundArgs>
598 friend struct _Bind_check_arity;
604 _Mem_fn_base(_MemObjPtr __pm) noexcept : _M_pm(__pm) { }
606 template<typename _Tp>
608 operator()(_Tp&& __obj) const
609 noexcept(noexcept(std::__invoke(_M_pm, std::forward<_Tp>(__obj))))
610 -> decltype(std::__invoke(_M_pm, std::forward<_Tp>(__obj)))
611 { return std::__invoke(_M_pm, std::forward<_Tp>(__obj)); }
614 template<typename _Res, typename _Class>
615 struct _Mem_fn<_Res _Class::*>
616 : _Mem_fn_base<_Res _Class::*>
618 using _Mem_fn_base<_Res _Class::*>::_Mem_fn_base;
621 // _GLIBCXX_RESOLVE_LIB_DEFECTS
622 // 2048. Unnecessary mem_fn overloads
624 * @brief Returns a function object that forwards to the member
628 template<typename _Tp, typename _Class>
629 inline _Mem_fn<_Tp _Class::*>
630 mem_fn(_Tp _Class::* __pm) noexcept
632 return _Mem_fn<_Tp _Class::*>(__pm);
636 * @brief Determines if the given type _Tp is a function object that
637 * should be treated as a subexpression when evaluating calls to
638 * function objects returned by bind().
640 * C++11 [func.bind.isbind].
643 template<typename _Tp>
644 struct is_bind_expression
645 : public false_type { };
648 * @brief Determines if the given type _Tp is a placeholder in a
649 * bind() expression and, if so, which placeholder it is.
651 * C++11 [func.bind.isplace].
654 template<typename _Tp>
655 struct is_placeholder
656 : public integral_constant<int, 0>
659 #if __cplusplus > 201402L
660 template <typename _Tp> constexpr bool is_bind_expression_v
661 = is_bind_expression<_Tp>::value;
662 template <typename _Tp> constexpr int is_placeholder_v
663 = is_placeholder<_Tp>::value;
666 /** @brief The type of placeholder objects defined by libstdc++.
669 template<int _Num> struct _Placeholder { };
671 _GLIBCXX_END_NAMESPACE_VERSION
673 /** @namespace std::placeholders
674 * @brief ISO C++11 entities sub-namespace for functional.
677 namespace placeholders
679 _GLIBCXX_BEGIN_NAMESPACE_VERSION
680 /* Define a large number of placeholders. There is no way to
681 * simplify this with variadic templates, because we're introducing
682 * unique names for each.
684 extern const _Placeholder<1> _1;
685 extern const _Placeholder<2> _2;
686 extern const _Placeholder<3> _3;
687 extern const _Placeholder<4> _4;
688 extern const _Placeholder<5> _5;
689 extern const _Placeholder<6> _6;
690 extern const _Placeholder<7> _7;
691 extern const _Placeholder<8> _8;
692 extern const _Placeholder<9> _9;
693 extern const _Placeholder<10> _10;
694 extern const _Placeholder<11> _11;
695 extern const _Placeholder<12> _12;
696 extern const _Placeholder<13> _13;
697 extern const _Placeholder<14> _14;
698 extern const _Placeholder<15> _15;
699 extern const _Placeholder<16> _16;
700 extern const _Placeholder<17> _17;
701 extern const _Placeholder<18> _18;
702 extern const _Placeholder<19> _19;
703 extern const _Placeholder<20> _20;
704 extern const _Placeholder<21> _21;
705 extern const _Placeholder<22> _22;
706 extern const _Placeholder<23> _23;
707 extern const _Placeholder<24> _24;
708 extern const _Placeholder<25> _25;
709 extern const _Placeholder<26> _26;
710 extern const _Placeholder<27> _27;
711 extern const _Placeholder<28> _28;
712 extern const _Placeholder<29> _29;
713 _GLIBCXX_END_NAMESPACE_VERSION
716 _GLIBCXX_BEGIN_NAMESPACE_VERSION
719 * Partial specialization of is_placeholder that provides the placeholder
720 * number for the placeholder objects defined by libstdc++.
724 struct is_placeholder<_Placeholder<_Num> >
725 : public integral_constant<int, _Num>
729 struct is_placeholder<const _Placeholder<_Num> >
730 : public integral_constant<int, _Num>
734 // Like tuple_element_t but SFINAE-friendly.
735 template<std::size_t __i, typename _Tuple>
736 using _Safe_tuple_element_t
737 = typename enable_if<(__i < tuple_size<_Tuple>::value),
738 tuple_element<__i, _Tuple>>::type::type;
741 * Maps an argument to bind() into an actual argument to the bound
742 * function object [func.bind.bind]/10. Only the first parameter should
743 * be specified: the rest are used to determine among the various
744 * implementations. Note that, although this class is a function
745 * object, it isn't entirely normal because it takes only two
746 * parameters regardless of the number of parameters passed to the
747 * bind expression. The first parameter is the bound argument and
748 * the second parameter is a tuple containing references to the
749 * rest of the arguments.
751 template<typename _Arg,
752 bool _IsBindExp = is_bind_expression<_Arg>::value,
753 bool _IsPlaceholder = (is_placeholder<_Arg>::value > 0)>
757 * If the argument is reference_wrapper<_Tp>, returns the
758 * underlying reference.
759 * C++11 [func.bind.bind] p10 bullet 1.
761 template<typename _Tp>
762 class _Mu<reference_wrapper<_Tp>, false, false>
765 /* Note: This won't actually work for const volatile
766 * reference_wrappers, because reference_wrapper::get() is const
767 * but not volatile-qualified. This might be a defect in the TR.
769 template<typename _CVRef, typename _Tuple>
771 operator()(_CVRef& __arg, _Tuple&) const volatile
772 { return __arg.get(); }
776 * If the argument is a bind expression, we invoke the underlying
777 * function object with the same cv-qualifiers as we are given and
778 * pass along all of our arguments (unwrapped).
779 * C++11 [func.bind.bind] p10 bullet 2.
781 template<typename _Arg>
782 class _Mu<_Arg, true, false>
785 template<typename _CVArg, typename... _Args>
787 operator()(_CVArg& __arg,
788 tuple<_Args...>& __tuple) const volatile
789 -> decltype(__arg(declval<_Args>()...))
791 // Construct an index tuple and forward to __call
792 typedef typename _Build_index_tuple<sizeof...(_Args)>::__type
794 return this->__call(__arg, __tuple, _Indexes());
798 // Invokes the underlying function object __arg by unpacking all
799 // of the arguments in the tuple.
800 template<typename _CVArg, typename... _Args, std::size_t... _Indexes>
802 __call(_CVArg& __arg, tuple<_Args...>& __tuple,
803 const _Index_tuple<_Indexes...>&) const volatile
804 -> decltype(__arg(declval<_Args>()...))
806 return __arg(std::forward<_Args>(std::get<_Indexes>(__tuple))...);
811 * If the argument is a placeholder for the Nth argument, returns
812 * a reference to the Nth argument to the bind function object.
813 * C++11 [func.bind.bind] p10 bullet 3.
815 template<typename _Arg>
816 class _Mu<_Arg, false, true>
819 template<typename _Tuple>
820 _Safe_tuple_element_t<(is_placeholder<_Arg>::value - 1), _Tuple>&&
821 operator()(const volatile _Arg&, _Tuple& __tuple) const volatile
824 = __tuple_element_t<(is_placeholder<_Arg>::value - 1), _Tuple>;
825 return std::forward<__type>(
826 ::std::get<(is_placeholder<_Arg>::value - 1)>(__tuple));
831 * If the argument is just a value, returns a reference to that
832 * value. The cv-qualifiers on the reference are determined by the caller.
833 * C++11 [func.bind.bind] p10 bullet 4.
835 template<typename _Arg>
836 class _Mu<_Arg, false, false>
839 template<typename _CVArg, typename _Tuple>
841 operator()(_CVArg&& __arg, _Tuple&) const volatile
842 { return std::forward<_CVArg>(__arg); }
846 * Maps member pointers into instances of _Mem_fn but leaves all
847 * other function objects untouched. Used by std::bind(). The
848 * primary template handles the non-member-pointer case.
850 template<typename _Tp>
851 struct _Maybe_wrap_member_pointer
855 static constexpr const _Tp&
856 __do_wrap(const _Tp& __x)
859 static constexpr _Tp&&
861 { return static_cast<_Tp&&>(__x); }
865 * Maps member pointers into instances of _Mem_fn but leaves all
866 * other function objects untouched. Used by std::bind(). This
867 * partial specialization handles the member pointer case.
869 template<typename _Tp, typename _Class>
870 struct _Maybe_wrap_member_pointer<_Tp _Class::*>
872 typedef _Mem_fn<_Tp _Class::*> type;
874 static constexpr type
875 __do_wrap(_Tp _Class::* __pm)
876 { return type(__pm); }
879 // Specialization needed to prevent "forming reference to void" errors when
880 // bind<void>() is called, because argument deduction instantiates
881 // _Maybe_wrap_member_pointer<void> outside the immediate context where
884 struct _Maybe_wrap_member_pointer<void>
889 // std::get<I> for volatile-qualified tuples
890 template<std::size_t _Ind, typename... _Tp>
892 __volget(volatile tuple<_Tp...>& __tuple)
893 -> __tuple_element_t<_Ind, tuple<_Tp...>> volatile&
894 { return std::get<_Ind>(const_cast<tuple<_Tp...>&>(__tuple)); }
896 // std::get<I> for const-volatile-qualified tuples
897 template<std::size_t _Ind, typename... _Tp>
899 __volget(const volatile tuple<_Tp...>& __tuple)
900 -> __tuple_element_t<_Ind, tuple<_Tp...>> const volatile&
901 { return std::get<_Ind>(const_cast<const tuple<_Tp...>&>(__tuple)); }
903 /// Type of the function object returned from bind().
904 template<typename _Signature>
907 template<typename _Functor, typename... _Bound_args>
908 class _Bind<_Functor(_Bound_args...)>
909 : public _Weak_result_type<_Functor>
911 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
915 tuple<_Bound_args...> _M_bound_args;
918 template<typename _Result, typename... _Args, std::size_t... _Indexes>
920 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>)
922 return _M_f(_Mu<_Bound_args>()
923 (std::get<_Indexes>(_M_bound_args), __args)...);
927 template<typename _Result, typename... _Args, std::size_t... _Indexes>
929 __call_c(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>) const
931 return _M_f(_Mu<_Bound_args>()
932 (std::get<_Indexes>(_M_bound_args), __args)...);
936 template<typename _Result, typename... _Args, std::size_t... _Indexes>
938 __call_v(tuple<_Args...>&& __args,
939 _Index_tuple<_Indexes...>) volatile
941 return _M_f(_Mu<_Bound_args>()
942 (__volget<_Indexes>(_M_bound_args), __args)...);
945 // Call as const volatile
946 template<typename _Result, typename... _Args, std::size_t... _Indexes>
948 __call_c_v(tuple<_Args...>&& __args,
949 _Index_tuple<_Indexes...>) const volatile
951 return _M_f(_Mu<_Bound_args>()
952 (__volget<_Indexes>(_M_bound_args), __args)...);
956 template<typename... _Args>
957 explicit _Bind(const _Functor& __f, _Args&&... __args)
958 : _M_f(__f), _M_bound_args(std::forward<_Args>(__args)...)
961 template<typename... _Args>
962 explicit _Bind(_Functor&& __f, _Args&&... __args)
963 : _M_f(std::move(__f)), _M_bound_args(std::forward<_Args>(__args)...)
966 _Bind(const _Bind&) = default;
969 : _M_f(std::move(__b._M_f)), _M_bound_args(std::move(__b._M_bound_args))
973 template<typename... _Args, typename _Result
974 = decltype( std::declval<_Functor&>()(
975 _Mu<_Bound_args>()( std::declval<_Bound_args&>(),
976 std::declval<tuple<_Args...>&>() )... ) )>
978 operator()(_Args&&... __args)
980 return this->__call<_Result>(
981 std::forward_as_tuple(std::forward<_Args>(__args)...),
986 template<typename... _Args, typename _Result
987 = decltype( std::declval<typename enable_if<(sizeof...(_Args) >= 0),
988 typename add_const<_Functor>::type&>::type>()(
989 _Mu<_Bound_args>()( std::declval<const _Bound_args&>(),
990 std::declval<tuple<_Args...>&>() )... ) )>
992 operator()(_Args&&... __args) const
994 return this->__call_c<_Result>(
995 std::forward_as_tuple(std::forward<_Args>(__args)...),
1000 template<typename... _Args, typename _Result
1001 = decltype( std::declval<typename enable_if<(sizeof...(_Args) >= 0),
1002 typename add_volatile<_Functor>::type&>::type>()(
1003 _Mu<_Bound_args>()( std::declval<volatile _Bound_args&>(),
1004 std::declval<tuple<_Args...>&>() )... ) )>
1006 operator()(_Args&&... __args) volatile
1008 return this->__call_v<_Result>(
1009 std::forward_as_tuple(std::forward<_Args>(__args)...),
1013 // Call as const volatile
1014 template<typename... _Args, typename _Result
1015 = decltype( std::declval<typename enable_if<(sizeof...(_Args) >= 0),
1016 typename add_cv<_Functor>::type&>::type>()(
1017 _Mu<_Bound_args>()( std::declval<const volatile _Bound_args&>(),
1018 std::declval<tuple<_Args...>&>() )... ) )>
1020 operator()(_Args&&... __args) const volatile
1022 return this->__call_c_v<_Result>(
1023 std::forward_as_tuple(std::forward<_Args>(__args)...),
1028 /// Type of the function object returned from bind<R>().
1029 template<typename _Result, typename _Signature>
1030 struct _Bind_result;
1032 template<typename _Result, typename _Functor, typename... _Bound_args>
1033 class _Bind_result<_Result, _Functor(_Bound_args...)>
1035 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
1039 tuple<_Bound_args...> _M_bound_args;
1042 template<typename _Res>
1043 struct __enable_if_void : enable_if<is_void<_Res>::value, int> { };
1044 template<typename _Res>
1045 struct __disable_if_void : enable_if<!is_void<_Res>::value, int> { };
1048 template<typename _Res, typename... _Args, std::size_t... _Indexes>
1050 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1051 typename __disable_if_void<_Res>::type = 0)
1053 return _M_f(_Mu<_Bound_args>()
1054 (std::get<_Indexes>(_M_bound_args), __args)...);
1057 // Call unqualified, return void
1058 template<typename _Res, typename... _Args, std::size_t... _Indexes>
1060 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1061 typename __enable_if_void<_Res>::type = 0)
1063 _M_f(_Mu<_Bound_args>()
1064 (std::get<_Indexes>(_M_bound_args), __args)...);
1068 template<typename _Res, typename... _Args, std::size_t... _Indexes>
1070 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1071 typename __disable_if_void<_Res>::type = 0) const
1073 return _M_f(_Mu<_Bound_args>()
1074 (std::get<_Indexes>(_M_bound_args), __args)...);
1077 // Call as const, return void
1078 template<typename _Res, typename... _Args, std::size_t... _Indexes>
1080 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1081 typename __enable_if_void<_Res>::type = 0) const
1083 _M_f(_Mu<_Bound_args>()
1084 (std::get<_Indexes>(_M_bound_args), __args)...);
1088 template<typename _Res, typename... _Args, std::size_t... _Indexes>
1090 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1091 typename __disable_if_void<_Res>::type = 0) volatile
1093 return _M_f(_Mu<_Bound_args>()
1094 (__volget<_Indexes>(_M_bound_args), __args)...);
1097 // Call as volatile, return void
1098 template<typename _Res, typename... _Args, std::size_t... _Indexes>
1100 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1101 typename __enable_if_void<_Res>::type = 0) volatile
1103 _M_f(_Mu<_Bound_args>()
1104 (__volget<_Indexes>(_M_bound_args), __args)...);
1107 // Call as const volatile
1108 template<typename _Res, typename... _Args, std::size_t... _Indexes>
1110 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1111 typename __disable_if_void<_Res>::type = 0) const volatile
1113 return _M_f(_Mu<_Bound_args>()
1114 (__volget<_Indexes>(_M_bound_args), __args)...);
1117 // Call as const volatile, return void
1118 template<typename _Res, typename... _Args, std::size_t... _Indexes>
1120 __call(tuple<_Args...>&& __args,
1121 _Index_tuple<_Indexes...>,
1122 typename __enable_if_void<_Res>::type = 0) const volatile
1124 _M_f(_Mu<_Bound_args>()
1125 (__volget<_Indexes>(_M_bound_args), __args)...);
1129 typedef _Result result_type;
1131 template<typename... _Args>
1132 explicit _Bind_result(const _Functor& __f, _Args&&... __args)
1133 : _M_f(__f), _M_bound_args(std::forward<_Args>(__args)...)
1136 template<typename... _Args>
1137 explicit _Bind_result(_Functor&& __f, _Args&&... __args)
1138 : _M_f(std::move(__f)), _M_bound_args(std::forward<_Args>(__args)...)
1141 _Bind_result(const _Bind_result&) = default;
1143 _Bind_result(_Bind_result&& __b)
1144 : _M_f(std::move(__b._M_f)), _M_bound_args(std::move(__b._M_bound_args))
1148 template<typename... _Args>
1150 operator()(_Args&&... __args)
1152 return this->__call<_Result>(
1153 std::forward_as_tuple(std::forward<_Args>(__args)...),
1158 template<typename... _Args>
1160 operator()(_Args&&... __args) const
1162 return this->__call<_Result>(
1163 std::forward_as_tuple(std::forward<_Args>(__args)...),
1168 template<typename... _Args>
1170 operator()(_Args&&... __args) volatile
1172 return this->__call<_Result>(
1173 std::forward_as_tuple(std::forward<_Args>(__args)...),
1177 // Call as const volatile
1178 template<typename... _Args>
1180 operator()(_Args&&... __args) const volatile
1182 return this->__call<_Result>(
1183 std::forward_as_tuple(std::forward<_Args>(__args)...),
1189 * @brief Class template _Bind is always a bind expression.
1192 template<typename _Signature>
1193 struct is_bind_expression<_Bind<_Signature> >
1194 : public true_type { };
1197 * @brief Class template _Bind is always a bind expression.
1200 template<typename _Signature>
1201 struct is_bind_expression<const _Bind<_Signature> >
1202 : public true_type { };
1205 * @brief Class template _Bind is always a bind expression.
1208 template<typename _Signature>
1209 struct is_bind_expression<volatile _Bind<_Signature> >
1210 : public true_type { };
1213 * @brief Class template _Bind is always a bind expression.
1216 template<typename _Signature>
1217 struct is_bind_expression<const volatile _Bind<_Signature>>
1218 : public true_type { };
1221 * @brief Class template _Bind_result is always a bind expression.
1224 template<typename _Result, typename _Signature>
1225 struct is_bind_expression<_Bind_result<_Result, _Signature>>
1226 : public true_type { };
1229 * @brief Class template _Bind_result is always a bind expression.
1232 template<typename _Result, typename _Signature>
1233 struct is_bind_expression<const _Bind_result<_Result, _Signature>>
1234 : public true_type { };
1237 * @brief Class template _Bind_result is always a bind expression.
1240 template<typename _Result, typename _Signature>
1241 struct is_bind_expression<volatile _Bind_result<_Result, _Signature>>
1242 : public true_type { };
1245 * @brief Class template _Bind_result is always a bind expression.
1248 template<typename _Result, typename _Signature>
1249 struct is_bind_expression<const volatile _Bind_result<_Result, _Signature>>
1250 : public true_type { };
1252 template<typename _Func, typename... _BoundArgs>
1253 struct _Bind_check_arity { };
1255 template<typename _Ret, typename... _Args, typename... _BoundArgs>
1256 struct _Bind_check_arity<_Ret (*)(_Args...), _BoundArgs...>
1258 static_assert(sizeof...(_BoundArgs) == sizeof...(_Args),
1259 "Wrong number of arguments for function");
1262 template<typename _Ret, typename... _Args, typename... _BoundArgs>
1263 struct _Bind_check_arity<_Ret (*)(_Args......), _BoundArgs...>
1265 static_assert(sizeof...(_BoundArgs) >= sizeof...(_Args),
1266 "Wrong number of arguments for function");
1269 template<typename _Tp, typename _Class, typename... _BoundArgs>
1270 struct _Bind_check_arity<_Tp _Class::*, _BoundArgs...>
1272 using _Arity = typename _Mem_fn<_Tp _Class::*>::_Arity;
1273 using _Varargs = typename _Mem_fn<_Tp _Class::*>::_Varargs;
1274 static_assert(_Varargs::value
1275 ? sizeof...(_BoundArgs) >= _Arity::value + 1
1276 : sizeof...(_BoundArgs) == _Arity::value + 1,
1277 "Wrong number of arguments for pointer-to-member");
1280 // Trait type used to remove std::bind() from overload set via SFINAE
1281 // when first argument has integer type, so that std::bind() will
1282 // not be a better match than ::bind() from the BSD Sockets API.
1283 template<typename _Tp, typename _Tp2 = typename decay<_Tp>::type>
1284 using __is_socketlike = __or_<is_integral<_Tp2>, is_enum<_Tp2>>;
1286 template<bool _SocketLike, typename _Func, typename... _BoundArgs>
1288 : _Bind_check_arity<typename decay<_Func>::type, _BoundArgs...>
1290 typedef _Maybe_wrap_member_pointer<typename decay<_Func>::type>
1292 typedef typename __maybe_type::type __func_type;
1293 typedef _Bind<__func_type(typename decay<_BoundArgs>::type...)> type;
1296 // Partial specialization for is_socketlike == true, does not define
1297 // nested type so std::bind() will not participate in overload resolution
1298 // when the first argument might be a socket file descriptor.
1299 template<typename _Func, typename... _BoundArgs>
1300 struct _Bind_helper<true, _Func, _BoundArgs...>
1304 * @brief Function template for std::bind.
1307 template<typename _Func, typename... _BoundArgs>
1309 _Bind_helper<__is_socketlike<_Func>::value, _Func, _BoundArgs...>::type
1310 bind(_Func&& __f, _BoundArgs&&... __args)
1312 typedef _Bind_helper<false, _Func, _BoundArgs...> __helper_type;
1313 typedef typename __helper_type::__maybe_type __maybe_type;
1314 typedef typename __helper_type::type __result_type;
1315 return __result_type(__maybe_type::__do_wrap(std::forward<_Func>(__f)),
1316 std::forward<_BoundArgs>(__args)...);
1319 template<typename _Result, typename _Func, typename... _BoundArgs>
1320 struct _Bindres_helper
1321 : _Bind_check_arity<typename decay<_Func>::type, _BoundArgs...>
1323 typedef _Maybe_wrap_member_pointer<typename decay<_Func>::type>
1325 typedef typename __maybe_type::type __functor_type;
1326 typedef _Bind_result<_Result,
1327 __functor_type(typename decay<_BoundArgs>::type...)>
1332 * @brief Function template for std::bind<R>.
1335 template<typename _Result, typename _Func, typename... _BoundArgs>
1337 typename _Bindres_helper<_Result, _Func, _BoundArgs...>::type
1338 bind(_Func&& __f, _BoundArgs&&... __args)
1340 typedef _Bindres_helper<_Result, _Func, _BoundArgs...> __helper_type;
1341 typedef typename __helper_type::__maybe_type __maybe_type;
1342 typedef typename __helper_type::type __result_type;
1343 return __result_type(__maybe_type::__do_wrap(std::forward<_Func>(__f)),
1344 std::forward<_BoundArgs>(__args)...);
1347 template<typename _Signature>
1348 struct _Bind_simple;
1350 template<typename _Callable, typename... _Args>
1351 struct _Bind_simple<_Callable(_Args...)>
1353 typedef typename result_of<_Callable(_Args...)>::type result_type;
1355 template<typename _Tp, typename... _Up>
1357 _Bind_simple(_Tp&& __f, _Up&&... __args)
1358 : _M_bound(std::forward<_Tp>(__f), std::forward<_Up>(__args)...)
1361 _Bind_simple(const _Bind_simple&) = default;
1362 _Bind_simple(_Bind_simple&&) = default;
1367 typedef typename _Build_index_tuple<sizeof...(_Args)>::__type _Indices;
1368 return _M_invoke(_Indices());
1372 template<std::size_t... _Indices>
1373 typename result_of<_Callable(_Args...)>::type
1374 _M_invoke(_Index_tuple<_Indices...>)
1376 // std::bind always forwards bound arguments as lvalues,
1377 // but this type can call functions which only accept rvalues.
1378 return std::forward<_Callable>(std::get<0>(_M_bound))(
1379 std::forward<_Args>(std::get<_Indices+1>(_M_bound))...);
1382 std::tuple<_Callable, _Args...> _M_bound;
1385 template<typename _Func, typename... _BoundArgs>
1386 struct _Bind_simple_helper
1387 : _Bind_check_arity<typename decay<_Func>::type, _BoundArgs...>
1389 typedef _Maybe_wrap_member_pointer<typename decay<_Func>::type>
1391 typedef typename __maybe_type::type __func_type;
1392 typedef _Bind_simple<__func_type(typename decay<_BoundArgs>::type...)>
1396 // Simplified version of std::bind for internal use, without support for
1397 // unbound arguments, placeholders or nested bind expressions.
1398 template<typename _Callable, typename... _Args>
1399 typename _Bind_simple_helper<_Callable, _Args...>::__type
1400 __bind_simple(_Callable&& __callable, _Args&&... __args)
1402 typedef _Bind_simple_helper<_Callable, _Args...> __helper_type;
1403 typedef typename __helper_type::__maybe_type __maybe_type;
1404 typedef typename __helper_type::__type __result_type;
1405 return __result_type(
1406 __maybe_type::__do_wrap( std::forward<_Callable>(__callable)),
1407 std::forward<_Args>(__args)...);
1411 * @brief Exception class thrown when class template function's
1412 * operator() is called with an empty target.
1413 * @ingroup exceptions
1415 class bad_function_call : public std::exception
1418 virtual ~bad_function_call() noexcept;
1420 const char* what() const noexcept;
1424 * Trait identifying "location-invariant" types, meaning that the
1425 * address of the object (or any of its members) will not escape.
1426 * Trivially copyable types are location-invariant and users can
1427 * specialize this trait for other types.
1429 template<typename _Tp>
1430 struct __is_location_invariant
1431 : is_trivially_copyable<_Tp>::type
1434 class _Undefined_class;
1439 const void* _M_const_object;
1440 void (*_M_function_pointer)();
1441 void (_Undefined_class::*_M_member_pointer)();
1446 void* _M_access() { return &_M_pod_data[0]; }
1447 const void* _M_access() const { return &_M_pod_data[0]; }
1449 template<typename _Tp>
1452 { return *static_cast<_Tp*>(_M_access()); }
1454 template<typename _Tp>
1457 { return *static_cast<const _Tp*>(_M_access()); }
1459 _Nocopy_types _M_unused;
1460 char _M_pod_data[sizeof(_Nocopy_types)];
1463 enum _Manager_operation
1471 // Simple type wrapper that helps avoid annoying const problems
1472 // when casting between void pointers and pointers-to-pointers.
1473 template<typename _Tp>
1474 struct _Simple_type_wrapper
1476 _Simple_type_wrapper(_Tp __value) : __value(__value) { }
1481 template<typename _Tp>
1482 struct __is_location_invariant<_Simple_type_wrapper<_Tp> >
1483 : __is_location_invariant<_Tp>
1486 template<typename _Signature>
1489 /// Base class of all polymorphic function object wrappers.
1490 class _Function_base
1493 static const std::size_t _M_max_size = sizeof(_Nocopy_types);
1494 static const std::size_t _M_max_align = __alignof__(_Nocopy_types);
1496 template<typename _Functor>
1500 static const bool __stored_locally =
1501 (__is_location_invariant<_Functor>::value
1502 && sizeof(_Functor) <= _M_max_size
1503 && __alignof__(_Functor) <= _M_max_align
1504 && (_M_max_align % __alignof__(_Functor) == 0));
1506 typedef integral_constant<bool, __stored_locally> _Local_storage;
1508 // Retrieve a pointer to the function object
1510 _M_get_pointer(const _Any_data& __source)
1512 const _Functor* __ptr =
1513 __stored_locally? std::__addressof(__source._M_access<_Functor>())
1514 /* have stored a pointer */ : __source._M_access<_Functor*>();
1515 return const_cast<_Functor*>(__ptr);
1518 // Clone a location-invariant function object that fits within
1519 // an _Any_data structure.
1521 _M_clone(_Any_data& __dest, const _Any_data& __source, true_type)
1523 new (__dest._M_access()) _Functor(__source._M_access<_Functor>());
1526 // Clone a function object that is not location-invariant or
1527 // that cannot fit into an _Any_data structure.
1529 _M_clone(_Any_data& __dest, const _Any_data& __source, false_type)
1531 __dest._M_access<_Functor*>() =
1532 new _Functor(*__source._M_access<_Functor*>());
1535 // Destroying a location-invariant object may still require
1538 _M_destroy(_Any_data& __victim, true_type)
1540 __victim._M_access<_Functor>().~_Functor();
1543 // Destroying an object located on the heap.
1545 _M_destroy(_Any_data& __victim, false_type)
1547 delete __victim._M_access<_Functor*>();
1552 _M_manager(_Any_data& __dest, const _Any_data& __source,
1553 _Manager_operation __op)
1558 case __get_type_info:
1559 __dest._M_access<const type_info*>() = &typeid(_Functor);
1562 case __get_functor_ptr:
1563 __dest._M_access<_Functor*>() = _M_get_pointer(__source);
1566 case __clone_functor:
1567 _M_clone(__dest, __source, _Local_storage());
1570 case __destroy_functor:
1571 _M_destroy(__dest, _Local_storage());
1578 _M_init_functor(_Any_data& __functor, _Functor&& __f)
1579 { _M_init_functor(__functor, std::move(__f), _Local_storage()); }
1581 template<typename _Signature>
1583 _M_not_empty_function(const function<_Signature>& __f)
1584 { return static_cast<bool>(__f); }
1586 template<typename _Tp>
1588 _M_not_empty_function(_Tp* __fp)
1589 { return __fp != nullptr; }
1591 template<typename _Class, typename _Tp>
1593 _M_not_empty_function(_Tp _Class::* __mp)
1594 { return __mp != nullptr; }
1596 template<typename _Tp>
1598 _M_not_empty_function(const _Tp&)
1603 _M_init_functor(_Any_data& __functor, _Functor&& __f, true_type)
1604 { new (__functor._M_access()) _Functor(std::move(__f)); }
1607 _M_init_functor(_Any_data& __functor, _Functor&& __f, false_type)
1608 { __functor._M_access<_Functor*>() = new _Functor(std::move(__f)); }
1611 template<typename _Functor>
1612 class _Ref_manager : public _Base_manager<_Functor*>
1614 typedef _Function_base::_Base_manager<_Functor*> _Base;
1618 _M_manager(_Any_data& __dest, const _Any_data& __source,
1619 _Manager_operation __op)
1624 case __get_type_info:
1625 __dest._M_access<const type_info*>() = &typeid(_Functor);
1628 case __get_functor_ptr:
1629 __dest._M_access<_Functor*>() = *_Base::_M_get_pointer(__source);
1630 return is_const<_Functor>::value;
1634 _Base::_M_manager(__dest, __source, __op);
1640 _M_init_functor(_Any_data& __functor, reference_wrapper<_Functor> __f)
1642 _Base::_M_init_functor(__functor, std::__addressof(__f.get()));
1646 _Function_base() : _M_manager(nullptr) { }
1651 _M_manager(_M_functor, _M_functor, __destroy_functor);
1655 bool _M_empty() const { return !_M_manager; }
1657 typedef bool (*_Manager_type)(_Any_data&, const _Any_data&,
1658 _Manager_operation);
1660 _Any_data _M_functor;
1661 _Manager_type _M_manager;
1664 template<typename _Signature, typename _Functor>
1665 class _Function_handler;
1667 template<typename _Res, typename _Functor, typename... _ArgTypes>
1668 class _Function_handler<_Res(_ArgTypes...), _Functor>
1669 : public _Function_base::_Base_manager<_Functor>
1671 typedef _Function_base::_Base_manager<_Functor> _Base;
1675 _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args)
1677 return (*_Base::_M_get_pointer(__functor))(
1678 std::forward<_ArgTypes>(__args)...);
1682 template<typename _Functor, typename... _ArgTypes>
1683 class _Function_handler<void(_ArgTypes...), _Functor>
1684 : public _Function_base::_Base_manager<_Functor>
1686 typedef _Function_base::_Base_manager<_Functor> _Base;
1690 _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args)
1692 (*_Base::_M_get_pointer(__functor))(
1693 std::forward<_ArgTypes>(__args)...);
1697 template<typename _Res, typename _Functor, typename... _ArgTypes>
1698 class _Function_handler<_Res(_ArgTypes...), reference_wrapper<_Functor> >
1699 : public _Function_base::_Ref_manager<_Functor>
1701 typedef _Function_base::_Ref_manager<_Functor> _Base;
1705 _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args)
1707 return std::__invoke(**_Base::_M_get_pointer(__functor),
1708 std::forward<_ArgTypes>(__args)...);
1712 template<typename _Functor, typename... _ArgTypes>
1713 class _Function_handler<void(_ArgTypes...), reference_wrapper<_Functor> >
1714 : public _Function_base::_Ref_manager<_Functor>
1716 typedef _Function_base::_Ref_manager<_Functor> _Base;
1720 _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args)
1722 std::__invoke(**_Base::_M_get_pointer(__functor),
1723 std::forward<_ArgTypes>(__args)...);
1727 template<typename _Class, typename _Member, typename _Res,
1728 typename... _ArgTypes>
1729 class _Function_handler<_Res(_ArgTypes...), _Member _Class::*>
1730 : public _Function_handler<void(_ArgTypes...), _Member _Class::*>
1732 typedef _Function_handler<void(_ArgTypes...), _Member _Class::*>
1737 _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args)
1739 return std::__invoke(_Base::_M_get_pointer(__functor)->__value,
1740 std::forward<_ArgTypes>(__args)...);
1744 template<typename _Class, typename _Member, typename... _ArgTypes>
1745 class _Function_handler<void(_ArgTypes...), _Member _Class::*>
1746 : public _Function_base::_Base_manager<
1747 _Simple_type_wrapper< _Member _Class::* > >
1749 typedef _Member _Class::* _Functor;
1750 typedef _Simple_type_wrapper<_Functor> _Wrapper;
1751 typedef _Function_base::_Base_manager<_Wrapper> _Base;
1755 _M_manager(_Any_data& __dest, const _Any_data& __source,
1756 _Manager_operation __op)
1761 case __get_type_info:
1762 __dest._M_access<const type_info*>() = &typeid(_Functor);
1765 case __get_functor_ptr:
1766 __dest._M_access<_Functor*>() =
1767 &_Base::_M_get_pointer(__source)->__value;
1771 _Base::_M_manager(__dest, __source, __op);
1777 _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args)
1779 std::__invoke(_Base::_M_get_pointer(__functor)->__value,
1780 std::forward<_ArgTypes>(__args)...);
1784 template<typename _From, typename _To>
1785 using __check_func_return_type
1786 = __or_<is_void<_To>, is_same<_From, _To>, is_convertible<_From, _To>>;
1789 * @brief Primary class template for std::function.
1792 * Polymorphic function wrapper.
1794 template<typename _Res, typename... _ArgTypes>
1795 class function<_Res(_ArgTypes...)>
1796 : public _Maybe_unary_or_binary_function<_Res, _ArgTypes...>,
1797 private _Function_base
1799 typedef _Res _Signature_type(_ArgTypes...);
1801 template<typename _Func,
1802 typename _Res2 = typename result_of<_Func&(_ArgTypes...)>::type>
1803 struct _Callable : __check_func_return_type<_Res2, _Res> { };
1805 // Used so the return type convertibility checks aren't done when
1806 // performing overload resolution for copy construction/assignment.
1807 template<typename _Tp>
1808 struct _Callable<function, _Tp> : false_type { };
1810 template<typename _Cond, typename _Tp>
1811 using _Requires = typename enable_if<_Cond::value, _Tp>::type;
1814 typedef _Res result_type;
1816 // [3.7.2.1] construct/copy/destroy
1819 * @brief Default construct creates an empty function call wrapper.
1820 * @post @c !(bool)*this
1823 : _Function_base() { }
1826 * @brief Creates an empty function call wrapper.
1827 * @post @c !(bool)*this
1829 function(nullptr_t) noexcept
1830 : _Function_base() { }
1833 * @brief %Function copy constructor.
1834 * @param __x A %function object with identical call signature.
1835 * @post @c bool(*this) == bool(__x)
1837 * The newly-created %function contains a copy of the target of @a
1838 * __x (if it has one).
1840 function(const function& __x);
1843 * @brief %Function move constructor.
1844 * @param __x A %function object rvalue with identical call signature.
1846 * The newly-created %function contains the target of @a __x
1849 function(function&& __x) : _Function_base()
1855 * @brief Builds a %function that targets a copy of the incoming
1857 * @param __f A %function object that is callable with parameters of
1858 * type @c T1, @c T2, ..., @c TN and returns a value convertible
1861 * The newly-created %function object will target a copy of
1862 * @a __f. If @a __f is @c reference_wrapper<F>, then this function
1863 * object will contain a reference to the function object @c
1864 * __f.get(). If @a __f is a NULL function pointer or NULL
1865 * pointer-to-member, the newly-created object will be empty.
1867 * If @a __f is a non-NULL function pointer or an object of type @c
1868 * reference_wrapper<F>, this function will not throw.
1870 template<typename _Functor,
1871 typename = _Requires<__not_<is_same<_Functor, function>>, void>,
1872 typename = _Requires<_Callable<_Functor>, void>>
1876 * @brief %Function assignment operator.
1877 * @param __x A %function with identical call signature.
1878 * @post @c (bool)*this == (bool)x
1881 * The target of @a __x is copied to @c *this. If @a __x has no
1882 * target, then @c *this will be empty.
1884 * If @a __x targets a function pointer or a reference to a function
1885 * object, then this operation will not throw an %exception.
1888 operator=(const function& __x)
1890 function(__x).swap(*this);
1895 * @brief %Function move-assignment operator.
1896 * @param __x A %function rvalue with identical call signature.
1899 * The target of @a __x is moved to @c *this. If @a __x has no
1900 * target, then @c *this will be empty.
1902 * If @a __x targets a function pointer or a reference to a function
1903 * object, then this operation will not throw an %exception.
1906 operator=(function&& __x)
1908 function(std::move(__x)).swap(*this);
1913 * @brief %Function assignment to zero.
1914 * @post @c !(bool)*this
1917 * The target of @c *this is deallocated, leaving it empty.
1920 operator=(nullptr_t) noexcept
1924 _M_manager(_M_functor, _M_functor, __destroy_functor);
1925 _M_manager = nullptr;
1926 _M_invoker = nullptr;
1932 * @brief %Function assignment to a new target.
1933 * @param __f A %function object that is callable with parameters of
1934 * type @c T1, @c T2, ..., @c TN and returns a value convertible
1938 * This %function object wrapper will target a copy of @a
1939 * __f. If @a __f is @c reference_wrapper<F>, then this function
1940 * object will contain a reference to the function object @c
1941 * __f.get(). If @a __f is a NULL function pointer or NULL
1942 * pointer-to-member, @c this object will be empty.
1944 * If @a __f is a non-NULL function pointer or an object of type @c
1945 * reference_wrapper<F>, this function will not throw.
1947 template<typename _Functor>
1948 _Requires<_Callable<typename decay<_Functor>::type>, function&>
1949 operator=(_Functor&& __f)
1951 function(std::forward<_Functor>(__f)).swap(*this);
1956 template<typename _Functor>
1958 operator=(reference_wrapper<_Functor> __f) noexcept
1960 function(__f).swap(*this);
1964 // [3.7.2.2] function modifiers
1967 * @brief Swap the targets of two %function objects.
1968 * @param __x A %function with identical call signature.
1970 * Swap the targets of @c this function object and @a __f. This
1971 * function will not throw an %exception.
1973 void swap(function& __x)
1975 std::swap(_M_functor, __x._M_functor);
1976 std::swap(_M_manager, __x._M_manager);
1977 std::swap(_M_invoker, __x._M_invoker);
1980 // [3.7.2.3] function capacity
1983 * @brief Determine if the %function wrapper has a target.
1985 * @return @c true when this %function object contains a target,
1986 * or @c false when it is empty.
1988 * This function will not throw an %exception.
1990 explicit operator bool() const noexcept
1991 { return !_M_empty(); }
1993 // [3.7.2.4] function invocation
1996 * @brief Invokes the function targeted by @c *this.
1997 * @returns the result of the target.
1998 * @throws bad_function_call when @c !(bool)*this
2000 * The function call operator invokes the target function object
2001 * stored by @c this.
2003 _Res operator()(_ArgTypes... __args) const;
2006 // [3.7.2.5] function target access
2008 * @brief Determine the type of the target of this function object
2011 * @returns the type identifier of the target function object, or
2012 * @c typeid(void) if @c !(bool)*this.
2014 * This function will not throw an %exception.
2016 const type_info& target_type() const noexcept;
2019 * @brief Access the stored target function object.
2021 * @return Returns a pointer to the stored target function object,
2022 * if @c typeid(Functor).equals(target_type()); otherwise, a NULL
2025 * This function will not throw an %exception.
2027 template<typename _Functor> _Functor* target() noexcept;
2030 template<typename _Functor> const _Functor* target() const noexcept;
2034 using _Invoker_type = _Res (*)(const _Any_data&, _ArgTypes&&...);
2035 _Invoker_type _M_invoker;
2038 // Out-of-line member definitions.
2039 template<typename _Res, typename... _ArgTypes>
2040 function<_Res(_ArgTypes...)>::
2041 function(const function& __x)
2044 if (static_cast<bool>(__x))
2046 __x._M_manager(_M_functor, __x._M_functor, __clone_functor);
2047 _M_invoker = __x._M_invoker;
2048 _M_manager = __x._M_manager;
2052 template<typename _Res, typename... _ArgTypes>
2053 template<typename _Functor, typename, typename>
2054 function<_Res(_ArgTypes...)>::
2055 function(_Functor __f)
2058 typedef _Function_handler<_Signature_type, _Functor> _My_handler;
2060 if (_My_handler::_M_not_empty_function(__f))
2062 _My_handler::_M_init_functor(_M_functor, std::move(__f));
2063 _M_invoker = &_My_handler::_M_invoke;
2064 _M_manager = &_My_handler::_M_manager;
2068 template<typename _Res, typename... _ArgTypes>
2070 function<_Res(_ArgTypes...)>::
2071 operator()(_ArgTypes... __args) const
2074 __throw_bad_function_call();
2075 return _M_invoker(_M_functor, std::forward<_ArgTypes>(__args)...);
2079 template<typename _Res, typename... _ArgTypes>
2081 function<_Res(_ArgTypes...)>::
2082 target_type() const noexcept
2086 _Any_data __typeinfo_result;
2087 _M_manager(__typeinfo_result, _M_functor, __get_type_info);
2088 return *__typeinfo_result._M_access<const type_info*>();
2091 return typeid(void);
2094 template<typename _Res, typename... _ArgTypes>
2095 template<typename _Functor>
2097 function<_Res(_ArgTypes...)>::
2100 if (typeid(_Functor) == target_type() && _M_manager)
2103 if (_M_manager(__ptr, _M_functor, __get_functor_ptr)
2104 && !is_const<_Functor>::value)
2107 return __ptr._M_access<_Functor*>();
2113 template<typename _Res, typename... _ArgTypes>
2114 template<typename _Functor>
2116 function<_Res(_ArgTypes...)>::
2117 target() const noexcept
2119 if (typeid(_Functor) == target_type() && _M_manager)
2122 _M_manager(__ptr, _M_functor, __get_functor_ptr);
2123 return __ptr._M_access<const _Functor*>();
2130 // [20.7.15.2.6] null pointer comparisons
2133 * @brief Compares a polymorphic function object wrapper against 0
2134 * (the NULL pointer).
2135 * @returns @c true if the wrapper has no target, @c false otherwise
2137 * This function will not throw an %exception.
2139 template<typename _Res, typename... _Args>
2141 operator==(const function<_Res(_Args...)>& __f, nullptr_t) noexcept
2142 { return !static_cast<bool>(__f); }
2145 template<typename _Res, typename... _Args>
2147 operator==(nullptr_t, const function<_Res(_Args...)>& __f) noexcept
2148 { return !static_cast<bool>(__f); }
2151 * @brief Compares a polymorphic function object wrapper against 0
2152 * (the NULL pointer).
2153 * @returns @c false if the wrapper has no target, @c true otherwise
2155 * This function will not throw an %exception.
2157 template<typename _Res, typename... _Args>
2159 operator!=(const function<_Res(_Args...)>& __f, nullptr_t) noexcept
2160 { return static_cast<bool>(__f); }
2163 template<typename _Res, typename... _Args>
2165 operator!=(nullptr_t, const function<_Res(_Args...)>& __f) noexcept
2166 { return static_cast<bool>(__f); }
2168 // [20.7.15.2.7] specialized algorithms
2171 * @brief Swap the targets of two polymorphic function object wrappers.
2173 * This function will not throw an %exception.
2175 template<typename _Res, typename... _Args>
2177 swap(function<_Res(_Args...)>& __x, function<_Res(_Args...)>& __y)
2180 _GLIBCXX_END_NAMESPACE_VERSION
2185 #endif // _GLIBCXX_FUNCTIONAL