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>
59 #include <bits/invoke.h>
61 #if __cplusplus > 201402L
62 #include <unordered_map>
66 #include <bits/stl_algo.h>
69 namespace std _GLIBCXX_VISIBILITY(default)
71 _GLIBCXX_BEGIN_NAMESPACE_VERSION
73 template<typename _MemberPointer>
75 template<typename _Tp, typename _Class>
76 _Mem_fn<_Tp _Class::*>
77 mem_fn(_Tp _Class::*) noexcept;
79 /// If we have found a result_type, extract it.
80 template<typename _Functor, typename = __void_t<>>
81 struct _Maybe_get_result_type
84 template<typename _Functor>
85 struct _Maybe_get_result_type<_Functor,
86 __void_t<typename _Functor::result_type>>
87 { typedef typename _Functor::result_type result_type; };
90 * Base class for any function object that has a weak result type, as
91 * defined in 20.8.2 [func.require] of C++11.
93 template<typename _Functor>
94 struct _Weak_result_type_impl
95 : _Maybe_get_result_type<_Functor>
98 /// Retrieve the result type for a function type.
99 template<typename _Res, typename... _ArgTypes>
100 struct _Weak_result_type_impl<_Res(_ArgTypes...)>
101 { typedef _Res result_type; };
103 template<typename _Res, typename... _ArgTypes>
104 struct _Weak_result_type_impl<_Res(_ArgTypes......)>
105 { typedef _Res result_type; };
107 template<typename _Res, typename... _ArgTypes>
108 struct _Weak_result_type_impl<_Res(_ArgTypes...) const>
109 { typedef _Res result_type; };
111 template<typename _Res, typename... _ArgTypes>
112 struct _Weak_result_type_impl<_Res(_ArgTypes......) const>
113 { typedef _Res result_type; };
115 template<typename _Res, typename... _ArgTypes>
116 struct _Weak_result_type_impl<_Res(_ArgTypes...) volatile>
117 { typedef _Res result_type; };
119 template<typename _Res, typename... _ArgTypes>
120 struct _Weak_result_type_impl<_Res(_ArgTypes......) volatile>
121 { typedef _Res result_type; };
123 template<typename _Res, typename... _ArgTypes>
124 struct _Weak_result_type_impl<_Res(_ArgTypes...) const volatile>
125 { typedef _Res result_type; };
127 template<typename _Res, typename... _ArgTypes>
128 struct _Weak_result_type_impl<_Res(_ArgTypes......) const volatile>
129 { typedef _Res result_type; };
131 /// Retrieve the result type for a function reference.
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 the result type for a function pointer.
141 template<typename _Res, typename... _ArgTypes>
142 struct _Weak_result_type_impl<_Res(*)(_ArgTypes...)>
143 { typedef _Res result_type; };
145 template<typename _Res, typename... _ArgTypes>
146 struct _Weak_result_type_impl<_Res(*)(_ArgTypes......)>
147 { typedef _Res result_type; };
149 /// Retrieve result type for a member function pointer.
150 template<typename _Res, typename _Class, typename... _ArgTypes>
151 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...)>
152 { typedef _Res result_type; };
154 template<typename _Res, typename _Class, typename... _ArgTypes>
155 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes......)>
156 { typedef _Res result_type; };
158 /// Retrieve result type for a const member function pointer.
159 template<typename _Res, typename _Class, typename... _ArgTypes>
160 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) const>
161 { typedef _Res result_type; };
163 template<typename _Res, typename _Class, typename... _ArgTypes>
164 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes......) const>
165 { typedef _Res result_type; };
167 /// Retrieve result type for a volatile member function pointer.
168 template<typename _Res, typename _Class, typename... _ArgTypes>
169 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) volatile>
170 { typedef _Res result_type; };
172 template<typename _Res, typename _Class, typename... _ArgTypes>
173 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes......) volatile>
174 { typedef _Res result_type; };
176 /// Retrieve result type for a const volatile member function pointer.
177 template<typename _Res, typename _Class, typename... _ArgTypes>
178 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...)
180 { typedef _Res result_type; };
182 template<typename _Res, typename _Class, typename... _ArgTypes>
183 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes......)
185 { typedef _Res result_type; };
188 * Strip top-level cv-qualifiers from the function object and let
189 * _Weak_result_type_impl perform the real work.
191 template<typename _Functor>
192 struct _Weak_result_type
193 : _Weak_result_type_impl<typename remove_cv<_Functor>::type>
196 #if __cplusplus > 201402L
197 # define __cpp_lib_invoke 201411
199 /// Invoke a callable object.
200 template<typename _Callable, typename... _Args>
201 inline result_of_t<_Callable&&(_Args&&...)>
202 invoke(_Callable&& __fn, _Args&&... __args)
203 noexcept(is_nothrow_callable_v<_Callable&&(_Args&&...)>)
205 return std::__invoke(std::forward<_Callable>(__fn),
206 std::forward<_Args>(__args)...);
210 // Detect nested argument_type.
211 template<typename _Tp, typename = __void_t<>>
212 struct _Refwrap_base_arg1
215 // Nested argument_type.
216 template<typename _Tp>
217 struct _Refwrap_base_arg1<_Tp,
218 __void_t<typename _Tp::argument_type>>
220 typedef typename _Tp::argument_type argument_type;
223 // Detect nested first_argument_type and second_argument_type.
224 template<typename _Tp, typename = __void_t<>>
225 struct _Refwrap_base_arg2
228 // Nested first_argument_type and second_argument_type.
229 template<typename _Tp>
230 struct _Refwrap_base_arg2<_Tp,
231 __void_t<typename _Tp::first_argument_type,
232 typename _Tp::second_argument_type>>
234 typedef typename _Tp::first_argument_type first_argument_type;
235 typedef typename _Tp::second_argument_type second_argument_type;
239 * Derives from unary_function or binary_function when it
240 * can. Specializations handle all of the easy cases. The primary
241 * template determines what to do with a class type, which may
242 * derive from both unary_function and binary_function.
244 template<typename _Tp>
245 struct _Reference_wrapper_base
246 : _Weak_result_type<_Tp>, _Refwrap_base_arg1<_Tp>, _Refwrap_base_arg2<_Tp>
249 // - a function type (unary)
250 template<typename _Res, typename _T1>
251 struct _Reference_wrapper_base<_Res(_T1)>
252 : unary_function<_T1, _Res>
255 template<typename _Res, typename _T1>
256 struct _Reference_wrapper_base<_Res(_T1) const>
257 : unary_function<_T1, _Res>
260 template<typename _Res, typename _T1>
261 struct _Reference_wrapper_base<_Res(_T1) volatile>
262 : unary_function<_T1, _Res>
265 template<typename _Res, typename _T1>
266 struct _Reference_wrapper_base<_Res(_T1) const volatile>
267 : unary_function<_T1, _Res>
270 // - a function type (binary)
271 template<typename _Res, typename _T1, typename _T2>
272 struct _Reference_wrapper_base<_Res(_T1, _T2)>
273 : binary_function<_T1, _T2, _Res>
276 template<typename _Res, typename _T1, typename _T2>
277 struct _Reference_wrapper_base<_Res(_T1, _T2) const>
278 : binary_function<_T1, _T2, _Res>
281 template<typename _Res, typename _T1, typename _T2>
282 struct _Reference_wrapper_base<_Res(_T1, _T2) volatile>
283 : binary_function<_T1, _T2, _Res>
286 template<typename _Res, typename _T1, typename _T2>
287 struct _Reference_wrapper_base<_Res(_T1, _T2) const volatile>
288 : binary_function<_T1, _T2, _Res>
291 // - a function pointer type (unary)
292 template<typename _Res, typename _T1>
293 struct _Reference_wrapper_base<_Res(*)(_T1)>
294 : unary_function<_T1, _Res>
297 // - a function pointer type (binary)
298 template<typename _Res, typename _T1, typename _T2>
299 struct _Reference_wrapper_base<_Res(*)(_T1, _T2)>
300 : binary_function<_T1, _T2, _Res>
303 // - a pointer to member function type (unary, no qualifiers)
304 template<typename _Res, typename _T1>
305 struct _Reference_wrapper_base<_Res (_T1::*)()>
306 : unary_function<_T1*, _Res>
309 // - a pointer to member function type (binary, no qualifiers)
310 template<typename _Res, typename _T1, typename _T2>
311 struct _Reference_wrapper_base<_Res (_T1::*)(_T2)>
312 : binary_function<_T1*, _T2, _Res>
315 // - a pointer to member function type (unary, const)
316 template<typename _Res, typename _T1>
317 struct _Reference_wrapper_base<_Res (_T1::*)() const>
318 : unary_function<const _T1*, _Res>
321 // - a pointer to member function type (binary, const)
322 template<typename _Res, typename _T1, typename _T2>
323 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const>
324 : binary_function<const _T1*, _T2, _Res>
327 // - a pointer to member function type (unary, volatile)
328 template<typename _Res, typename _T1>
329 struct _Reference_wrapper_base<_Res (_T1::*)() volatile>
330 : unary_function<volatile _T1*, _Res>
333 // - a pointer to member function type (binary, volatile)
334 template<typename _Res, typename _T1, typename _T2>
335 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) volatile>
336 : binary_function<volatile _T1*, _T2, _Res>
339 // - a pointer to member function type (unary, const volatile)
340 template<typename _Res, typename _T1>
341 struct _Reference_wrapper_base<_Res (_T1::*)() const volatile>
342 : unary_function<const volatile _T1*, _Res>
345 // - a pointer to member function type (binary, const volatile)
346 template<typename _Res, typename _T1, typename _T2>
347 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const volatile>
348 : binary_function<const volatile _T1*, _T2, _Res>
352 * @brief Primary class template for reference_wrapper.
356 template<typename _Tp>
357 class reference_wrapper
358 : public _Reference_wrapper_base<typename remove_cv<_Tp>::type>
365 reference_wrapper(_Tp& __indata) noexcept
366 : _M_data(std::__addressof(__indata))
369 reference_wrapper(_Tp&&) = delete;
371 reference_wrapper(const reference_wrapper&) = default;
374 operator=(const reference_wrapper&) = default;
376 operator _Tp&() const noexcept
377 { return this->get(); }
383 template<typename... _Args>
384 typename result_of<_Tp&(_Args&&...)>::type
385 operator()(_Args&&... __args) const
387 return std::__invoke(get(), std::forward<_Args>(__args)...);
392 /// Denotes a reference should be taken to a variable.
393 template<typename _Tp>
394 inline reference_wrapper<_Tp>
395 ref(_Tp& __t) noexcept
396 { return reference_wrapper<_Tp>(__t); }
398 /// Denotes a const reference should be taken to a variable.
399 template<typename _Tp>
400 inline reference_wrapper<const _Tp>
401 cref(const _Tp& __t) noexcept
402 { return reference_wrapper<const _Tp>(__t); }
404 template<typename _Tp>
405 void ref(const _Tp&&) = delete;
407 template<typename _Tp>
408 void cref(const _Tp&&) = delete;
410 /// Partial specialization.
411 template<typename _Tp>
412 inline reference_wrapper<_Tp>
413 ref(reference_wrapper<_Tp> __t) noexcept
414 { return ref(__t.get()); }
416 /// Partial specialization.
417 template<typename _Tp>
418 inline reference_wrapper<const _Tp>
419 cref(reference_wrapper<_Tp> __t) noexcept
420 { return cref(__t.get()); }
424 template<typename... _Types>
425 struct _Pack : integral_constant<size_t, sizeof...(_Types)>
428 template<typename _From, typename _To, bool = _From::value == _To::value>
429 struct _AllConvertible : false_type
432 template<typename... _From, typename... _To>
433 struct _AllConvertible<_Pack<_From...>, _Pack<_To...>, true>
434 : __and_<is_convertible<_From, _To>...>
437 template<typename _Tp1, typename _Tp2>
438 using _NotSame = __not_<is_same<typename std::decay<_Tp1>::type,
439 typename std::decay<_Tp2>::type>>;
442 * Derives from @c unary_function or @c binary_function, or perhaps
443 * nothing, depending on the number of arguments provided. The
444 * primary template is the basis case, which derives nothing.
446 template<typename _Res, typename... _ArgTypes>
447 struct _Maybe_unary_or_binary_function { };
449 /// Derives from @c unary_function, as appropriate.
450 template<typename _Res, typename _T1>
451 struct _Maybe_unary_or_binary_function<_Res, _T1>
452 : std::unary_function<_T1, _Res> { };
454 /// Derives from @c binary_function, as appropriate.
455 template<typename _Res, typename _T1, typename _T2>
456 struct _Maybe_unary_or_binary_function<_Res, _T1, _T2>
457 : std::binary_function<_T1, _T2, _Res> { };
459 template<typename _Signature>
460 struct _Mem_fn_traits;
462 template<typename _Res, typename _Class, typename... _ArgTypes>
463 struct _Mem_fn_traits_base
465 using __result_type = _Res;
467 = _Maybe_unary_or_binary_function<_Res, _Class*, _ArgTypes...>;
468 using __arity = integral_constant<size_t, sizeof...(_ArgTypes)>;
471 #define _GLIBCXX_MEM_FN_TRAITS2(_CV, _REF, _LVAL, _RVAL) \
472 template<typename _Res, typename _Class, typename... _ArgTypes> \
473 struct _Mem_fn_traits<_Res (_Class::*)(_ArgTypes...) _CV _REF> \
474 : _Mem_fn_traits_base<_Res, _CV _Class, _ArgTypes...> \
476 using __vararg = false_type; \
478 template<typename _Res, typename _Class, typename... _ArgTypes> \
479 struct _Mem_fn_traits<_Res (_Class::*)(_ArgTypes... ...) _CV _REF> \
480 : _Mem_fn_traits_base<_Res, _CV _Class, _ArgTypes...> \
482 using __vararg = true_type; \
485 #define _GLIBCXX_MEM_FN_TRAITS(_REF, _LVAL, _RVAL) \
486 _GLIBCXX_MEM_FN_TRAITS2( , _REF, _LVAL, _RVAL) \
487 _GLIBCXX_MEM_FN_TRAITS2(const , _REF, _LVAL, _RVAL) \
488 _GLIBCXX_MEM_FN_TRAITS2(volatile , _REF, _LVAL, _RVAL) \
489 _GLIBCXX_MEM_FN_TRAITS2(const volatile, _REF, _LVAL, _RVAL)
491 _GLIBCXX_MEM_FN_TRAITS( , true_type, true_type)
492 _GLIBCXX_MEM_FN_TRAITS(&, true_type, false_type)
493 _GLIBCXX_MEM_FN_TRAITS(&&, false_type, true_type)
495 #undef _GLIBCXX_MEM_FN_TRAITS
496 #undef _GLIBCXX_MEM_FN_TRAITS2
498 template<typename _MemFunPtr,
499 bool __is_mem_fn = is_member_function_pointer<_MemFunPtr>::value>
501 : public _Mem_fn_traits<_MemFunPtr>::__maybe_type
503 using _Traits = _Mem_fn_traits<_MemFunPtr>;
505 using _Arity = typename _Traits::__arity;
506 using _Varargs = typename _Traits::__vararg;
508 template<typename _Func, typename... _BoundArgs>
509 friend struct _Bind_check_arity;
515 using result_type = typename _Traits::__result_type;
518 _Mem_fn_base(_MemFunPtr __pmf) noexcept : _M_pmf(__pmf) { }
520 template<typename... _Args>
522 operator()(_Args&&... __args) const
524 std::__invoke(_M_pmf, std::forward<_Args>(__args)...)))
525 -> decltype(std::__invoke(_M_pmf, std::forward<_Args>(__args)...))
526 { return std::__invoke(_M_pmf, std::forward<_Args>(__args)...); }
529 // Partial specialization for member object pointers.
530 template<typename _MemObjPtr>
531 class _Mem_fn_base<_MemObjPtr, false>
533 using _Arity = integral_constant<size_t, 0>;
534 using _Varargs = false_type;
536 template<typename _Func, typename... _BoundArgs>
537 friend struct _Bind_check_arity;
543 _Mem_fn_base(_MemObjPtr __pm) noexcept : _M_pm(__pm) { }
545 template<typename _Tp>
547 operator()(_Tp&& __obj) const
548 noexcept(noexcept(std::__invoke(_M_pm, std::forward<_Tp>(__obj))))
549 -> decltype(std::__invoke(_M_pm, std::forward<_Tp>(__obj)))
550 { return std::__invoke(_M_pm, std::forward<_Tp>(__obj)); }
553 template<typename _Res, typename _Class>
554 struct _Mem_fn<_Res _Class::*>
555 : _Mem_fn_base<_Res _Class::*>
557 using _Mem_fn_base<_Res _Class::*>::_Mem_fn_base;
560 // _GLIBCXX_RESOLVE_LIB_DEFECTS
561 // 2048. Unnecessary mem_fn overloads
563 * @brief Returns a function object that forwards to the member
567 template<typename _Tp, typename _Class>
568 inline _Mem_fn<_Tp _Class::*>
569 mem_fn(_Tp _Class::* __pm) noexcept
571 return _Mem_fn<_Tp _Class::*>(__pm);
575 * @brief Determines if the given type _Tp is a function object that
576 * should be treated as a subexpression when evaluating calls to
577 * function objects returned by bind().
579 * C++11 [func.bind.isbind].
582 template<typename _Tp>
583 struct is_bind_expression
584 : public false_type { };
587 * @brief Determines if the given type _Tp is a placeholder in a
588 * bind() expression and, if so, which placeholder it is.
590 * C++11 [func.bind.isplace].
593 template<typename _Tp>
594 struct is_placeholder
595 : public integral_constant<int, 0>
598 #if __cplusplus > 201402L
599 template <typename _Tp> constexpr bool is_bind_expression_v
600 = is_bind_expression<_Tp>::value;
601 template <typename _Tp> constexpr int is_placeholder_v
602 = is_placeholder<_Tp>::value;
605 /** @brief The type of placeholder objects defined by libstdc++.
608 template<int _Num> struct _Placeholder { };
610 _GLIBCXX_END_NAMESPACE_VERSION
612 /** @namespace std::placeholders
613 * @brief ISO C++11 entities sub-namespace for functional.
616 namespace placeholders
618 _GLIBCXX_BEGIN_NAMESPACE_VERSION
619 /* Define a large number of placeholders. There is no way to
620 * simplify this with variadic templates, because we're introducing
621 * unique names for each.
623 extern const _Placeholder<1> _1;
624 extern const _Placeholder<2> _2;
625 extern const _Placeholder<3> _3;
626 extern const _Placeholder<4> _4;
627 extern const _Placeholder<5> _5;
628 extern const _Placeholder<6> _6;
629 extern const _Placeholder<7> _7;
630 extern const _Placeholder<8> _8;
631 extern const _Placeholder<9> _9;
632 extern const _Placeholder<10> _10;
633 extern const _Placeholder<11> _11;
634 extern const _Placeholder<12> _12;
635 extern const _Placeholder<13> _13;
636 extern const _Placeholder<14> _14;
637 extern const _Placeholder<15> _15;
638 extern const _Placeholder<16> _16;
639 extern const _Placeholder<17> _17;
640 extern const _Placeholder<18> _18;
641 extern const _Placeholder<19> _19;
642 extern const _Placeholder<20> _20;
643 extern const _Placeholder<21> _21;
644 extern const _Placeholder<22> _22;
645 extern const _Placeholder<23> _23;
646 extern const _Placeholder<24> _24;
647 extern const _Placeholder<25> _25;
648 extern const _Placeholder<26> _26;
649 extern const _Placeholder<27> _27;
650 extern const _Placeholder<28> _28;
651 extern const _Placeholder<29> _29;
652 _GLIBCXX_END_NAMESPACE_VERSION
655 _GLIBCXX_BEGIN_NAMESPACE_VERSION
658 * Partial specialization of is_placeholder that provides the placeholder
659 * number for the placeholder objects defined by libstdc++.
663 struct is_placeholder<_Placeholder<_Num> >
664 : public integral_constant<int, _Num>
668 struct is_placeholder<const _Placeholder<_Num> >
669 : public integral_constant<int, _Num>
673 // Like tuple_element_t but SFINAE-friendly.
674 template<std::size_t __i, typename _Tuple>
675 using _Safe_tuple_element_t
676 = typename enable_if<(__i < tuple_size<_Tuple>::value),
677 tuple_element<__i, _Tuple>>::type::type;
680 * Maps an argument to bind() into an actual argument to the bound
681 * function object [func.bind.bind]/10. Only the first parameter should
682 * be specified: the rest are used to determine among the various
683 * implementations. Note that, although this class is a function
684 * object, it isn't entirely normal because it takes only two
685 * parameters regardless of the number of parameters passed to the
686 * bind expression. The first parameter is the bound argument and
687 * the second parameter is a tuple containing references to the
688 * rest of the arguments.
690 template<typename _Arg,
691 bool _IsBindExp = is_bind_expression<_Arg>::value,
692 bool _IsPlaceholder = (is_placeholder<_Arg>::value > 0)>
696 * If the argument is reference_wrapper<_Tp>, returns the
697 * underlying reference.
698 * C++11 [func.bind.bind] p10 bullet 1.
700 template<typename _Tp>
701 class _Mu<reference_wrapper<_Tp>, false, false>
704 /* Note: This won't actually work for const volatile
705 * reference_wrappers, because reference_wrapper::get() is const
706 * but not volatile-qualified. This might be a defect in the TR.
708 template<typename _CVRef, typename _Tuple>
710 operator()(_CVRef& __arg, _Tuple&) const volatile
711 { return __arg.get(); }
715 * If the argument is a bind expression, we invoke the underlying
716 * function object with the same cv-qualifiers as we are given and
717 * pass along all of our arguments (unwrapped).
718 * C++11 [func.bind.bind] p10 bullet 2.
720 template<typename _Arg>
721 class _Mu<_Arg, true, false>
724 template<typename _CVArg, typename... _Args>
726 operator()(_CVArg& __arg,
727 tuple<_Args...>& __tuple) const volatile
728 -> decltype(__arg(declval<_Args>()...))
730 // Construct an index tuple and forward to __call
731 typedef typename _Build_index_tuple<sizeof...(_Args)>::__type
733 return this->__call(__arg, __tuple, _Indexes());
737 // Invokes the underlying function object __arg by unpacking all
738 // of the arguments in the tuple.
739 template<typename _CVArg, typename... _Args, std::size_t... _Indexes>
741 __call(_CVArg& __arg, tuple<_Args...>& __tuple,
742 const _Index_tuple<_Indexes...>&) const volatile
743 -> decltype(__arg(declval<_Args>()...))
745 return __arg(std::get<_Indexes>(std::move(__tuple))...);
750 * If the argument is a placeholder for the Nth argument, returns
751 * a reference to the Nth argument to the bind function object.
752 * C++11 [func.bind.bind] p10 bullet 3.
754 template<typename _Arg>
755 class _Mu<_Arg, false, true>
758 template<typename _Tuple>
759 _Safe_tuple_element_t<(is_placeholder<_Arg>::value - 1), _Tuple>&&
760 operator()(const volatile _Arg&, _Tuple& __tuple) const volatile
763 ::std::get<(is_placeholder<_Arg>::value - 1)>(std::move(__tuple));
768 * If the argument is just a value, returns a reference to that
769 * value. The cv-qualifiers on the reference are determined by the caller.
770 * C++11 [func.bind.bind] p10 bullet 4.
772 template<typename _Arg>
773 class _Mu<_Arg, false, false>
776 template<typename _CVArg, typename _Tuple>
778 operator()(_CVArg&& __arg, _Tuple&) const volatile
779 { return std::forward<_CVArg>(__arg); }
782 // std::get<I> for volatile-qualified tuples
783 template<std::size_t _Ind, typename... _Tp>
785 __volget(volatile tuple<_Tp...>& __tuple)
786 -> __tuple_element_t<_Ind, tuple<_Tp...>> volatile&
787 { return std::get<_Ind>(const_cast<tuple<_Tp...>&>(__tuple)); }
789 // std::get<I> for const-volatile-qualified tuples
790 template<std::size_t _Ind, typename... _Tp>
792 __volget(const volatile tuple<_Tp...>& __tuple)
793 -> __tuple_element_t<_Ind, tuple<_Tp...>> const volatile&
794 { return std::get<_Ind>(const_cast<const tuple<_Tp...>&>(__tuple)); }
796 /// Type of the function object returned from bind().
797 template<typename _Signature>
800 template<typename _Functor, typename... _Bound_args>
801 class _Bind<_Functor(_Bound_args...)>
802 : public _Weak_result_type<_Functor>
804 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
808 tuple<_Bound_args...> _M_bound_args;
811 template<typename _Result, typename... _Args, std::size_t... _Indexes>
813 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>)
815 return std::__invoke(_M_f,
816 _Mu<_Bound_args>()(std::get<_Indexes>(_M_bound_args), __args)...
821 template<typename _Result, typename... _Args, std::size_t... _Indexes>
823 __call_c(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>) const
825 return std::__invoke(_M_f,
826 _Mu<_Bound_args>()(std::get<_Indexes>(_M_bound_args), __args)...
831 template<typename _Result, typename... _Args, std::size_t... _Indexes>
833 __call_v(tuple<_Args...>&& __args,
834 _Index_tuple<_Indexes...>) volatile
836 return std::__invoke(_M_f,
837 _Mu<_Bound_args>()(__volget<_Indexes>(_M_bound_args), __args)...
841 // Call as const volatile
842 template<typename _Result, typename... _Args, std::size_t... _Indexes>
844 __call_c_v(tuple<_Args...>&& __args,
845 _Index_tuple<_Indexes...>) const volatile
847 return std::__invoke(_M_f,
848 _Mu<_Bound_args>()(__volget<_Indexes>(_M_bound_args), __args)...
852 template<typename _BoundArg, typename _CallArgs>
853 using _Mu_type = decltype(
854 _Mu<typename remove_cv<_BoundArg>::type>()(
855 std::declval<_BoundArg&>(), std::declval<_CallArgs&>()) );
857 template<typename _Fn, typename _CallArgs, typename... _BArgs>
859 = typename result_of< _Fn&(_Mu_type<_BArgs, _CallArgs>...) >::type;
861 template<typename _CallArgs>
862 using _Res_type = _Res_type_impl<_Functor, _CallArgs, _Bound_args...>;
864 template<typename _CallArgs>
865 using __dependent = typename
866 enable_if<bool(tuple_size<_CallArgs>::value+1), _Functor>::type;
868 template<typename _CallArgs, template<class> class __cv_quals>
869 using _Res_type_cv = _Res_type_impl<
870 typename __cv_quals<__dependent<_CallArgs>>::type,
872 typename __cv_quals<_Bound_args>::type...>;
875 template<typename... _Args>
876 explicit _Bind(const _Functor& __f, _Args&&... __args)
877 : _M_f(__f), _M_bound_args(std::forward<_Args>(__args)...)
880 template<typename... _Args>
881 explicit _Bind(_Functor&& __f, _Args&&... __args)
882 : _M_f(std::move(__f)), _M_bound_args(std::forward<_Args>(__args)...)
885 _Bind(const _Bind&) = default;
888 : _M_f(std::move(__b._M_f)), _M_bound_args(std::move(__b._M_bound_args))
892 template<typename... _Args,
893 typename _Result = _Res_type<tuple<_Args...>>>
895 operator()(_Args&&... __args)
897 return this->__call<_Result>(
898 std::forward_as_tuple(std::forward<_Args>(__args)...),
903 template<typename... _Args,
904 typename _Result = _Res_type_cv<tuple<_Args...>, add_const>>
906 operator()(_Args&&... __args) const
908 return this->__call_c<_Result>(
909 std::forward_as_tuple(std::forward<_Args>(__args)...),
913 #if __cplusplus > 201402L
914 # define _GLIBCXX_DEPR_BIND \
915 [[deprecated("std::bind does not support volatile in C++17")]]
917 # define _GLIBCXX_DEPR_BIND
920 template<typename... _Args,
921 typename _Result = _Res_type_cv<tuple<_Args...>, add_volatile>>
924 operator()(_Args&&... __args) volatile
926 return this->__call_v<_Result>(
927 std::forward_as_tuple(std::forward<_Args>(__args)...),
931 // Call as const volatile
932 template<typename... _Args,
933 typename _Result = _Res_type_cv<tuple<_Args...>, add_cv>>
936 operator()(_Args&&... __args) const volatile
938 return this->__call_c_v<_Result>(
939 std::forward_as_tuple(std::forward<_Args>(__args)...),
944 /// Type of the function object returned from bind<R>().
945 template<typename _Result, typename _Signature>
948 template<typename _Result, typename _Functor, typename... _Bound_args>
949 class _Bind_result<_Result, _Functor(_Bound_args...)>
951 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
955 tuple<_Bound_args...> _M_bound_args;
958 template<typename _Res>
959 using __enable_if_void
960 = typename enable_if<is_void<_Res>{}>::type;
962 template<typename _Res>
963 using __disable_if_void
964 = typename enable_if<!is_void<_Res>{}, _Result>::type;
967 template<typename _Res, typename... _Args, std::size_t... _Indexes>
968 __disable_if_void<_Res>
969 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>)
971 return std::__invoke(_M_f, _Mu<_Bound_args>()
972 (std::get<_Indexes>(_M_bound_args), __args)...);
975 // Call unqualified, return void
976 template<typename _Res, typename... _Args, std::size_t... _Indexes>
977 __enable_if_void<_Res>
978 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>)
980 std::__invoke(_M_f, _Mu<_Bound_args>()
981 (std::get<_Indexes>(_M_bound_args), __args)...);
985 template<typename _Res, typename... _Args, std::size_t... _Indexes>
986 __disable_if_void<_Res>
987 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>) const
989 return std::__invoke(_M_f, _Mu<_Bound_args>()
990 (std::get<_Indexes>(_M_bound_args), __args)...);
993 // Call as const, return void
994 template<typename _Res, typename... _Args, std::size_t... _Indexes>
995 __enable_if_void<_Res>
996 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>) const
998 std::__invoke(_M_f, _Mu<_Bound_args>()
999 (std::get<_Indexes>(_M_bound_args), __args)...);
1003 template<typename _Res, typename... _Args, std::size_t... _Indexes>
1004 __disable_if_void<_Res>
1005 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>) volatile
1007 return std::__invoke(_M_f, _Mu<_Bound_args>()
1008 (__volget<_Indexes>(_M_bound_args), __args)...);
1011 // Call as volatile, return void
1012 template<typename _Res, typename... _Args, std::size_t... _Indexes>
1013 __enable_if_void<_Res>
1014 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>) volatile
1016 std::__invoke(_M_f, _Mu<_Bound_args>()
1017 (__volget<_Indexes>(_M_bound_args), __args)...);
1020 // Call as const volatile
1021 template<typename _Res, typename... _Args, std::size_t... _Indexes>
1022 __disable_if_void<_Res>
1023 __call(tuple<_Args...>&& __args,
1024 _Index_tuple<_Indexes...>) const volatile
1026 return std::__invoke(_M_f, _Mu<_Bound_args>()
1027 (__volget<_Indexes>(_M_bound_args), __args)...);
1030 // Call as const volatile, return void
1031 template<typename _Res, typename... _Args, std::size_t... _Indexes>
1032 __enable_if_void<_Res>
1033 __call(tuple<_Args...>&& __args,
1034 _Index_tuple<_Indexes...>) const volatile
1036 std::__invoke(_M_f, _Mu<_Bound_args>()
1037 (__volget<_Indexes>(_M_bound_args), __args)...);
1041 typedef _Result result_type;
1043 template<typename... _Args>
1044 explicit _Bind_result(const _Functor& __f, _Args&&... __args)
1045 : _M_f(__f), _M_bound_args(std::forward<_Args>(__args)...)
1048 template<typename... _Args>
1049 explicit _Bind_result(_Functor&& __f, _Args&&... __args)
1050 : _M_f(std::move(__f)), _M_bound_args(std::forward<_Args>(__args)...)
1053 _Bind_result(const _Bind_result&) = default;
1055 _Bind_result(_Bind_result&& __b)
1056 : _M_f(std::move(__b._M_f)), _M_bound_args(std::move(__b._M_bound_args))
1060 template<typename... _Args>
1062 operator()(_Args&&... __args)
1064 return this->__call<_Result>(
1065 std::forward_as_tuple(std::forward<_Args>(__args)...),
1070 template<typename... _Args>
1072 operator()(_Args&&... __args) const
1074 return this->__call<_Result>(
1075 std::forward_as_tuple(std::forward<_Args>(__args)...),
1080 template<typename... _Args>
1083 operator()(_Args&&... __args) volatile
1085 return this->__call<_Result>(
1086 std::forward_as_tuple(std::forward<_Args>(__args)...),
1090 // Call as const volatile
1091 template<typename... _Args>
1094 operator()(_Args&&... __args) const volatile
1096 return this->__call<_Result>(
1097 std::forward_as_tuple(std::forward<_Args>(__args)...),
1101 #undef _GLIBCXX_DEPR_BIND
1104 * @brief Class template _Bind is always a bind expression.
1107 template<typename _Signature>
1108 struct is_bind_expression<_Bind<_Signature> >
1109 : public true_type { };
1112 * @brief Class template _Bind is always a bind expression.
1115 template<typename _Signature>
1116 struct is_bind_expression<const _Bind<_Signature> >
1117 : public true_type { };
1120 * @brief Class template _Bind is always a bind expression.
1123 template<typename _Signature>
1124 struct is_bind_expression<volatile _Bind<_Signature> >
1125 : public true_type { };
1128 * @brief Class template _Bind is always a bind expression.
1131 template<typename _Signature>
1132 struct is_bind_expression<const volatile _Bind<_Signature>>
1133 : public true_type { };
1136 * @brief Class template _Bind_result is always a bind expression.
1139 template<typename _Result, typename _Signature>
1140 struct is_bind_expression<_Bind_result<_Result, _Signature>>
1141 : public true_type { };
1144 * @brief Class template _Bind_result is always a bind expression.
1147 template<typename _Result, typename _Signature>
1148 struct is_bind_expression<const _Bind_result<_Result, _Signature>>
1149 : public true_type { };
1152 * @brief Class template _Bind_result is always a bind expression.
1155 template<typename _Result, typename _Signature>
1156 struct is_bind_expression<volatile _Bind_result<_Result, _Signature>>
1157 : public true_type { };
1160 * @brief Class template _Bind_result is always a bind expression.
1163 template<typename _Result, typename _Signature>
1164 struct is_bind_expression<const volatile _Bind_result<_Result, _Signature>>
1165 : public true_type { };
1167 template<typename _Func, typename... _BoundArgs>
1168 struct _Bind_check_arity { };
1170 template<typename _Ret, typename... _Args, typename... _BoundArgs>
1171 struct _Bind_check_arity<_Ret (*)(_Args...), _BoundArgs...>
1173 static_assert(sizeof...(_BoundArgs) == sizeof...(_Args),
1174 "Wrong number of arguments for function");
1177 template<typename _Ret, typename... _Args, typename... _BoundArgs>
1178 struct _Bind_check_arity<_Ret (*)(_Args......), _BoundArgs...>
1180 static_assert(sizeof...(_BoundArgs) >= sizeof...(_Args),
1181 "Wrong number of arguments for function");
1184 template<typename _Tp, typename _Class, typename... _BoundArgs>
1185 struct _Bind_check_arity<_Tp _Class::*, _BoundArgs...>
1187 using _Arity = typename _Mem_fn<_Tp _Class::*>::_Arity;
1188 using _Varargs = typename _Mem_fn<_Tp _Class::*>::_Varargs;
1189 static_assert(_Varargs::value
1190 ? sizeof...(_BoundArgs) >= _Arity::value + 1
1191 : sizeof...(_BoundArgs) == _Arity::value + 1,
1192 "Wrong number of arguments for pointer-to-member");
1195 // Trait type used to remove std::bind() from overload set via SFINAE
1196 // when first argument has integer type, so that std::bind() will
1197 // not be a better match than ::bind() from the BSD Sockets API.
1198 template<typename _Tp, typename _Tp2 = typename decay<_Tp>::type>
1199 using __is_socketlike = __or_<is_integral<_Tp2>, is_enum<_Tp2>>;
1201 template<bool _SocketLike, typename _Func, typename... _BoundArgs>
1203 : _Bind_check_arity<typename decay<_Func>::type, _BoundArgs...>
1205 typedef typename decay<_Func>::type __func_type;
1206 typedef _Bind<__func_type(typename decay<_BoundArgs>::type...)> type;
1209 // Partial specialization for is_socketlike == true, does not define
1210 // nested type so std::bind() will not participate in overload resolution
1211 // when the first argument might be a socket file descriptor.
1212 template<typename _Func, typename... _BoundArgs>
1213 struct _Bind_helper<true, _Func, _BoundArgs...>
1217 * @brief Function template for std::bind.
1220 template<typename _Func, typename... _BoundArgs>
1222 _Bind_helper<__is_socketlike<_Func>::value, _Func, _BoundArgs...>::type
1223 bind(_Func&& __f, _BoundArgs&&... __args)
1225 typedef _Bind_helper<false, _Func, _BoundArgs...> __helper_type;
1226 return typename __helper_type::type(std::forward<_Func>(__f),
1227 std::forward<_BoundArgs>(__args)...);
1230 template<typename _Result, typename _Func, typename... _BoundArgs>
1231 struct _Bindres_helper
1232 : _Bind_check_arity<typename decay<_Func>::type, _BoundArgs...>
1234 typedef typename decay<_Func>::type __functor_type;
1235 typedef _Bind_result<_Result,
1236 __functor_type(typename decay<_BoundArgs>::type...)>
1241 * @brief Function template for std::bind<R>.
1244 template<typename _Result, typename _Func, typename... _BoundArgs>
1246 typename _Bindres_helper<_Result, _Func, _BoundArgs...>::type
1247 bind(_Func&& __f, _BoundArgs&&... __args)
1249 typedef _Bindres_helper<_Result, _Func, _BoundArgs...> __helper_type;
1250 return typename __helper_type::type(std::forward<_Func>(__f),
1251 std::forward<_BoundArgs>(__args)...);
1255 * @brief Exception class thrown when class template function's
1256 * operator() is called with an empty target.
1257 * @ingroup exceptions
1259 class bad_function_call : public std::exception
1262 virtual ~bad_function_call() noexcept;
1264 const char* what() const noexcept;
1268 * Trait identifying "location-invariant" types, meaning that the
1269 * address of the object (or any of its members) will not escape.
1270 * Trivially copyable types are location-invariant and users can
1271 * specialize this trait for other types.
1273 template<typename _Tp>
1274 struct __is_location_invariant
1275 : is_trivially_copyable<_Tp>::type
1278 class _Undefined_class;
1283 const void* _M_const_object;
1284 void (*_M_function_pointer)();
1285 void (_Undefined_class::*_M_member_pointer)();
1288 union [[gnu::may_alias]] _Any_data
1290 void* _M_access() { return &_M_pod_data[0]; }
1291 const void* _M_access() const { return &_M_pod_data[0]; }
1293 template<typename _Tp>
1296 { return *static_cast<_Tp*>(_M_access()); }
1298 template<typename _Tp>
1301 { return *static_cast<const _Tp*>(_M_access()); }
1303 _Nocopy_types _M_unused;
1304 char _M_pod_data[sizeof(_Nocopy_types)];
1307 enum _Manager_operation
1315 // Simple type wrapper that helps avoid annoying const problems
1316 // when casting between void pointers and pointers-to-pointers.
1317 template<typename _Tp>
1318 struct _Simple_type_wrapper
1320 _Simple_type_wrapper(_Tp __value) : __value(__value) { }
1325 template<typename _Tp>
1326 struct __is_location_invariant<_Simple_type_wrapper<_Tp> >
1327 : __is_location_invariant<_Tp>
1330 template<typename _Signature>
1333 /// Base class of all polymorphic function object wrappers.
1334 class _Function_base
1337 static const std::size_t _M_max_size = sizeof(_Nocopy_types);
1338 static const std::size_t _M_max_align = __alignof__(_Nocopy_types);
1340 template<typename _Functor>
1344 static const bool __stored_locally =
1345 (__is_location_invariant<_Functor>::value
1346 && sizeof(_Functor) <= _M_max_size
1347 && __alignof__(_Functor) <= _M_max_align
1348 && (_M_max_align % __alignof__(_Functor) == 0));
1350 typedef integral_constant<bool, __stored_locally> _Local_storage;
1352 // Retrieve a pointer to the function object
1354 _M_get_pointer(const _Any_data& __source)
1356 const _Functor* __ptr =
1357 __stored_locally? std::__addressof(__source._M_access<_Functor>())
1358 /* have stored a pointer */ : __source._M_access<_Functor*>();
1359 return const_cast<_Functor*>(__ptr);
1362 // Clone a location-invariant function object that fits within
1363 // an _Any_data structure.
1365 _M_clone(_Any_data& __dest, const _Any_data& __source, true_type)
1367 ::new (__dest._M_access()) _Functor(__source._M_access<_Functor>());
1370 // Clone a function object that is not location-invariant or
1371 // that cannot fit into an _Any_data structure.
1373 _M_clone(_Any_data& __dest, const _Any_data& __source, false_type)
1375 __dest._M_access<_Functor*>() =
1376 new _Functor(*__source._M_access<_Functor*>());
1379 // Destroying a location-invariant object may still require
1382 _M_destroy(_Any_data& __victim, true_type)
1384 __victim._M_access<_Functor>().~_Functor();
1387 // Destroying an object located on the heap.
1389 _M_destroy(_Any_data& __victim, false_type)
1391 delete __victim._M_access<_Functor*>();
1396 _M_manager(_Any_data& __dest, const _Any_data& __source,
1397 _Manager_operation __op)
1402 case __get_type_info:
1403 __dest._M_access<const type_info*>() = &typeid(_Functor);
1406 case __get_functor_ptr:
1407 __dest._M_access<_Functor*>() = _M_get_pointer(__source);
1410 case __clone_functor:
1411 _M_clone(__dest, __source, _Local_storage());
1414 case __destroy_functor:
1415 _M_destroy(__dest, _Local_storage());
1422 _M_init_functor(_Any_data& __functor, _Functor&& __f)
1423 { _M_init_functor(__functor, std::move(__f), _Local_storage()); }
1425 template<typename _Signature>
1427 _M_not_empty_function(const function<_Signature>& __f)
1428 { return static_cast<bool>(__f); }
1430 template<typename _Tp>
1432 _M_not_empty_function(_Tp* __fp)
1433 { return __fp != nullptr; }
1435 template<typename _Class, typename _Tp>
1437 _M_not_empty_function(_Tp _Class::* __mp)
1438 { return __mp != nullptr; }
1440 template<typename _Tp>
1442 _M_not_empty_function(const _Tp&)
1447 _M_init_functor(_Any_data& __functor, _Functor&& __f, true_type)
1448 { ::new (__functor._M_access()) _Functor(std::move(__f)); }
1451 _M_init_functor(_Any_data& __functor, _Functor&& __f, false_type)
1452 { __functor._M_access<_Functor*>() = new _Functor(std::move(__f)); }
1455 template<typename _Functor>
1456 class _Ref_manager : public _Base_manager<_Functor*>
1458 typedef _Function_base::_Base_manager<_Functor*> _Base;
1462 _M_manager(_Any_data& __dest, const _Any_data& __source,
1463 _Manager_operation __op)
1468 case __get_type_info:
1469 __dest._M_access<const type_info*>() = &typeid(_Functor);
1472 case __get_functor_ptr:
1473 __dest._M_access<_Functor*>() = *_Base::_M_get_pointer(__source);
1474 return is_const<_Functor>::value;
1478 _Base::_M_manager(__dest, __source, __op);
1484 _M_init_functor(_Any_data& __functor, reference_wrapper<_Functor> __f)
1486 _Base::_M_init_functor(__functor, std::__addressof(__f.get()));
1490 _Function_base() : _M_manager(nullptr) { }
1495 _M_manager(_M_functor, _M_functor, __destroy_functor);
1499 bool _M_empty() const { return !_M_manager; }
1501 typedef bool (*_Manager_type)(_Any_data&, const _Any_data&,
1502 _Manager_operation);
1504 _Any_data _M_functor;
1505 _Manager_type _M_manager;
1508 template<typename _Signature, typename _Functor>
1509 class _Function_handler;
1511 template<typename _Res, typename _Functor, typename... _ArgTypes>
1512 class _Function_handler<_Res(_ArgTypes...), _Functor>
1513 : public _Function_base::_Base_manager<_Functor>
1515 typedef _Function_base::_Base_manager<_Functor> _Base;
1519 _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args)
1521 return (*_Base::_M_get_pointer(__functor))(
1522 std::forward<_ArgTypes>(__args)...);
1526 template<typename _Functor, typename... _ArgTypes>
1527 class _Function_handler<void(_ArgTypes...), _Functor>
1528 : public _Function_base::_Base_manager<_Functor>
1530 typedef _Function_base::_Base_manager<_Functor> _Base;
1534 _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args)
1536 (*_Base::_M_get_pointer(__functor))(
1537 std::forward<_ArgTypes>(__args)...);
1541 template<typename _Res, typename _Functor, typename... _ArgTypes>
1542 class _Function_handler<_Res(_ArgTypes...), reference_wrapper<_Functor> >
1543 : public _Function_base::_Ref_manager<_Functor>
1545 typedef _Function_base::_Ref_manager<_Functor> _Base;
1549 _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args)
1551 return std::__invoke(**_Base::_M_get_pointer(__functor),
1552 std::forward<_ArgTypes>(__args)...);
1556 template<typename _Functor, typename... _ArgTypes>
1557 class _Function_handler<void(_ArgTypes...), reference_wrapper<_Functor> >
1558 : public _Function_base::_Ref_manager<_Functor>
1560 typedef _Function_base::_Ref_manager<_Functor> _Base;
1564 _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args)
1566 std::__invoke(**_Base::_M_get_pointer(__functor),
1567 std::forward<_ArgTypes>(__args)...);
1571 template<typename _Class, typename _Member, typename _Res,
1572 typename... _ArgTypes>
1573 class _Function_handler<_Res(_ArgTypes...), _Member _Class::*>
1574 : public _Function_handler<void(_ArgTypes...), _Member _Class::*>
1576 typedef _Function_handler<void(_ArgTypes...), _Member _Class::*>
1581 _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args)
1583 return std::__invoke(_Base::_M_get_pointer(__functor)->__value,
1584 std::forward<_ArgTypes>(__args)...);
1588 template<typename _Class, typename _Member, typename... _ArgTypes>
1589 class _Function_handler<void(_ArgTypes...), _Member _Class::*>
1590 : public _Function_base::_Base_manager<
1591 _Simple_type_wrapper< _Member _Class::* > >
1593 typedef _Member _Class::* _Functor;
1594 typedef _Simple_type_wrapper<_Functor> _Wrapper;
1595 typedef _Function_base::_Base_manager<_Wrapper> _Base;
1599 _M_manager(_Any_data& __dest, const _Any_data& __source,
1600 _Manager_operation __op)
1605 case __get_type_info:
1606 __dest._M_access<const type_info*>() = &typeid(_Functor);
1609 case __get_functor_ptr:
1610 __dest._M_access<_Functor*>() =
1611 &_Base::_M_get_pointer(__source)->__value;
1615 _Base::_M_manager(__dest, __source, __op);
1621 _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args)
1623 std::__invoke(_Base::_M_get_pointer(__functor)->__value,
1624 std::forward<_ArgTypes>(__args)...);
1628 template<typename _From, typename _To>
1629 using __check_func_return_type
1630 = __or_<is_void<_To>, is_same<_From, _To>, is_convertible<_From, _To>>;
1633 * @brief Primary class template for std::function.
1636 * Polymorphic function wrapper.
1638 template<typename _Res, typename... _ArgTypes>
1639 class function<_Res(_ArgTypes...)>
1640 : public _Maybe_unary_or_binary_function<_Res, _ArgTypes...>,
1641 private _Function_base
1643 typedef _Res _Signature_type(_ArgTypes...);
1645 template<typename _Func,
1646 typename _Res2 = typename result_of<_Func&(_ArgTypes...)>::type>
1647 struct _Callable : __check_func_return_type<_Res2, _Res> { };
1649 // Used so the return type convertibility checks aren't done when
1650 // performing overload resolution for copy construction/assignment.
1651 template<typename _Tp>
1652 struct _Callable<function, _Tp> : false_type { };
1654 template<typename _Cond, typename _Tp>
1655 using _Requires = typename enable_if<_Cond::value, _Tp>::type;
1658 typedef _Res result_type;
1660 // [3.7.2.1] construct/copy/destroy
1663 * @brief Default construct creates an empty function call wrapper.
1664 * @post @c !(bool)*this
1667 : _Function_base() { }
1670 * @brief Creates an empty function call wrapper.
1671 * @post @c !(bool)*this
1673 function(nullptr_t) noexcept
1674 : _Function_base() { }
1677 * @brief %Function copy constructor.
1678 * @param __x A %function object with identical call signature.
1679 * @post @c bool(*this) == bool(__x)
1681 * The newly-created %function contains a copy of the target of @a
1682 * __x (if it has one).
1684 function(const function& __x);
1687 * @brief %Function move constructor.
1688 * @param __x A %function object rvalue with identical call signature.
1690 * The newly-created %function contains the target of @a __x
1693 function(function&& __x) : _Function_base()
1699 * @brief Builds a %function that targets a copy of the incoming
1701 * @param __f A %function object that is callable with parameters of
1702 * type @c T1, @c T2, ..., @c TN and returns a value convertible
1705 * The newly-created %function object will target a copy of
1706 * @a __f. If @a __f is @c reference_wrapper<F>, then this function
1707 * object will contain a reference to the function object @c
1708 * __f.get(). If @a __f is a NULL function pointer or NULL
1709 * pointer-to-member, the newly-created object will be empty.
1711 * If @a __f is a non-NULL function pointer or an object of type @c
1712 * reference_wrapper<F>, this function will not throw.
1714 template<typename _Functor,
1715 typename = _Requires<__not_<is_same<_Functor, function>>, void>,
1716 typename = _Requires<_Callable<_Functor>, void>>
1720 * @brief %Function assignment operator.
1721 * @param __x A %function with identical call signature.
1722 * @post @c (bool)*this == (bool)x
1725 * The target of @a __x is copied to @c *this. If @a __x has no
1726 * target, then @c *this will be empty.
1728 * If @a __x targets a function pointer or a reference to a function
1729 * object, then this operation will not throw an %exception.
1732 operator=(const function& __x)
1734 function(__x).swap(*this);
1739 * @brief %Function move-assignment operator.
1740 * @param __x A %function rvalue with identical call signature.
1743 * The target of @a __x is moved to @c *this. If @a __x has no
1744 * target, then @c *this will be empty.
1746 * If @a __x targets a function pointer or a reference to a function
1747 * object, then this operation will not throw an %exception.
1750 operator=(function&& __x)
1752 function(std::move(__x)).swap(*this);
1757 * @brief %Function assignment to zero.
1758 * @post @c !(bool)*this
1761 * The target of @c *this is deallocated, leaving it empty.
1764 operator=(nullptr_t) noexcept
1768 _M_manager(_M_functor, _M_functor, __destroy_functor);
1769 _M_manager = nullptr;
1770 _M_invoker = nullptr;
1776 * @brief %Function assignment to a new target.
1777 * @param __f A %function object that is callable with parameters of
1778 * type @c T1, @c T2, ..., @c TN and returns a value convertible
1782 * This %function object wrapper will target a copy of @a
1783 * __f. If @a __f is @c reference_wrapper<F>, then this function
1784 * object will contain a reference to the function object @c
1785 * __f.get(). If @a __f is a NULL function pointer or NULL
1786 * pointer-to-member, @c this object will be empty.
1788 * If @a __f is a non-NULL function pointer or an object of type @c
1789 * reference_wrapper<F>, this function will not throw.
1791 template<typename _Functor>
1792 _Requires<_Callable<typename decay<_Functor>::type>, function&>
1793 operator=(_Functor&& __f)
1795 function(std::forward<_Functor>(__f)).swap(*this);
1800 template<typename _Functor>
1802 operator=(reference_wrapper<_Functor> __f) noexcept
1804 function(__f).swap(*this);
1808 // [3.7.2.2] function modifiers
1811 * @brief Swap the targets of two %function objects.
1812 * @param __x A %function with identical call signature.
1814 * Swap the targets of @c this function object and @a __f. This
1815 * function will not throw an %exception.
1817 void swap(function& __x) noexcept
1819 std::swap(_M_functor, __x._M_functor);
1820 std::swap(_M_manager, __x._M_manager);
1821 std::swap(_M_invoker, __x._M_invoker);
1824 // [3.7.2.3] function capacity
1827 * @brief Determine if the %function wrapper has a target.
1829 * @return @c true when this %function object contains a target,
1830 * or @c false when it is empty.
1832 * This function will not throw an %exception.
1834 explicit operator bool() const noexcept
1835 { return !_M_empty(); }
1837 // [3.7.2.4] function invocation
1840 * @brief Invokes the function targeted by @c *this.
1841 * @returns the result of the target.
1842 * @throws bad_function_call when @c !(bool)*this
1844 * The function call operator invokes the target function object
1845 * stored by @c this.
1847 _Res operator()(_ArgTypes... __args) const;
1850 // [3.7.2.5] function target access
1852 * @brief Determine the type of the target of this function object
1855 * @returns the type identifier of the target function object, or
1856 * @c typeid(void) if @c !(bool)*this.
1858 * This function will not throw an %exception.
1860 const type_info& target_type() const noexcept;
1863 * @brief Access the stored target function object.
1865 * @return Returns a pointer to the stored target function object,
1866 * if @c typeid(Functor).equals(target_type()); otherwise, a NULL
1869 * This function will not throw an %exception.
1871 template<typename _Functor> _Functor* target() noexcept;
1874 template<typename _Functor> const _Functor* target() const noexcept;
1878 using _Invoker_type = _Res (*)(const _Any_data&, _ArgTypes&&...);
1879 _Invoker_type _M_invoker;
1882 // Out-of-line member definitions.
1883 template<typename _Res, typename... _ArgTypes>
1884 function<_Res(_ArgTypes...)>::
1885 function(const function& __x)
1888 if (static_cast<bool>(__x))
1890 __x._M_manager(_M_functor, __x._M_functor, __clone_functor);
1891 _M_invoker = __x._M_invoker;
1892 _M_manager = __x._M_manager;
1896 template<typename _Res, typename... _ArgTypes>
1897 template<typename _Functor, typename, typename>
1898 function<_Res(_ArgTypes...)>::
1899 function(_Functor __f)
1902 typedef _Function_handler<_Signature_type, _Functor> _My_handler;
1904 if (_My_handler::_M_not_empty_function(__f))
1906 _My_handler::_M_init_functor(_M_functor, std::move(__f));
1907 _M_invoker = &_My_handler::_M_invoke;
1908 _M_manager = &_My_handler::_M_manager;
1912 template<typename _Res, typename... _ArgTypes>
1914 function<_Res(_ArgTypes...)>::
1915 operator()(_ArgTypes... __args) const
1918 __throw_bad_function_call();
1919 return _M_invoker(_M_functor, std::forward<_ArgTypes>(__args)...);
1923 template<typename _Res, typename... _ArgTypes>
1925 function<_Res(_ArgTypes...)>::
1926 target_type() const noexcept
1930 _Any_data __typeinfo_result;
1931 _M_manager(__typeinfo_result, _M_functor, __get_type_info);
1932 return *__typeinfo_result._M_access<const type_info*>();
1935 return typeid(void);
1938 template<typename _Res, typename... _ArgTypes>
1939 template<typename _Functor>
1941 function<_Res(_ArgTypes...)>::
1944 if (typeid(_Functor) == target_type() && _M_manager)
1947 if (_M_manager(__ptr, _M_functor, __get_functor_ptr)
1948 && !is_const<_Functor>::value)
1951 return __ptr._M_access<_Functor*>();
1957 template<typename _Res, typename... _ArgTypes>
1958 template<typename _Functor>
1960 function<_Res(_ArgTypes...)>::
1961 target() const noexcept
1963 if (typeid(_Functor) == target_type() && _M_manager)
1966 _M_manager(__ptr, _M_functor, __get_functor_ptr);
1967 return __ptr._M_access<const _Functor*>();
1974 // [20.7.15.2.6] null pointer comparisons
1977 * @brief Compares a polymorphic function object wrapper against 0
1978 * (the NULL pointer).
1979 * @returns @c true if the wrapper has no target, @c false otherwise
1981 * This function will not throw an %exception.
1983 template<typename _Res, typename... _Args>
1985 operator==(const function<_Res(_Args...)>& __f, nullptr_t) noexcept
1986 { return !static_cast<bool>(__f); }
1989 template<typename _Res, typename... _Args>
1991 operator==(nullptr_t, const function<_Res(_Args...)>& __f) noexcept
1992 { return !static_cast<bool>(__f); }
1995 * @brief Compares a polymorphic function object wrapper against 0
1996 * (the NULL pointer).
1997 * @returns @c false if the wrapper has no target, @c true otherwise
1999 * This function will not throw an %exception.
2001 template<typename _Res, typename... _Args>
2003 operator!=(const function<_Res(_Args...)>& __f, nullptr_t) noexcept
2004 { return static_cast<bool>(__f); }
2007 template<typename _Res, typename... _Args>
2009 operator!=(nullptr_t, const function<_Res(_Args...)>& __f) noexcept
2010 { return static_cast<bool>(__f); }
2012 // [20.7.15.2.7] specialized algorithms
2015 * @brief Swap the targets of two polymorphic function object wrappers.
2017 * This function will not throw an %exception.
2019 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2020 // 2062. Effect contradictions w/o no-throw guarantee of std::function swaps
2021 template<typename _Res, typename... _Args>
2023 swap(function<_Res(_Args...)>& __x, function<_Res(_Args...)>& __y) noexcept
2026 #if __cplusplus >= 201402L
2027 /// Generalized negator.
2028 template<typename _Fn>
2031 template<typename _Tp>
2032 using __is_nothrow_negatable
2033 = __bool_constant<noexcept(!std::declval<_Tp>())>;
2035 template<typename _Fn2, typename... _Args>
2036 using __noexcept_cond = __and_<
2037 __is_nothrow_callable<_Fn2(_Args&&...)>,
2038 __is_nothrow_negatable<result_of_t<_Fn2(_Args&&...)>>
2042 template<typename _Fn2>
2043 _Not_fn(_Fn2&& __fn, int)
2044 : _M_fn(std::forward<_Fn2>(__fn)) { }
2046 _Not_fn(const _Not_fn& __fn) = default;
2047 _Not_fn(_Not_fn&& __fn) = default;
2048 ~_Not_fn() = default;
2050 template<typename... _Args>
2052 operator()(_Args&&... __args) &
2053 noexcept(__noexcept_cond<_Fn&, _Args&&...>::value)
2054 -> decltype(!std::declval<result_of_t<_Fn&(_Args&&...)>>())
2055 { return !std::__invoke(_M_fn, std::forward<_Args>(__args)...); }
2057 template<typename... _Args>
2059 operator()(_Args&&... __args) const &
2060 noexcept(__noexcept_cond<const _Fn&, _Args&&...>::value)
2061 -> decltype(!std::declval<result_of_t<const _Fn&(_Args&&...)>>())
2062 { return !std::__invoke(_M_fn, std::forward<_Args>(__args)...); }
2064 template<typename... _Args>
2066 operator()(_Args&&... __args) &&
2067 noexcept(__noexcept_cond<_Fn&&, _Args&&...>::value)
2068 -> decltype(!std::declval<result_of_t<_Fn&&(_Args&&...)>>())
2070 return !std::__invoke(std::move(_M_fn),
2071 std::forward<_Args>(__args)...);
2074 template<typename... _Args>
2076 operator()(_Args&&... __args) const &&
2077 noexcept(__noexcept_cond<const _Fn&&, _Args&&...>::value)
2078 -> decltype(!std::declval<result_of_t<const _Fn&&(_Args&&...)>>())
2080 return !std::__invoke(std::move(_M_fn),
2081 std::forward<_Args>(__args)...);
2089 #if __cplusplus > 201402L
2091 #define __cpp_lib_not_fn 201603
2093 /// [func.not_fn] Function template not_fn
2094 template<typename _Fn>
2097 noexcept(std::is_nothrow_constructible<std::decay_t<_Fn>, _Fn&&>::value)
2099 return _Not_fn<std::decay_t<_Fn>>{std::forward<_Fn>(__fn), 0};
2103 #define __cpp_lib_boyer_moore_searcher 201603
2105 template<typename _ForwardIterator1, typename _BinaryPredicate = equal_to<>>
2106 class default_searcher
2109 default_searcher(_ForwardIterator1 __pat_first,
2110 _ForwardIterator1 __pat_last,
2111 _BinaryPredicate __pred = _BinaryPredicate())
2112 : _M_m(__pat_first, __pat_last, std::move(__pred))
2115 template<typename _ForwardIterator2>
2116 pair<_ForwardIterator2, _ForwardIterator2>
2117 operator()(_ForwardIterator2 __first, _ForwardIterator2 __last) const
2119 _ForwardIterator2 __first_ret =
2120 std::search(__first, __last,
2121 std::get<0>(_M_m), std::get<1>(_M_m),
2123 _ForwardIterator2 __second_ret = __first_ret == __last ?
2124 __last : std::next(__first_ret, std::distance(std::get<0>(_M_m),
2125 std::get<1>(_M_m)));
2126 return std::make_pair(__first_ret, __second_ret);
2130 std::tuple<_ForwardIterator1, _ForwardIterator1, _BinaryPredicate> _M_m;
2133 template<typename _Key, typename _Tp, typename _Hash, typename _Pred>
2134 struct __boyer_moore_map_base
2136 template<typename _RAIter>
2137 __boyer_moore_map_base(_RAIter __pat, size_t __patlen,
2138 _Hash&& __hf, _Pred&& __pred)
2139 : _M_bad_char{ __patlen, std::move(__hf), std::move(__pred) }
2142 for (__diff_type __i = 0; __i < __patlen - 1; ++__i)
2143 _M_bad_char[__pat[__i]] = __patlen - 1 - __i;
2146 using __diff_type = _Tp;
2149 _M_lookup(_Key __key, __diff_type __not_found) const
2151 auto __iter = _M_bad_char.find(__key);
2152 if (__iter == _M_bad_char.end())
2154 return __iter->second;
2158 _M_pred() const { return _M_bad_char.key_eq(); }
2160 _GLIBCXX_STD_C::unordered_map<_Key, _Tp, _Hash, _Pred> _M_bad_char;
2163 template<typename _Tp, size_t _Len, typename _Pred>
2164 struct __boyer_moore_array_base
2166 template<typename _RAIter, typename _Unused>
2167 __boyer_moore_array_base(_RAIter __pat, size_t __patlen,
2168 _Unused&&, _Pred&& __pred)
2169 : _M_bad_char{ _GLIBCXX_STD_C::array<_Tp, _Len>{}, std::move(__pred) }
2171 std::get<0>(_M_bad_char).fill(__patlen);
2173 for (__diff_type __i = 0; __i < __patlen - 1; ++__i)
2175 auto __ch = __pat[__i];
2176 using _UCh = std::make_unsigned_t<decltype(__ch)>;
2177 auto __uch = static_cast<_UCh>(__ch);
2178 std::get<0>(_M_bad_char)[__uch] = __patlen - 1 - __i;
2182 using __diff_type = _Tp;
2184 template<typename _Key>
2186 _M_lookup(_Key __key, __diff_type __not_found) const
2188 auto __ukey = static_cast<std::make_unsigned_t<_Key>>(__key);
2191 return std::get<0>(_M_bad_char)[__ukey];
2195 _M_pred() const { return std::get<1>(_M_bad_char); }
2197 std::tuple<_GLIBCXX_STD_C::array<_Tp, _Len>, _Pred> _M_bad_char;
2200 template<typename _Pred>
2201 struct __is_std_equal_to : std::false_type { };
2204 struct __is_std_equal_to<std::equal_to<void>> : std::true_type { };
2206 // Use __boyer_moore_array_base when pattern consists of narrow characters
2207 // and uses std::equal_to as the predicate.
2208 template<typename _RAIter, typename _Hash, typename _Pred,
2209 typename _Val = typename iterator_traits<_RAIter>::value_type,
2210 typename _Diff = typename iterator_traits<_RAIter>::difference_type>
2211 using __boyer_moore_base_t
2212 = std::conditional_t<sizeof(_Val) == 1 && is_integral<_Val>::value
2213 && __is_std_equal_to<_Pred>::value,
2214 __boyer_moore_array_base<_Diff, 256, _Pred>,
2215 __boyer_moore_map_base<_Val, _Diff, _Hash, _Pred>>;
2217 template<typename _RAIter, typename _Hash
2218 = std::hash<typename std::iterator_traits<_RAIter>::value_type>,
2219 typename _BinaryPredicate = std::equal_to<>>
2220 class boyer_moore_searcher
2221 : __boyer_moore_base_t<_RAIter, _Hash, _BinaryPredicate>
2223 using _Base = __boyer_moore_base_t<_RAIter, _Hash, _BinaryPredicate>;
2224 using typename _Base::__diff_type;
2227 boyer_moore_searcher(_RAIter __pat_first, _RAIter __pat_last,
2228 _Hash __hf = _Hash(),
2229 _BinaryPredicate __pred = _BinaryPredicate());
2231 template<typename _RandomAccessIterator2>
2232 pair<_RandomAccessIterator2, _RandomAccessIterator2>
2233 operator()(_RandomAccessIterator2 __first,
2234 _RandomAccessIterator2 __last) const;
2238 _M_is_prefix(_RAIter __word, __diff_type __len,
2241 const auto& __pred = this->_M_pred();
2242 __diff_type __suffixlen = __len - __pos;
2243 for (__diff_type __i = 0; __i < __suffixlen; ++__i)
2244 if (!__pred(__word[__i], __word[__pos + __i]))
2250 _M_suffix_length(_RAIter __word, __diff_type __len,
2253 const auto& __pred = this->_M_pred();
2254 __diff_type __i = 0;
2255 while (__pred(__word[__pos - __i], __word[__len - 1 - __i])
2263 template<typename _Tp>
2265 _M_bad_char_shift(_Tp __c) const
2266 { return this->_M_lookup(__c, _M_pat_end - _M_pat); }
2270 _GLIBCXX_STD_C::vector<__diff_type> _M_good_suffix;
2273 template<typename _RAIter, typename _Hash
2274 = std::hash<typename std::iterator_traits<_RAIter>::value_type>,
2275 typename _BinaryPredicate = std::equal_to<>>
2276 class boyer_moore_horspool_searcher
2277 : __boyer_moore_base_t<_RAIter, _Hash, _BinaryPredicate>
2279 using _Base = __boyer_moore_base_t<_RAIter, _Hash, _BinaryPredicate>;
2280 using typename _Base::__diff_type;
2283 boyer_moore_horspool_searcher(_RAIter __pat,
2285 _Hash __hf = _Hash(),
2286 _BinaryPredicate __pred
2287 = _BinaryPredicate())
2288 : _Base(__pat, __pat_end - __pat, std::move(__hf), std::move(__pred)),
2289 _M_pat(__pat), _M_pat_end(__pat_end)
2292 template<typename _RandomAccessIterator2>
2293 pair<_RandomAccessIterator2, _RandomAccessIterator2>
2294 operator()(_RandomAccessIterator2 __first,
2295 _RandomAccessIterator2 __last) const
2297 const auto& __pred = this->_M_pred();
2298 auto __patlen = _M_pat_end - _M_pat;
2300 return std::make_pair(__first, __first);
2301 auto __len = __last - __first;
2302 while (__len >= __patlen)
2304 for (auto __scan = __patlen - 1;
2305 __pred(__first[__scan], _M_pat[__scan]); --__scan)
2307 return std::make_pair(__first,
2308 std::next(__first, __patlen));
2309 auto __shift = _M_bad_char_shift(__first[__patlen - 1]);
2313 return std::make_pair(__last, __last);
2317 template<typename _Tp>
2319 _M_bad_char_shift(_Tp __c) const
2320 { return this->_M_lookup(__c, _M_pat_end - _M_pat); }
2326 /// Generator function for default_searcher
2327 template<typename _ForwardIterator,
2328 typename _BinaryPredicate = std::equal_to<>>
2329 inline default_searcher<_ForwardIterator, _BinaryPredicate>
2330 make_default_searcher(_ForwardIterator __pat_first,
2331 _ForwardIterator __pat_last,
2332 _BinaryPredicate __pred = _BinaryPredicate())
2333 { return { __pat_first, __pat_last, __pred }; }
2335 /// Generator function for boyer_moore_searcher
2336 template<typename _RAIter, typename _Hash
2337 = std::hash<typename std::iterator_traits<_RAIter>::value_type>,
2338 typename _BinaryPredicate = equal_to<>>
2339 inline boyer_moore_searcher<_RAIter, _Hash, _BinaryPredicate>
2340 make_boyer_moore_searcher(_RAIter __pat_first, _RAIter __pat_last,
2341 _Hash __hf = _Hash(),
2342 _BinaryPredicate __pred = _BinaryPredicate())
2343 { return { __pat_first, __pat_last, std::move(__hf), std::move(__pred) }; }
2345 /// Generator function for boyer_moore_horspool_searcher
2346 template<typename _RAIter, typename _Hash
2347 = std::hash<typename std::iterator_traits<_RAIter>::value_type>,
2348 typename _BinaryPredicate = equal_to<>>
2349 inline boyer_moore_horspool_searcher<_RAIter, _Hash, _BinaryPredicate>
2350 make_boyer_moore_horspool_searcher(_RAIter __pat_first, _RAIter __pat_last,
2351 _Hash __hf = _Hash(),
2352 _BinaryPredicate __pred
2353 = _BinaryPredicate())
2354 { return { __pat_first, __pat_last, std::move(__hf), std::move(__pred) }; }
2356 template<typename _RAIter, typename _Hash, typename _BinaryPredicate>
2357 boyer_moore_searcher<_RAIter, _Hash, _BinaryPredicate>::
2358 boyer_moore_searcher(_RAIter __pat, _RAIter __pat_end,
2359 _Hash __hf, _BinaryPredicate __pred)
2360 : _Base(__pat, __pat_end - __pat, std::move(__hf), std::move(__pred)),
2361 _M_pat(__pat), _M_pat_end(__pat_end), _M_good_suffix(__pat_end - __pat)
2363 auto __patlen = __pat_end - __pat;
2366 __diff_type __last_prefix = __patlen - 1;
2367 for (__diff_type __p = __patlen - 1; __p >= 0; --__p)
2369 if (_M_is_prefix(__pat, __patlen, __p + 1))
2370 __last_prefix = __p + 1;
2371 _M_good_suffix[__p] = __last_prefix + (__patlen - 1 - __p);
2373 for (__diff_type __p = 0; __p < __patlen - 1; ++__p)
2375 auto __slen = _M_suffix_length(__pat, __patlen, __p);
2376 auto __pos = __patlen - 1 - __slen;
2377 if (!__pred(__pat[__p - __slen], __pat[__pos]))
2378 _M_good_suffix[__pos] = __patlen - 1 - __p + __slen;
2382 template<typename _RAIter, typename _Hash, typename _BinaryPredicate>
2383 template<typename _RandomAccessIterator2>
2384 pair<_RandomAccessIterator2, _RandomAccessIterator2>
2385 boyer_moore_searcher<_RAIter, _Hash, _BinaryPredicate>::
2386 operator()(_RandomAccessIterator2 __first,
2387 _RandomAccessIterator2 __last) const
2389 auto __patlen = _M_pat_end - _M_pat;
2391 return std::make_pair(__first, __first);
2392 const auto& __pred = this->_M_pred();
2393 __diff_type __i = __patlen - 1;
2394 auto __stringlen = __last - __first;
2395 while (__i < __stringlen)
2397 __diff_type __j = __patlen - 1;
2398 while (__j >= 0 && __pred(__first[__i], _M_pat[__j]))
2404 return std::make_pair(__first + __i + 1, std::next(__first,
2406 __i += std::max(_M_bad_char_shift(__first[__i]),
2407 _M_good_suffix[__j]);
2409 return std::make_pair(__last, __last);
2414 _GLIBCXX_END_NAMESPACE_VERSION
2419 #endif // _GLIBCXX_FUNCTIONAL