1 // Functor implementations -*- C++ -*-
3 // Copyright (C) 2001-2021 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/>.
28 * Hewlett-Packard Company
30 * Permission to use, copy, modify, distribute and sell this software
31 * and its documentation for any purpose is hereby granted without fee,
32 * provided that the above copyright notice appear in all copies and
33 * that both that copyright notice and this permission notice appear
34 * in supporting documentation. Hewlett-Packard Company makes no
35 * representations about the suitability of this software for any
36 * purpose. It is provided "as is" without express or implied warranty.
39 * Copyright (c) 1996-1998
40 * Silicon Graphics Computer Systems, Inc.
42 * Permission to use, copy, modify, distribute and sell this software
43 * and its documentation for any purpose is hereby granted without fee,
44 * provided that the above copyright notice appear in all copies and
45 * that both that copyright notice and this permission notice appear
46 * in supporting documentation. Silicon Graphics makes no
47 * representations about the suitability of this software for any
48 * purpose. It is provided "as is" without express or implied warranty.
51 /** @file bits/stl_function.h
52 * This is an internal header file, included by other library headers.
53 * Do not attempt to use it directly. @headername{functional}
56 #ifndef _STL_FUNCTION_H
57 #define _STL_FUNCTION_H 1
59 #if __cplusplus > 201103L
60 #include <bits/move.h>
63 namespace std
_GLIBCXX_VISIBILITY(default)
65 _GLIBCXX_BEGIN_NAMESPACE_VERSION
67 // 20.3.1 base classes
68 /** @defgroup functors Function Objects
71 * Function objects, or _functors_, are objects with an `operator()`
72 * defined and accessible. They can be passed as arguments to algorithm
73 * templates and used in place of a function pointer. Not only is the
74 * resulting expressiveness of the library increased, but the generated
75 * code can be more efficient than what you might write by hand. When we
76 * refer to _functors_, then, generally we include function pointers in
77 * the description as well.
79 * Often, functors are only created as temporaries passed to algorithm
80 * calls, rather than being created as named variables.
82 * Two examples taken from the standard itself follow. To perform a
83 * by-element addition of two vectors `a` and `b` containing `double`,
84 * and put the result in `a`, use
86 * transform (a.begin(), a.end(), b.begin(), a.begin(), plus<double>());
88 * To negate every element in `a`, use
90 * transform(a.begin(), a.end(), a.begin(), negate<double>());
92 * The addition and negation functions will usually be inlined directly.
94 * An _adaptable function object_ is one which provides nested typedefs
95 * `result_type` and either `argument_type` (for a unary function) or
96 * `first_argument_type` and `second_argument_type` (for a binary function).
97 * Those typedefs are used by function object adaptors such as `bind2nd`.
98 * The standard library provides two class templates, `unary_function` and
99 * `binary_function`, which define those typedefs and so can be used as
100 * base classes of adaptable function objects.
102 * Since C++11 the use of function object adaptors has been superseded by
103 * more powerful tools such as lambda expressions, `function<>`, and more
104 * powerful type deduction (using `auto` and `decltype`). The helpers for
105 * defining adaptable function objects are deprecated since C++11, and no
106 * longer part of the standard library since C++17. However, they are still
107 * defined and used by libstdc++ after C++17, as a conforming extension.
113 * Helper for defining adaptable unary function objects.
114 * @deprecated Deprecated in C++11, no longer in the standard since C++17.
116 template<typename _Arg
, typename _Result
>
117 struct unary_function
119 /// @c argument_type is the type of the argument
120 typedef _Arg argument_type
;
122 /// @c result_type is the return type
123 typedef _Result result_type
;
127 * Helper for defining adaptable binary function objects.
128 * @deprecated Deprecated in C++11, no longer in the standard since C++17.
130 template<typename _Arg1
, typename _Arg2
, typename _Result
>
131 struct binary_function
133 /// @c first_argument_type is the type of the first argument
134 typedef _Arg1 first_argument_type
;
136 /// @c second_argument_type is the type of the second argument
137 typedef _Arg2 second_argument_type
;
139 /// @c result_type is the return type
140 typedef _Result result_type
;
146 /** @defgroup arithmetic_functors Arithmetic Function Object Classes
149 * The library provides function objects for basic arithmetic operations.
150 * See the documentation for @link functors function objects @endlink
151 * for examples of their use.
156 #if __cplusplus > 201103L
157 struct __is_transparent
; // undefined
159 template<typename _Tp
= void>
162 template<typename _Tp
= void>
165 template<typename _Tp
= void>
168 template<typename _Tp
= void>
171 template<typename _Tp
= void>
174 template<typename _Tp
= void>
178 /// One of the @link arithmetic_functors math functors@endlink.
179 template<typename _Tp
>
180 struct plus
: public binary_function
<_Tp
, _Tp
, _Tp
>
185 operator()(const _Tp
& __x
, const _Tp
& __y
) const
186 { return __x
+ __y
; }
189 /// One of the @link arithmetic_functors math functors@endlink.
190 template<typename _Tp
>
191 struct minus
: public binary_function
<_Tp
, _Tp
, _Tp
>
195 operator()(const _Tp
& __x
, const _Tp
& __y
) const
196 { return __x
- __y
; }
199 /// One of the @link arithmetic_functors math functors@endlink.
200 template<typename _Tp
>
201 struct multiplies
: public binary_function
<_Tp
, _Tp
, _Tp
>
205 operator()(const _Tp
& __x
, const _Tp
& __y
) const
206 { return __x
* __y
; }
209 /// One of the @link arithmetic_functors math functors@endlink.
210 template<typename _Tp
>
211 struct divides
: public binary_function
<_Tp
, _Tp
, _Tp
>
215 operator()(const _Tp
& __x
, const _Tp
& __y
) const
216 { return __x
/ __y
; }
219 /// One of the @link arithmetic_functors math functors@endlink.
220 template<typename _Tp
>
221 struct modulus
: public binary_function
<_Tp
, _Tp
, _Tp
>
225 operator()(const _Tp
& __x
, const _Tp
& __y
) const
226 { return __x
% __y
; }
229 /// One of the @link arithmetic_functors math functors@endlink.
230 template<typename _Tp
>
231 struct negate
: public unary_function
<_Tp
, _Tp
>
235 operator()(const _Tp
& __x
) const
239 #if __cplusplus > 201103L
241 #define __cpp_lib_transparent_operators 201510
246 template <typename _Tp
, typename _Up
>
249 operator()(_Tp
&& __t
, _Up
&& __u
) const
250 noexcept(noexcept(std::forward
<_Tp
>(__t
) + std::forward
<_Up
>(__u
)))
251 -> decltype(std::forward
<_Tp
>(__t
) + std::forward
<_Up
>(__u
))
252 { return std::forward
<_Tp
>(__t
) + std::forward
<_Up
>(__u
); }
254 typedef __is_transparent is_transparent
;
257 /// One of the @link arithmetic_functors math functors@endlink.
261 template <typename _Tp
, typename _Up
>
264 operator()(_Tp
&& __t
, _Up
&& __u
) const
265 noexcept(noexcept(std::forward
<_Tp
>(__t
) - std::forward
<_Up
>(__u
)))
266 -> decltype(std::forward
<_Tp
>(__t
) - std::forward
<_Up
>(__u
))
267 { return std::forward
<_Tp
>(__t
) - std::forward
<_Up
>(__u
); }
269 typedef __is_transparent is_transparent
;
272 /// One of the @link arithmetic_functors math functors@endlink.
274 struct multiplies
<void>
276 template <typename _Tp
, typename _Up
>
279 operator()(_Tp
&& __t
, _Up
&& __u
) const
280 noexcept(noexcept(std::forward
<_Tp
>(__t
) * std::forward
<_Up
>(__u
)))
281 -> decltype(std::forward
<_Tp
>(__t
) * std::forward
<_Up
>(__u
))
282 { return std::forward
<_Tp
>(__t
) * std::forward
<_Up
>(__u
); }
284 typedef __is_transparent is_transparent
;
287 /// One of the @link arithmetic_functors math functors@endlink.
291 template <typename _Tp
, typename _Up
>
294 operator()(_Tp
&& __t
, _Up
&& __u
) const
295 noexcept(noexcept(std::forward
<_Tp
>(__t
) / std::forward
<_Up
>(__u
)))
296 -> decltype(std::forward
<_Tp
>(__t
) / std::forward
<_Up
>(__u
))
297 { return std::forward
<_Tp
>(__t
) / std::forward
<_Up
>(__u
); }
299 typedef __is_transparent is_transparent
;
302 /// One of the @link arithmetic_functors math functors@endlink.
306 template <typename _Tp
, typename _Up
>
309 operator()(_Tp
&& __t
, _Up
&& __u
) const
310 noexcept(noexcept(std::forward
<_Tp
>(__t
) % std::forward
<_Up
>(__u
)))
311 -> decltype(std::forward
<_Tp
>(__t
) % std::forward
<_Up
>(__u
))
312 { return std::forward
<_Tp
>(__t
) % std::forward
<_Up
>(__u
); }
314 typedef __is_transparent is_transparent
;
317 /// One of the @link arithmetic_functors math functors@endlink.
321 template <typename _Tp
>
324 operator()(_Tp
&& __t
) const
325 noexcept(noexcept(-std::forward
<_Tp
>(__t
)))
326 -> decltype(-std::forward
<_Tp
>(__t
))
327 { return -std::forward
<_Tp
>(__t
); }
329 typedef __is_transparent is_transparent
;
334 // 20.3.3 comparisons
335 /** @defgroup comparison_functors Comparison Classes
338 * The library provides six wrapper functors for all the basic comparisons
343 #if __cplusplus > 201103L
344 template<typename _Tp
= void>
347 template<typename _Tp
= void>
350 template<typename _Tp
= void>
353 template<typename _Tp
= void>
356 template<typename _Tp
= void>
357 struct greater_equal
;
359 template<typename _Tp
= void>
363 /// One of the @link comparison_functors comparison functors@endlink.
364 template<typename _Tp
>
365 struct equal_to
: public binary_function
<_Tp
, _Tp
, bool>
369 operator()(const _Tp
& __x
, const _Tp
& __y
) const
370 { return __x
== __y
; }
373 /// One of the @link comparison_functors comparison functors@endlink.
374 template<typename _Tp
>
375 struct not_equal_to
: public binary_function
<_Tp
, _Tp
, bool>
379 operator()(const _Tp
& __x
, const _Tp
& __y
) const
380 { return __x
!= __y
; }
383 /// One of the @link comparison_functors comparison functors@endlink.
384 template<typename _Tp
>
385 struct greater
: public binary_function
<_Tp
, _Tp
, bool>
389 operator()(const _Tp
& __x
, const _Tp
& __y
) const
390 { return __x
> __y
; }
393 /// One of the @link comparison_functors comparison functors@endlink.
394 template<typename _Tp
>
395 struct less
: public binary_function
<_Tp
, _Tp
, bool>
399 operator()(const _Tp
& __x
, const _Tp
& __y
) const
400 { return __x
< __y
; }
403 /// One of the @link comparison_functors comparison functors@endlink.
404 template<typename _Tp
>
405 struct greater_equal
: public binary_function
<_Tp
, _Tp
, bool>
409 operator()(const _Tp
& __x
, const _Tp
& __y
) const
410 { return __x
>= __y
; }
413 /// One of the @link comparison_functors comparison functors@endlink.
414 template<typename _Tp
>
415 struct less_equal
: public binary_function
<_Tp
, _Tp
, bool>
419 operator()(const _Tp
& __x
, const _Tp
& __y
) const
420 { return __x
<= __y
; }
423 // Partial specialization of std::greater for pointers.
424 template<typename _Tp
>
425 struct greater
<_Tp
*> : public binary_function
<_Tp
*, _Tp
*, bool>
427 _GLIBCXX14_CONSTEXPR
bool
428 operator()(_Tp
* __x
, _Tp
* __y
) const _GLIBCXX_NOTHROW
430 #if __cplusplus >= 201402L
431 if (std::__is_constant_evaluated())
434 return (__UINTPTR_TYPE__
)__x
> (__UINTPTR_TYPE__
)__y
;
438 // Partial specialization of std::less for pointers.
439 template<typename _Tp
>
440 struct less
<_Tp
*> : public binary_function
<_Tp
*, _Tp
*, bool>
442 _GLIBCXX14_CONSTEXPR
bool
443 operator()(_Tp
* __x
, _Tp
* __y
) const _GLIBCXX_NOTHROW
445 #if __cplusplus >= 201402L
446 if (std::__is_constant_evaluated())
449 return (__UINTPTR_TYPE__
)__x
< (__UINTPTR_TYPE__
)__y
;
453 // Partial specialization of std::greater_equal for pointers.
454 template<typename _Tp
>
455 struct greater_equal
<_Tp
*> : public binary_function
<_Tp
*, _Tp
*, bool>
457 _GLIBCXX14_CONSTEXPR
bool
458 operator()(_Tp
* __x
, _Tp
* __y
) const _GLIBCXX_NOTHROW
460 #if __cplusplus >= 201402L
461 if (std::__is_constant_evaluated())
464 return (__UINTPTR_TYPE__
)__x
>= (__UINTPTR_TYPE__
)__y
;
468 // Partial specialization of std::less_equal for pointers.
469 template<typename _Tp
>
470 struct less_equal
<_Tp
*> : public binary_function
<_Tp
*, _Tp
*, bool>
472 _GLIBCXX14_CONSTEXPR
bool
473 operator()(_Tp
* __x
, _Tp
* __y
) const _GLIBCXX_NOTHROW
475 #if __cplusplus >= 201402L
476 if (std::__is_constant_evaluated())
479 return (__UINTPTR_TYPE__
)__x
<= (__UINTPTR_TYPE__
)__y
;
483 #if __cplusplus >= 201402L
484 /// One of the @link comparison_functors comparison functors@endlink.
486 struct equal_to
<void>
488 template <typename _Tp
, typename _Up
>
490 operator()(_Tp
&& __t
, _Up
&& __u
) const
491 noexcept(noexcept(std::forward
<_Tp
>(__t
) == std::forward
<_Up
>(__u
)))
492 -> decltype(std::forward
<_Tp
>(__t
) == std::forward
<_Up
>(__u
))
493 { return std::forward
<_Tp
>(__t
) == std::forward
<_Up
>(__u
); }
495 typedef __is_transparent is_transparent
;
498 /// One of the @link comparison_functors comparison functors@endlink.
500 struct not_equal_to
<void>
502 template <typename _Tp
, typename _Up
>
504 operator()(_Tp
&& __t
, _Up
&& __u
) const
505 noexcept(noexcept(std::forward
<_Tp
>(__t
) != std::forward
<_Up
>(__u
)))
506 -> decltype(std::forward
<_Tp
>(__t
) != std::forward
<_Up
>(__u
))
507 { return std::forward
<_Tp
>(__t
) != std::forward
<_Up
>(__u
); }
509 typedef __is_transparent is_transparent
;
512 /// One of the @link comparison_functors comparison functors@endlink.
516 template <typename _Tp
, typename _Up
>
518 operator()(_Tp
&& __t
, _Up
&& __u
) const
519 noexcept(noexcept(std::forward
<_Tp
>(__t
) > std::forward
<_Up
>(__u
)))
520 -> decltype(std::forward
<_Tp
>(__t
) > std::forward
<_Up
>(__u
))
522 return _S_cmp(std::forward
<_Tp
>(__t
), std::forward
<_Up
>(__u
),
523 __ptr_cmp
<_Tp
, _Up
>{});
526 template<typename _Tp
, typename _Up
>
528 operator()(_Tp
* __t
, _Up
* __u
) const noexcept
529 { return greater
<common_type_t
<_Tp
*, _Up
*>>{}(__t
, __u
); }
531 typedef __is_transparent is_transparent
;
534 template <typename _Tp
, typename _Up
>
535 static constexpr decltype(auto)
536 _S_cmp(_Tp
&& __t
, _Up
&& __u
, false_type
)
537 { return std::forward
<_Tp
>(__t
) > std::forward
<_Up
>(__u
); }
539 template <typename _Tp
, typename _Up
>
540 static constexpr bool
541 _S_cmp(_Tp
&& __t
, _Up
&& __u
, true_type
) noexcept
543 return greater
<const volatile void*>{}(
544 static_cast<const volatile void*>(std::forward
<_Tp
>(__t
)),
545 static_cast<const volatile void*>(std::forward
<_Up
>(__u
)));
548 // True if there is no viable operator> member function.
549 template<typename _Tp
, typename _Up
, typename
= void>
550 struct __not_overloaded2
: true_type
{ };
552 // False if we can call T.operator>(U)
553 template<typename _Tp
, typename _Up
>
554 struct __not_overloaded2
<_Tp
, _Up
, __void_t
<
555 decltype(std::declval
<_Tp
>().operator>(std::declval
<_Up
>()))>>
558 // True if there is no overloaded operator> for these operands.
559 template<typename _Tp
, typename _Up
, typename
= void>
560 struct __not_overloaded
: __not_overloaded2
<_Tp
, _Up
> { };
562 // False if we can call operator>(T,U)
563 template<typename _Tp
, typename _Up
>
564 struct __not_overloaded
<_Tp
, _Up
, __void_t
<
565 decltype(operator>(std::declval
<_Tp
>(), std::declval
<_Up
>()))>>
568 template<typename _Tp
, typename _Up
>
569 using __ptr_cmp
= __and_
<__not_overloaded
<_Tp
, _Up
>,
570 is_convertible
<_Tp
, const volatile void*>,
571 is_convertible
<_Up
, const volatile void*>>;
574 /// One of the @link comparison_functors comparison functors@endlink.
578 template <typename _Tp
, typename _Up
>
580 operator()(_Tp
&& __t
, _Up
&& __u
) const
581 noexcept(noexcept(std::forward
<_Tp
>(__t
) < std::forward
<_Up
>(__u
)))
582 -> decltype(std::forward
<_Tp
>(__t
) < std::forward
<_Up
>(__u
))
584 return _S_cmp(std::forward
<_Tp
>(__t
), std::forward
<_Up
>(__u
),
585 __ptr_cmp
<_Tp
, _Up
>{});
588 template<typename _Tp
, typename _Up
>
590 operator()(_Tp
* __t
, _Up
* __u
) const noexcept
591 { return less
<common_type_t
<_Tp
*, _Up
*>>{}(__t
, __u
); }
593 typedef __is_transparent is_transparent
;
596 template <typename _Tp
, typename _Up
>
597 static constexpr decltype(auto)
598 _S_cmp(_Tp
&& __t
, _Up
&& __u
, false_type
)
599 { return std::forward
<_Tp
>(__t
) < std::forward
<_Up
>(__u
); }
601 template <typename _Tp
, typename _Up
>
602 static constexpr bool
603 _S_cmp(_Tp
&& __t
, _Up
&& __u
, true_type
) noexcept
605 return less
<const volatile void*>{}(
606 static_cast<const volatile void*>(std::forward
<_Tp
>(__t
)),
607 static_cast<const volatile void*>(std::forward
<_Up
>(__u
)));
610 // True if there is no viable operator< member function.
611 template<typename _Tp
, typename _Up
, typename
= void>
612 struct __not_overloaded2
: true_type
{ };
614 // False if we can call T.operator<(U)
615 template<typename _Tp
, typename _Up
>
616 struct __not_overloaded2
<_Tp
, _Up
, __void_t
<
617 decltype(std::declval
<_Tp
>().operator<(std::declval
<_Up
>()))>>
620 // True if there is no overloaded operator< for these operands.
621 template<typename _Tp
, typename _Up
, typename
= void>
622 struct __not_overloaded
: __not_overloaded2
<_Tp
, _Up
> { };
624 // False if we can call operator<(T,U)
625 template<typename _Tp
, typename _Up
>
626 struct __not_overloaded
<_Tp
, _Up
, __void_t
<
627 decltype(operator<(std::declval
<_Tp
>(), std::declval
<_Up
>()))>>
630 template<typename _Tp
, typename _Up
>
631 using __ptr_cmp
= __and_
<__not_overloaded
<_Tp
, _Up
>,
632 is_convertible
<_Tp
, const volatile void*>,
633 is_convertible
<_Up
, const volatile void*>>;
636 /// One of the @link comparison_functors comparison functors@endlink.
638 struct greater_equal
<void>
640 template <typename _Tp
, typename _Up
>
642 operator()(_Tp
&& __t
, _Up
&& __u
) const
643 noexcept(noexcept(std::forward
<_Tp
>(__t
) >= std::forward
<_Up
>(__u
)))
644 -> decltype(std::forward
<_Tp
>(__t
) >= std::forward
<_Up
>(__u
))
646 return _S_cmp(std::forward
<_Tp
>(__t
), std::forward
<_Up
>(__u
),
647 __ptr_cmp
<_Tp
, _Up
>{});
650 template<typename _Tp
, typename _Up
>
652 operator()(_Tp
* __t
, _Up
* __u
) const noexcept
653 { return greater_equal
<common_type_t
<_Tp
*, _Up
*>>{}(__t
, __u
); }
655 typedef __is_transparent is_transparent
;
658 template <typename _Tp
, typename _Up
>
659 static constexpr decltype(auto)
660 _S_cmp(_Tp
&& __t
, _Up
&& __u
, false_type
)
661 { return std::forward
<_Tp
>(__t
) >= std::forward
<_Up
>(__u
); }
663 template <typename _Tp
, typename _Up
>
664 static constexpr bool
665 _S_cmp(_Tp
&& __t
, _Up
&& __u
, true_type
) noexcept
667 return greater_equal
<const volatile void*>{}(
668 static_cast<const volatile void*>(std::forward
<_Tp
>(__t
)),
669 static_cast<const volatile void*>(std::forward
<_Up
>(__u
)));
672 // True if there is no viable operator>= member function.
673 template<typename _Tp
, typename _Up
, typename
= void>
674 struct __not_overloaded2
: true_type
{ };
676 // False if we can call T.operator>=(U)
677 template<typename _Tp
, typename _Up
>
678 struct __not_overloaded2
<_Tp
, _Up
, __void_t
<
679 decltype(std::declval
<_Tp
>().operator>=(std::declval
<_Up
>()))>>
682 // True if there is no overloaded operator>= for these operands.
683 template<typename _Tp
, typename _Up
, typename
= void>
684 struct __not_overloaded
: __not_overloaded2
<_Tp
, _Up
> { };
686 // False if we can call operator>=(T,U)
687 template<typename _Tp
, typename _Up
>
688 struct __not_overloaded
<_Tp
, _Up
, __void_t
<
689 decltype(operator>=(std::declval
<_Tp
>(), std::declval
<_Up
>()))>>
692 template<typename _Tp
, typename _Up
>
693 using __ptr_cmp
= __and_
<__not_overloaded
<_Tp
, _Up
>,
694 is_convertible
<_Tp
, const volatile void*>,
695 is_convertible
<_Up
, const volatile void*>>;
698 /// One of the @link comparison_functors comparison functors@endlink.
700 struct less_equal
<void>
702 template <typename _Tp
, typename _Up
>
704 operator()(_Tp
&& __t
, _Up
&& __u
) const
705 noexcept(noexcept(std::forward
<_Tp
>(__t
) <= std::forward
<_Up
>(__u
)))
706 -> decltype(std::forward
<_Tp
>(__t
) <= std::forward
<_Up
>(__u
))
708 return _S_cmp(std::forward
<_Tp
>(__t
), std::forward
<_Up
>(__u
),
709 __ptr_cmp
<_Tp
, _Up
>{});
712 template<typename _Tp
, typename _Up
>
714 operator()(_Tp
* __t
, _Up
* __u
) const noexcept
715 { return less_equal
<common_type_t
<_Tp
*, _Up
*>>{}(__t
, __u
); }
717 typedef __is_transparent is_transparent
;
720 template <typename _Tp
, typename _Up
>
721 static constexpr decltype(auto)
722 _S_cmp(_Tp
&& __t
, _Up
&& __u
, false_type
)
723 { return std::forward
<_Tp
>(__t
) <= std::forward
<_Up
>(__u
); }
725 template <typename _Tp
, typename _Up
>
726 static constexpr bool
727 _S_cmp(_Tp
&& __t
, _Up
&& __u
, true_type
) noexcept
729 return less_equal
<const volatile void*>{}(
730 static_cast<const volatile void*>(std::forward
<_Tp
>(__t
)),
731 static_cast<const volatile void*>(std::forward
<_Up
>(__u
)));
734 // True if there is no viable operator<= member function.
735 template<typename _Tp
, typename _Up
, typename
= void>
736 struct __not_overloaded2
: true_type
{ };
738 // False if we can call T.operator<=(U)
739 template<typename _Tp
, typename _Up
>
740 struct __not_overloaded2
<_Tp
, _Up
, __void_t
<
741 decltype(std::declval
<_Tp
>().operator<=(std::declval
<_Up
>()))>>
744 // True if there is no overloaded operator<= for these operands.
745 template<typename _Tp
, typename _Up
, typename
= void>
746 struct __not_overloaded
: __not_overloaded2
<_Tp
, _Up
> { };
748 // False if we can call operator<=(T,U)
749 template<typename _Tp
, typename _Up
>
750 struct __not_overloaded
<_Tp
, _Up
, __void_t
<
751 decltype(operator<=(std::declval
<_Tp
>(), std::declval
<_Up
>()))>>
754 template<typename _Tp
, typename _Up
>
755 using __ptr_cmp
= __and_
<__not_overloaded
<_Tp
, _Up
>,
756 is_convertible
<_Tp
, const volatile void*>,
757 is_convertible
<_Up
, const volatile void*>>;
762 // 20.3.4 logical operations
763 /** @defgroup logical_functors Boolean Operations Classes
766 * The library provides function objects for the logical operations:
767 * `&&`, `||`, and `!`.
771 #if __cplusplus > 201103L
772 template<typename _Tp
= void>
775 template<typename _Tp
= void>
778 template<typename _Tp
= void>
782 /// One of the @link logical_functors Boolean operations functors@endlink.
783 template<typename _Tp
>
784 struct logical_and
: public binary_function
<_Tp
, _Tp
, bool>
788 operator()(const _Tp
& __x
, const _Tp
& __y
) const
789 { return __x
&& __y
; }
792 /// One of the @link logical_functors Boolean operations functors@endlink.
793 template<typename _Tp
>
794 struct logical_or
: public binary_function
<_Tp
, _Tp
, bool>
798 operator()(const _Tp
& __x
, const _Tp
& __y
) const
799 { return __x
|| __y
; }
802 /// One of the @link logical_functors Boolean operations functors@endlink.
803 template<typename _Tp
>
804 struct logical_not
: public unary_function
<_Tp
, bool>
808 operator()(const _Tp
& __x
) const
812 #if __cplusplus > 201103L
813 /// One of the @link logical_functors Boolean operations functors@endlink.
815 struct logical_and
<void>
817 template <typename _Tp
, typename _Up
>
820 operator()(_Tp
&& __t
, _Up
&& __u
) const
821 noexcept(noexcept(std::forward
<_Tp
>(__t
) && std::forward
<_Up
>(__u
)))
822 -> decltype(std::forward
<_Tp
>(__t
) && std::forward
<_Up
>(__u
))
823 { return std::forward
<_Tp
>(__t
) && std::forward
<_Up
>(__u
); }
825 typedef __is_transparent is_transparent
;
828 /// One of the @link logical_functors Boolean operations functors@endlink.
830 struct logical_or
<void>
832 template <typename _Tp
, typename _Up
>
835 operator()(_Tp
&& __t
, _Up
&& __u
) const
836 noexcept(noexcept(std::forward
<_Tp
>(__t
) || std::forward
<_Up
>(__u
)))
837 -> decltype(std::forward
<_Tp
>(__t
) || std::forward
<_Up
>(__u
))
838 { return std::forward
<_Tp
>(__t
) || std::forward
<_Up
>(__u
); }
840 typedef __is_transparent is_transparent
;
843 /// One of the @link logical_functors Boolean operations functors@endlink.
845 struct logical_not
<void>
847 template <typename _Tp
>
850 operator()(_Tp
&& __t
) const
851 noexcept(noexcept(!std::forward
<_Tp
>(__t
)))
852 -> decltype(!std::forward
<_Tp
>(__t
))
853 { return !std::forward
<_Tp
>(__t
); }
855 typedef __is_transparent is_transparent
;
860 #if __cplusplus > 201103L
861 template<typename _Tp
= void>
864 template<typename _Tp
= void>
867 template<typename _Tp
= void>
870 template<typename _Tp
= void>
874 // _GLIBCXX_RESOLVE_LIB_DEFECTS
875 // DR 660. Missing Bitwise Operations.
876 template<typename _Tp
>
877 struct bit_and
: public binary_function
<_Tp
, _Tp
, _Tp
>
881 operator()(const _Tp
& __x
, const _Tp
& __y
) const
882 { return __x
& __y
; }
885 template<typename _Tp
>
886 struct bit_or
: public binary_function
<_Tp
, _Tp
, _Tp
>
890 operator()(const _Tp
& __x
, const _Tp
& __y
) const
891 { return __x
| __y
; }
894 template<typename _Tp
>
895 struct bit_xor
: public binary_function
<_Tp
, _Tp
, _Tp
>
899 operator()(const _Tp
& __x
, const _Tp
& __y
) const
900 { return __x
^ __y
; }
903 template<typename _Tp
>
904 struct bit_not
: public unary_function
<_Tp
, _Tp
>
908 operator()(const _Tp
& __x
) const
912 #if __cplusplus > 201103L
916 template <typename _Tp
, typename _Up
>
919 operator()(_Tp
&& __t
, _Up
&& __u
) const
920 noexcept(noexcept(std::forward
<_Tp
>(__t
) & std::forward
<_Up
>(__u
)))
921 -> decltype(std::forward
<_Tp
>(__t
) & std::forward
<_Up
>(__u
))
922 { return std::forward
<_Tp
>(__t
) & std::forward
<_Up
>(__u
); }
924 typedef __is_transparent is_transparent
;
930 template <typename _Tp
, typename _Up
>
933 operator()(_Tp
&& __t
, _Up
&& __u
) const
934 noexcept(noexcept(std::forward
<_Tp
>(__t
) | std::forward
<_Up
>(__u
)))
935 -> decltype(std::forward
<_Tp
>(__t
) | std::forward
<_Up
>(__u
))
936 { return std::forward
<_Tp
>(__t
) | std::forward
<_Up
>(__u
); }
938 typedef __is_transparent is_transparent
;
944 template <typename _Tp
, typename _Up
>
947 operator()(_Tp
&& __t
, _Up
&& __u
) const
948 noexcept(noexcept(std::forward
<_Tp
>(__t
) ^ std::forward
<_Up
>(__u
)))
949 -> decltype(std::forward
<_Tp
>(__t
) ^ std::forward
<_Up
>(__u
))
950 { return std::forward
<_Tp
>(__t
) ^ std::forward
<_Up
>(__u
); }
952 typedef __is_transparent is_transparent
;
958 template <typename _Tp
>
961 operator()(_Tp
&& __t
) const
962 noexcept(noexcept(~std::forward
<_Tp
>(__t
)))
963 -> decltype(~std::forward
<_Tp
>(__t
))
964 { return ~std::forward
<_Tp
>(__t
); }
966 typedef __is_transparent is_transparent
;
971 /** @defgroup negators Negators
974 * The function templates `not1` and `not2` are function object adaptors,
975 * which each take a predicate functor and wrap it in an instance of
976 * `unary_negate` or `binary_negate`, respectively. Those classes are
977 * functors whose `operator()` evaluates the wrapped predicate function
978 * and then returns the negation of the result.
980 * For example, given a vector of integers and a trivial predicate,
982 * struct IntGreaterThanThree
983 * : public std::unary_function<int, bool>
985 * bool operator() (int x) const { return x > 3; }
988 * std::find_if (v.begin(), v.end(), not1(IntGreaterThanThree()));
990 * The call to `find_if` will locate the first index (i) of `v` for which
991 * `!(v[i] > 3)` is true.
993 * The not1/unary_negate combination works on predicates taking a single
994 * argument. The not2/binary_negate combination works on predicates taking
997 * @deprecated Deprecated in C++17, no longer in the standard since C++20.
998 * Use `not_fn` instead.
1002 /// One of the @link negators negation functors@endlink.
1003 template<typename _Predicate
>
1005 : public unary_function
<typename
_Predicate::argument_type
, bool>
1011 _GLIBCXX14_CONSTEXPR
1013 unary_negate(const _Predicate
& __x
) : _M_pred(__x
) { }
1015 _GLIBCXX14_CONSTEXPR
1017 operator()(const typename
_Predicate::argument_type
& __x
) const
1018 { return !_M_pred(__x
); }
1021 /// One of the @link negators negation functors@endlink.
1022 template<typename _Predicate
>
1023 _GLIBCXX14_CONSTEXPR
1024 inline unary_negate
<_Predicate
>
1025 not1(const _Predicate
& __pred
)
1026 { return unary_negate
<_Predicate
>(__pred
); }
1028 /// One of the @link negators negation functors@endlink.
1029 template<typename _Predicate
>
1031 : public binary_function
<typename
_Predicate::first_argument_type
,
1032 typename
_Predicate::second_argument_type
, bool>
1038 _GLIBCXX14_CONSTEXPR
1040 binary_negate(const _Predicate
& __x
) : _M_pred(__x
) { }
1042 _GLIBCXX14_CONSTEXPR
1044 operator()(const typename
_Predicate::first_argument_type
& __x
,
1045 const typename
_Predicate::second_argument_type
& __y
) const
1046 { return !_M_pred(__x
, __y
); }
1049 /// One of the @link negators negation functors@endlink.
1050 template<typename _Predicate
>
1051 _GLIBCXX14_CONSTEXPR
1052 inline binary_negate
<_Predicate
>
1053 not2(const _Predicate
& __pred
)
1054 { return binary_negate
<_Predicate
>(__pred
); }
1057 // 20.3.7 adaptors pointers functions
1058 /** @defgroup pointer_adaptors Adaptors for pointers to functions
1061 * The advantage of function objects over pointers to functions is that
1062 * the objects in the standard library declare nested typedefs describing
1063 * their argument and result types with uniform names (e.g., `result_type`
1064 * from the base classes `unary_function` and `binary_function`).
1065 * Sometimes those typedefs are required, not just optional.
1067 * Adaptors are provided to turn pointers to unary (single-argument) and
1068 * binary (double-argument) functions into function objects. The
1069 * long-winded functor `pointer_to_unary_function` is constructed with a
1070 * function pointer `f`, and its `operator()` called with argument `x`
1071 * returns `f(x)`. The functor `pointer_to_binary_function` does the same
1072 * thing, but with a double-argument `f` and `operator()`.
1074 * The function `ptr_fun` takes a pointer-to-function `f` and constructs
1075 * an instance of the appropriate functor.
1077 * @deprecated Deprecated in C++11, no longer in the standard since C++17.
1081 /// One of the @link pointer_adaptors adaptors for function pointers@endlink.
1082 template<typename _Arg
, typename _Result
>
1083 class pointer_to_unary_function
: public unary_function
<_Arg
, _Result
>
1086 _Result (*_M_ptr
)(_Arg
);
1089 pointer_to_unary_function() { }
1092 pointer_to_unary_function(_Result (*__x
)(_Arg
))
1096 operator()(_Arg __x
) const
1097 { return _M_ptr(__x
); }
1100 /// One of the @link pointer_adaptors adaptors for function pointers@endlink.
1101 template<typename _Arg
, typename _Result
>
1102 inline pointer_to_unary_function
<_Arg
, _Result
>
1103 ptr_fun(_Result (*__x
)(_Arg
))
1104 { return pointer_to_unary_function
<_Arg
, _Result
>(__x
); }
1106 /// One of the @link pointer_adaptors adaptors for function pointers@endlink.
1107 template<typename _Arg1
, typename _Arg2
, typename _Result
>
1108 class pointer_to_binary_function
1109 : public binary_function
<_Arg1
, _Arg2
, _Result
>
1112 _Result (*_M_ptr
)(_Arg1
, _Arg2
);
1115 pointer_to_binary_function() { }
1118 pointer_to_binary_function(_Result (*__x
)(_Arg1
, _Arg2
))
1122 operator()(_Arg1 __x
, _Arg2 __y
) const
1123 { return _M_ptr(__x
, __y
); }
1126 /// One of the @link pointer_adaptors adaptors for function pointers@endlink.
1127 template<typename _Arg1
, typename _Arg2
, typename _Result
>
1128 inline pointer_to_binary_function
<_Arg1
, _Arg2
, _Result
>
1129 ptr_fun(_Result (*__x
)(_Arg1
, _Arg2
))
1130 { return pointer_to_binary_function
<_Arg1
, _Arg2
, _Result
>(__x
); }
1133 template<typename _Tp
>
1135 : public unary_function
<_Tp
, _Tp
>
1138 operator()(_Tp
& __x
) const
1142 operator()(const _Tp
& __x
) const
1146 // Partial specialization, avoids confusing errors in e.g. std::set<const T>.
1147 template<typename _Tp
> struct _Identity
<const _Tp
> : _Identity
<_Tp
> { };
1149 template<typename _Pair
>
1151 : public unary_function
<_Pair
, typename
_Pair::first_type
>
1153 typename
_Pair::first_type
&
1154 operator()(_Pair
& __x
) const
1155 { return __x
.first
; }
1157 const typename
_Pair::first_type
&
1158 operator()(const _Pair
& __x
) const
1159 { return __x
.first
; }
1161 #if __cplusplus >= 201103L
1162 template<typename _Pair2
>
1163 typename
_Pair2::first_type
&
1164 operator()(_Pair2
& __x
) const
1165 { return __x
.first
; }
1167 template<typename _Pair2
>
1168 const typename
_Pair2::first_type
&
1169 operator()(const _Pair2
& __x
) const
1170 { return __x
.first
; }
1174 template<typename _Pair
>
1176 : public unary_function
<_Pair
, typename
_Pair::second_type
>
1178 typename
_Pair::second_type
&
1179 operator()(_Pair
& __x
) const
1180 { return __x
.second
; }
1182 const typename
_Pair::second_type
&
1183 operator()(const _Pair
& __x
) const
1184 { return __x
.second
; }
1187 // 20.3.8 adaptors pointers members
1188 /** @defgroup ptrmem_adaptors Adaptors for pointers to members
1191 * There are a total of 8 = 2^3 function objects in this family.
1192 * (1) Member functions taking no arguments vs member functions taking
1194 * (2) Call through pointer vs call through reference.
1195 * (3) Const vs non-const member function.
1197 * All of this complexity is in the function objects themselves. You can
1198 * ignore it by using the helper function `mem_fun` and `mem_fun_ref`,
1199 * which create whichever type of adaptor is appropriate.
1201 * @deprecated Deprecated in C++11, no longer in the standard since C++17.
1202 * Use `mem_fn` instead.
1206 /// One of the @link ptrmem_adaptors adaptors for member pointers@endlink.
1207 template<typename _Ret
, typename _Tp
>
1208 class mem_fun_t
: public unary_function
<_Tp
*, _Ret
>
1212 mem_fun_t(_Ret (_Tp::*__pf
)())
1216 operator()(_Tp
* __p
) const
1217 { return (__p
->*_M_f
)(); }
1220 _Ret (_Tp::*_M_f
)();
1223 /// One of the @link ptrmem_adaptors adaptors for member pointers@endlink.
1224 template<typename _Ret
, typename _Tp
>
1225 class const_mem_fun_t
: public unary_function
<const _Tp
*, _Ret
>
1229 const_mem_fun_t(_Ret (_Tp::*__pf
)() const)
1233 operator()(const _Tp
* __p
) const
1234 { return (__p
->*_M_f
)(); }
1237 _Ret (_Tp::*_M_f
)() const;
1240 /// One of the @link ptrmem_adaptors adaptors for member pointers@endlink.
1241 template<typename _Ret
, typename _Tp
>
1242 class mem_fun_ref_t
: public unary_function
<_Tp
, _Ret
>
1246 mem_fun_ref_t(_Ret (_Tp::*__pf
)())
1250 operator()(_Tp
& __r
) const
1251 { return (__r
.*_M_f
)(); }
1254 _Ret (_Tp::*_M_f
)();
1257 /// One of the @link ptrmem_adaptors adaptors for member pointers@endlink.
1258 template<typename _Ret
, typename _Tp
>
1259 class const_mem_fun_ref_t
: public unary_function
<_Tp
, _Ret
>
1263 const_mem_fun_ref_t(_Ret (_Tp::*__pf
)() const)
1267 operator()(const _Tp
& __r
) const
1268 { return (__r
.*_M_f
)(); }
1271 _Ret (_Tp::*_M_f
)() const;
1274 /// One of the @link ptrmem_adaptors adaptors for member pointers@endlink.
1275 template<typename _Ret
, typename _Tp
, typename _Arg
>
1276 class mem_fun1_t
: public binary_function
<_Tp
*, _Arg
, _Ret
>
1280 mem_fun1_t(_Ret (_Tp::*__pf
)(_Arg
))
1284 operator()(_Tp
* __p
, _Arg __x
) const
1285 { return (__p
->*_M_f
)(__x
); }
1288 _Ret (_Tp::*_M_f
)(_Arg
);
1291 /// One of the @link ptrmem_adaptors adaptors for member pointers@endlink.
1292 template<typename _Ret
, typename _Tp
, typename _Arg
>
1293 class const_mem_fun1_t
: public binary_function
<const _Tp
*, _Arg
, _Ret
>
1297 const_mem_fun1_t(_Ret (_Tp::*__pf
)(_Arg
) const)
1301 operator()(const _Tp
* __p
, _Arg __x
) const
1302 { return (__p
->*_M_f
)(__x
); }
1305 _Ret (_Tp::*_M_f
)(_Arg
) const;
1308 /// One of the @link ptrmem_adaptors adaptors for member pointers@endlink.
1309 template<typename _Ret
, typename _Tp
, typename _Arg
>
1310 class mem_fun1_ref_t
: public binary_function
<_Tp
, _Arg
, _Ret
>
1314 mem_fun1_ref_t(_Ret (_Tp::*__pf
)(_Arg
))
1318 operator()(_Tp
& __r
, _Arg __x
) const
1319 { return (__r
.*_M_f
)(__x
); }
1322 _Ret (_Tp::*_M_f
)(_Arg
);
1325 /// One of the @link ptrmem_adaptors adaptors for member pointers@endlink.
1326 template<typename _Ret
, typename _Tp
, typename _Arg
>
1327 class const_mem_fun1_ref_t
: public binary_function
<_Tp
, _Arg
, _Ret
>
1331 const_mem_fun1_ref_t(_Ret (_Tp::*__pf
)(_Arg
) const)
1335 operator()(const _Tp
& __r
, _Arg __x
) const
1336 { return (__r
.*_M_f
)(__x
); }
1339 _Ret (_Tp::*_M_f
)(_Arg
) const;
1342 // Mem_fun adaptor helper functions. There are only two:
1343 // mem_fun and mem_fun_ref.
1344 template<typename _Ret
, typename _Tp
>
1345 inline mem_fun_t
<_Ret
, _Tp
>
1346 mem_fun(_Ret (_Tp::*__f
)())
1347 { return mem_fun_t
<_Ret
, _Tp
>(__f
); }
1349 template<typename _Ret
, typename _Tp
>
1350 inline const_mem_fun_t
<_Ret
, _Tp
>
1351 mem_fun(_Ret (_Tp::*__f
)() const)
1352 { return const_mem_fun_t
<_Ret
, _Tp
>(__f
); }
1354 template<typename _Ret
, typename _Tp
>
1355 inline mem_fun_ref_t
<_Ret
, _Tp
>
1356 mem_fun_ref(_Ret (_Tp::*__f
)())
1357 { return mem_fun_ref_t
<_Ret
, _Tp
>(__f
); }
1359 template<typename _Ret
, typename _Tp
>
1360 inline const_mem_fun_ref_t
<_Ret
, _Tp
>
1361 mem_fun_ref(_Ret (_Tp::*__f
)() const)
1362 { return const_mem_fun_ref_t
<_Ret
, _Tp
>(__f
); }
1364 template<typename _Ret
, typename _Tp
, typename _Arg
>
1365 inline mem_fun1_t
<_Ret
, _Tp
, _Arg
>
1366 mem_fun(_Ret (_Tp::*__f
)(_Arg
))
1367 { return mem_fun1_t
<_Ret
, _Tp
, _Arg
>(__f
); }
1369 template<typename _Ret
, typename _Tp
, typename _Arg
>
1370 inline const_mem_fun1_t
<_Ret
, _Tp
, _Arg
>
1371 mem_fun(_Ret (_Tp::*__f
)(_Arg
) const)
1372 { return const_mem_fun1_t
<_Ret
, _Tp
, _Arg
>(__f
); }
1374 template<typename _Ret
, typename _Tp
, typename _Arg
>
1375 inline mem_fun1_ref_t
<_Ret
, _Tp
, _Arg
>
1376 mem_fun_ref(_Ret (_Tp::*__f
)(_Arg
))
1377 { return mem_fun1_ref_t
<_Ret
, _Tp
, _Arg
>(__f
); }
1379 template<typename _Ret
, typename _Tp
, typename _Arg
>
1380 inline const_mem_fun1_ref_t
<_Ret
, _Tp
, _Arg
>
1381 mem_fun_ref(_Ret (_Tp::*__f
)(_Arg
) const)
1382 { return const_mem_fun1_ref_t
<_Ret
, _Tp
, _Arg
>(__f
); }
1386 #if __cplusplus >= 201402L
1387 template<typename _Func
, typename _SfinaeType
, typename
= __void_t
<>>
1388 struct __has_is_transparent
1391 template<typename _Func
, typename _SfinaeType
>
1392 struct __has_is_transparent
<_Func
, _SfinaeType
,
1393 __void_t
<typename
_Func::is_transparent
>>
1394 { typedef void type
; };
1396 template<typename _Func
, typename _SfinaeType
>
1397 using __has_is_transparent_t
1398 = typename __has_is_transparent
<_Func
, _SfinaeType
>::type
;
1401 _GLIBCXX_END_NAMESPACE_VERSION
1404 #if (__cplusplus < 201103L) || _GLIBCXX_USE_DEPRECATED
1405 # include <backward/binders.h>
1408 #endif /* _STL_FUNCTION_H */