libstdc++: functional Source File

libstdc++

functional
Go to the documentation of this file.
1// <functional> -*- C++ -*-
2
3// Copyright (C) 2001-2026 Free Software Foundation, Inc.
4//
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)
9// any later version.
10
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.
15
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.
19
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/>.
24
25/*
26 * Copyright (c) 1997
27 * Silicon Graphics Computer Systems, Inc.
28 *
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.
36 *
37 */
38
39/** @file include/functional
40 * This is a Standard C++ Library header.
41 */
42
43#ifndef _GLIBCXX_FUNCTIONAL
44#define _GLIBCXX_FUNCTIONAL 1
45
46#ifdef _GLIBCXX_SYSHDR
47#pragma GCC system_header
48#endif
49
50#include <bits/c++config.h>
51#include <bits/stl_function.h> // std::equal_to, std::unary_function etc.
52
53#if __cplusplus >= 201103L
54
55#define __glibcxx_want_boyer_moore_searcher
56#define __glibcxx_want_bind_front
57#define __glibcxx_want_bind_back
58#define __glibcxx_want_constexpr_functional
59#define __glibcxx_want_copyable_function
60#define __glibcxx_want_function_ref
61#define __glibcxx_want_invoke
62#define __glibcxx_want_invoke_r
63#define __glibcxx_want_move_only_function
64#define __glibcxx_want_not_fn
65#define __glibcxx_want_ranges
66#define __glibcxx_want_reference_wrapper
67#define __glibcxx_want_common_reference_wrapper
68#define __glibcxx_want_transparent_operators
69#include <bits/version.h>
70
71#include <tuple>
72#include <bits/functional_hash.h>
73#include <bits/invoke.h>
74#include <bits/refwrap.h> // std::reference_wrapper and _Mem_fn_traits
75#if _GLIBCXX_HOSTED
76# include <bits/std_function.h> // std::function
77#endif
78#if __cplusplus >= 201703L
79# if _GLIBCXX_HOSTED
80# include <unordered_map>
81# include <vector>
82# include <array>
83# endif
84# include <bits/stl_algobase.h> // std::search
85#endif
86#if __cplusplus >= 202002L
87# include <bits/binders.h>
88# include <bits/ranges_cmp.h> // std::identity, ranges::equal_to etc.
89# include <compare>
90#endif
91#if __glibcxx_move_only_function || __glibcxx_copyable_function || \
92 __glibcxx_function_ref
93# include <bits/funcwrap.h>
94#endif
95
96#endif // C++11
97
98namespace std _GLIBCXX_VISIBILITY(default)
99{
100_GLIBCXX_BEGIN_NAMESPACE_VERSION
101
102 /** @brief The type of placeholder objects defined by libstdc++.
103 * @ingroup binders
104 * @since C++11
105 */
106 template<int _Num> struct _Placeholder { };
107
108#ifdef __cpp_lib_invoke // C++ >= 17
109
110 /** Invoke a callable object.
111 *
112 * `std::invoke` takes a callable object as its first argument and calls it
113 * with the remaining arguments. The callable object can be a pointer or
114 * reference to a function, a lambda closure, a class with `operator()`,
115 * or even a pointer-to-member. For a pointer-to-member the first argument
116 * must be a reference or pointer to the object that the pointer-to-member
117 * will be applied to.
118 *
119 * @since C++17
120 */
121 template<typename _Callable, typename... _Args>
122 inline _GLIBCXX20_CONSTEXPR invoke_result_t<_Callable, _Args...>
123 invoke(_Callable&& __fn, _Args&&... __args)
124 noexcept(is_nothrow_invocable_v<_Callable, _Args...>)
125 {
126 return std::__invoke(std::forward<_Callable>(__fn),
127 std::forward<_Args>(__args)...);
128 }
129#endif
130
131#ifdef __cpp_lib_invoke_r // C++ >= 23
132
133 /** Invoke a callable object and convert the result to `_Res`.
134 *
135 * `std::invoke_r<R>(f, args...)` is equivalent to `std::invoke(f, args...)`
136 * with the result implicitly converted to `R`.
137 *
138 * @since C++23
139 */
140 template<typename _Res, typename _Callable, typename... _Args>
141 requires is_invocable_r_v<_Res, _Callable, _Args...>
142 constexpr _Res
143 invoke_r(_Callable&& __fn, _Args&&... __args)
144 noexcept(is_nothrow_invocable_r_v<_Res, _Callable, _Args...>)
145 {
146 return std::__invoke_r<_Res>(std::forward<_Callable>(__fn),
147 std::forward<_Args>(__args)...);
148 }
149#endif // __cpp_lib_invoke_r
150
151#if __cplusplus >= 201103L
152 /// @cond undocumented
153
154 template<typename _MemFunPtr,
155 bool __is_mem_fn = is_member_function_pointer<_MemFunPtr>::value>
156 class _Mem_fn_base
157 : public _Mem_fn_traits<_MemFunPtr>::__maybe_type
158 {
159 using _Traits = _Mem_fn_traits<_MemFunPtr>;
160
161 using _Arity = typename _Traits::__arity;
162 using _Varargs = typename _Traits::__vararg;
163
164 template<typename _Func, typename... _BoundArgs>
165 friend struct _Bind_check_arity;
166
167 _MemFunPtr _M_pmf;
168
169 public:
170
171 using result_type = typename _Traits::__result_type;
172
173 explicit constexpr
174 _Mem_fn_base(_MemFunPtr __pmf) noexcept : _M_pmf(__pmf) { }
175
176 template<typename... _Args>
177 _GLIBCXX20_CONSTEXPR
178 auto
179 operator()(_Args&&... __args) const
180 noexcept(noexcept(
181 std::__invoke(_M_pmf, std::forward<_Args>(__args)...)))
182 -> decltype(std::__invoke(_M_pmf, std::forward<_Args>(__args)...))
183 { return std::__invoke(_M_pmf, std::forward<_Args>(__args)...); }
184 };
185
186 // Partial specialization for member object pointers.
187 template<typename _MemObjPtr>
188 class _Mem_fn_base<_MemObjPtr, false>
189 {
190 using _Arity = integral_constant<size_t, 0>;
191 using _Varargs = false_type;
192
193 template<typename _Func, typename... _BoundArgs>
194 friend struct _Bind_check_arity;
195
196 _MemObjPtr _M_pm;
197
198 public:
199 explicit constexpr
200 _Mem_fn_base(_MemObjPtr __pm) noexcept : _M_pm(__pm) { }
201
202 template<typename _Tp>
203 _GLIBCXX20_CONSTEXPR
204 auto
205 operator()(_Tp&& __obj) const
206 noexcept(noexcept(std::__invoke(_M_pm, std::forward<_Tp>(__obj))))
207 -> decltype(std::__invoke(_M_pm, std::forward<_Tp>(__obj)))
208 { return std::__invoke(_M_pm, std::forward<_Tp>(__obj)); }
209 };
210
211 template<typename _MemberPointer>
212 struct _Mem_fn; // undefined
213
214 template<typename _Res, typename _Class>
215 struct _Mem_fn<_Res _Class::*>
216 : _Mem_fn_base<_Res _Class::*>
217 {
218 using _Mem_fn_base<_Res _Class::*>::_Mem_fn_base;
219 };
220 /// @endcond
221
222 // _GLIBCXX_RESOLVE_LIB_DEFECTS
223 // 2048. Unnecessary mem_fn overloads
224 /**
225 * @brief Returns a function object that forwards to the member pointer
226 * pointer `pm`.
227 *
228 * This allows a pointer-to-member to be transformed into a function object
229 * that can be called with an object expression as its first argument.
230 *
231 * For a pointer-to-data-member the result must be called with exactly one
232 * argument, the object expression that would be used as the first operand
233 * in a `obj.*memptr` or `objp->*memptr` expression.
234 *
235 * For a pointer-to-member-function the result must be called with an object
236 * expression and any additional arguments to pass to the member function,
237 * as in an expression like `(obj.*memfun)(args...)` or
238 * `(objp->*memfun)(args...)`.
239 *
240 * The object expression can be a pointer, reference, `reference_wrapper`,
241 * or smart pointer, and the call wrapper will dereference it as needed
242 * to apply the pointer-to-member.
243 *
244 * @ingroup functors
245 * @since C++11
246 */
247 template<typename _Tp, typename _Class>
248 _GLIBCXX20_CONSTEXPR
249 inline _Mem_fn<_Tp _Class::*>
250 mem_fn(_Tp _Class::* __pm) noexcept
251 {
252 return _Mem_fn<_Tp _Class::*>(__pm);
253 }
254
255 /**
256 * @brief Trait that identifies a bind expression.
257 *
258 * Determines if the given type `_Tp` is a function object that
259 * should be treated as a subexpression when evaluating calls to
260 * function objects returned by `std::bind`.
261 *
262 * C++11 [func.bind.isbind].
263 * @ingroup binders
264 * @since C++11
265 */
266 template<typename _Tp>
267 struct is_bind_expression
268 : public false_type { };
269
270 /**
271 * @brief Determines if the given type _Tp is a placeholder in a
272 * bind() expression and, if so, which placeholder it is.
273 *
274 * C++11 [func.bind.isplace].
275 * @ingroup binders
276 * @since C++11
277 */
278 template<typename _Tp>
279 struct is_placeholder
280 : public integral_constant<int, 0>
281 { };
282
283#if __cplusplus > 201402L
284 template <typename _Tp> inline constexpr bool is_bind_expression_v
285 = is_bind_expression<_Tp>::value;
286 template <typename _Tp> inline constexpr int is_placeholder_v
287 = is_placeholder<_Tp>::value;
288#endif // C++17
289
290 /** @namespace std::placeholders
291 * @brief ISO C++ 2011 namespace for std::bind placeholders.
292 * @ingroup binders
293 * @since C++11
294 */
295 namespace placeholders
296 {
297 /* Define a large number of placeholders. There is no way to
298 * simplify this with variadic templates, because we're introducing
299 * unique names for each.
300 */
301#if __cpp_inline_variables
302# define _GLIBCXX_PLACEHOLDER inline
303#else
304# define _GLIBCXX_PLACEHOLDER extern
305#endif
306
307 _GLIBCXX_PLACEHOLDER const _Placeholder<1> _1;
308 _GLIBCXX_PLACEHOLDER const _Placeholder<2> _2;
309 _GLIBCXX_PLACEHOLDER const _Placeholder<3> _3;
310 _GLIBCXX_PLACEHOLDER const _Placeholder<4> _4;
311 _GLIBCXX_PLACEHOLDER const _Placeholder<5> _5;
312 _GLIBCXX_PLACEHOLDER const _Placeholder<6> _6;
313 _GLIBCXX_PLACEHOLDER const _Placeholder<7> _7;
314 _GLIBCXX_PLACEHOLDER const _Placeholder<8> _8;
315 _GLIBCXX_PLACEHOLDER const _Placeholder<9> _9;
316 _GLIBCXX_PLACEHOLDER const _Placeholder<10> _10;
317 _GLIBCXX_PLACEHOLDER const _Placeholder<11> _11;
318 _GLIBCXX_PLACEHOLDER const _Placeholder<12> _12;
319 _GLIBCXX_PLACEHOLDER const _Placeholder<13> _13;
320 _GLIBCXX_PLACEHOLDER const _Placeholder<14> _14;
321 _GLIBCXX_PLACEHOLDER const _Placeholder<15> _15;
322 _GLIBCXX_PLACEHOLDER const _Placeholder<16> _16;
323 _GLIBCXX_PLACEHOLDER const _Placeholder<17> _17;
324 _GLIBCXX_PLACEHOLDER const _Placeholder<18> _18;
325 _GLIBCXX_PLACEHOLDER const _Placeholder<19> _19;
326 _GLIBCXX_PLACEHOLDER const _Placeholder<20> _20;
327 _GLIBCXX_PLACEHOLDER const _Placeholder<21> _21;
328 _GLIBCXX_PLACEHOLDER const _Placeholder<22> _22;
329 _GLIBCXX_PLACEHOLDER const _Placeholder<23> _23;
330 _GLIBCXX_PLACEHOLDER const _Placeholder<24> _24;
331 _GLIBCXX_PLACEHOLDER const _Placeholder<25> _25;
332 _GLIBCXX_PLACEHOLDER const _Placeholder<26> _26;
333 _GLIBCXX_PLACEHOLDER const _Placeholder<27> _27;
334 _GLIBCXX_PLACEHOLDER const _Placeholder<28> _28;
335 _GLIBCXX_PLACEHOLDER const _Placeholder<29> _29;
336
337#undef _GLIBCXX_PLACEHOLDER
338 }
339
340 /**
341 * Partial specialization of is_placeholder that provides the placeholder
342 * number for the placeholder objects defined by libstdc++.
343 * @ingroup binders
344 * @since C++11
345 */
346 template<int _Num>
347 struct is_placeholder<_Placeholder<_Num> >
348 : public integral_constant<int, _Num>
349 { };
350
351 template<int _Num>
352 struct is_placeholder<const _Placeholder<_Num> >
353 : public integral_constant<int, _Num>
354 { };
355
356 /// @cond undocumented
357
358 // Like tuple_element_t but SFINAE-friendly.
359 template<std::size_t __i, typename _Tuple>
360 using _Safe_tuple_element_t
361 = typename enable_if<(__i < tuple_size<_Tuple>::value),
362 tuple_element<__i, _Tuple>>::type::type;
363
364 /**
365 * Maps an argument to bind() into an actual argument to the bound
366 * function object [func.bind.bind]/10. Only the first parameter should
367 * be specified: the rest are used to determine among the various
368 * implementations. Note that, although this class is a function
369 * object, it isn't entirely normal because it takes only two
370 * parameters regardless of the number of parameters passed to the
371 * bind expression. The first parameter is the bound argument and
372 * the second parameter is a tuple containing references to the
373 * rest of the arguments.
374 */
375 template<typename _Arg,
376 bool _IsBindExp = is_bind_expression<_Arg>::value,
377 bool _IsPlaceholder = (is_placeholder<_Arg>::value > 0)>
378 class _Mu;
379
380 /**
381 * If the argument is reference_wrapper<_Tp>, returns the
382 * underlying reference.
383 * C++11 [func.bind.bind] p10 bullet 1.
384 */
385 template<typename _Tp>
386 class _Mu<reference_wrapper<_Tp>, false, false>
387 {
388 public:
389 /* Note: This won't actually work for const volatile
390 * reference_wrappers, because reference_wrapper::get() is const
391 * but not volatile-qualified. This might be a defect in the TR.
392 */
393 template<typename _CVRef, typename _Tuple>
394 _GLIBCXX20_CONSTEXPR
395 _Tp&
396 operator()(_CVRef& __arg, _Tuple&) const volatile
397 { return __arg.get(); }
398 };
399
400 /**
401 * If the argument is a bind expression, we invoke the underlying
402 * function object with the same cv-qualifiers as we are given and
403 * pass along all of our arguments (unwrapped).
404 * C++11 [func.bind.bind] p10 bullet 2.
405 */
406 template<typename _Arg>
407 class _Mu<_Arg, true, false>
408 {
409 public:
410 template<typename _CVArg, typename... _Args>
411 _GLIBCXX20_CONSTEXPR
412 auto
413 operator()(_CVArg& __arg,
414 tuple<_Args...>& __tuple) const volatile
415 -> decltype(__arg(declval<_Args>()...))
416 {
417 // Construct an index tuple and forward to __call
418 typedef typename _Build_index_tuple<sizeof...(_Args)>::__type
419 _Indexes;
420 return this->__call(__arg, __tuple, _Indexes());
421 }
422
423 private:
424 // Invokes the underlying function object __arg by unpacking all
425 // of the arguments in the tuple.
426 template<typename _CVArg, typename... _Args, std::size_t... _Indexes>
427 _GLIBCXX20_CONSTEXPR
428 auto
429 __call(_CVArg& __arg, tuple<_Args...>& __tuple,
430 const _Index_tuple<_Indexes...>&) const volatile
431 -> decltype(__arg(declval<_Args>()...))
432 {
433 return __arg(std::get<_Indexes>(std::move(__tuple))...);
434 }
435 };
436
437 /**
438 * If the argument is a placeholder for the Nth argument, returns
439 * a reference to the Nth argument to the bind function object.
440 * C++11 [func.bind.bind] p10 bullet 3.
441 */
442 template<typename _Arg>
443 class _Mu<_Arg, false, true>
444 {
445 public:
446 template<typename _Tuple>
447 _GLIBCXX20_CONSTEXPR
448 _Safe_tuple_element_t<(is_placeholder<_Arg>::value - 1), _Tuple>&&
449 operator()(const volatile _Arg&, _Tuple& __tuple) const volatile
450 {
451 return
452 ::std::get<(is_placeholder<_Arg>::value - 1)>(std::move(__tuple));
453 }
454 };
455
456 /**
457 * If the argument is just a value, returns a reference to that
458 * value. The cv-qualifiers on the reference are determined by the caller.
459 * C++11 [func.bind.bind] p10 bullet 4.
460 */
461 template<typename _Arg>
462 class _Mu<_Arg, false, false>
463 {
464 public:
465 template<typename _CVArg, typename _Tuple>
466 _GLIBCXX20_CONSTEXPR
467 _CVArg&&
468 operator()(_CVArg&& __arg, _Tuple&) const volatile
469 { return std::forward<_CVArg>(__arg); }
470 };
471
472 // std::get<I> for volatile-qualified tuples
473 template<std::size_t _Ind, typename... _Tp>
474 inline auto
475 __volget(volatile tuple<_Tp...>& __tuple)
476 -> __tuple_element_t<_Ind, tuple<_Tp...>> volatile&
477 { return std::get<_Ind>(const_cast<tuple<_Tp...>&>(__tuple)); }
478
479 // std::get<I> for const-volatile-qualified tuples
480 template<std::size_t _Ind, typename... _Tp>
481 inline auto
482 __volget(const volatile tuple<_Tp...>& __tuple)
483 -> __tuple_element_t<_Ind, tuple<_Tp...>> const volatile&
484 { return std::get<_Ind>(const_cast<const tuple<_Tp...>&>(__tuple)); }
485
486 /// @endcond
487
488#if __cplusplus == 201703L && _GLIBCXX_USE_DEPRECATED
489# define _GLIBCXX_VOLATILE_BIND
490// _GLIBCXX_RESOLVE_LIB_DEFECTS
491// 2487. bind() should be const-overloaded, not cv-overloaded
492# define _GLIBCXX_DEPR_BIND \
493 [[deprecated("std::bind does not support volatile in C++17")]]
494#elif __cplusplus < 201703L
495# define _GLIBCXX_VOLATILE_BIND
496# define _GLIBCXX_DEPR_BIND
497#endif
498
499#if _GLIBCXX_EXPLICIT_THIS_PARAMETER
500 // Return a _Up that has the same cv-quals as _Tp.
501 template<typename _Tp, typename _Up> // _Up should be cv-unqualified
502 struct __cv_like
503 { using type = _Up; };
504
505 template<typename _Tp, typename _Up>
506 struct __cv_like<const _Tp, _Up>
507 { using type = const _Up; };
508
509 template<typename _Tp, typename _Up>
510 struct __cv_like<volatile _Tp, _Up>
511 { using type = volatile _Up; };
512
513 template<typename _Tp, typename _Up>
514 struct __cv_like<const volatile _Tp, _Up>
515 { using type = const volatile _Up; };
516
517 template<typename _Tp, typename _Up>
518 using __cv_like_t = typename __cv_like<_Tp, _Up>::type;
519#endif
520
521 /// Type of the function object returned from bind().
522 template<typename _Signature>
523 class _Bind;
524
525 template<typename _Functor, typename... _Bound_args>
526 class _Bind<_Functor(_Bound_args...)>
527 : public _Weak_result_type<_Functor>
528 {
529 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
530 _Bound_indexes;
531
532 _Functor _M_f;
533 tuple<_Bound_args...> _M_bound_args;
534
535 // Call unqualified
536 template<typename _Result, typename... _Args, std::size_t... _Indexes>
537 _GLIBCXX20_CONSTEXPR
538 _Result
539 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>)
540 {
541 return std::__invoke(_M_f,
542 _Mu<_Bound_args>()(std::get<_Indexes>(_M_bound_args), __args)...
543 );
544 }
545
546 // Call as const
547 template<typename _Result, typename... _Args, std::size_t... _Indexes>
548 _GLIBCXX20_CONSTEXPR
549 _Result
550 __call_c(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>) const
551 {
552 return std::__invoke(_M_f,
553 _Mu<_Bound_args>()(std::get<_Indexes>(_M_bound_args), __args)...
554 );
555 }
556
557#ifdef _GLIBCXX_VOLATILE_BIND
558 // Call as volatile
559 template<typename _Result, typename... _Args, std::size_t... _Indexes>
560 _Result
561 __call_v(tuple<_Args...>&& __args,
562 _Index_tuple<_Indexes...>) volatile
563 {
564 return std::__invoke(_M_f,
565 _Mu<_Bound_args>()(__volget<_Indexes>(_M_bound_args), __args)...
566 );
567 }
568
569 // Call as const volatile
570 template<typename _Result, typename... _Args, std::size_t... _Indexes>
571 _Result
572 __call_c_v(tuple<_Args...>&& __args,
573 _Index_tuple<_Indexes...>) const volatile
574 {
575 return std::__invoke(_M_f,
576 _Mu<_Bound_args>()(__volget<_Indexes>(_M_bound_args), __args)...
577 );
578 }
579#endif // volatile
580
581 template<typename _BoundArg, typename _CallArgs>
582 using _Mu_type = decltype(
583 _Mu<typename remove_cv<_BoundArg>::type>()(
584 std::declval<_BoundArg&>(), std::declval<_CallArgs&>()) );
585
586 template<typename _Fn, typename _CallArgs, typename... _BArgs>
587 using _Res_type_impl
588 = __invoke_result_t<_Fn&, _Mu_type<_BArgs, _CallArgs>&&...>;
589
590#if !_GLIBCXX_EXPLICIT_THIS_PARAMETER
591 template<typename _CallArgs>
592 using _Res_type = _Res_type_impl<_Functor, _CallArgs, _Bound_args...>;
593
594 template<typename _CallArgs>
595 using __dependent = typename
596 enable_if<bool(tuple_size<_CallArgs>::value+1), _Functor>::type;
597
598 template<typename _CallArgs, template<class> class __cv_quals>
599 using _Res_type_cv = _Res_type_impl<
600 typename __cv_quals<__dependent<_CallArgs>>::type,
601 _CallArgs,
602 typename __cv_quals<_Bound_args>::type...>;
603#endif
604
605 public:
606 template<typename... _Args>
607 explicit _GLIBCXX20_CONSTEXPR
608 _Bind(const _Functor& __f, _Args&&... __args)
609 : _M_f(__f), _M_bound_args(std::forward<_Args>(__args)...)
610 { }
611
612 template<typename... _Args>
613 explicit _GLIBCXX20_CONSTEXPR
614 _Bind(_Functor&& __f, _Args&&... __args)
615 : _M_f(std::move(__f)), _M_bound_args(std::forward<_Args>(__args)...)
616 { }
617
618 _Bind(const _Bind&) = default;
619 _Bind(_Bind&&) = default;
620
621#if _GLIBCXX_EXPLICIT_THIS_PARAMETER
622# pragma GCC diagnostic push
623# pragma GCC diagnostic ignored "-Wc++17-extensions" // if constexpr
624# pragma GCC diagnostic ignored "-Wc++23-extensions" // deducing this
625 template<typename... _Args,
626 typename _Self,
627 typename _Self_nonref = typename remove_reference<_Self>::type,
628 __enable_if_t<!is_volatile<_Self_nonref>::value, int> = 0,
629 typename _Result
630 = _Res_type_impl<__cv_like_t<_Self_nonref, _Functor>,
631 tuple<_Args...>,
632 __cv_like_t<_Self_nonref, _Bound_args>...>>
633 _GLIBCXX20_CONSTEXPR
634 _Result
635 operator()(this _Self&& __self, _Args&&... __args)
636 {
637 using _Bind_ref = __cv_like_t<_Self_nonref, _Bind>&;
638 if constexpr (is_const<_Self_nonref>::value)
639 return _Bind_ref(__self)
640 .template __call_c<_Result>(std::forward_as_tuple
641 (std::forward<_Args>(__args)...),
642 _Bound_indexes());
643 else
644 return _Bind_ref(__self)
645 .template __call<_Result>(std::forward_as_tuple
646 (std::forward<_Args>(__args)...),
647 _Bound_indexes());
648 }
649
650# if defined(_GLIBCXX_VOLATILE_BIND)
651 template<typename... _Args,
652 typename _Self,
653 typename _Self_nonref = typename remove_reference<_Self>::type,
654 __enable_if_t<is_volatile<_Self_nonref>::value, int> = 0,
655 typename _Result
656 = _Res_type_impl<__cv_like_t<_Self_nonref, _Functor>,
657 tuple<_Args...>,
658 __cv_like_t<_Self_nonref, _Bound_args>...>>
659 _GLIBCXX_DEPR_BIND
660 _Result
661 operator()(this _Self&& __self, _Args&&... __args)
662 {
663 using _Bind_ref = __cv_like_t<_Self_nonref, _Bind>&;
664 if constexpr (is_const<_Self_nonref>::value)
665 return _Bind_ref(__self)
666 .template __call_c_v<_Result>(std::forward_as_tuple
667 (std::forward<_Args>(__args)...),
668 _Bound_indexes());
669 else
670 return _Bind_ref(__self)
671 .template __call_v<_Result>(std::forward_as_tuple
672 (std::forward<_Args>(__args)...),
673 _Bound_indexes());
674 }
675# endif
676# pragma GCC diagnostic pop
677#else
678 // Call unqualified
679 template<typename... _Args,
680 typename _Result = _Res_type<tuple<_Args...>>>
681 _GLIBCXX20_CONSTEXPR
682 _Result
683 operator()(_Args&&... __args)
684 {
685 return this->__call<_Result>(
686 std::forward_as_tuple(std::forward<_Args>(__args)...),
687 _Bound_indexes());
688 }
689
690 // Call as const
691 template<typename... _Args,
692 typename _Result = _Res_type_cv<tuple<_Args...>, add_const>>
693 _GLIBCXX20_CONSTEXPR
694 _Result
695 operator()(_Args&&... __args) const
696 {
697 return this->__call_c<_Result>(
698 std::forward_as_tuple(std::forward<_Args>(__args)...),
699 _Bound_indexes());
700 }
701
702#ifdef _GLIBCXX_VOLATILE_BIND
703 // Call as volatile
704 template<typename... _Args,
705 typename _Result = _Res_type_cv<tuple<_Args...>, add_volatile>>
706 _GLIBCXX_DEPR_BIND
707 _Result
708 operator()(_Args&&... __args) volatile
709 {
710 return this->__call_v<_Result>(
711 std::forward_as_tuple(std::forward<_Args>(__args)...),
712 _Bound_indexes());
713 }
714
715 // Call as const volatile
716 template<typename... _Args,
717 typename _Result = _Res_type_cv<tuple<_Args...>, add_cv>>
718 _GLIBCXX_DEPR_BIND
719 _Result
720 operator()(_Args&&... __args) const volatile
721 {
722 return this->__call_c_v<_Result>(
723 std::forward_as_tuple(std::forward<_Args>(__args)...),
724 _Bound_indexes());
725 }
726#endif // volatile
727#endif
728 };
729
730 /// Type of the function object returned from bind<R>().
731 template<typename _Result, typename _Signature>
732 class _Bind_result;
733
734 template<typename _Result, typename _Functor, typename... _Bound_args>
735 class _Bind_result<_Result, _Functor(_Bound_args...)>
736 {
737 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
738 _Bound_indexes;
739
740 _Functor _M_f;
741 tuple<_Bound_args...> _M_bound_args;
742
743 // Call unqualified
744 template<typename _Res, typename... _Args, std::size_t... _Indexes>
745 _GLIBCXX20_CONSTEXPR
746 _Res
747 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>)
748 {
749 return std::__invoke_r<_Res>(_M_f, _Mu<_Bound_args>()
750 (std::get<_Indexes>(_M_bound_args), __args)...);
751 }
752
753 // Call as const
754 template<typename _Res, typename... _Args, std::size_t... _Indexes>
755 _GLIBCXX20_CONSTEXPR
756 _Res
757 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>) const
758 {
759 return std::__invoke_r<_Res>(_M_f, _Mu<_Bound_args>()
760 (std::get<_Indexes>(_M_bound_args), __args)...);
761 }
762
763#ifdef _GLIBCXX_VOLATILE_BIND
764 // Call as volatile
765 template<typename _Res, typename... _Args, std::size_t... _Indexes>
766 _Res
767 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>) volatile
768 {
769 return std::__invoke_r<_Res>(_M_f, _Mu<_Bound_args>()
770 (__volget<_Indexes>(_M_bound_args), __args)...);
771 }
772
773 // Call as const volatile
774 template<typename _Res, typename... _Args, std::size_t... _Indexes>
775 _Res
776 __call(tuple<_Args...>&& __args,
777 _Index_tuple<_Indexes...>) const volatile
778 {
779 return std::__invoke_r<_Res>(_M_f, _Mu<_Bound_args>()
780 (__volget<_Indexes>(_M_bound_args), __args)...);
781 }
782#endif // volatile
783
784 public:
785 typedef _Result result_type;
786
787 template<typename... _Args>
788 explicit _GLIBCXX20_CONSTEXPR
789 _Bind_result(const _Functor& __f, _Args&&... __args)
790 : _M_f(__f), _M_bound_args(std::forward<_Args>(__args)...)
791 { }
792
793 template<typename... _Args>
794 explicit _GLIBCXX20_CONSTEXPR
795 _Bind_result(_Functor&& __f, _Args&&... __args)
796 : _M_f(std::move(__f)), _M_bound_args(std::forward<_Args>(__args)...)
797 { }
798
799 _Bind_result(const _Bind_result&) = default;
800 _Bind_result(_Bind_result&&) = default;
801
802 // Call unqualified
803 template<typename... _Args>
804 _GLIBCXX20_CONSTEXPR
805 result_type
806 operator()(_Args&&... __args)
807 {
808 return this->__call<_Result>(
809 std::forward_as_tuple(std::forward<_Args>(__args)...),
810 _Bound_indexes());
811 }
812
813 // Call as const
814 template<typename... _Args>
815 _GLIBCXX20_CONSTEXPR
816 result_type
817 operator()(_Args&&... __args) const
818 {
819 return this->__call<_Result>(
820 std::forward_as_tuple(std::forward<_Args>(__args)...),
821 _Bound_indexes());
822 }
823
824#ifdef _GLIBCXX_VOLATILE_BIND
825 // Call as volatile
826 template<typename... _Args>
827 _GLIBCXX_DEPR_BIND
828 result_type
829 operator()(_Args&&... __args) volatile
830 {
831 return this->__call<_Result>(
832 std::forward_as_tuple(std::forward<_Args>(__args)...),
833 _Bound_indexes());
834 }
835
836 // Call as const volatile
837 template<typename... _Args>
838 _GLIBCXX_DEPR_BIND
839 result_type
840 operator()(_Args&&... __args) const volatile
841 {
842 return this->__call<_Result>(
843 std::forward_as_tuple(std::forward<_Args>(__args)...),
844 _Bound_indexes());
845 }
846#else
847 template<typename... _Args>
848 void operator()(_Args&&...) const volatile = delete;
849#endif // volatile
850 };
851
852#undef _GLIBCXX_VOLATILE_BIND
853#undef _GLIBCXX_DEPR_BIND
854
855 /**
856 * @brief Class template _Bind is always a bind expression.
857 * @ingroup binders
858 */
859 template<typename _Signature>
860 struct is_bind_expression<_Bind<_Signature> >
861 : public true_type { };
862
863 /**
864 * @brief Class template _Bind is always a bind expression.
865 * @ingroup binders
866 */
867 template<typename _Signature>
868 struct is_bind_expression<const _Bind<_Signature> >
869 : public true_type { };
870
871 /**
872 * @brief Class template _Bind is always a bind expression.
873 * @ingroup binders
874 */
875 template<typename _Signature>
876 struct is_bind_expression<volatile _Bind<_Signature> >
877 : public true_type { };
878
879 /**
880 * @brief Class template _Bind is always a bind expression.
881 * @ingroup binders
882 */
883 template<typename _Signature>
884 struct is_bind_expression<const volatile _Bind<_Signature>>
885 : public true_type { };
886
887 /**
888 * @brief Class template _Bind_result is always a bind expression.
889 * @ingroup binders
890 */
891 template<typename _Result, typename _Signature>
892 struct is_bind_expression<_Bind_result<_Result, _Signature>>
893 : public true_type { };
894
895 /**
896 * @brief Class template _Bind_result is always a bind expression.
897 * @ingroup binders
898 */
899 template<typename _Result, typename _Signature>
900 struct is_bind_expression<const _Bind_result<_Result, _Signature>>
901 : public true_type { };
902
903 /**
904 * @brief Class template _Bind_result is always a bind expression.
905 * @ingroup binders
906 */
907 template<typename _Result, typename _Signature>
908 struct is_bind_expression<volatile _Bind_result<_Result, _Signature>>
909 : public true_type { };
910
911 /**
912 * @brief Class template _Bind_result is always a bind expression.
913 * @ingroup binders
914 */
915 template<typename _Result, typename _Signature>
916 struct is_bind_expression<const volatile _Bind_result<_Result, _Signature>>
917 : public true_type { };
918
919 template<typename _Func, typename... _BoundArgs>
920 struct _Bind_check_arity { };
921
922 template<typename _Ret, typename... _Args, typename... _BoundArgs>
923 struct _Bind_check_arity<_Ret (*)(_Args...), _BoundArgs...>
924 {
925 static_assert(sizeof...(_BoundArgs) == sizeof...(_Args),
926 "Wrong number of arguments for function");
927 };
928
929 template<typename _Ret, typename... _Args, typename... _BoundArgs>
930 struct _Bind_check_arity<_Ret (*)(_Args..., ...), _BoundArgs...>
931 {
932 static_assert(sizeof...(_BoundArgs) >= sizeof...(_Args),
933 "Wrong number of arguments for function");
934 };
935
936 template<typename _Tp, typename _Class, typename... _BoundArgs>
937 struct _Bind_check_arity<_Tp _Class::*, _BoundArgs...>
938 {
939 using _Arity = typename _Mem_fn<_Tp _Class::*>::_Arity;
940 using _Varargs = typename _Mem_fn<_Tp _Class::*>::_Varargs;
941 static_assert(_Varargs::value
942 ? sizeof...(_BoundArgs) >= _Arity::value + 1
943 : sizeof...(_BoundArgs) == _Arity::value + 1,
944 "Wrong number of arguments for pointer-to-member");
945 };
946
947 // Trait type used to remove std::bind() from overload set via SFINAE
948 // when first argument has integer type, so that std::bind() will
949 // not be a better match than ::bind() from the BSD Sockets API.
950 template<typename _Tp, typename _Tp2 = typename decay<_Tp>::type>
951 using __is_socketlike = __or_<is_integral<_Tp2>, is_enum<_Tp2>>;
952
953 template<bool _SocketLike, typename _Func, typename... _BoundArgs>
954 struct _Bind_helper
955 : _Bind_check_arity<typename decay<_Func>::type, _BoundArgs...>
956 {
957 typedef typename decay<_Func>::type __func_type;
958 typedef _Bind<__func_type(typename decay<_BoundArgs>::type...)> type;
959 };
960
961 // Partial specialization for is_socketlike == true, does not define
962 // nested type so std::bind() will not participate in overload resolution
963 // when the first argument might be a socket file descriptor.
964 template<typename _Func, typename... _BoundArgs>
965 struct _Bind_helper<true, _Func, _BoundArgs...>
966 { };
967
968 /**
969 * @brief Function template for std::bind.
970 * @ingroup binders
971 * @since C++11
972 */
973 template<typename _Func, typename... _BoundArgs>
974 inline _GLIBCXX20_CONSTEXPR typename
975 _Bind_helper<__is_socketlike<_Func>::value, _Func, _BoundArgs...>::type
976 bind(_Func&& __f, _BoundArgs&&... __args)
977 {
978 typedef _Bind_helper<false, _Func, _BoundArgs...> __helper_type;
979 return typename __helper_type::type(std::forward<_Func>(__f),
980 std::forward<_BoundArgs>(__args)...);
981 }
982
983 template<typename _Result, typename _Func, typename... _BoundArgs>
984 struct _Bindres_helper
985 : _Bind_check_arity<typename decay<_Func>::type, _BoundArgs...>
986 {
987 typedef typename decay<_Func>::type __functor_type;
988 typedef _Bind_result<_Result,
989 __functor_type(typename decay<_BoundArgs>::type...)>
990 type;
991 };
992
993 /**
994 * @brief Function template for std::bind<R>.
995 * @ingroup binders
996 * @since C++11
997 */
998 template<typename _Result, typename _Func, typename... _BoundArgs>
999 inline _GLIBCXX20_CONSTEXPR
1000 typename _Bindres_helper<_Result, _Func, _BoundArgs...>::type
1001 bind(_Func&& __f, _BoundArgs&&... __args)
1002 {
1003 typedef _Bindres_helper<_Result, _Func, _BoundArgs...> __helper_type;
1004 return typename __helper_type::type(std::forward<_Func>(__f),
1005 std::forward<_BoundArgs>(__args)...);
1006 }
1007
1008#if __cpp_lib_bind_front >= 202306L || __cpp_lib_bind_back >= 202306L
1009 template <auto __fn>
1010 struct _Bind_fn_t
1011 {
1012 using _Fn = const decltype(__fn)&;
1013 template <typename... _Args>
1014 requires is_invocable_v<_Fn, _Args...>
1015 constexpr static decltype(auto)
1016 operator()(_Args&&... __args)
1017 noexcept(is_nothrow_invocable_v<_Fn, _Args...>)
1018 { return std::invoke(__fn, std::forward<_Args>(__args)...); }
1019 };
1020#endif
1021
1022#ifdef __cpp_lib_bind_front // C++ >= 20
1023 /** Create call wrapper by partial application of arguments to function.
1024 *
1025 * The result of `std::bind_front(f, args...)` is a function object that
1026 * stores `f` and the bound arguments, `args...`. When that function
1027 * object is invoked with `call_args...` it returns the result of calling
1028 * `f(args..., call_args...)`.
1029 *
1030 * @since C++20
1031 */
1032 template<typename _Fn, typename... _Args>
1033 constexpr _Bind_front_t<_Fn, _Args...>
1034 bind_front(_Fn&& __fn, _Args&&... __args)
1035 noexcept(is_nothrow_constructible_v<_Bind_front_t<_Fn, _Args...>,
1036 int, _Fn, _Args...>)
1037 {
1038 return _Bind_front_t<_Fn, _Args...>(0, std::forward<_Fn>(__fn),
1039 std::forward<_Args>(__args)...);
1040 }
1041
1042#if __cpp_lib_bind_front >= 202306L
1043 /** Create call wrapper by partial application of arguments to function.
1044 *
1045 * The result of `std::bind_front<fn>(bind_args...)` is a function object
1046 * that stores the bound arguments, `bind_args...`. When that function
1047 * object is invoked with `call_args...` it returns the result of calling
1048 * `fn(bind_args..., call_args...)`.
1049 *
1050 * @since C++26
1051 */
1052 template<auto __fn, typename... _BindArgs>
1053 constexpr decltype(auto)
1054 bind_front(_BindArgs&&... __bind_args)
1055 noexcept(__and_v<is_nothrow_constructible<_BindArgs>...>)
1056 {
1057 using _Fn = decltype(__fn);
1058 if constexpr (is_pointer_v<_Fn> || is_member_pointer_v<_Fn>)
1059 static_assert(__fn != nullptr);
1060
1061 if constexpr (sizeof...(_BindArgs) == 0)
1062 return _Bind_fn_t<__fn>{};
1063 else
1064 return _Bind_front_t<_Bind_fn_t<__fn>, _BindArgs...>(0,
1065 _Bind_fn_t<__fn>{}, std::forward<_BindArgs>(__bind_args)...);
1066 }
1067
1068#endif // __cpp_lib_bind_front // C++26
1069#endif // __cpp_lib_bind_front
1070
1071#ifdef __cpp_lib_bind_back // C++ >= 23
1072 /** Create call wrapper by partial application of arguments to function.
1073 *
1074 * The result of `std::bind_back(f, args...)` is a function object that
1075 * stores `f` and the bound arguments, `args...`. When that function
1076 * object is invoked with `call_args...` it returns the result of calling
1077 * `f(call_args..., args...)`.
1078 *
1079 * @since C++23
1080 */
1081 template<typename _Fn, typename... _Args>
1082 constexpr _Bind_back_t<_Fn, _Args...>
1083 bind_back(_Fn&& __fn, _Args&&... __args)
1084 noexcept(is_nothrow_constructible_v<_Bind_back_t<_Fn, _Args...>,
1085 int, _Fn, _Args...>)
1086 {
1087 return _Bind_back_t<_Fn, _Args...>(0, std::forward<_Fn>(__fn),
1088 std::forward<_Args>(__args)...);
1089 }
1090
1091#if __cpp_lib_bind_back >= 202306L
1092
1093 /** Create call wrapper by partial application of arguments to function.
1094 *
1095 * The result of `std::bind_back<fn>(bind_args...)` is a function object
1096 * that stores the arguments, `bind_args...`. When that function object
1097 * is invoked with `call_args...` it returns the result of calling
1098 * `fn(call_args..., bind_args...)`.
1099 *
1100 * @since C++26
1101 */
1102 template<auto __fn, typename... _BindArgs>
1103 constexpr decltype(auto)
1104 bind_back(_BindArgs&&... __bind_args)
1105 noexcept(__and_v<is_nothrow_constructible<_BindArgs>...>)
1106 {
1107 using _Fn = decltype(__fn);
1108 if constexpr (is_pointer_v<_Fn> || is_member_pointer_v<_Fn>)
1109 static_assert(__fn != nullptr);
1110
1111 if constexpr (sizeof...(_BindArgs) == 0)
1112 return _Bind_fn_t<__fn>{};
1113 else
1114 return _Bind_back_t<_Bind_fn_t<__fn>, _BindArgs...>(0,
1115 _Bind_fn_t<__fn>{}, std::forward<_BindArgs>(__bind_args)...);
1116 }
1117#endif // __cpp_lib_bind_back // C++26, nttp
1118#endif // __cpp_lib_bind_back
1119
1120#if __cplusplus >= 201402L
1121 /// Generalized negator.
1122 template<typename _Fn>
1123 class _Not_fn
1124 {
1125 template<typename _Fn2, typename... _Args>
1126 using __inv_res_t = typename __invoke_result<_Fn2, _Args...>::type;
1127
1128 template<typename _Tp>
1129 static decltype(!std::declval<_Tp>())
1130 _S_not() noexcept(noexcept(!std::declval<_Tp>()));
1131
1132 public:
1133 template<typename _Fn2>
1134 constexpr
1135 _Not_fn(_Fn2&& __fn, int)
1136 : _M_fn(std::forward<_Fn2>(__fn)) { }
1137
1138 _Not_fn(const _Not_fn& __fn) = default;
1139 _Not_fn(_Not_fn&& __fn) = default;
1140 ~_Not_fn() = default;
1141
1142#if _GLIBCXX_EXPLICIT_THIS_PARAMETER
1143# pragma GCC diagnostic push
1144# pragma GCC diagnostic ignored "-Wc++23-extensions" // deducing this
1145 template<typename _Self, typename... _Args>
1146 _GLIBCXX20_CONSTEXPR
1147 decltype(_S_not<__inv_res_t<__like_t<_Self, _Fn>, _Args...>>())
1148 operator()(this _Self&& __self, _Args&&... __args)
1149 noexcept(__is_nothrow_invocable<__like_t<_Self, _Fn>, _Args...>::value
1150 && noexcept(_S_not<__inv_res_t<__like_t<_Self, _Fn>, _Args...>>()))
1151 {
1152 return !std::__invoke(__like_t<_Self, _Not_fn>(__self)._M_fn,
1153 std::forward<_Args>(__args)...);
1154 }
1155# pragma GCC diagnostic pop
1156#else
1157 // Macro to define operator() with given cv-qualifiers ref-qualifiers,
1158 // forwarding _M_fn and the function arguments with the same qualifiers,
1159 // and deducing the return type and exception-specification.
1160#define _GLIBCXX_NOT_FN_CALL_OP( _QUALS ) \
1161 template<typename... _Args, \
1162 typename = enable_if_t<__is_invocable<_Fn _QUALS, _Args...>::value>> \
1163 _GLIBCXX20_CONSTEXPR \
1164 decltype(_S_not<__inv_res_t<_Fn _QUALS, _Args...>>()) \
1165 operator()(_Args&&... __args) _QUALS \
1166 noexcept(__is_nothrow_invocable<_Fn _QUALS, _Args...>::value \
1167 && noexcept(_S_not<__inv_res_t<_Fn _QUALS, _Args...>>())) \
1168 { \
1169 return !std::__invoke(std::forward< _Fn _QUALS >(_M_fn), \
1170 std::forward<_Args>(__args)...); \
1171 } \
1172 \
1173 template<typename... _Args, \
1174 typename = enable_if_t<!__is_invocable<_Fn _QUALS, _Args...>::value>> \
1175 void operator()(_Args&&... __args) _QUALS = delete;
1176
1177 _GLIBCXX_NOT_FN_CALL_OP( & )
1178 _GLIBCXX_NOT_FN_CALL_OP( const & )
1179 _GLIBCXX_NOT_FN_CALL_OP( && )
1180 _GLIBCXX_NOT_FN_CALL_OP( const && )
1181#undef _GLIBCXX_NOT_FN_CALL_OP
1182#endif
1183
1184 private:
1185 _Fn _M_fn;
1186 };
1187
1188 template<typename _Tp, typename _Pred>
1189 struct __is_byte_like : false_type { };
1190
1191 template<typename _Tp>
1192 struct __is_byte_like<_Tp, equal_to<_Tp>>
1193 : __bool_constant<sizeof(_Tp) == 1 && is_integral<_Tp>::value> { };
1194
1195 template<typename _Tp>
1196 struct __is_byte_like<_Tp, equal_to<void>>
1197 : __bool_constant<sizeof(_Tp) == 1 && is_integral<_Tp>::value> { };
1198
1199#if __cplusplus >= 201703L
1200 // Declare std::byte (full definition is in <cstddef>).
1201 enum class byte : unsigned char;
1202
1203 template<>
1204 struct __is_byte_like<byte, equal_to<byte>>
1205 : true_type { };
1206
1207 template<>
1208 struct __is_byte_like<byte, equal_to<void>>
1209 : true_type { };
1210#endif
1211
1212 // [func.not_fn] Function template not_fn
1213#ifdef __cpp_lib_not_fn // C++ >= 17
1214 /** Wrap a function object to create one that negates its result.
1215 *
1216 * The function template `std::not_fn` creates a "forwarding call wrapper",
1217 * which is a function object that wraps another function object and
1218 * when called, forwards its arguments to the wrapped function object.
1219 *
1220 * The result of invoking the wrapper is the negation (using `!`) of
1221 * the wrapped function object.
1222 *
1223 * @ingroup functors
1224 * @since C++17
1225 */
1226 template<typename _Fn>
1227 _GLIBCXX20_CONSTEXPR
1228 inline auto
1229 not_fn(_Fn&& __fn)
1230 noexcept(std::is_nothrow_constructible<std::decay_t<_Fn>, _Fn&&>::value)
1231 {
1232 return _Not_fn<std::decay_t<_Fn>>{std::forward<_Fn>(__fn), 0};
1233 }
1234
1235#if __cpp_lib_not_fn >= 202306L
1236 /** Wrap a function type to create a function object that negates its result.
1237 *
1238 * The function template `std::not_fn` creates a "forwarding call wrapper",
1239 * which is a function object that when called forwards its arguments to
1240 * its invocable template argument.
1241 *
1242 * The result of invoking the wrapper is the negation (using `!`) of
1243 * the wrapped function object.
1244 *
1245 * @ingroup functors
1246 * @since C++26
1247 */
1248 template<auto __fn>
1249 constexpr decltype(auto)
1250 not_fn() noexcept
1251 {
1252 using _Fn = decltype(__fn);
1253 if constexpr (is_pointer_v<_Fn> || is_member_pointer_v<_Fn>)
1254 static_assert(__fn != nullptr);
1255 return []<typename... _Args>(_Args&&... __args) static
1256 noexcept(noexcept(
1257 !std::invoke(__fn, std::forward<_Args>(__args)...) ))
1258 -> decltype(auto)
1259 requires requires {
1260 !std::invoke(__fn, std::forward<_Args>(__args)...); }
1261 { return !std::invoke(__fn, std::forward<_Args>(__args)...); };
1262 };
1263#endif // __cpp_lib_not_fn >= 202306L
1264#endif // __cpp_lib_not_fn
1265
1266#if __cplusplus >= 201703L
1267 // Searchers
1268
1269 template<typename _ForwardIterator1, typename _BinaryPredicate = equal_to<>>
1270 class default_searcher
1271 {
1272 public:
1273 _GLIBCXX20_CONSTEXPR
1274 default_searcher(_ForwardIterator1 __pat_first,
1275 _ForwardIterator1 __pat_last,
1276 _BinaryPredicate __pred = _BinaryPredicate())
1277 : _M_m(__pat_first, __pat_last, std::move(__pred))
1278 { }
1279
1280 template<typename _ForwardIterator2>
1281 _GLIBCXX20_CONSTEXPR
1282 pair<_ForwardIterator2, _ForwardIterator2>
1283 operator()(_ForwardIterator2 __first, _ForwardIterator2 __last) const
1284 {
1285 _ForwardIterator2 __first_ret =
1286 std::search(__first, __last, std::get<0>(_M_m), std::get<1>(_M_m),
1287 std::get<2>(_M_m));
1288 auto __ret = std::make_pair(__first_ret, __first_ret);
1289 if (__ret.first != __last)
1290 std::advance(__ret.second, std::distance(std::get<0>(_M_m),
1291 std::get<1>(_M_m)));
1292 return __ret;
1293 }
1294
1295 private:
1296 tuple<_ForwardIterator1, _ForwardIterator1, _BinaryPredicate> _M_m;
1297 };
1298
1299#ifdef __cpp_lib_boyer_moore_searcher // C++ >= 17 && HOSTED
1300
1301 template<typename _Key, typename _Tp, typename _Hash, typename _Pred>
1302 struct __boyer_moore_map_base
1303 {
1304 template<typename _RAIter>
1305 __boyer_moore_map_base(_RAIter __pat, size_t __patlen,
1306 _Hash&& __hf, _Pred&& __pred)
1307 : _M_bad_char{ __patlen, std::move(__hf), std::move(__pred) }
1308 {
1309 if (__patlen > 0)
1310 for (__diff_type __i = 0; __i < __patlen - 1; ++__i)
1311 _M_bad_char[__pat[__i]] = __patlen - 1 - __i;
1312 }
1313
1314 using __diff_type = _Tp;
1315
1316 __diff_type
1317 _M_lookup(_Key __key, __diff_type __not_found) const
1318 {
1319 auto __iter = _M_bad_char.find(__key);
1320 if (__iter == _M_bad_char.end())
1321 return __not_found;
1322 return __iter->second;
1323 }
1324
1325 _Pred
1326 _M_pred() const { return _M_bad_char.key_eq(); }
1327
1328 _GLIBCXX_STD_C::unordered_map<_Key, _Tp, _Hash, _Pred> _M_bad_char;
1329 };
1330
1331 template<typename _Tp, size_t _Len, typename _Pred>
1332 struct __boyer_moore_array_base
1333 {
1334 template<typename _RAIter, typename _Unused>
1335 __boyer_moore_array_base(_RAIter __pat, size_t __patlen,
1336 _Unused&&, _Pred&& __pred)
1337 : _M_bad_char{ array<_Tp, _Len>{}, std::move(__pred) }
1338 {
1339 std::get<0>(_M_bad_char).fill(__patlen);
1340 if (__patlen > 0)
1341 for (__diff_type __i = 0; __i < __patlen - 1; ++__i)
1342 {
1343 auto __ch = __pat[__i];
1344 using _UCh = make_unsigned_t<decltype(__ch)>;
1345 auto __uch = static_cast<_UCh>(__ch);
1346 std::get<0>(_M_bad_char)[__uch] = __patlen - 1 - __i;
1347 }
1348 }
1349
1350 using __diff_type = _Tp;
1351
1352 template<typename _Key>
1353 __diff_type
1354 _M_lookup(_Key __key, __diff_type __not_found) const
1355 {
1356 auto __ukey = static_cast<make_unsigned_t<_Key>>(__key);
1357 if (__ukey >= _Len)
1358 return __not_found;
1359 return std::get<0>(_M_bad_char)[__ukey];
1360 }
1361
1362 const _Pred&
1363 _M_pred() const { return std::get<1>(_M_bad_char); }
1364
1365 tuple<array<_Tp, _Len>, _Pred> _M_bad_char;
1366 };
1367
1368 // Use __boyer_moore_array_base when pattern consists of narrow characters
1369 // (or std::byte) and uses std::equal_to as the predicate.
1370 template<typename _RAIter, typename _Hash, typename _Pred,
1371 typename _Val = typename iterator_traits<_RAIter>::value_type,
1372 typename _Diff = typename iterator_traits<_RAIter>::difference_type>
1373 using __boyer_moore_base_t
1374 = __conditional_t<__is_byte_like<_Val, _Pred>::value,
1375 __boyer_moore_array_base<_Diff, 256, _Pred>,
1376 __boyer_moore_map_base<_Val, _Diff, _Hash, _Pred>>;
1377
1378 template<typename _RAIter, typename _Hash
1379 = hash<typename iterator_traits<_RAIter>::value_type>,
1380 typename _BinaryPredicate = equal_to<>>
1381 class boyer_moore_searcher
1382 : __boyer_moore_base_t<_RAIter, _Hash, _BinaryPredicate>
1383 {
1384 using _Base = __boyer_moore_base_t<_RAIter, _Hash, _BinaryPredicate>;
1385 using typename _Base::__diff_type;
1386
1387 public:
1388 boyer_moore_searcher(_RAIter __pat_first, _RAIter __pat_last,
1389 _Hash __hf = _Hash(),
1390 _BinaryPredicate __pred = _BinaryPredicate());
1391
1392 template<typename _RandomAccessIterator2>
1393 pair<_RandomAccessIterator2, _RandomAccessIterator2>
1394 operator()(_RandomAccessIterator2 __first,
1395 _RandomAccessIterator2 __last) const;
1396
1397 private:
1398 bool
1399 _M_is_prefix(_RAIter __word, __diff_type __len,
1400 __diff_type __pos)
1401 {
1402 const auto& __pred = this->_M_pred();
1403 __diff_type __suffixlen = __len - __pos;
1404 for (__diff_type __i = 0; __i < __suffixlen; ++__i)
1405 if (!__pred(__word[__i], __word[__pos + __i]))
1406 return false;
1407 return true;
1408 }
1409
1410 __diff_type
1411 _M_suffix_length(_RAIter __word, __diff_type __len,
1412 __diff_type __pos)
1413 {
1414 const auto& __pred = this->_M_pred();
1415 __diff_type __i = 0;
1416 while (__pred(__word[__pos - __i], __word[__len - 1 - __i])
1417 && __i < __pos)
1418 {
1419 ++__i;
1420 }
1421 return __i;
1422 }
1423
1424 template<typename _Tp>
1425 __diff_type
1426 _M_bad_char_shift(_Tp __c) const
1427 { return this->_M_lookup(__c, _M_pat_end - _M_pat); }
1428
1429 _RAIter _M_pat;
1430 _RAIter _M_pat_end;
1431 _GLIBCXX_STD_C::vector<__diff_type> _M_good_suffix;
1432 };
1433
1434 template<typename _RAIter, typename _Hash
1435 = hash<typename iterator_traits<_RAIter>::value_type>,
1436 typename _BinaryPredicate = equal_to<>>
1437 class boyer_moore_horspool_searcher
1438 : __boyer_moore_base_t<_RAIter, _Hash, _BinaryPredicate>
1439 {
1440 using _Base = __boyer_moore_base_t<_RAIter, _Hash, _BinaryPredicate>;
1441 using typename _Base::__diff_type;
1442
1443 public:
1444 boyer_moore_horspool_searcher(_RAIter __pat,
1445 _RAIter __pat_end,
1446 _Hash __hf = _Hash(),
1447 _BinaryPredicate __pred
1448 = _BinaryPredicate())
1449 : _Base(__pat, __pat_end - __pat, std::move(__hf), std::move(__pred)),
1450 _M_pat(__pat), _M_pat_end(__pat_end)
1451 { }
1452
1453 template<typename _RandomAccessIterator2>
1454 pair<_RandomAccessIterator2, _RandomAccessIterator2>
1455 operator()(_RandomAccessIterator2 __first,
1456 _RandomAccessIterator2 __last) const
1457 {
1458#ifdef __glibcxx_concepts // >= C++20
1459 // Value types must be the same for hash function and predicate
1460 // to give consistent results for lookup in the map.
1461 static_assert(is_same_v<iter_value_t<_RAIter>,
1462 iter_value_t<_RandomAccessIterator2>>);
1463#endif
1464 const auto& __pred = this->_M_pred();
1465 auto __patlen = _M_pat_end - _M_pat;
1466 if (__patlen == 0)
1467 return std::make_pair(__first, __first);
1468 auto __len = __last - __first;
1469 while (__len >= __patlen)
1470 {
1471 for (auto __scan = __patlen - 1;
1472 __pred(__first[__scan], _M_pat[__scan]); --__scan)
1473 if (__scan == 0)
1474 return std::make_pair(__first, __first + __patlen);
1475 auto __shift = _M_bad_char_shift(__first[__patlen - 1]);
1476 __len -= __shift;
1477 __first += __shift;
1478 }
1479 return std::make_pair(__last, __last);
1480 }
1481
1482 private:
1483 template<typename _Tp>
1484 __diff_type
1485 _M_bad_char_shift(_Tp __c) const
1486 { return this->_M_lookup(__c, _M_pat_end - _M_pat); }
1487
1488 _RAIter _M_pat;
1489 _RAIter _M_pat_end;
1490 };
1491
1492 template<typename _RAIter, typename _Hash, typename _BinaryPredicate>
1493 boyer_moore_searcher<_RAIter, _Hash, _BinaryPredicate>::
1494 boyer_moore_searcher(_RAIter __pat, _RAIter __pat_end,
1495 _Hash __hf, _BinaryPredicate __pred)
1496 : _Base(__pat, __pat_end - __pat, std::move(__hf), std::move(__pred)),
1497 _M_pat(__pat), _M_pat_end(__pat_end), _M_good_suffix(__pat_end - __pat)
1498 {
1499 auto __patlen = __pat_end - __pat;
1500 if (__patlen == 0)
1501 return;
1502 __diff_type __last_prefix = __patlen - 1;
1503 for (__diff_type __p = __patlen - 1; __p >= 0; --__p)
1504 {
1505 if (_M_is_prefix(__pat, __patlen, __p + 1))
1506 __last_prefix = __p + 1;
1507 _M_good_suffix[__p] = __last_prefix + (__patlen - 1 - __p);
1508 }
1509 for (__diff_type __p = 0; __p < __patlen - 1; ++__p)
1510 {
1511 auto __slen = _M_suffix_length(__pat, __patlen, __p);
1512 auto __pos = __patlen - 1 - __slen;
1513 if (!__pred(__pat[__p - __slen], __pat[__pos]))
1514 _M_good_suffix[__pos] = __patlen - 1 - __p + __slen;
1515 }
1516 }
1517
1518 template<typename _RAIter, typename _Hash, typename _BinaryPredicate>
1519 template<typename _RandomAccessIterator2>
1520 pair<_RandomAccessIterator2, _RandomAccessIterator2>
1521 boyer_moore_searcher<_RAIter, _Hash, _BinaryPredicate>::
1522 operator()(_RandomAccessIterator2 __first,
1523 _RandomAccessIterator2 __last) const
1524 {
1525#ifdef __glibcxx_concepts // >= C++20
1526 // Value types must be the same for hash function and predicate
1527 // to give consistent results for lookup in the map.
1528 static_assert(is_same_v<iter_value_t<_RAIter>,
1529 iter_value_t<_RandomAccessIterator2>>);
1530#endif
1531 auto __patlen = _M_pat_end - _M_pat;
1532 if (__patlen == 0)
1533 return std::make_pair(__first, __first);
1534 const auto& __pred = this->_M_pred();
1535 __diff_type __i = __patlen - 1;
1536 auto __stringlen = __last - __first;
1537 while (__i < __stringlen)
1538 {
1539 __diff_type __j = __patlen - 1;
1540 while (__j >= 0 && __pred(__first[__i], _M_pat[__j]))
1541 {
1542 --__i;
1543 --__j;
1544 }
1545 if (__j < 0)
1546 {
1547 const auto __match = __first + __diff_type(__i + 1);
1548 return std::make_pair(__match, __match + __patlen);
1549 }
1550 __i += std::max(_M_bad_char_shift(__first[__i]),
1551 _M_good_suffix[__j]);
1552 }
1553 return std::make_pair(__last, __last);
1554 }
1555#endif // __cpp_lib_boyer_moore_searcher
1556
1557#endif // C++17
1558#endif // C++14
1559#endif // C++11
1560
1561_GLIBCXX_END_NAMESPACE_VERSION
1562} // namespace std
1563
1564#endif // _GLIBCXX_FUNCTIONAL
compare
array
tuple
unordered_map
vector
functional_hash.h
invoke.h
ranges_cmp.h
refwrap.h
stl_algobase.h
stl_function.h
version.h
funcwrap.h
std_function.h
c++config.h
std::true_type
__bool_constant< true > true_type
The type used as a compile-time boolean with true value.
Definition type_traits:119
std::make_unsigned_t
typename make_unsigned< _Tp >::type make_unsigned_t
Alias template for make_unsigned.
Definition type_traits:2273
std::false_type
__bool_constant< false > false_type
The type used as a compile-time boolean with false value.
Definition type_traits:122
std::pair::pair
pair(_T1, _T2) -> pair< _T1, _T2 >
Two pairs are equal iff their members are equal.
std::make_pair
constexpr pair< typename __decay_and_strip< _T1 >::__type, typename __decay_and_strip< _T2 >::__type > make_pair(_T1 &&__x, _T2 &&__y)
A convenience wrapper for creating a pair from two objects.
Definition stl_pair.h:1169
std::declval
auto declval() noexcept -> decltype(__declval< _Tp >(0))
Definition type_traits:2741
std::forward_as_tuple
constexpr tuple< _Elements &&... > forward_as_tuple(_Elements &&... __args) noexcept
Create a tuple of lvalue or rvalue references to the arguments.
Definition tuple:2698
std::move
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Definition move.h:138
std::__invoke
constexpr __invoke_result< _Callable, _Args... >::type __invoke(_Callable &&__fn, _Args &&... __args) noexcept(__is_nothrow_invocable< _Callable, _Args... >::value)
Invoke a callable object.
Definition invoke.h:92
std::forward
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
Definition move.h:72
std::max
constexpr const _Tp & max(const _Tp &, const _Tp &)
This does what you think it does.
Definition stl_algobase.h:256
std::mem_fn
constexpr _Mem_fn< _Tp _Class::* > mem_fn(_Tp _Class::*__pm) noexcept
Returns a function object that forwards to the member pointer pointer pm.
Definition functional:250
std::bind
constexpr _Bind_helper< __is_socketlike< _Func >::value, _Func, _BoundArgs... >::type bind(_Func &&__f, _BoundArgs &&... __args)
Function template for std::bind.
Definition functional:976
std
ISO C++ entities toplevel namespace is std.
std::distance
constexpr iterator_traits< _InputIterator >::difference_type distance(_InputIterator __first, _InputIterator __last)
A generalization of pointer arithmetic.
Definition stl_iterator_base_funcs.h:177
std::advance
constexpr void advance(_InputIterator &__i, _Distance __n)
A generalization of pointer arithmetic.
Definition stl_iterator_base_funcs.h:270
std::experimental::fundamentals_v2::not_fn
auto not_fn(_Fn &&__fn) noexcept(std::is_nothrow_constructible< std::decay_t< _Fn >, _Fn && >::value)
[func.not_fn] Function template not_fn
Definition experimental/functional:373
std::placeholders
ISO C++ 2011 namespace for std::bind placeholders.
std::_Placeholder
The type of placeholder objects defined by libstdc++.
Definition functional:106
std::is_bind_expression
Trait that identifies a bind expression.
Definition functional:268
std::is_placeholder
Determines if the given type _Tp is a placeholder in a bind() expression and, if so,...
Definition functional:281
std::_Bind
Type of the function object returned from bind().
Definition functional:523
std::_Bind_result
Type of the function object returned from bind<R>().
Definition functional:732
std::_Not_fn
Generalized negator.
Definition functional:1124
std::tuple
Primary class template, tuple.
Definition tuple:795
std::integral_constant
integral_constant
Definition type_traits:96
std::is_member_function_pointer
is_member_function_pointer
Definition type_traits:722
std::is_enum
is_enum
Definition type_traits:746
std::equal_to
One of the comparison functors.
Definition stl_function.h:374
std::pair
Struct holding two objects (or references) of arbitrary type.
Definition stl_pair.h:307
binders.h