| 1 | // Copyright (C) 2020-2025 Free Software Foundation, Inc. |
| 2 | // |
| 3 | // This file is part of the GNU ISO C++ Library. This library is free |
| 4 | // software; you can redistribute it and/or modify it under the |
| 5 | // terms of the GNU General Public License as published by the |
| 6 | // Free Software Foundation; either version 3, or (at your option) |
| 7 | // any later version. |
| 8 | |
| 9 | // This library is distributed in the hope that it will be useful, |
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | // GNU General Public License for more details. |
| 13 | |
| 14 | // You should have received a copy of the GNU General Public License along |
| 15 | // with this library; see the file COPYING3. If not see |
| 16 | // <http://www.gnu.org/licenses/>. |
| 17 | |
| 18 | // { dg-do compile } |
| 19 | |
| 20 | #include <iterator> |
| 21 | |
| 22 | template<int> |
| 23 | struct Iter |
| 24 | { |
| 25 | typedef std::random_access_iterator_tag iterator_category; |
| 26 | typedef int value_type; |
| 27 | typedef int* pointer; |
| 28 | typedef int& reference; |
| 29 | typedef std::ptrdiff_t difference_type; |
| 30 | |
| 31 | Iter(); |
| 32 | |
| 33 | Iter& operator++(); |
| 34 | Iter operator++(int); |
| 35 | Iter& operator--(); |
| 36 | Iter operator--(int); |
| 37 | int& operator*() const; |
| 38 | int* operator->() const; |
| 39 | |
| 40 | int& operator[](difference_type) const; |
| 41 | |
| 42 | Iter& operator+=(difference_type); |
| 43 | Iter& operator-=(difference_type); |
| 44 | |
| 45 | template<int N> friend Iter operator+(Iter<N>, difference_type); |
| 46 | template<int N> friend Iter operator+(difference_type, Iter<N>); |
| 47 | template<int N> friend Iter operator-(Iter<N>, difference_type); |
| 48 | template<int N> friend difference_type operator-(Iter<N>, Iter<N>); |
| 49 | |
| 50 | // Define the full set of operators for same-type comparisons |
| 51 | template<int N> friend bool operator==(Iter<N>, Iter<N>); // synthesizes != |
| 52 | template<int N> friend bool operator<(Iter<N>, Iter<N>); |
| 53 | template<int N> friend bool operator>(Iter<N>, Iter<N>); |
| 54 | template<int N> friend bool operator<=(Iter<N>, Iter<N>); |
| 55 | template<int N> friend bool operator>=(Iter<N>, Iter<N>); |
| 56 | }; |
| 57 | |
| 58 | // Define a single kind of mixed-type comparison for each specialization. |
| 59 | int operator==(Iter<0>, long*); |
| 60 | void* operator!=(Iter<1>, long*); |
| 61 | bool& operator< (Iter<2>, long*); |
| 62 | int operator> (Iter<3>, long*); |
| 63 | void* operator<=(Iter<4>, long*); |
| 64 | bool& operator>=(Iter<5>, long*); |
| 65 | |
| 66 | using std::reverse_iterator; |
| 67 | |
| 68 | reverse_iterator< Iter<0> > l0; |
| 69 | reverse_iterator< Iter<1> > l1; |
| 70 | reverse_iterator< Iter<2> > l2; |
| 71 | reverse_iterator< Iter<3> > l3; |
| 72 | reverse_iterator< Iter<4> > l4; |
| 73 | reverse_iterator< Iter<5> > l5; |
| 74 | reverse_iterator<long*> r; |
| 75 | |
| 76 | // PR libstdc++/94354 |
| 77 | // Check that these operators use the correct operator on the |
| 78 | // underlying iterator types. |
| 79 | bool b0 = l0 == r; |
| 80 | bool b1 = l1 != r; |
| 81 | bool b2 = l2 > r; |
| 82 | bool b3 = l3 < r; |
| 83 | bool b4 = l4 >= r; |
| 84 | bool b5 = l5 <= r; |
| 85 | |
| 86 | #if __cplusplus >= 201703L |
| 87 | int arr[3] = { 1, 2, 3 }; |
| 88 | constexpr std::reverse_iterator<int*> rbeg = std::rbegin(arr); |
| 89 | constexpr std::reverse_iterator<const int*> crbeg = std::crbegin(arr); |
| 90 | static_assert( rbeg == crbeg ); |
| 91 | static_assert( rbeg <= crbeg ); |
| 92 | static_assert( rbeg >= crbeg ); |
| 93 | constexpr std::reverse_iterator<const int*> crend = std::crend(arr); |
| 94 | static_assert( rbeg != crend ); |
| 95 | static_assert( rbeg < crend ); |
| 96 | static_assert( crend > rbeg ); |
| 97 | static_assert( rbeg <= crend ); |
| 98 | static_assert( crend >= rbeg ); |
| 99 | #endif // C++17 |