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