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