| 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 |
#include <testsuite_iterators.h> |
| 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 |
template<int N> friend bool operator==(Iter<N>, Iter<N>); |
| 52 |
template<int N> friend std::weak_ordering operator<=>(Iter<N>, Iter<N>); |
| 53 |
}; |
| 54 |
|
| 55 |
static_assert( std::random_access_iterator<Iter<0>> ); |
| 56 |
|
| 57 |
int operator==(Iter<0>, long*); |
| 58 |
void* operator< (Iter<1>, long*); |
| 59 |
bool& operator< (long*, Iter<2>); |
| 60 |
|
| 61 |
using std::move_iterator; |
| 62 |
|
| 63 |
static_assert( std::three_way_comparable<move_iterator<Iter<0>>> ); |
| 64 |
|
| 65 |
move_iterator<Iter<0>> l0{Iter<0>()}; |
| 66 |
move_iterator<Iter<1>> l1{Iter<1>()}; |
| 67 |
move_iterator<Iter<2>> l2{Iter<2>()}; |
| 68 |
move_iterator<long*> r{nullptr}; |
| 69 |
|
| 70 |
bool b0 = l0 == r; |
| 71 |
bool b1 = l0 != r; |
| 72 |
bool b2 = l1 < r; |
| 73 |
bool b3 = l2 > r; |
| 74 |
bool b4 = l2 <= r; |
| 75 |
bool b5 = l1 >= r; |
| 76 |
|
| 77 |
template<int N> |
| 78 |
concept has_eq |
| 79 |
= requires (move_iterator<Iter<N>> l, move_iterator<long*> r) |
| 80 |
{ l == r; }; |
| 81 |
|
| 82 |
template<int N> |
| 83 |
concept has_ne |
| 84 |
= requires (move_iterator<Iter<N>> l, move_iterator<long*> r) |
| 85 |
{ l != r; }; |
| 86 |
|
| 87 |
template<int N> |
| 88 |
concept has_lt |
| 89 |
= requires (move_iterator<Iter<N>> l, move_iterator<long*> r) |
| 90 |
{ l < r; }; |
| 91 |
|
| 92 |
template<int N> |
| 93 |
concept has_gt |
| 94 |
= requires (move_iterator<Iter<N>> l, move_iterator<long*> r) |
| 95 |
{ l > r; }; |
| 96 |
|
| 97 |
template<int N> |
| 98 |
concept has_le |
| 99 |
= requires (move_iterator<Iter<N>> l, move_iterator<long*> r) |
| 100 |
{ l <= r; }; |
| 101 |
|
| 102 |
template<int N> |
| 103 |
concept has_ge |
| 104 |
= requires (move_iterator<Iter<N>> l, move_iterator<long*> r) |
| 105 |
{ l >= r; }; |
| 106 |
|
| 107 |
static_assert( has_eq<0> ); |
| 108 |
static_assert( ! has_eq<1> ); |
| 109 |
static_assert( ! has_eq<2> ); |
| 110 |
|
| 111 |
static_assert( has_ne<0> ); // uses synthesized operator!= |
| 112 |
static_assert( ! has_ne<1> ); |
| 113 |
static_assert( ! has_ne<2> ); |
| 114 |
|
| 115 |
static_assert( ! has_lt<0> ); |
| 116 |
static_assert( has_lt<1> ); |
| 117 |
static_assert( ! has_lt<2> ); |
| 118 |
|
| 119 |
static_assert( ! has_gt<0> ); |
| 120 |
static_assert( ! has_gt<1> ); |
| 121 |
static_assert( has_gt<2> ); |
| 122 |
|
| 123 |
static_assert( ! has_le<0> ); |
| 124 |
static_assert( ! has_le<1> ); |
| 125 |
static_assert( has_le<2> ); |
| 126 |
|
| 127 |
static_assert( ! has_ge<0> ); |
| 128 |
static_assert( has_ge<1> ); |
| 129 |
static_assert( ! has_ge<2> ); |
| 130 |
|
| 131 |
int arr[3] = { 1, 2, 3 }; |
| 132 |
constexpr std::move_iterator<int*> beg{std::begin(arr)}; |
| 133 |
constexpr std::move_iterator<const int*> cbeg{std::cbegin(arr)}; |
| 134 |
static_assert( beg == cbeg ); |
| 135 |
static_assert( beg <= cbeg ); |
| 136 |
static_assert( beg >= cbeg ); |
| 137 |
static_assert( std::is_eq(beg <=> cbeg) ); |
| 138 |
constexpr std::move_iterator<const int*> cend{std::cend(arr)}; |
| 139 |
static_assert( beg != cend ); |
| 140 |
static_assert( beg < cend ); |
| 141 |
static_assert( cend > beg ); |
| 142 |
static_assert( beg <= cend ); |
| 143 |
static_assert( cend >= beg ); |
| 144 |
static_assert( std::is_lt(beg <=> cend) ); |
| 145 |
|
| 146 |
template<typename I> |
| 147 |
concept has_plus = requires(std::iter_difference_t<I> n, I i) { |
| 148 |
{ n + i } -> std::same_as<I>; |
| 149 |
}; |
| 150 |
|
| 151 |
using namespace __gnu_test; |
| 152 |
using MBI = std::move_iterator<bidirectional_iterator_wrapper<int>>; |
| 153 |
static_assert( ! has_plus<MBI> ); |
| 154 |
using MRI = std::move_iterator<random_access_iterator_wrapper<int>>; |
| 155 |
static_assert( has_plus<MRI> ); |