Import GCC-8 to a new vendor branch
[dragonfly.git] / contrib / gcc-8.0 / libstdc++-v3 / include / bits / stl_function.h
blob9e81ad3f20e30324ba19c09fd1b5782b7ad35b29
1 // Functor implementations -*- C++ -*-
3 // Copyright (C) 2001-2018 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.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
27 * Copyright (c) 1994
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>
61 #endif
63 namespace std _GLIBCXX_VISIBILITY(default)
65 _GLIBCXX_BEGIN_NAMESPACE_VERSION
67 // 20.3.1 base classes
68 /** @defgroup functors Function Objects
69 * @ingroup utilities
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
85 * \code
86 * transform (a.begin(), a.end(), b.begin(), a.begin(), plus<double>());
87 * \endcode
88 * To negate every element in @c a, use
89 * \code
90 * transform(a.begin(), a.end(), a.begin(), negate<double>());
91 * \endcode
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.
99 * @{
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;
129 /** @} */
131 // 20.3.2 arithmetic
132 /** @defgroup arithmetic_functors Arithmetic Classes
133 * @ingroup functors
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.
140 * @{
143 #if __cplusplus > 201103L
144 struct __is_transparent; // undefined
146 template<typename _Tp = void>
147 struct plus;
149 template<typename _Tp = void>
150 struct minus;
152 template<typename _Tp = void>
153 struct multiplies;
155 template<typename _Tp = void>
156 struct divides;
158 template<typename _Tp = void>
159 struct modulus;
161 template<typename _Tp = void>
162 struct negate;
163 #endif
165 /// One of the @link arithmetic_functors math functors@endlink.
166 template<typename _Tp>
167 struct plus : public binary_function<_Tp, _Tp, _Tp>
169 _GLIBCXX14_CONSTEXPR
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>
179 _GLIBCXX14_CONSTEXPR
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>
189 _GLIBCXX14_CONSTEXPR
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>
199 _GLIBCXX14_CONSTEXPR
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>
209 _GLIBCXX14_CONSTEXPR
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>
219 _GLIBCXX14_CONSTEXPR
221 operator()(const _Tp& __x) const
222 { return -__x; }
225 #if __cplusplus > 201103L
227 #define __cpp_lib_transparent_operators 201510
229 template<>
230 struct plus<void>
232 template <typename _Tp, typename _Up>
233 _GLIBCXX14_CONSTEXPR
234 auto
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.
244 template<>
245 struct minus<void>
247 template <typename _Tp, typename _Up>
248 _GLIBCXX14_CONSTEXPR
249 auto
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.
259 template<>
260 struct multiplies<void>
262 template <typename _Tp, typename _Up>
263 _GLIBCXX14_CONSTEXPR
264 auto
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.
274 template<>
275 struct divides<void>
277 template <typename _Tp, typename _Up>
278 _GLIBCXX14_CONSTEXPR
279 auto
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.
289 template<>
290 struct modulus<void>
292 template <typename _Tp, typename _Up>
293 _GLIBCXX14_CONSTEXPR
294 auto
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.
304 template<>
305 struct negate<void>
307 template <typename _Tp>
308 _GLIBCXX14_CONSTEXPR
309 auto
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;
317 #endif
318 /** @} */
320 // 20.3.3 comparisons
321 /** @defgroup comparison_functors Comparison Classes
322 * @ingroup functors
324 * The library provides six wrapper functors for all the basic comparisons
325 * in C++, like @c <.
327 * @{
329 #if __cplusplus > 201103L
330 template<typename _Tp = void>
331 struct equal_to;
333 template<typename _Tp = void>
334 struct not_equal_to;
336 template<typename _Tp = void>
337 struct greater;
339 template<typename _Tp = void>
340 struct less;
342 template<typename _Tp = void>
343 struct greater_equal;
345 template<typename _Tp = void>
346 struct less_equal;
347 #endif
349 /// One of the @link comparison_functors comparison functors@endlink.
350 template<typename _Tp>
351 struct equal_to : public binary_function<_Tp, _Tp, bool>
353 _GLIBCXX14_CONSTEXPR
354 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>
363 _GLIBCXX14_CONSTEXPR
364 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>
373 _GLIBCXX14_CONSTEXPR
374 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>
383 _GLIBCXX14_CONSTEXPR
384 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>
393 _GLIBCXX14_CONSTEXPR
394 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>
403 _GLIBCXX14_CONSTEXPR
404 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))
417 return __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))
430 return __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))
443 return __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))
456 return __x <= __y;
457 return (__UINTPTR_TYPE__)__x <= (__UINTPTR_TYPE__)__y;
461 #if __cplusplus >= 201402L
462 /// One of the @link comparison_functors comparison functors@endlink.
463 template<>
464 struct equal_to<void>
466 template <typename _Tp, typename _Up>
467 constexpr auto
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.
477 template<>
478 struct not_equal_to<void>
480 template <typename _Tp, typename _Up>
481 constexpr auto
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.
491 template<>
492 struct greater<void>
494 template <typename _Tp, typename _Up>
495 constexpr auto
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>
505 constexpr bool
506 operator()(_Tp* __t, _Up* __u) const noexcept
507 { return greater<common_type_t<_Tp*, _Up*>>{}(__t, __u); }
509 typedef __is_transparent is_transparent;
511 private:
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>()))>>
534 : false_type { };
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>()))>>
544 : false_type { };
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.
553 template<>
554 struct less<void>
556 template <typename _Tp, typename _Up>
557 constexpr auto
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>
567 constexpr bool
568 operator()(_Tp* __t, _Up* __u) const noexcept
569 { return less<common_type_t<_Tp*, _Up*>>{}(__t, __u); }
571 typedef __is_transparent is_transparent;
573 private:
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>()))>>
596 : false_type { };
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>()))>>
606 : false_type { };
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.
615 template<>
616 struct greater_equal<void>
618 template <typename _Tp, typename _Up>
619 constexpr auto
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>
629 constexpr bool
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;
635 private:
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>()))>>
658 : false_type { };
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>()))>>
668 : false_type { };
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.
677 template<>
678 struct less_equal<void>
680 template <typename _Tp, typename _Up>
681 constexpr auto
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>
691 constexpr bool
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;
697 private:
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>()))>>
720 : false_type { };
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>()))>>
730 : false_type { };
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*>>;
737 #endif // C++14
738 /** @} */
740 // 20.3.4 logical operations
741 /** @defgroup logical_functors Boolean Operations Classes
742 * @ingroup functors
744 * Here are wrapper functors for Boolean operations: @c &&, @c ||,
745 * and @c !.
747 * @{
749 #if __cplusplus > 201103L
750 template<typename _Tp = void>
751 struct logical_and;
753 template<typename _Tp = void>
754 struct logical_or;
756 template<typename _Tp = void>
757 struct logical_not;
758 #endif
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>
764 _GLIBCXX14_CONSTEXPR
765 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>
774 _GLIBCXX14_CONSTEXPR
775 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>
784 _GLIBCXX14_CONSTEXPR
785 bool
786 operator()(const _Tp& __x) const
787 { return !__x; }
790 #if __cplusplus > 201103L
791 /// One of the @link logical_functors Boolean operations functors@endlink.
792 template<>
793 struct logical_and<void>
795 template <typename _Tp, typename _Up>
796 _GLIBCXX14_CONSTEXPR
797 auto
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.
807 template<>
808 struct logical_or<void>
810 template <typename _Tp, typename _Up>
811 _GLIBCXX14_CONSTEXPR
812 auto
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.
822 template<>
823 struct logical_not<void>
825 template <typename _Tp>
826 _GLIBCXX14_CONSTEXPR
827 auto
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;
835 #endif
836 /** @} */
838 #if __cplusplus > 201103L
839 template<typename _Tp = void>
840 struct bit_and;
842 template<typename _Tp = void>
843 struct bit_or;
845 template<typename _Tp = void>
846 struct bit_xor;
848 template<typename _Tp = void>
849 struct bit_not;
850 #endif
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>
857 _GLIBCXX14_CONSTEXPR
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>
866 _GLIBCXX14_CONSTEXPR
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>
875 _GLIBCXX14_CONSTEXPR
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>
884 _GLIBCXX14_CONSTEXPR
886 operator()(const _Tp& __x) const
887 { return ~__x; }
890 #if __cplusplus > 201103L
891 template <>
892 struct bit_and<void>
894 template <typename _Tp, typename _Up>
895 _GLIBCXX14_CONSTEXPR
896 auto
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;
905 template <>
906 struct bit_or<void>
908 template <typename _Tp, typename _Up>
909 _GLIBCXX14_CONSTEXPR
910 auto
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;
919 template <>
920 struct bit_xor<void>
922 template <typename _Tp, typename _Up>
923 _GLIBCXX14_CONSTEXPR
924 auto
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;
933 template <>
934 struct bit_not<void>
936 template <typename _Tp>
937 _GLIBCXX14_CONSTEXPR
938 auto
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;
946 #endif
948 // 20.3.5 negators
949 /** @defgroup negators Negators
950 * @ingroup functors
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,
959 * \code
960 * struct IntGreaterThanThree
961 * : public std::unary_function<int, bool>
963 * bool operator() (int x) { return x > 3; }
964 * };
966 * std::find_if (v.begin(), v.end(), not1(IntGreaterThanThree()));
967 * \endcode
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.
975 * @{
977 /// One of the @link negators negation functors@endlink.
978 template<typename _Predicate>
979 class unary_negate
980 : public unary_function<typename _Predicate::argument_type, bool>
982 protected:
983 _Predicate _M_pred;
985 public:
986 _GLIBCXX14_CONSTEXPR
987 explicit
988 unary_negate(const _Predicate& __x) : _M_pred(__x) { }
990 _GLIBCXX14_CONSTEXPR
991 bool
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>
998 _GLIBCXX14_CONSTEXPR
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>
1005 class binary_negate
1006 : public binary_function<typename _Predicate::first_argument_type,
1007 typename _Predicate::second_argument_type, bool>
1009 protected:
1010 _Predicate _M_pred;
1012 public:
1013 _GLIBCXX14_CONSTEXPR
1014 explicit
1015 binary_negate(const _Predicate& __x) : _M_pred(__x) { }
1017 _GLIBCXX14_CONSTEXPR
1018 bool
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); }
1030 /** @} */
1032 // 20.3.7 adaptors pointers functions
1033 /** @defgroup pointer_adaptors Adaptors for pointers to functions
1034 * @ingroup functors
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.
1052 * @{
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>
1058 protected:
1059 _Result (*_M_ptr)(_Arg);
1061 public:
1062 pointer_to_unary_function() { }
1064 explicit
1065 pointer_to_unary_function(_Result (*__x)(_Arg))
1066 : _M_ptr(__x) { }
1068 _Result
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>
1084 protected:
1085 _Result (*_M_ptr)(_Arg1, _Arg2);
1087 public:
1088 pointer_to_binary_function() { }
1090 explicit
1091 pointer_to_binary_function(_Result (*__x)(_Arg1, _Arg2))
1092 : _M_ptr(__x) { }
1094 _Result
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); }
1104 /** @} */
1106 template<typename _Tp>
1107 struct _Identity
1108 : public unary_function<_Tp, _Tp>
1110 _Tp&
1111 operator()(_Tp& __x) const
1112 { return __x; }
1114 const _Tp&
1115 operator()(const _Tp& __x) const
1116 { return __x; }
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>
1123 struct _Select1st
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; }
1144 #endif
1147 template<typename _Pair>
1148 struct _Select2nd
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
1162 * @ingroup functors
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
1166 * one argument.
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.
1174 * @{
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>
1181 public:
1182 explicit
1183 mem_fun_t(_Ret (_Tp::*__pf)())
1184 : _M_f(__pf) { }
1186 _Ret
1187 operator()(_Tp* __p) const
1188 { return (__p->*_M_f)(); }
1190 private:
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>
1199 public:
1200 explicit
1201 const_mem_fun_t(_Ret (_Tp::*__pf)() const)
1202 : _M_f(__pf) { }
1204 _Ret
1205 operator()(const _Tp* __p) const
1206 { return (__p->*_M_f)(); }
1208 private:
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>
1217 public:
1218 explicit
1219 mem_fun_ref_t(_Ret (_Tp::*__pf)())
1220 : _M_f(__pf) { }
1222 _Ret
1223 operator()(_Tp& __r) const
1224 { return (__r.*_M_f)(); }
1226 private:
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>
1235 public:
1236 explicit
1237 const_mem_fun_ref_t(_Ret (_Tp::*__pf)() const)
1238 : _M_f(__pf) { }
1240 _Ret
1241 operator()(const _Tp& __r) const
1242 { return (__r.*_M_f)(); }
1244 private:
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>
1253 public:
1254 explicit
1255 mem_fun1_t(_Ret (_Tp::*__pf)(_Arg))
1256 : _M_f(__pf) { }
1258 _Ret
1259 operator()(_Tp* __p, _Arg __x) const
1260 { return (__p->*_M_f)(__x); }
1262 private:
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>
1271 public:
1272 explicit
1273 const_mem_fun1_t(_Ret (_Tp::*__pf)(_Arg) const)
1274 : _M_f(__pf) { }
1276 _Ret
1277 operator()(const _Tp* __p, _Arg __x) const
1278 { return (__p->*_M_f)(__x); }
1280 private:
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>
1289 public:
1290 explicit
1291 mem_fun1_ref_t(_Ret (_Tp::*__pf)(_Arg))
1292 : _M_f(__pf) { }
1294 _Ret
1295 operator()(_Tp& __r, _Arg __x) const
1296 { return (__r.*_M_f)(__x); }
1298 private:
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>
1307 public:
1308 explicit
1309 const_mem_fun1_ref_t(_Ret (_Tp::*__pf)(_Arg) const)
1310 : _M_f(__pf) { }
1312 _Ret
1313 operator()(const _Tp& __r, _Arg __x) const
1314 { return (__r.*_M_f)(__x); }
1316 private:
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); }
1362 /** @} */
1364 _GLIBCXX_END_NAMESPACE_VERSION
1365 } // namespace
1367 #if (__cplusplus < 201103L) || _GLIBCXX_USE_DEPRECATED
1368 # include <backward/binders.h>
1369 #endif
1371 #endif /* _STL_FUNCTION_H */