Debian Codegraph @

libstdc++-v3/testsuite/24_iterators/reverse_iterator/rel_ops_c++20.cc

raw
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 { target c++20 } }
19
20 #include <iterator>
21
22 template<int>
23 struct Iter
24 {
25 using iterator_category = std::random_access_iterator_tag;
26 using value_type = int;
27 using pointer = int*;
28 using reference = int&;
29 using difference_type = std::ptrdiff_t;
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 static_assert( std::random_access_iterator<Iter<0>> );
59
60 // Define a single kind of mixed-type comparison for each specialization.
61 int operator==(Iter<0>, long*);
62 void* operator!=(Iter<1>, long*);
63 bool& operator< (Iter<2>, long*);
64 int operator> (Iter<3>, long*);
65 void* operator<=(Iter<4>, long*);
66 bool& operator>=(Iter<5>, long*);
67
68 using std::reverse_iterator;
69
70 reverse_iterator<Iter<0>> l0{Iter<0>()};
71 reverse_iterator<Iter<1>> l1{Iter<1>()};
72 reverse_iterator<Iter<2>> l2{Iter<2>()};
73 reverse_iterator<Iter<3>> l3{Iter<3>()};
74 reverse_iterator<Iter<4>> l4{Iter<4>()};
75 reverse_iterator<Iter<5>> l5{Iter<5>()};
76 reverse_iterator<long*> r{nullptr};
77
78 bool b0 = l0 == r;
79 bool b1 = l1 != r;
80 bool b2 = l2 > r;
81 bool b3 = l3 < r;
82 bool b4 = l4 >= r;
83 bool b5 = l5 <= r;
84
85 template<int N>
86 concept has_eq
87 = requires (reverse_iterator<Iter<N>> l, reverse_iterator<long*> r)
88 { l == r; };
89
90 template<int N>
91 concept has_ne
92 = requires (reverse_iterator<Iter<N>> l, reverse_iterator<long*> r)
93 { l != r; };
94
95 template<int N>
96 concept has_lt
97 = requires (reverse_iterator<Iter<N>> l, reverse_iterator<long*> r)
98 { l < r; };
99
100 template<int N>
101 concept has_gt
102 = requires (reverse_iterator<Iter<N>> l, reverse_iterator<long*> r)
103 { l > r; };
104
105 template<int N>
106 concept has_le
107 = requires (reverse_iterator<Iter<N>> l, reverse_iterator<long*> r)
108 { l <= r; };
109
110 template<int N>
111 concept has_ge
112 = requires (reverse_iterator<Iter<N>> l, reverse_iterator<long*> r)
113 { l >= r; };
114
115 static_assert( has_eq<0> );
116 static_assert( ! has_eq<1> );
117 static_assert( ! has_eq<2> );
118 static_assert( ! has_eq<3> );
119 static_assert( ! has_eq<4> );
120 static_assert( ! has_eq<5> );
121
122 static_assert( has_ne<0> ); // uses synthesized operator!=
123 static_assert( has_ne<1> );
124 static_assert( ! has_ne<2> );
125 static_assert( ! has_ne<3> );
126 static_assert( ! has_ne<4> );
127 static_assert( ! has_ne<5> );
128
129 static_assert( ! has_lt<0> );
130 static_assert( ! has_lt<1> );
131 static_assert( ! has_lt<2> );
132 static_assert( has_lt<3> );
133 static_assert( ! has_lt<4> );
134 static_assert( ! has_lt<5> );
135
136 static_assert( ! has_gt<0> );
137 static_assert( ! has_gt<1> );
138 static_assert( has_gt<2> );
139 static_assert( ! has_gt<3> );
140 static_assert( ! has_gt<4> );
141 static_assert( ! has_gt<5> );
142
143 static_assert( ! has_le<0> );
144 static_assert( ! has_le<1> );
145 static_assert( ! has_le<2> );
146 static_assert( ! has_le<3> );
147 static_assert( ! has_le<4> );
148 static_assert( has_le<5> );
149
150 static_assert( ! has_ge<0> );
151 static_assert( ! has_ge<1> );
152 static_assert( ! has_ge<2> );
153 static_assert( ! has_ge<3> );
154 static_assert( has_ge<4> );
155 static_assert( ! has_ge<5> );
156
157 int arr[3] = { 1, 2, 3 };
158 constexpr std::reverse_iterator<int*> rbeg = std::rbegin(arr);
159 constexpr std::reverse_iterator<const int*> crbeg = std::crbegin(arr);
160 static_assert( rbeg == crbeg );
161 static_assert( rbeg <= crbeg );
162 static_assert( rbeg >= crbeg );
163 static_assert( std::is_eq(rbeg <=> crbeg) );
164 constexpr std::reverse_iterator<const int*> crend = std::crend(arr);
165 static_assert( rbeg != crend );
166 static_assert( rbeg < crend );
167 static_assert( crend > rbeg );
168 static_assert( rbeg <= crend );
169 static_assert( crend >= rbeg );
170 static_assert( std::is_lt(rbeg <=> crend) );