1 // <functional> -*- C++ -*-
3 // Copyright (C) 2001-2020 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
55 #include <type_traits>
56 #include <bits/functional_hash.h>
57 #include <bits/invoke.h>
58 #include <bits/refwrap.h> // std::reference_wrapper and _Mem_fn_traits
59 #include <bits/std_function.h> // std::function
60 #if __cplusplus > 201402L
61 # include <unordered_map>
65 # include <bits/stl_algo.h>
67 #if __cplusplus > 201703L
68 # include <bits/range_cmp.h>
72 namespace std _GLIBCXX_VISIBILITY(default)
74 _GLIBCXX_BEGIN_NAMESPACE_VERSION
76 #if __cplusplus >= 201703L
77 # define __cpp_lib_invoke 201411L
78 # if __cplusplus > 201703L
79 # define __cpp_lib_constexpr_functional 201907L
82 /// Invoke a callable object.
83 template<typename _Callable, typename... _Args>
84 inline _GLIBCXX20_CONSTEXPR invoke_result_t<_Callable, _Args...>
85 invoke(_Callable&& __fn, _Args&&... __args)
86 noexcept(is_nothrow_invocable_v<_Callable, _Args...>)
88 return std::__invoke(std::forward<_Callable>(__fn),
89 std::forward<_Args>(__args)...);
93 template<typename _MemFunPtr,
94 bool __is_mem_fn = is_member_function_pointer<_MemFunPtr>::value>
96 : public _Mem_fn_traits<_MemFunPtr>::__maybe_type
98 using _Traits = _Mem_fn_traits<_MemFunPtr>;
100 using _Arity = typename _Traits::__arity;
101 using _Varargs = typename _Traits::__vararg;
103 template<typename _Func, typename... _BoundArgs>
104 friend struct _Bind_check_arity;
110 using result_type = typename _Traits::__result_type;
113 _Mem_fn_base(_MemFunPtr __pmf) noexcept : _M_pmf(__pmf) { }
115 template<typename... _Args>
118 operator()(_Args&&... __args) const
120 std::__invoke(_M_pmf, std::forward<_Args>(__args)...)))
121 -> decltype(std::__invoke(_M_pmf, std::forward<_Args>(__args)...))
122 { return std::__invoke(_M_pmf, std::forward<_Args>(__args)...); }
125 // Partial specialization for member object pointers.
126 template<typename _MemObjPtr>
127 class _Mem_fn_base<_MemObjPtr, false>
129 using _Arity = integral_constant<size_t, 0>;
130 using _Varargs = false_type;
132 template<typename _Func, typename... _BoundArgs>
133 friend struct _Bind_check_arity;
139 _Mem_fn_base(_MemObjPtr __pm) noexcept : _M_pm(__pm) { }
141 template<typename _Tp>
144 operator()(_Tp&& __obj) const
145 noexcept(noexcept(std::__invoke(_M_pm, std::forward<_Tp>(__obj))))
146 -> decltype(std::__invoke(_M_pm, std::forward<_Tp>(__obj)))
147 { return std::__invoke(_M_pm, std::forward<_Tp>(__obj)); }
150 template<typename _MemberPointer>
151 struct _Mem_fn; // undefined
153 template<typename _Res, typename _Class>
154 struct _Mem_fn<_Res _Class::*>
155 : _Mem_fn_base<_Res _Class::*>
157 using _Mem_fn_base<_Res _Class::*>::_Mem_fn_base;
160 // _GLIBCXX_RESOLVE_LIB_DEFECTS
161 // 2048. Unnecessary mem_fn overloads
163 * @brief Returns a function object that forwards to the member
167 template<typename _Tp, typename _Class>
169 inline _Mem_fn<_Tp _Class::*>
170 mem_fn(_Tp _Class::* __pm) noexcept
172 return _Mem_fn<_Tp _Class::*>(__pm);
176 * @brief Determines if the given type _Tp is a function object that
177 * should be treated as a subexpression when evaluating calls to
178 * function objects returned by bind().
180 * C++11 [func.bind.isbind].
183 template<typename _Tp>
184 struct is_bind_expression
185 : public false_type { };
188 * @brief Determines if the given type _Tp is a placeholder in a
189 * bind() expression and, if so, which placeholder it is.
191 * C++11 [func.bind.isplace].
194 template<typename _Tp>
195 struct is_placeholder
196 : public integral_constant<int, 0>
199 #if __cplusplus > 201402L
200 template <typename _Tp> inline constexpr bool is_bind_expression_v
201 = is_bind_expression<_Tp>::value;
202 template <typename _Tp> inline constexpr int is_placeholder_v
203 = is_placeholder<_Tp>::value;
206 /** @brief The type of placeholder objects defined by libstdc++.
209 template<int _Num> struct _Placeholder { };
211 /** @namespace std::placeholders
212 * @brief ISO C++ 2011 namespace for std::bind placeholders.
215 namespace placeholders
217 /* Define a large number of placeholders. There is no way to
218 * simplify this with variadic templates, because we're introducing
219 * unique names for each.
221 extern const _Placeholder<1> _1;
222 extern const _Placeholder<2> _2;
223 extern const _Placeholder<3> _3;
224 extern const _Placeholder<4> _4;
225 extern const _Placeholder<5> _5;
226 extern const _Placeholder<6> _6;
227 extern const _Placeholder<7> _7;
228 extern const _Placeholder<8> _8;
229 extern const _Placeholder<9> _9;
230 extern const _Placeholder<10> _10;
231 extern const _Placeholder<11> _11;
232 extern const _Placeholder<12> _12;
233 extern const _Placeholder<13> _13;
234 extern const _Placeholder<14> _14;
235 extern const _Placeholder<15> _15;
236 extern const _Placeholder<16> _16;
237 extern const _Placeholder<17> _17;
238 extern const _Placeholder<18> _18;
239 extern const _Placeholder<19> _19;
240 extern const _Placeholder<20> _20;
241 extern const _Placeholder<21> _21;
242 extern const _Placeholder<22> _22;
243 extern const _Placeholder<23> _23;
244 extern const _Placeholder<24> _24;
245 extern const _Placeholder<25> _25;
246 extern const _Placeholder<26> _26;
247 extern const _Placeholder<27> _27;
248 extern const _Placeholder<28> _28;
249 extern const _Placeholder<29> _29;
253 * Partial specialization of is_placeholder that provides the placeholder
254 * number for the placeholder objects defined by libstdc++.
258 struct is_placeholder<_Placeholder<_Num> >
259 : public integral_constant<int, _Num>
263 struct is_placeholder<const _Placeholder<_Num> >
264 : public integral_constant<int, _Num>
268 // Like tuple_element_t but SFINAE-friendly.
269 template<std::size_t __i, typename _Tuple>
270 using _Safe_tuple_element_t
271 = typename enable_if<(__i < tuple_size<_Tuple>::value),
272 tuple_element<__i, _Tuple>>::type::type;
275 * Maps an argument to bind() into an actual argument to the bound
276 * function object [func.bind.bind]/10. Only the first parameter should
277 * be specified: the rest are used to determine among the various
278 * implementations. Note that, although this class is a function
279 * object, it isn't entirely normal because it takes only two
280 * parameters regardless of the number of parameters passed to the
281 * bind expression. The first parameter is the bound argument and
282 * the second parameter is a tuple containing references to the
283 * rest of the arguments.
285 template<typename _Arg,
286 bool _IsBindExp = is_bind_expression<_Arg>::value,
287 bool _IsPlaceholder = (is_placeholder<_Arg>::value > 0)>
291 * If the argument is reference_wrapper<_Tp>, returns the
292 * underlying reference.
293 * C++11 [func.bind.bind] p10 bullet 1.
295 template<typename _Tp>
296 class _Mu<reference_wrapper<_Tp>, false, false>
299 /* Note: This won't actually work for const volatile
300 * reference_wrappers, because reference_wrapper::get() is const
301 * but not volatile-qualified. This might be a defect in the TR.
303 template<typename _CVRef, typename _Tuple>
306 operator()(_CVRef& __arg, _Tuple&) const volatile
307 { return __arg.get(); }
311 * If the argument is a bind expression, we invoke the underlying
312 * function object with the same cv-qualifiers as we are given and
313 * pass along all of our arguments (unwrapped).
314 * C++11 [func.bind.bind] p10 bullet 2.
316 template<typename _Arg>
317 class _Mu<_Arg, true, false>
320 template<typename _CVArg, typename... _Args>
323 operator()(_CVArg& __arg,
324 tuple<_Args...>& __tuple) const volatile
325 -> decltype(__arg(declval<_Args>()...))
327 // Construct an index tuple and forward to __call
328 typedef typename _Build_index_tuple<sizeof...(_Args)>::__type
330 return this->__call(__arg, __tuple, _Indexes());
334 // Invokes the underlying function object __arg by unpacking all
335 // of the arguments in the tuple.
336 template<typename _CVArg, typename... _Args, std::size_t... _Indexes>
339 __call(_CVArg& __arg, tuple<_Args...>& __tuple,
340 const _Index_tuple<_Indexes...>&) const volatile
341 -> decltype(__arg(declval<_Args>()...))
343 return __arg(std::get<_Indexes>(std::move(__tuple))...);
348 * If the argument is a placeholder for the Nth argument, returns
349 * a reference to the Nth argument to the bind function object.
350 * C++11 [func.bind.bind] p10 bullet 3.
352 template<typename _Arg>
353 class _Mu<_Arg, false, true>
356 template<typename _Tuple>
358 _Safe_tuple_element_t<(is_placeholder<_Arg>::value - 1), _Tuple>&&
359 operator()(const volatile _Arg&, _Tuple& __tuple) const volatile
362 ::std::get<(is_placeholder<_Arg>::value - 1)>(std::move(__tuple));
367 * If the argument is just a value, returns a reference to that
368 * value. The cv-qualifiers on the reference are determined by the caller.
369 * C++11 [func.bind.bind] p10 bullet 4.
371 template<typename _Arg>
372 class _Mu<_Arg, false, false>
375 template<typename _CVArg, typename _Tuple>
378 operator()(_CVArg&& __arg, _Tuple&) const volatile
379 { return std::forward<_CVArg>(__arg); }
382 // std::get<I> for volatile-qualified tuples
383 template<std::size_t _Ind, typename... _Tp>
385 __volget(volatile tuple<_Tp...>& __tuple)
386 -> __tuple_element_t<_Ind, tuple<_Tp...>> volatile&
387 { return std::get<_Ind>(const_cast<tuple<_Tp...>&>(__tuple)); }
389 // std::get<I> for const-volatile-qualified tuples
390 template<std::size_t _Ind, typename... _Tp>
392 __volget(const volatile tuple<_Tp...>& __tuple)
393 -> __tuple_element_t<_Ind, tuple<_Tp...>> const volatile&
394 { return std::get<_Ind>(const_cast<const tuple<_Tp...>&>(__tuple)); }
396 /// Type of the function object returned from bind().
397 template<typename _Signature>
400 template<typename _Functor, typename... _Bound_args>
401 class _Bind<_Functor(_Bound_args...)>
402 : public _Weak_result_type<_Functor>
404 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
408 tuple<_Bound_args...> _M_bound_args;
411 template<typename _Result, typename... _Args, std::size_t... _Indexes>
414 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>)
416 return std::__invoke(_M_f,
417 _Mu<_Bound_args>()(std::get<_Indexes>(_M_bound_args), __args)...
422 template<typename _Result, typename... _Args, std::size_t... _Indexes>
425 __call_c(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>) const
427 return std::__invoke(_M_f,
428 _Mu<_Bound_args>()(std::get<_Indexes>(_M_bound_args), __args)...
433 template<typename _Result, typename... _Args, std::size_t... _Indexes>
435 __call_v(tuple<_Args...>&& __args,
436 _Index_tuple<_Indexes...>) volatile
438 return std::__invoke(_M_f,
439 _Mu<_Bound_args>()(__volget<_Indexes>(_M_bound_args), __args)...
443 // Call as const volatile
444 template<typename _Result, typename... _Args, std::size_t... _Indexes>
446 __call_c_v(tuple<_Args...>&& __args,
447 _Index_tuple<_Indexes...>) const volatile
449 return std::__invoke(_M_f,
450 _Mu<_Bound_args>()(__volget<_Indexes>(_M_bound_args), __args)...
454 template<typename _BoundArg, typename _CallArgs>
455 using _Mu_type = decltype(
456 _Mu<typename remove_cv<_BoundArg>::type>()(
457 std::declval<_BoundArg&>(), std::declval<_CallArgs&>()) );
459 template<typename _Fn, typename _CallArgs, typename... _BArgs>
461 = typename result_of< _Fn&(_Mu_type<_BArgs, _CallArgs>&&...) >::type;
463 template<typename _CallArgs>
464 using _Res_type = _Res_type_impl<_Functor, _CallArgs, _Bound_args...>;
466 template<typename _CallArgs>
467 using __dependent = typename
468 enable_if<bool(tuple_size<_CallArgs>::value+1), _Functor>::type;
470 template<typename _CallArgs, template<class> class __cv_quals>
471 using _Res_type_cv = _Res_type_impl<
472 typename __cv_quals<__dependent<_CallArgs>>::type,
474 typename __cv_quals<_Bound_args>::type...>;
477 template<typename... _Args>
478 explicit _GLIBCXX20_CONSTEXPR
479 _Bind(const _Functor& __f, _Args&&... __args)
480 : _M_f(__f), _M_bound_args(std::forward<_Args>(__args)...)
483 template<typename... _Args>
484 explicit _GLIBCXX20_CONSTEXPR
485 _Bind(_Functor&& __f, _Args&&... __args)
486 : _M_f(std::move(__f)), _M_bound_args(std::forward<_Args>(__args)...)
489 _Bind(const _Bind&) = default;
490 _Bind(_Bind&&) = default;
493 template<typename... _Args,
494 typename _Result = _Res_type<tuple<_Args...>>>
497 operator()(_Args&&... __args)
499 return this->__call<_Result>(
500 std::forward_as_tuple(std::forward<_Args>(__args)...),
505 template<typename... _Args,
506 typename _Result = _Res_type_cv<tuple<_Args...>, add_const>>
509 operator()(_Args&&... __args) const
511 return this->__call_c<_Result>(
512 std::forward_as_tuple(std::forward<_Args>(__args)...),
516 #if __cplusplus > 201402L
517 # define _GLIBCXX_DEPR_BIND \
518 [[deprecated("std::bind does not support volatile in C++17")]]
520 # define _GLIBCXX_DEPR_BIND
523 template<typename... _Args,
524 typename _Result = _Res_type_cv<tuple<_Args...>, add_volatile>>
527 operator()(_Args&&... __args) volatile
529 return this->__call_v<_Result>(
530 std::forward_as_tuple(std::forward<_Args>(__args)...),
534 // Call as const volatile
535 template<typename... _Args,
536 typename _Result = _Res_type_cv<tuple<_Args...>, add_cv>>
539 operator()(_Args&&... __args) const volatile
541 return this->__call_c_v<_Result>(
542 std::forward_as_tuple(std::forward<_Args>(__args)...),
547 /// Type of the function object returned from bind<R>().
548 template<typename _Result, typename _Signature>
551 template<typename _Result, typename _Functor, typename... _Bound_args>
552 class _Bind_result<_Result, _Functor(_Bound_args...)>
554 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
558 tuple<_Bound_args...> _M_bound_args;
561 template<typename _Res, typename... _Args, std::size_t... _Indexes>
564 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>)
566 return std::__invoke_r<_Res>(_M_f, _Mu<_Bound_args>()
567 (std::get<_Indexes>(_M_bound_args), __args)...);
571 template<typename _Res, typename... _Args, std::size_t... _Indexes>
574 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>) const
576 return std::__invoke_r<_Res>(_M_f, _Mu<_Bound_args>()
577 (std::get<_Indexes>(_M_bound_args), __args)...);
581 template<typename _Res, typename... _Args, std::size_t... _Indexes>
584 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>) volatile
586 return std::__invoke_r<_Res>(_M_f, _Mu<_Bound_args>()
587 (__volget<_Indexes>(_M_bound_args), __args)...);
590 // Call as const volatile
591 template<typename _Res, typename... _Args, std::size_t... _Indexes>
594 __call(tuple<_Args...>&& __args,
595 _Index_tuple<_Indexes...>) const volatile
597 return std::__invoke_r<_Res>(_M_f, _Mu<_Bound_args>()
598 (__volget<_Indexes>(_M_bound_args), __args)...);
602 typedef _Result result_type;
604 template<typename... _Args>
605 explicit _GLIBCXX20_CONSTEXPR
606 _Bind_result(const _Functor& __f, _Args&&... __args)
607 : _M_f(__f), _M_bound_args(std::forward<_Args>(__args)...)
610 template<typename... _Args>
611 explicit _GLIBCXX20_CONSTEXPR
612 _Bind_result(_Functor&& __f, _Args&&... __args)
613 : _M_f(std::move(__f)), _M_bound_args(std::forward<_Args>(__args)...)
616 _Bind_result(const _Bind_result&) = default;
617 _Bind_result(_Bind_result&&) = default;
620 template<typename... _Args>
623 operator()(_Args&&... __args)
625 return this->__call<_Result>(
626 std::forward_as_tuple(std::forward<_Args>(__args)...),
631 template<typename... _Args>
634 operator()(_Args&&... __args) const
636 return this->__call<_Result>(
637 std::forward_as_tuple(std::forward<_Args>(__args)...),
642 template<typename... _Args>
645 operator()(_Args&&... __args) volatile
647 return this->__call<_Result>(
648 std::forward_as_tuple(std::forward<_Args>(__args)...),
652 // Call as const volatile
653 template<typename... _Args>
656 operator()(_Args&&... __args) const volatile
658 return this->__call<_Result>(
659 std::forward_as_tuple(std::forward<_Args>(__args)...),
663 #undef _GLIBCXX_DEPR_BIND
666 * @brief Class template _Bind is always a bind expression.
669 template<typename _Signature>
670 struct is_bind_expression<_Bind<_Signature> >
671 : public true_type { };
674 * @brief Class template _Bind is always a bind expression.
677 template<typename _Signature>
678 struct is_bind_expression<const _Bind<_Signature> >
679 : public true_type { };
682 * @brief Class template _Bind is always a bind expression.
685 template<typename _Signature>
686 struct is_bind_expression<volatile _Bind<_Signature> >
687 : public true_type { };
690 * @brief Class template _Bind is always a bind expression.
693 template<typename _Signature>
694 struct is_bind_expression<const volatile _Bind<_Signature>>
695 : public true_type { };
698 * @brief Class template _Bind_result is always a bind expression.
701 template<typename _Result, typename _Signature>
702 struct is_bind_expression<_Bind_result<_Result, _Signature>>
703 : public true_type { };
706 * @brief Class template _Bind_result is always a bind expression.
709 template<typename _Result, typename _Signature>
710 struct is_bind_expression<const _Bind_result<_Result, _Signature>>
711 : public true_type { };
714 * @brief Class template _Bind_result is always a bind expression.
717 template<typename _Result, typename _Signature>
718 struct is_bind_expression<volatile _Bind_result<_Result, _Signature>>
719 : public true_type { };
722 * @brief Class template _Bind_result is always a bind expression.
725 template<typename _Result, typename _Signature>
726 struct is_bind_expression<const volatile _Bind_result<_Result, _Signature>>
727 : public true_type { };
729 template<typename _Func, typename... _BoundArgs>
730 struct _Bind_check_arity { };
732 template<typename _Ret, typename... _Args, typename... _BoundArgs>
733 struct _Bind_check_arity<_Ret (*)(_Args...), _BoundArgs...>
735 static_assert(sizeof...(_BoundArgs) == sizeof...(_Args),
736 "Wrong number of arguments for function");
739 template<typename _Ret, typename... _Args, typename... _BoundArgs>
740 struct _Bind_check_arity<_Ret (*)(_Args......), _BoundArgs...>
742 static_assert(sizeof...(_BoundArgs) >= sizeof...(_Args),
743 "Wrong number of arguments for function");
746 template<typename _Tp, typename _Class, typename... _BoundArgs>
747 struct _Bind_check_arity<_Tp _Class::*, _BoundArgs...>
749 using _Arity = typename _Mem_fn<_Tp _Class::*>::_Arity;
750 using _Varargs = typename _Mem_fn<_Tp _Class::*>::_Varargs;
751 static_assert(_Varargs::value
752 ? sizeof...(_BoundArgs) >= _Arity::value + 1
753 : sizeof...(_BoundArgs) == _Arity::value + 1,
754 "Wrong number of arguments for pointer-to-member");
757 // Trait type used to remove std::bind() from overload set via SFINAE
758 // when first argument has integer type, so that std::bind() will
759 // not be a better match than ::bind() from the BSD Sockets API.
760 template<typename _Tp, typename _Tp2 = typename decay<_Tp>::type>
761 using __is_socketlike = __or_<is_integral<_Tp2>, is_enum<_Tp2>>;
763 template<bool _SocketLike, typename _Func, typename... _BoundArgs>
765 : _Bind_check_arity<typename decay<_Func>::type, _BoundArgs...>
767 typedef typename decay<_Func>::type __func_type;
768 typedef _Bind<__func_type(typename decay<_BoundArgs>::type...)> type;
771 // Partial specialization for is_socketlike == true, does not define
772 // nested type so std::bind() will not participate in overload resolution
773 // when the first argument might be a socket file descriptor.
774 template<typename _Func, typename... _BoundArgs>
775 struct _Bind_helper<true, _Func, _BoundArgs...>
779 * @brief Function template for std::bind.
782 template<typename _Func, typename... _BoundArgs>
783 inline _GLIBCXX20_CONSTEXPR typename
784 _Bind_helper<__is_socketlike<_Func>::value, _Func, _BoundArgs...>::type
785 bind(_Func&& __f, _BoundArgs&&... __args)
787 typedef _Bind_helper<false, _Func, _BoundArgs...> __helper_type;
788 return typename __helper_type::type(std::forward<_Func>(__f),
789 std::forward<_BoundArgs>(__args)...);
792 template<typename _Result, typename _Func, typename... _BoundArgs>
793 struct _Bindres_helper
794 : _Bind_check_arity<typename decay<_Func>::type, _BoundArgs...>
796 typedef typename decay<_Func>::type __functor_type;
797 typedef _Bind_result<_Result,
798 __functor_type(typename decay<_BoundArgs>::type...)>
803 * @brief Function template for std::bind<R>.
806 template<typename _Result, typename _Func, typename... _BoundArgs>
807 inline _GLIBCXX20_CONSTEXPR
808 typename _Bindres_helper<_Result, _Func, _BoundArgs...>::type
809 bind(_Func&& __f, _BoundArgs&&... __args)
811 typedef _Bindres_helper<_Result, _Func, _BoundArgs...> __helper_type;
812 return typename __helper_type::type(std::forward<_Func>(__f),
813 std::forward<_BoundArgs>(__args)...);
816 #if __cplusplus > 201703L
817 #define __cpp_lib_bind_front 201907L
819 template<typename _Fd, typename... _BoundArgs>
822 static_assert(is_move_constructible_v<_Fd>);
823 static_assert((is_move_constructible_v<_BoundArgs> && ...));
825 // First parameter is to ensure this constructor is never used
826 // instead of the copy/move constructor.
827 template<typename _Fn, typename... _Args>
829 _Bind_front(int, _Fn&& __fn, _Args&&... __args)
830 noexcept(__and_<is_nothrow_constructible<_Fd, _Fn>,
831 is_nothrow_constructible<_BoundArgs, _Args>...>::value)
832 : _M_fd(std::forward<_Fn>(__fn)),
833 _M_bound_args(std::forward<_Args>(__args)...)
834 { static_assert(sizeof...(_Args) == sizeof...(_BoundArgs)); }
836 _Bind_front(const _Bind_front&) = default;
837 _Bind_front(_Bind_front&&) = default;
838 _Bind_front& operator=(const _Bind_front&) = default;
839 _Bind_front& operator=(_Bind_front&&) = default;
840 ~_Bind_front() = default;
842 template<typename... _CallArgs>
844 invoke_result_t<_Fd&, _BoundArgs&..., _CallArgs...>
845 operator()(_CallArgs&&... __call_args) &
846 noexcept(is_nothrow_invocable_v<_Fd&, _BoundArgs&..., _CallArgs...>)
848 return _S_call(*this, _BoundIndices(),
849 std::forward<_CallArgs>(__call_args)...);
852 template<typename... _CallArgs>
854 invoke_result_t<const _Fd&, const _BoundArgs&..., _CallArgs...>
855 operator()(_CallArgs&&... __call_args) const &
856 noexcept(is_nothrow_invocable_v<const _Fd&, const _BoundArgs&...,
859 return _S_call(*this, _BoundIndices(),
860 std::forward<_CallArgs>(__call_args)...);
863 template<typename... _CallArgs>
865 invoke_result_t<_Fd, _BoundArgs..., _CallArgs...>
866 operator()(_CallArgs&&... __call_args) &&
867 noexcept(is_nothrow_invocable_v<_Fd, _BoundArgs..., _CallArgs...>)
869 return _S_call(std::move(*this), _BoundIndices(),
870 std::forward<_CallArgs>(__call_args)...);
873 template<typename... _CallArgs>
875 invoke_result_t<const _Fd, const _BoundArgs..., _CallArgs...>
876 operator()(_CallArgs&&... __call_args) const &&
877 noexcept(is_nothrow_invocable_v<const _Fd, const _BoundArgs...,
880 return _S_call(std::move(*this), _BoundIndices(),
881 std::forward<_CallArgs>(__call_args)...);
885 using _BoundIndices = index_sequence_for<_BoundArgs...>;
887 template<typename _Tp, size_t... _Ind, typename... _CallArgs>
890 _S_call(_Tp&& __g, index_sequence<_Ind...>, _CallArgs&&... __call_args)
892 return std::invoke(std::forward<_Tp>(__g)._M_fd,
893 std::get<_Ind>(std::forward<_Tp>(__g)._M_bound_args)...,
894 std::forward<_CallArgs>(__call_args)...);
898 std::tuple<_BoundArgs...> _M_bound_args;
901 template<typename _Fn, typename... _Args>
903 = _Bind_front<decay_t<_Fn>, decay_t<_Args>...>;
905 template<typename _Fn, typename... _Args>
906 constexpr _Bind_front_t<_Fn, _Args...>
907 bind_front(_Fn&& __fn, _Args&&... __args)
908 noexcept(is_nothrow_constructible_v<_Bind_front_t<_Fn, _Args...>,
911 return _Bind_front_t<_Fn, _Args...>(0, std::forward<_Fn>(__fn),
912 std::forward<_Args>(__args)...);
916 #if __cplusplus >= 201402L
917 /// Generalized negator.
918 template<typename _Fn>
921 template<typename _Fn2, typename... _Args>
922 using __inv_res_t = typename __invoke_result<_Fn2, _Args...>::type;
924 template<typename _Tp>
925 static decltype(!std::declval<_Tp>())
926 _S_not() noexcept(noexcept(!std::declval<_Tp>()));
929 template<typename _Fn2>
931 _Not_fn(_Fn2&& __fn, int)
932 : _M_fn(std::forward<_Fn2>(__fn)) { }
934 _Not_fn(const _Not_fn& __fn) = default;
935 _Not_fn(_Not_fn&& __fn) = default;
936 ~_Not_fn() = default;
938 // Macro to define operator() with given cv-qualifiers ref-qualifiers,
939 // forwarding _M_fn and the function arguments with the same qualifiers,
940 // and deducing the return type and exception-specification.
941 #define _GLIBCXX_NOT_FN_CALL_OP( _QUALS ) \
942 template<typename... _Args> \
943 _GLIBCXX20_CONSTEXPR \
944 decltype(_S_not<__inv_res_t<_Fn _QUALS, _Args...>>()) \
945 operator()(_Args&&... __args) _QUALS \
946 noexcept(__is_nothrow_invocable<_Fn _QUALS, _Args...>::value \
947 && noexcept(_S_not<__inv_res_t<_Fn _QUALS, _Args...>>())) \
949 return !std::__invoke(std::forward< _Fn _QUALS >(_M_fn), \
950 std::forward<_Args>(__args)...); \
952 _GLIBCXX_NOT_FN_CALL_OP( & )
953 _GLIBCXX_NOT_FN_CALL_OP( const & )
954 _GLIBCXX_NOT_FN_CALL_OP( && )
955 _GLIBCXX_NOT_FN_CALL_OP( const && )
956 #undef _GLIBCXX_NOT_FN_CALL_OP
962 template<typename _Tp, typename _Pred>
963 struct __is_byte_like : false_type { };
965 template<typename _Tp>
966 struct __is_byte_like<_Tp, equal_to<_Tp>>
967 : __bool_constant<sizeof(_Tp) == 1 && is_integral<_Tp>::value> { };
969 template<typename _Tp>
970 struct __is_byte_like<_Tp, equal_to<void>>
971 : __bool_constant<sizeof(_Tp) == 1 && is_integral<_Tp>::value> { };
973 #if __cplusplus >= 201703L
974 // Declare std::byte (full definition is in <cstddef>).
975 enum class byte : unsigned char;
978 struct __is_byte_like<byte, equal_to<byte>>
982 struct __is_byte_like<byte, equal_to<void>>
985 #define __cpp_lib_not_fn 201603
986 /// [func.not_fn] Function template not_fn
987 template<typename _Fn>
991 noexcept(std::is_nothrow_constructible<std::decay_t<_Fn>, _Fn&&>::value)
993 return _Not_fn<std::decay_t<_Fn>>{std::forward<_Fn>(__fn), 0};
997 #define __cpp_lib_boyer_moore_searcher 201603
999 template<typename _ForwardIterator1, typename _BinaryPredicate = equal_to<>>
1000 class default_searcher
1003 _GLIBCXX20_CONSTEXPR
1004 default_searcher(_ForwardIterator1 __pat_first,
1005 _ForwardIterator1 __pat_last,
1006 _BinaryPredicate __pred = _BinaryPredicate())
1007 : _M_m(__pat_first, __pat_last, std::move(__pred))
1010 template<typename _ForwardIterator2>
1011 _GLIBCXX20_CONSTEXPR
1012 pair<_ForwardIterator2, _ForwardIterator2>
1013 operator()(_ForwardIterator2 __first, _ForwardIterator2 __last) const
1015 _ForwardIterator2 __first_ret =
1016 std::search(__first, __last, std::get<0>(_M_m), std::get<1>(_M_m),
1018 auto __ret = std::make_pair(__first_ret, __first_ret);
1019 if (__ret.first != __last)
1020 std::advance(__ret.second, std::distance(std::get<0>(_M_m),
1021 std::get<1>(_M_m)));
1026 tuple<_ForwardIterator1, _ForwardIterator1, _BinaryPredicate> _M_m;
1029 template<typename _Key, typename _Tp, typename _Hash, typename _Pred>
1030 struct __boyer_moore_map_base
1032 template<typename _RAIter>
1033 __boyer_moore_map_base(_RAIter __pat, size_t __patlen,
1034 _Hash&& __hf, _Pred&& __pred)
1035 : _M_bad_char{ __patlen, std::move(__hf), std::move(__pred) }
1038 for (__diff_type __i = 0; __i < __patlen - 1; ++__i)
1039 _M_bad_char[__pat[__i]] = __patlen - 1 - __i;
1042 using __diff_type = _Tp;
1045 _M_lookup(_Key __key, __diff_type __not_found) const
1047 auto __iter = _M_bad_char.find(__key);
1048 if (__iter == _M_bad_char.end())
1050 return __iter->second;
1054 _M_pred() const { return _M_bad_char.key_eq(); }
1056 _GLIBCXX_STD_C::unordered_map<_Key, _Tp, _Hash, _Pred> _M_bad_char;
1059 template<typename _Tp, size_t _Len, typename _Pred>
1060 struct __boyer_moore_array_base
1062 template<typename _RAIter, typename _Unused>
1063 __boyer_moore_array_base(_RAIter __pat, size_t __patlen,
1064 _Unused&&, _Pred&& __pred)
1065 : _M_bad_char{ _GLIBCXX_STD_C::array<_Tp, _Len>{}, std::move(__pred) }
1067 std::get<0>(_M_bad_char).fill(__patlen);
1069 for (__diff_type __i = 0; __i < __patlen - 1; ++__i)
1071 auto __ch = __pat[__i];
1072 using _UCh = make_unsigned_t<decltype(__ch)>;
1073 auto __uch = static_cast<_UCh>(__ch);
1074 std::get<0>(_M_bad_char)[__uch] = __patlen - 1 - __i;
1078 using __diff_type = _Tp;
1080 template<typename _Key>
1082 _M_lookup(_Key __key, __diff_type __not_found) const
1084 auto __ukey = static_cast<make_unsigned_t<_Key>>(__key);
1087 return std::get<0>(_M_bad_char)[__ukey];
1091 _M_pred() const { return std::get<1>(_M_bad_char); }
1093 tuple<_GLIBCXX_STD_C::array<_Tp, _Len>, _Pred> _M_bad_char;
1096 // Use __boyer_moore_array_base when pattern consists of narrow characters
1097 // (or std::byte) and uses std::equal_to as the predicate.
1098 template<typename _RAIter, typename _Hash, typename _Pred,
1099 typename _Val = typename iterator_traits<_RAIter>::value_type,
1100 typename _Diff = typename iterator_traits<_RAIter>::difference_type>
1101 using __boyer_moore_base_t
1102 = conditional_t<__is_byte_like<_Val, _Pred>::value,
1103 __boyer_moore_array_base<_Diff, 256, _Pred>,
1104 __boyer_moore_map_base<_Val, _Diff, _Hash, _Pred>>;
1106 template<typename _RAIter, typename _Hash
1107 = hash<typename iterator_traits<_RAIter>::value_type>,
1108 typename _BinaryPredicate = equal_to<>>
1109 class boyer_moore_searcher
1110 : __boyer_moore_base_t<_RAIter, _Hash, _BinaryPredicate>
1112 using _Base = __boyer_moore_base_t<_RAIter, _Hash, _BinaryPredicate>;
1113 using typename _Base::__diff_type;
1116 boyer_moore_searcher(_RAIter __pat_first, _RAIter __pat_last,
1117 _Hash __hf = _Hash(),
1118 _BinaryPredicate __pred = _BinaryPredicate());
1120 template<typename _RandomAccessIterator2>
1121 pair<_RandomAccessIterator2, _RandomAccessIterator2>
1122 operator()(_RandomAccessIterator2 __first,
1123 _RandomAccessIterator2 __last) const;
1127 _M_is_prefix(_RAIter __word, __diff_type __len,
1130 const auto& __pred = this->_M_pred();
1131 __diff_type __suffixlen = __len - __pos;
1132 for (__diff_type __i = 0; __i < __suffixlen; ++__i)
1133 if (!__pred(__word[__i], __word[__pos + __i]))
1139 _M_suffix_length(_RAIter __word, __diff_type __len,
1142 const auto& __pred = this->_M_pred();
1143 __diff_type __i = 0;
1144 while (__pred(__word[__pos - __i], __word[__len - 1 - __i])
1152 template<typename _Tp>
1154 _M_bad_char_shift(_Tp __c) const
1155 { return this->_M_lookup(__c, _M_pat_end - _M_pat); }
1159 _GLIBCXX_STD_C::vector<__diff_type> _M_good_suffix;
1162 template<typename _RAIter, typename _Hash
1163 = hash<typename iterator_traits<_RAIter>::value_type>,
1164 typename _BinaryPredicate = equal_to<>>
1165 class boyer_moore_horspool_searcher
1166 : __boyer_moore_base_t<_RAIter, _Hash, _BinaryPredicate>
1168 using _Base = __boyer_moore_base_t<_RAIter, _Hash, _BinaryPredicate>;
1169 using typename _Base::__diff_type;
1172 boyer_moore_horspool_searcher(_RAIter __pat,
1174 _Hash __hf = _Hash(),
1175 _BinaryPredicate __pred
1176 = _BinaryPredicate())
1177 : _Base(__pat, __pat_end - __pat, std::move(__hf), std::move(__pred)),
1178 _M_pat(__pat), _M_pat_end(__pat_end)
1181 template<typename _RandomAccessIterator2>
1182 pair<_RandomAccessIterator2, _RandomAccessIterator2>
1183 operator()(_RandomAccessIterator2 __first,
1184 _RandomAccessIterator2 __last) const
1186 const auto& __pred = this->_M_pred();
1187 auto __patlen = _M_pat_end - _M_pat;
1189 return std::make_pair(__first, __first);
1190 auto __len = __last - __first;
1191 while (__len >= __patlen)
1193 for (auto __scan = __patlen - 1;
1194 __pred(__first[__scan], _M_pat[__scan]); --__scan)
1196 return std::make_pair(__first, __first + __patlen);
1197 auto __shift = _M_bad_char_shift(__first[__patlen - 1]);
1201 return std::make_pair(__last, __last);
1205 template<typename _Tp>
1207 _M_bad_char_shift(_Tp __c) const
1208 { return this->_M_lookup(__c, _M_pat_end - _M_pat); }
1214 template<typename _RAIter, typename _Hash, typename _BinaryPredicate>
1215 boyer_moore_searcher<_RAIter, _Hash, _BinaryPredicate>::
1216 boyer_moore_searcher(_RAIter __pat, _RAIter __pat_end,
1217 _Hash __hf, _BinaryPredicate __pred)
1218 : _Base(__pat, __pat_end - __pat, std::move(__hf), std::move(__pred)),
1219 _M_pat(__pat), _M_pat_end(__pat_end), _M_good_suffix(__pat_end - __pat)
1221 auto __patlen = __pat_end - __pat;
1224 __diff_type __last_prefix = __patlen - 1;
1225 for (__diff_type __p = __patlen - 1; __p >= 0; --__p)
1227 if (_M_is_prefix(__pat, __patlen, __p + 1))
1228 __last_prefix = __p + 1;
1229 _M_good_suffix[__p] = __last_prefix + (__patlen - 1 - __p);
1231 for (__diff_type __p = 0; __p < __patlen - 1; ++__p)
1233 auto __slen = _M_suffix_length(__pat, __patlen, __p);
1234 auto __pos = __patlen - 1 - __slen;
1235 if (!__pred(__pat[__p - __slen], __pat[__pos]))
1236 _M_good_suffix[__pos] = __patlen - 1 - __p + __slen;
1240 template<typename _RAIter, typename _Hash, typename _BinaryPredicate>
1241 template<typename _RandomAccessIterator2>
1242 pair<_RandomAccessIterator2, _RandomAccessIterator2>
1243 boyer_moore_searcher<_RAIter, _Hash, _BinaryPredicate>::
1244 operator()(_RandomAccessIterator2 __first,
1245 _RandomAccessIterator2 __last) const
1247 auto __patlen = _M_pat_end - _M_pat;
1249 return std::make_pair(__first, __first);
1250 const auto& __pred = this->_M_pred();
1251 __diff_type __i = __patlen - 1;
1252 auto __stringlen = __last - __first;
1253 while (__i < __stringlen)
1255 __diff_type __j = __patlen - 1;
1256 while (__j >= 0 && __pred(__first[__i], _M_pat[__j]))
1263 const auto __match = __first + __i + 1;
1264 return std::make_pair(__match, __match + __patlen);
1266 __i += std::max(_M_bad_char_shift(__first[__i]),
1267 _M_good_suffix[__j]);
1269 return std::make_pair(__last, __last);
1275 _GLIBCXX_END_NAMESPACE_VERSION
1280 #endif // _GLIBCXX_FUNCTIONAL