1 // Functor implementations -*- C++ -*-
3 // Copyright (C) 2001-2019 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 @e functors, are objects with an @c 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 @a 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 @c a and @c b containing @c double,
84 * and put the result in @c a, use
86 * transform (a.begin(), a.end(), b.begin(), a.begin(), plus<double>());
88 * To negate every element in @c a, use
90 * transform(a.begin(), a.end(), a.begin(), negate<double>());
92 * The addition and negation functions will be inlined directly.
94 * The standard functors are derived from structs named @c unary_function
95 * and @c binary_function. These two classes contain nothing but typedefs,
96 * to aid in generic (template) programming. If you write your own
97 * functors, you might consider doing the same.
102 * This is one of the @link functors functor base classes@endlink.
104 template<typename _Arg
, typename _Result
>
105 struct unary_function
107 /// @c argument_type is the type of the argument
108 typedef _Arg argument_type
;
110 /// @c result_type is the return type
111 typedef _Result result_type
;
115 * This is one of the @link functors functor base classes@endlink.
117 template<typename _Arg1
, typename _Arg2
, typename _Result
>
118 struct binary_function
120 /// @c first_argument_type is the type of the first argument
121 typedef _Arg1 first_argument_type
;
123 /// @c second_argument_type is the type of the second argument
124 typedef _Arg2 second_argument_type
;
126 /// @c result_type is the return type
127 typedef _Result result_type
;
132 /** @defgroup arithmetic_functors Arithmetic Classes
135 * Because basic math often needs to be done during an algorithm,
136 * the library provides functors for those operations. See the
137 * documentation for @link functors the base classes@endlink
138 * for examples of their use.
143 #if __cplusplus > 201103L
144 struct __is_transparent
; // undefined
146 template<typename _Tp
= void>
149 template<typename _Tp
= void>
152 template<typename _Tp
= void>
155 template<typename _Tp
= void>
158 template<typename _Tp
= void>
161 template<typename _Tp
= void>
165 /// One of the @link arithmetic_functors math functors@endlink.
166 template<typename _Tp
>
167 struct plus
: public binary_function
<_Tp
, _Tp
, _Tp
>
171 operator()(const _Tp
& __x
, const _Tp
& __y
) const
172 { return __x
+ __y
; }
175 /// One of the @link arithmetic_functors math functors@endlink.
176 template<typename _Tp
>
177 struct minus
: public binary_function
<_Tp
, _Tp
, _Tp
>
181 operator()(const _Tp
& __x
, const _Tp
& __y
) const
182 { return __x
- __y
; }
185 /// One of the @link arithmetic_functors math functors@endlink.
186 template<typename _Tp
>
187 struct multiplies
: public binary_function
<_Tp
, _Tp
, _Tp
>
191 operator()(const _Tp
& __x
, const _Tp
& __y
) const
192 { return __x
* __y
; }
195 /// One of the @link arithmetic_functors math functors@endlink.
196 template<typename _Tp
>
197 struct divides
: public binary_function
<_Tp
, _Tp
, _Tp
>
201 operator()(const _Tp
& __x
, const _Tp
& __y
) const
202 { return __x
/ __y
; }
205 /// One of the @link arithmetic_functors math functors@endlink.
206 template<typename _Tp
>
207 struct modulus
: public binary_function
<_Tp
, _Tp
, _Tp
>
211 operator()(const _Tp
& __x
, const _Tp
& __y
) const
212 { return __x
% __y
; }
215 /// One of the @link arithmetic_functors math functors@endlink.
216 template<typename _Tp
>
217 struct negate
: public unary_function
<_Tp
, _Tp
>
221 operator()(const _Tp
& __x
) const
225 #if __cplusplus > 201103L
227 #define __cpp_lib_transparent_operators 201510
232 template <typename _Tp
, typename _Up
>
235 operator()(_Tp
&& __t
, _Up
&& __u
) const
236 noexcept(noexcept(std::forward
<_Tp
>(__t
) + std::forward
<_Up
>(__u
)))
237 -> decltype(std::forward
<_Tp
>(__t
) + std::forward
<_Up
>(__u
))
238 { return std::forward
<_Tp
>(__t
) + std::forward
<_Up
>(__u
); }
240 typedef __is_transparent is_transparent
;
243 /// One of the @link arithmetic_functors math functors@endlink.
247 template <typename _Tp
, typename _Up
>
250 operator()(_Tp
&& __t
, _Up
&& __u
) const
251 noexcept(noexcept(std::forward
<_Tp
>(__t
) - std::forward
<_Up
>(__u
)))
252 -> decltype(std::forward
<_Tp
>(__t
) - std::forward
<_Up
>(__u
))
253 { return std::forward
<_Tp
>(__t
) - std::forward
<_Up
>(__u
); }
255 typedef __is_transparent is_transparent
;
258 /// One of the @link arithmetic_functors math functors@endlink.
260 struct multiplies
<void>
262 template <typename _Tp
, typename _Up
>
265 operator()(_Tp
&& __t
, _Up
&& __u
) const
266 noexcept(noexcept(std::forward
<_Tp
>(__t
) * std::forward
<_Up
>(__u
)))
267 -> decltype(std::forward
<_Tp
>(__t
) * std::forward
<_Up
>(__u
))
268 { return std::forward
<_Tp
>(__t
) * std::forward
<_Up
>(__u
); }
270 typedef __is_transparent is_transparent
;
273 /// One of the @link arithmetic_functors math functors@endlink.
277 template <typename _Tp
, typename _Up
>
280 operator()(_Tp
&& __t
, _Up
&& __u
) const
281 noexcept(noexcept(std::forward
<_Tp
>(__t
) / std::forward
<_Up
>(__u
)))
282 -> decltype(std::forward
<_Tp
>(__t
) / std::forward
<_Up
>(__u
))
283 { return std::forward
<_Tp
>(__t
) / std::forward
<_Up
>(__u
); }
285 typedef __is_transparent is_transparent
;
288 /// One of the @link arithmetic_functors math functors@endlink.
292 template <typename _Tp
, typename _Up
>
295 operator()(_Tp
&& __t
, _Up
&& __u
) const
296 noexcept(noexcept(std::forward
<_Tp
>(__t
) % std::forward
<_Up
>(__u
)))
297 -> decltype(std::forward
<_Tp
>(__t
) % std::forward
<_Up
>(__u
))
298 { return std::forward
<_Tp
>(__t
) % std::forward
<_Up
>(__u
); }
300 typedef __is_transparent is_transparent
;
303 /// One of the @link arithmetic_functors math functors@endlink.
307 template <typename _Tp
>
310 operator()(_Tp
&& __t
) const
311 noexcept(noexcept(-std::forward
<_Tp
>(__t
)))
312 -> decltype(-std::forward
<_Tp
>(__t
))
313 { return -std::forward
<_Tp
>(__t
); }
315 typedef __is_transparent is_transparent
;
320 // 20.3.3 comparisons
321 /** @defgroup comparison_functors Comparison Classes
324 * The library provides six wrapper functors for all the basic comparisons
329 #if __cplusplus > 201103L
330 template<typename _Tp
= void>
333 template<typename _Tp
= void>
336 template<typename _Tp
= void>
339 template<typename _Tp
= void>
342 template<typename _Tp
= void>
343 struct greater_equal
;
345 template<typename _Tp
= void>
349 /// One of the @link comparison_functors comparison functors@endlink.
350 template<typename _Tp
>
351 struct equal_to
: public binary_function
<_Tp
, _Tp
, bool>
355 operator()(const _Tp
& __x
, const _Tp
& __y
) const
356 { return __x
== __y
; }
359 /// One of the @link comparison_functors comparison functors@endlink.
360 template<typename _Tp
>
361 struct not_equal_to
: public binary_function
<_Tp
, _Tp
, bool>
365 operator()(const _Tp
& __x
, const _Tp
& __y
) const
366 { return __x
!= __y
; }
369 /// One of the @link comparison_functors comparison functors@endlink.
370 template<typename _Tp
>
371 struct greater
: public binary_function
<_Tp
, _Tp
, bool>
375 operator()(const _Tp
& __x
, const _Tp
& __y
) const
376 { return __x
> __y
; }
379 /// One of the @link comparison_functors comparison functors@endlink.
380 template<typename _Tp
>
381 struct less
: public binary_function
<_Tp
, _Tp
, bool>
385 operator()(const _Tp
& __x
, const _Tp
& __y
) const
386 { return __x
< __y
; }
389 /// One of the @link comparison_functors comparison functors@endlink.
390 template<typename _Tp
>
391 struct greater_equal
: public binary_function
<_Tp
, _Tp
, bool>
395 operator()(const _Tp
& __x
, const _Tp
& __y
) const
396 { return __x
>= __y
; }
399 /// One of the @link comparison_functors comparison functors@endlink.
400 template<typename _Tp
>
401 struct less_equal
: public binary_function
<_Tp
, _Tp
, bool>
405 operator()(const _Tp
& __x
, const _Tp
& __y
) const
406 { return __x
<= __y
; }
409 // Partial specialization of std::greater for pointers.
410 template<typename _Tp
>
411 struct greater
<_Tp
*> : public binary_function
<_Tp
*, _Tp
*, bool>
413 _GLIBCXX14_CONSTEXPR
bool
414 operator()(_Tp
* __x
, _Tp
* __y
) const _GLIBCXX_NOTHROW
416 if (__builtin_constant_p (__x
> __y
))
418 return (__UINTPTR_TYPE__
)__x
> (__UINTPTR_TYPE__
)__y
;
422 // Partial specialization of std::less for pointers.
423 template<typename _Tp
>
424 struct less
<_Tp
*> : public binary_function
<_Tp
*, _Tp
*, bool>
426 _GLIBCXX14_CONSTEXPR
bool
427 operator()(_Tp
* __x
, _Tp
* __y
) const _GLIBCXX_NOTHROW
429 if (__builtin_constant_p (__x
< __y
))
431 return (__UINTPTR_TYPE__
)__x
< (__UINTPTR_TYPE__
)__y
;
435 // Partial specialization of std::greater_equal for pointers.
436 template<typename _Tp
>
437 struct greater_equal
<_Tp
*> : public binary_function
<_Tp
*, _Tp
*, bool>
439 _GLIBCXX14_CONSTEXPR
bool
440 operator()(_Tp
* __x
, _Tp
* __y
) const _GLIBCXX_NOTHROW
442 if (__builtin_constant_p (__x
>= __y
))
444 return (__UINTPTR_TYPE__
)__x
>= (__UINTPTR_TYPE__
)__y
;
448 // Partial specialization of std::less_equal for pointers.
449 template<typename _Tp
>
450 struct less_equal
<_Tp
*> : public binary_function
<_Tp
*, _Tp
*, bool>
452 _GLIBCXX14_CONSTEXPR
bool
453 operator()(_Tp
* __x
, _Tp
* __y
) const _GLIBCXX_NOTHROW
455 if (__builtin_constant_p (__x
<= __y
))
457 return (__UINTPTR_TYPE__
)__x
<= (__UINTPTR_TYPE__
)__y
;
461 #if __cplusplus >= 201402L
462 /// One of the @link comparison_functors comparison functors@endlink.
464 struct equal_to
<void>
466 template <typename _Tp
, typename _Up
>
468 operator()(_Tp
&& __t
, _Up
&& __u
) const
469 noexcept(noexcept(std::forward
<_Tp
>(__t
) == std::forward
<_Up
>(__u
)))
470 -> decltype(std::forward
<_Tp
>(__t
) == std::forward
<_Up
>(__u
))
471 { return std::forward
<_Tp
>(__t
) == std::forward
<_Up
>(__u
); }
473 typedef __is_transparent is_transparent
;
476 /// One of the @link comparison_functors comparison functors@endlink.
478 struct not_equal_to
<void>
480 template <typename _Tp
, typename _Up
>
482 operator()(_Tp
&& __t
, _Up
&& __u
) const
483 noexcept(noexcept(std::forward
<_Tp
>(__t
) != std::forward
<_Up
>(__u
)))
484 -> decltype(std::forward
<_Tp
>(__t
) != std::forward
<_Up
>(__u
))
485 { return std::forward
<_Tp
>(__t
) != std::forward
<_Up
>(__u
); }
487 typedef __is_transparent is_transparent
;
490 /// One of the @link comparison_functors comparison functors@endlink.
494 template <typename _Tp
, typename _Up
>
496 operator()(_Tp
&& __t
, _Up
&& __u
) const
497 noexcept(noexcept(std::forward
<_Tp
>(__t
) > std::forward
<_Up
>(__u
)))
498 -> decltype(std::forward
<_Tp
>(__t
) > std::forward
<_Up
>(__u
))
500 return _S_cmp(std::forward
<_Tp
>(__t
), std::forward
<_Up
>(__u
),
501 __ptr_cmp
<_Tp
, _Up
>{});
504 template<typename _Tp
, typename _Up
>
506 operator()(_Tp
* __t
, _Up
* __u
) const noexcept
507 { return greater
<common_type_t
<_Tp
*, _Up
*>>{}(__t
, __u
); }
509 typedef __is_transparent is_transparent
;
512 template <typename _Tp
, typename _Up
>
513 static constexpr decltype(auto)
514 _S_cmp(_Tp
&& __t
, _Up
&& __u
, false_type
)
515 { return std::forward
<_Tp
>(__t
) > std::forward
<_Up
>(__u
); }
517 template <typename _Tp
, typename _Up
>
518 static constexpr bool
519 _S_cmp(_Tp
&& __t
, _Up
&& __u
, true_type
) noexcept
521 return greater
<const volatile void*>{}(
522 static_cast<const volatile void*>(std::forward
<_Tp
>(__t
)),
523 static_cast<const volatile void*>(std::forward
<_Up
>(__u
)));
526 // True if there is no viable operator> member function.
527 template<typename _Tp
, typename _Up
, typename
= void>
528 struct __not_overloaded2
: true_type
{ };
530 // False if we can call T.operator>(U)
531 template<typename _Tp
, typename _Up
>
532 struct __not_overloaded2
<_Tp
, _Up
, __void_t
<
533 decltype(std::declval
<_Tp
>().operator>(std::declval
<_Up
>()))>>
536 // True if there is no overloaded operator> for these operands.
537 template<typename _Tp
, typename _Up
, typename
= void>
538 struct __not_overloaded
: __not_overloaded2
<_Tp
, _Up
> { };
540 // False if we can call operator>(T,U)
541 template<typename _Tp
, typename _Up
>
542 struct __not_overloaded
<_Tp
, _Up
, __void_t
<
543 decltype(operator>(std::declval
<_Tp
>(), std::declval
<_Up
>()))>>
546 template<typename _Tp
, typename _Up
>
547 using __ptr_cmp
= __and_
<__not_overloaded
<_Tp
, _Up
>,
548 is_convertible
<_Tp
, const volatile void*>,
549 is_convertible
<_Up
, const volatile void*>>;
552 /// One of the @link comparison_functors comparison functors@endlink.
556 template <typename _Tp
, typename _Up
>
558 operator()(_Tp
&& __t
, _Up
&& __u
) const
559 noexcept(noexcept(std::forward
<_Tp
>(__t
) < std::forward
<_Up
>(__u
)))
560 -> decltype(std::forward
<_Tp
>(__t
) < std::forward
<_Up
>(__u
))
562 return _S_cmp(std::forward
<_Tp
>(__t
), std::forward
<_Up
>(__u
),
563 __ptr_cmp
<_Tp
, _Up
>{});
566 template<typename _Tp
, typename _Up
>
568 operator()(_Tp
* __t
, _Up
* __u
) const noexcept
569 { return less
<common_type_t
<_Tp
*, _Up
*>>{}(__t
, __u
); }
571 typedef __is_transparent is_transparent
;
574 template <typename _Tp
, typename _Up
>
575 static constexpr decltype(auto)
576 _S_cmp(_Tp
&& __t
, _Up
&& __u
, false_type
)
577 { return std::forward
<_Tp
>(__t
) < std::forward
<_Up
>(__u
); }
579 template <typename _Tp
, typename _Up
>
580 static constexpr bool
581 _S_cmp(_Tp
&& __t
, _Up
&& __u
, true_type
) noexcept
583 return less
<const volatile void*>{}(
584 static_cast<const volatile void*>(std::forward
<_Tp
>(__t
)),
585 static_cast<const volatile void*>(std::forward
<_Up
>(__u
)));
588 // True if there is no viable operator< member function.
589 template<typename _Tp
, typename _Up
, typename
= void>
590 struct __not_overloaded2
: true_type
{ };
592 // False if we can call T.operator<(U)
593 template<typename _Tp
, typename _Up
>
594 struct __not_overloaded2
<_Tp
, _Up
, __void_t
<
595 decltype(std::declval
<_Tp
>().operator<(std::declval
<_Up
>()))>>
598 // True if there is no overloaded operator< for these operands.
599 template<typename _Tp
, typename _Up
, typename
= void>
600 struct __not_overloaded
: __not_overloaded2
<_Tp
, _Up
> { };
602 // False if we can call operator<(T,U)
603 template<typename _Tp
, typename _Up
>
604 struct __not_overloaded
<_Tp
, _Up
, __void_t
<
605 decltype(operator<(std::declval
<_Tp
>(), std::declval
<_Up
>()))>>
608 template<typename _Tp
, typename _Up
>
609 using __ptr_cmp
= __and_
<__not_overloaded
<_Tp
, _Up
>,
610 is_convertible
<_Tp
, const volatile void*>,
611 is_convertible
<_Up
, const volatile void*>>;
614 /// One of the @link comparison_functors comparison functors@endlink.
616 struct greater_equal
<void>
618 template <typename _Tp
, typename _Up
>
620 operator()(_Tp
&& __t
, _Up
&& __u
) const
621 noexcept(noexcept(std::forward
<_Tp
>(__t
) >= std::forward
<_Up
>(__u
)))
622 -> decltype(std::forward
<_Tp
>(__t
) >= std::forward
<_Up
>(__u
))
624 return _S_cmp(std::forward
<_Tp
>(__t
), std::forward
<_Up
>(__u
),
625 __ptr_cmp
<_Tp
, _Up
>{});
628 template<typename _Tp
, typename _Up
>
630 operator()(_Tp
* __t
, _Up
* __u
) const noexcept
631 { return greater_equal
<common_type_t
<_Tp
*, _Up
*>>{}(__t
, __u
); }
633 typedef __is_transparent is_transparent
;
636 template <typename _Tp
, typename _Up
>
637 static constexpr decltype(auto)
638 _S_cmp(_Tp
&& __t
, _Up
&& __u
, false_type
)
639 { return std::forward
<_Tp
>(__t
) >= std::forward
<_Up
>(__u
); }
641 template <typename _Tp
, typename _Up
>
642 static constexpr bool
643 _S_cmp(_Tp
&& __t
, _Up
&& __u
, true_type
) noexcept
645 return greater_equal
<const volatile void*>{}(
646 static_cast<const volatile void*>(std::forward
<_Tp
>(__t
)),
647 static_cast<const volatile void*>(std::forward
<_Up
>(__u
)));
650 // True if there is no viable operator>= member function.
651 template<typename _Tp
, typename _Up
, typename
= void>
652 struct __not_overloaded2
: true_type
{ };
654 // False if we can call T.operator>=(U)
655 template<typename _Tp
, typename _Up
>
656 struct __not_overloaded2
<_Tp
, _Up
, __void_t
<
657 decltype(std::declval
<_Tp
>().operator>=(std::declval
<_Up
>()))>>
660 // True if there is no overloaded operator>= for these operands.
661 template<typename _Tp
, typename _Up
, typename
= void>
662 struct __not_overloaded
: __not_overloaded2
<_Tp
, _Up
> { };
664 // False if we can call operator>=(T,U)
665 template<typename _Tp
, typename _Up
>
666 struct __not_overloaded
<_Tp
, _Up
, __void_t
<
667 decltype(operator>=(std::declval
<_Tp
>(), std::declval
<_Up
>()))>>
670 template<typename _Tp
, typename _Up
>
671 using __ptr_cmp
= __and_
<__not_overloaded
<_Tp
, _Up
>,
672 is_convertible
<_Tp
, const volatile void*>,
673 is_convertible
<_Up
, const volatile void*>>;
676 /// One of the @link comparison_functors comparison functors@endlink.
678 struct less_equal
<void>
680 template <typename _Tp
, typename _Up
>
682 operator()(_Tp
&& __t
, _Up
&& __u
) const
683 noexcept(noexcept(std::forward
<_Tp
>(__t
) <= std::forward
<_Up
>(__u
)))
684 -> decltype(std::forward
<_Tp
>(__t
) <= std::forward
<_Up
>(__u
))
686 return _S_cmp(std::forward
<_Tp
>(__t
), std::forward
<_Up
>(__u
),
687 __ptr_cmp
<_Tp
, _Up
>{});
690 template<typename _Tp
, typename _Up
>
692 operator()(_Tp
* __t
, _Up
* __u
) const noexcept
693 { return less_equal
<common_type_t
<_Tp
*, _Up
*>>{}(__t
, __u
); }
695 typedef __is_transparent is_transparent
;
698 template <typename _Tp
, typename _Up
>
699 static constexpr decltype(auto)
700 _S_cmp(_Tp
&& __t
, _Up
&& __u
, false_type
)
701 { return std::forward
<_Tp
>(__t
) <= std::forward
<_Up
>(__u
); }
703 template <typename _Tp
, typename _Up
>
704 static constexpr bool
705 _S_cmp(_Tp
&& __t
, _Up
&& __u
, true_type
) noexcept
707 return less_equal
<const volatile void*>{}(
708 static_cast<const volatile void*>(std::forward
<_Tp
>(__t
)),
709 static_cast<const volatile void*>(std::forward
<_Up
>(__u
)));
712 // True if there is no viable operator<= member function.
713 template<typename _Tp
, typename _Up
, typename
= void>
714 struct __not_overloaded2
: true_type
{ };
716 // False if we can call T.operator<=(U)
717 template<typename _Tp
, typename _Up
>
718 struct __not_overloaded2
<_Tp
, _Up
, __void_t
<
719 decltype(std::declval
<_Tp
>().operator<=(std::declval
<_Up
>()))>>
722 // True if there is no overloaded operator<= for these operands.
723 template<typename _Tp
, typename _Up
, typename
= void>
724 struct __not_overloaded
: __not_overloaded2
<_Tp
, _Up
> { };
726 // False if we can call operator<=(T,U)
727 template<typename _Tp
, typename _Up
>
728 struct __not_overloaded
<_Tp
, _Up
, __void_t
<
729 decltype(operator<=(std::declval
<_Tp
>(), std::declval
<_Up
>()))>>
732 template<typename _Tp
, typename _Up
>
733 using __ptr_cmp
= __and_
<__not_overloaded
<_Tp
, _Up
>,
734 is_convertible
<_Tp
, const volatile void*>,
735 is_convertible
<_Up
, const volatile void*>>;
740 // 20.3.4 logical operations
741 /** @defgroup logical_functors Boolean Operations Classes
744 * Here are wrapper functors for Boolean operations: @c &&, @c ||,
749 #if __cplusplus > 201103L
750 template<typename _Tp
= void>
753 template<typename _Tp
= void>
756 template<typename _Tp
= void>
760 /// One of the @link logical_functors Boolean operations functors@endlink.
761 template<typename _Tp
>
762 struct logical_and
: public binary_function
<_Tp
, _Tp
, bool>
766 operator()(const _Tp
& __x
, const _Tp
& __y
) const
767 { return __x
&& __y
; }
770 /// One of the @link logical_functors Boolean operations functors@endlink.
771 template<typename _Tp
>
772 struct logical_or
: public binary_function
<_Tp
, _Tp
, bool>
776 operator()(const _Tp
& __x
, const _Tp
& __y
) const
777 { return __x
|| __y
; }
780 /// One of the @link logical_functors Boolean operations functors@endlink.
781 template<typename _Tp
>
782 struct logical_not
: public unary_function
<_Tp
, bool>
786 operator()(const _Tp
& __x
) const
790 #if __cplusplus > 201103L
791 /// One of the @link logical_functors Boolean operations functors@endlink.
793 struct logical_and
<void>
795 template <typename _Tp
, typename _Up
>
798 operator()(_Tp
&& __t
, _Up
&& __u
) const
799 noexcept(noexcept(std::forward
<_Tp
>(__t
) && std::forward
<_Up
>(__u
)))
800 -> decltype(std::forward
<_Tp
>(__t
) && std::forward
<_Up
>(__u
))
801 { return std::forward
<_Tp
>(__t
) && std::forward
<_Up
>(__u
); }
803 typedef __is_transparent is_transparent
;
806 /// One of the @link logical_functors Boolean operations functors@endlink.
808 struct logical_or
<void>
810 template <typename _Tp
, typename _Up
>
813 operator()(_Tp
&& __t
, _Up
&& __u
) const
814 noexcept(noexcept(std::forward
<_Tp
>(__t
) || std::forward
<_Up
>(__u
)))
815 -> decltype(std::forward
<_Tp
>(__t
) || std::forward
<_Up
>(__u
))
816 { return std::forward
<_Tp
>(__t
) || std::forward
<_Up
>(__u
); }
818 typedef __is_transparent is_transparent
;
821 /// One of the @link logical_functors Boolean operations functors@endlink.
823 struct logical_not
<void>
825 template <typename _Tp
>
828 operator()(_Tp
&& __t
) const
829 noexcept(noexcept(!std::forward
<_Tp
>(__t
)))
830 -> decltype(!std::forward
<_Tp
>(__t
))
831 { return !std::forward
<_Tp
>(__t
); }
833 typedef __is_transparent is_transparent
;
838 #if __cplusplus > 201103L
839 template<typename _Tp
= void>
842 template<typename _Tp
= void>
845 template<typename _Tp
= void>
848 template<typename _Tp
= void>
852 // _GLIBCXX_RESOLVE_LIB_DEFECTS
853 // DR 660. Missing Bitwise Operations.
854 template<typename _Tp
>
855 struct bit_and
: public binary_function
<_Tp
, _Tp
, _Tp
>
859 operator()(const _Tp
& __x
, const _Tp
& __y
) const
860 { return __x
& __y
; }
863 template<typename _Tp
>
864 struct bit_or
: public binary_function
<_Tp
, _Tp
, _Tp
>
868 operator()(const _Tp
& __x
, const _Tp
& __y
) const
869 { return __x
| __y
; }
872 template<typename _Tp
>
873 struct bit_xor
: public binary_function
<_Tp
, _Tp
, _Tp
>
877 operator()(const _Tp
& __x
, const _Tp
& __y
) const
878 { return __x
^ __y
; }
881 template<typename _Tp
>
882 struct bit_not
: public unary_function
<_Tp
, _Tp
>
886 operator()(const _Tp
& __x
) const
890 #if __cplusplus > 201103L
894 template <typename _Tp
, typename _Up
>
897 operator()(_Tp
&& __t
, _Up
&& __u
) const
898 noexcept(noexcept(std::forward
<_Tp
>(__t
) & std::forward
<_Up
>(__u
)))
899 -> decltype(std::forward
<_Tp
>(__t
) & std::forward
<_Up
>(__u
))
900 { return std::forward
<_Tp
>(__t
) & std::forward
<_Up
>(__u
); }
902 typedef __is_transparent is_transparent
;
908 template <typename _Tp
, typename _Up
>
911 operator()(_Tp
&& __t
, _Up
&& __u
) const
912 noexcept(noexcept(std::forward
<_Tp
>(__t
) | std::forward
<_Up
>(__u
)))
913 -> decltype(std::forward
<_Tp
>(__t
) | std::forward
<_Up
>(__u
))
914 { return std::forward
<_Tp
>(__t
) | std::forward
<_Up
>(__u
); }
916 typedef __is_transparent is_transparent
;
922 template <typename _Tp
, typename _Up
>
925 operator()(_Tp
&& __t
, _Up
&& __u
) const
926 noexcept(noexcept(std::forward
<_Tp
>(__t
) ^ std::forward
<_Up
>(__u
)))
927 -> decltype(std::forward
<_Tp
>(__t
) ^ std::forward
<_Up
>(__u
))
928 { return std::forward
<_Tp
>(__t
) ^ std::forward
<_Up
>(__u
); }
930 typedef __is_transparent is_transparent
;
936 template <typename _Tp
>
939 operator()(_Tp
&& __t
) const
940 noexcept(noexcept(~std::forward
<_Tp
>(__t
)))
941 -> decltype(~std::forward
<_Tp
>(__t
))
942 { return ~std::forward
<_Tp
>(__t
); }
944 typedef __is_transparent is_transparent
;
949 /** @defgroup negators Negators
952 * The functions @c not1 and @c not2 each take a predicate functor
953 * and return an instance of @c unary_negate or
954 * @c binary_negate, respectively. These classes are functors whose
955 * @c operator() performs the stored predicate function and then returns
956 * the negation of the result.
958 * For example, given a vector of integers and a trivial predicate,
960 * struct IntGreaterThanThree
961 * : public std::unary_function<int, bool>
963 * bool operator() (int x) { return x > 3; }
966 * std::find_if (v.begin(), v.end(), not1(IntGreaterThanThree()));
968 * The call to @c find_if will locate the first index (i) of @c v for which
969 * <code>!(v[i] > 3)</code> is true.
971 * The not1/unary_negate combination works on predicates taking a single
972 * argument. The not2/binary_negate combination works on predicates which
973 * take two arguments.
977 /// One of the @link negators negation functors@endlink.
978 template<typename _Predicate
>
980 : public unary_function
<typename
_Predicate::argument_type
, bool>
988 unary_negate(const _Predicate
& __x
) : _M_pred(__x
) { }
992 operator()(const typename
_Predicate::argument_type
& __x
) const
993 { return !_M_pred(__x
); }
996 /// One of the @link negators negation functors@endlink.
997 template<typename _Predicate
>
999 inline unary_negate
<_Predicate
>
1000 not1(const _Predicate
& __pred
)
1001 { return unary_negate
<_Predicate
>(__pred
); }
1003 /// One of the @link negators negation functors@endlink.
1004 template<typename _Predicate
>
1006 : public binary_function
<typename
_Predicate::first_argument_type
,
1007 typename
_Predicate::second_argument_type
, bool>
1013 _GLIBCXX14_CONSTEXPR
1015 binary_negate(const _Predicate
& __x
) : _M_pred(__x
) { }
1017 _GLIBCXX14_CONSTEXPR
1019 operator()(const typename
_Predicate::first_argument_type
& __x
,
1020 const typename
_Predicate::second_argument_type
& __y
) const
1021 { return !_M_pred(__x
, __y
); }
1024 /// One of the @link negators negation functors@endlink.
1025 template<typename _Predicate
>
1026 _GLIBCXX14_CONSTEXPR
1027 inline binary_negate
<_Predicate
>
1028 not2(const _Predicate
& __pred
)
1029 { return binary_negate
<_Predicate
>(__pred
); }
1032 // 20.3.7 adaptors pointers functions
1033 /** @defgroup pointer_adaptors Adaptors for pointers to functions
1036 * The advantage of function objects over pointers to functions is that
1037 * the objects in the standard library declare nested typedefs describing
1038 * their argument and result types with uniform names (e.g., @c result_type
1039 * from the base classes @c unary_function and @c binary_function).
1040 * Sometimes those typedefs are required, not just optional.
1042 * Adaptors are provided to turn pointers to unary (single-argument) and
1043 * binary (double-argument) functions into function objects. The
1044 * long-winded functor @c pointer_to_unary_function is constructed with a
1045 * function pointer @c f, and its @c operator() called with argument @c x
1046 * returns @c f(x). The functor @c pointer_to_binary_function does the same
1047 * thing, but with a double-argument @c f and @c operator().
1049 * The function @c ptr_fun takes a pointer-to-function @c f and constructs
1050 * an instance of the appropriate functor.
1054 /// One of the @link pointer_adaptors adaptors for function pointers@endlink.
1055 template<typename _Arg
, typename _Result
>
1056 class pointer_to_unary_function
: public unary_function
<_Arg
, _Result
>
1059 _Result (*_M_ptr
)(_Arg
);
1062 pointer_to_unary_function() { }
1065 pointer_to_unary_function(_Result (*__x
)(_Arg
))
1069 operator()(_Arg __x
) const
1070 { return _M_ptr(__x
); }
1073 /// One of the @link pointer_adaptors adaptors for function pointers@endlink.
1074 template<typename _Arg
, typename _Result
>
1075 inline pointer_to_unary_function
<_Arg
, _Result
>
1076 ptr_fun(_Result (*__x
)(_Arg
))
1077 { return pointer_to_unary_function
<_Arg
, _Result
>(__x
); }
1079 /// One of the @link pointer_adaptors adaptors for function pointers@endlink.
1080 template<typename _Arg1
, typename _Arg2
, typename _Result
>
1081 class pointer_to_binary_function
1082 : public binary_function
<_Arg1
, _Arg2
, _Result
>
1085 _Result (*_M_ptr
)(_Arg1
, _Arg2
);
1088 pointer_to_binary_function() { }
1091 pointer_to_binary_function(_Result (*__x
)(_Arg1
, _Arg2
))
1095 operator()(_Arg1 __x
, _Arg2 __y
) const
1096 { return _M_ptr(__x
, __y
); }
1099 /// One of the @link pointer_adaptors adaptors for function pointers@endlink.
1100 template<typename _Arg1
, typename _Arg2
, typename _Result
>
1101 inline pointer_to_binary_function
<_Arg1
, _Arg2
, _Result
>
1102 ptr_fun(_Result (*__x
)(_Arg1
, _Arg2
))
1103 { return pointer_to_binary_function
<_Arg1
, _Arg2
, _Result
>(__x
); }
1106 template<typename _Tp
>
1108 : public unary_function
<_Tp
, _Tp
>
1111 operator()(_Tp
& __x
) const
1115 operator()(const _Tp
& __x
) const
1119 // Partial specialization, avoids confusing errors in e.g. std::set<const T>.
1120 template<typename _Tp
> struct _Identity
<const _Tp
> : _Identity
<_Tp
> { };
1122 template<typename _Pair
>
1124 : public unary_function
<_Pair
, typename
_Pair::first_type
>
1126 typename
_Pair::first_type
&
1127 operator()(_Pair
& __x
) const
1128 { return __x
.first
; }
1130 const typename
_Pair::first_type
&
1131 operator()(const _Pair
& __x
) const
1132 { return __x
.first
; }
1134 #if __cplusplus >= 201103L
1135 template<typename _Pair2
>
1136 typename
_Pair2::first_type
&
1137 operator()(_Pair2
& __x
) const
1138 { return __x
.first
; }
1140 template<typename _Pair2
>
1141 const typename
_Pair2::first_type
&
1142 operator()(const _Pair2
& __x
) const
1143 { return __x
.first
; }
1147 template<typename _Pair
>
1149 : public unary_function
<_Pair
, typename
_Pair::second_type
>
1151 typename
_Pair::second_type
&
1152 operator()(_Pair
& __x
) const
1153 { return __x
.second
; }
1155 const typename
_Pair::second_type
&
1156 operator()(const _Pair
& __x
) const
1157 { return __x
.second
; }
1160 // 20.3.8 adaptors pointers members
1161 /** @defgroup memory_adaptors Adaptors for pointers to members
1164 * There are a total of 8 = 2^3 function objects in this family.
1165 * (1) Member functions taking no arguments vs member functions taking
1167 * (2) Call through pointer vs call through reference.
1168 * (3) Const vs non-const member function.
1170 * All of this complexity is in the function objects themselves. You can
1171 * ignore it by using the helper function mem_fun and mem_fun_ref,
1172 * which create whichever type of adaptor is appropriate.
1176 /// One of the @link memory_adaptors adaptors for member
1177 /// pointers@endlink.
1178 template<typename _Ret
, typename _Tp
>
1179 class mem_fun_t
: public unary_function
<_Tp
*, _Ret
>
1183 mem_fun_t(_Ret (_Tp::*__pf
)())
1187 operator()(_Tp
* __p
) const
1188 { return (__p
->*_M_f
)(); }
1191 _Ret (_Tp::*_M_f
)();
1194 /// One of the @link memory_adaptors adaptors for member
1195 /// pointers@endlink.
1196 template<typename _Ret
, typename _Tp
>
1197 class const_mem_fun_t
: public unary_function
<const _Tp
*, _Ret
>
1201 const_mem_fun_t(_Ret (_Tp::*__pf
)() const)
1205 operator()(const _Tp
* __p
) const
1206 { return (__p
->*_M_f
)(); }
1209 _Ret (_Tp::*_M_f
)() const;
1212 /// One of the @link memory_adaptors adaptors for member
1213 /// pointers@endlink.
1214 template<typename _Ret
, typename _Tp
>
1215 class mem_fun_ref_t
: public unary_function
<_Tp
, _Ret
>
1219 mem_fun_ref_t(_Ret (_Tp::*__pf
)())
1223 operator()(_Tp
& __r
) const
1224 { return (__r
.*_M_f
)(); }
1227 _Ret (_Tp::*_M_f
)();
1230 /// One of the @link memory_adaptors adaptors for member
1231 /// pointers@endlink.
1232 template<typename _Ret
, typename _Tp
>
1233 class const_mem_fun_ref_t
: public unary_function
<_Tp
, _Ret
>
1237 const_mem_fun_ref_t(_Ret (_Tp::*__pf
)() const)
1241 operator()(const _Tp
& __r
) const
1242 { return (__r
.*_M_f
)(); }
1245 _Ret (_Tp::*_M_f
)() const;
1248 /// One of the @link memory_adaptors adaptors for member
1249 /// pointers@endlink.
1250 template<typename _Ret
, typename _Tp
, typename _Arg
>
1251 class mem_fun1_t
: public binary_function
<_Tp
*, _Arg
, _Ret
>
1255 mem_fun1_t(_Ret (_Tp::*__pf
)(_Arg
))
1259 operator()(_Tp
* __p
, _Arg __x
) const
1260 { return (__p
->*_M_f
)(__x
); }
1263 _Ret (_Tp::*_M_f
)(_Arg
);
1266 /// One of the @link memory_adaptors adaptors for member
1267 /// pointers@endlink.
1268 template<typename _Ret
, typename _Tp
, typename _Arg
>
1269 class const_mem_fun1_t
: public binary_function
<const _Tp
*, _Arg
, _Ret
>
1273 const_mem_fun1_t(_Ret (_Tp::*__pf
)(_Arg
) const)
1277 operator()(const _Tp
* __p
, _Arg __x
) const
1278 { return (__p
->*_M_f
)(__x
); }
1281 _Ret (_Tp::*_M_f
)(_Arg
) const;
1284 /// One of the @link memory_adaptors adaptors for member
1285 /// pointers@endlink.
1286 template<typename _Ret
, typename _Tp
, typename _Arg
>
1287 class mem_fun1_ref_t
: public binary_function
<_Tp
, _Arg
, _Ret
>
1291 mem_fun1_ref_t(_Ret (_Tp::*__pf
)(_Arg
))
1295 operator()(_Tp
& __r
, _Arg __x
) const
1296 { return (__r
.*_M_f
)(__x
); }
1299 _Ret (_Tp::*_M_f
)(_Arg
);
1302 /// One of the @link memory_adaptors adaptors for member
1303 /// pointers@endlink.
1304 template<typename _Ret
, typename _Tp
, typename _Arg
>
1305 class const_mem_fun1_ref_t
: public binary_function
<_Tp
, _Arg
, _Ret
>
1309 const_mem_fun1_ref_t(_Ret (_Tp::*__pf
)(_Arg
) const)
1313 operator()(const _Tp
& __r
, _Arg __x
) const
1314 { return (__r
.*_M_f
)(__x
); }
1317 _Ret (_Tp::*_M_f
)(_Arg
) const;
1320 // Mem_fun adaptor helper functions. There are only two:
1321 // mem_fun and mem_fun_ref.
1322 template<typename _Ret
, typename _Tp
>
1323 inline mem_fun_t
<_Ret
, _Tp
>
1324 mem_fun(_Ret (_Tp::*__f
)())
1325 { return mem_fun_t
<_Ret
, _Tp
>(__f
); }
1327 template<typename _Ret
, typename _Tp
>
1328 inline const_mem_fun_t
<_Ret
, _Tp
>
1329 mem_fun(_Ret (_Tp::*__f
)() const)
1330 { return const_mem_fun_t
<_Ret
, _Tp
>(__f
); }
1332 template<typename _Ret
, typename _Tp
>
1333 inline mem_fun_ref_t
<_Ret
, _Tp
>
1334 mem_fun_ref(_Ret (_Tp::*__f
)())
1335 { return mem_fun_ref_t
<_Ret
, _Tp
>(__f
); }
1337 template<typename _Ret
, typename _Tp
>
1338 inline const_mem_fun_ref_t
<_Ret
, _Tp
>
1339 mem_fun_ref(_Ret (_Tp::*__f
)() const)
1340 { return const_mem_fun_ref_t
<_Ret
, _Tp
>(__f
); }
1342 template<typename _Ret
, typename _Tp
, typename _Arg
>
1343 inline mem_fun1_t
<_Ret
, _Tp
, _Arg
>
1344 mem_fun(_Ret (_Tp::*__f
)(_Arg
))
1345 { return mem_fun1_t
<_Ret
, _Tp
, _Arg
>(__f
); }
1347 template<typename _Ret
, typename _Tp
, typename _Arg
>
1348 inline const_mem_fun1_t
<_Ret
, _Tp
, _Arg
>
1349 mem_fun(_Ret (_Tp::*__f
)(_Arg
) const)
1350 { return const_mem_fun1_t
<_Ret
, _Tp
, _Arg
>(__f
); }
1352 template<typename _Ret
, typename _Tp
, typename _Arg
>
1353 inline mem_fun1_ref_t
<_Ret
, _Tp
, _Arg
>
1354 mem_fun_ref(_Ret (_Tp::*__f
)(_Arg
))
1355 { return mem_fun1_ref_t
<_Ret
, _Tp
, _Arg
>(__f
); }
1357 template<typename _Ret
, typename _Tp
, typename _Arg
>
1358 inline const_mem_fun1_ref_t
<_Ret
, _Tp
, _Arg
>
1359 mem_fun_ref(_Ret (_Tp::*__f
)(_Arg
) const)
1360 { return const_mem_fun1_ref_t
<_Ret
, _Tp
, _Arg
>(__f
); }
1364 _GLIBCXX_END_NAMESPACE_VERSION
1367 #if (__cplusplus < 201103L) || _GLIBCXX_USE_DEPRECATED
1368 # include <backward/binders.h>
1371 #endif /* _STL_FUNCTION_H */