1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
|
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14, c++17
// template<bool OtherConst>
// requires sentinel_for<sentinel_t<Base>, iterator_t<maybe-const<OtherConst, V>>>
// friend constexpr bool operator==(const iterator<OtherConst>& x, const sentinel& y);
#include <array>
#include <cassert>
#include <ranges>
#include "../types.h"
template <bool Const>
struct Iter {
std::tuple<int>* it_;
using value_type = std::tuple<int>;
using difference_type = std::intptr_t;
using iterator_concept = std::input_iterator_tag;
constexpr decltype(auto) operator*() const { return *it_; }
constexpr Iter& operator++() {
++it_;
return *this;
}
constexpr void operator++(int) { ++it_; }
};
template <bool Const>
struct Sent {
std::tuple<int>* end_;
constexpr bool operator==(const Iter<Const>& i) const { return i.it_ == end_; }
};
template <bool Const>
struct CrossComparableSent {
std::tuple<int>* end_;
template <bool C>
constexpr bool operator==(const Iter<C>& i) const {
return i.it_ == end_;
}
};
template <template <bool> typename St>
struct Range : TupleBufferView {
using TupleBufferView::TupleBufferView;
constexpr Iter<false> begin() { return Iter<false>{buffer_}; }
constexpr Iter<true> begin() const { return Iter<true>{buffer_}; }
constexpr St<false> end() { return St<false>{buffer_ + size_}; }
constexpr St<true> end() const { return St<true>{buffer_ + size_}; }
};
using R = Range<Sent>;
using CrossComparableR = Range<CrossComparableSent>;
// Test Constraint
template <class I, class S>
concept HasEqual = requires(const I i, const S s) { i == s; };
using std::ranges::elements_view;
using std::ranges::iterator_t;
using std::ranges::sentinel_t;
static_assert(HasEqual<iterator_t<elements_view<R, 0>>, //
sentinel_t<elements_view<R, 0>>>);
static_assert(!HasEqual<iterator_t<const elements_view<R, 0>>, //
sentinel_t<elements_view<R, 0>>>);
static_assert(!HasEqual<iterator_t<elements_view<R, 0>>, //
sentinel_t<const elements_view<R, 0>>>);
static_assert(HasEqual<iterator_t<const elements_view<R, 0>>, //
sentinel_t<const elements_view<R, 0>>>);
static_assert(HasEqual<iterator_t<elements_view<R, 0>>, //
sentinel_t<elements_view<R, 0>>>);
static_assert(HasEqual<iterator_t<const elements_view<CrossComparableR, 0>>, //
sentinel_t<elements_view<CrossComparableR, 0>>>);
static_assert(HasEqual<iterator_t<elements_view<CrossComparableR, 0>>, //
sentinel_t<const elements_view<CrossComparableR, 0>>>);
static_assert(HasEqual<iterator_t<const elements_view<CrossComparableR, 0>>, //
sentinel_t<const elements_view<CrossComparableR, 0>>>);
template <class R, bool ConstIter, bool ConstSent>
constexpr void testOne() {
auto getBegin = [](auto&& rng) {
if constexpr (ConstIter) {
return std::as_const(rng).begin();
} else {
return rng.begin();
}
};
auto getEnd = [](auto&& rng) {
if constexpr (ConstSent) {
return std::as_const(rng).end();
} else {
return rng.end();
}
};
// iter == sentinel.base
{
std::tuple<int> buffer[] = {{1}};
R v{buffer};
std::ranges::elements_view<R, 0> ev(v);
auto iter = getBegin(ev);
auto st = getEnd(ev);
++iter;
assert(iter == st);
}
// iter != sentinel.base
{
std::tuple<int> buffer[] = {{1}};
R v{buffer};
std::ranges::elements_view<R, 0> ev(v);
auto iter = getBegin(ev);
auto st = getEnd(ev);
assert(iter != st);
}
// empty range
{
std::array<std::tuple<int>, 0> arr;
R v{arr};
std::ranges::elements_view<R, 0> ev(v);
auto iter = getBegin(ev);
auto sent = getEnd(ev);
assert(iter == sent);
}
}
constexpr bool test() {
testOne<R, false, false>();
testOne<R, true, true>();
testOne<CrossComparableR, false, false>();
testOne<CrossComparableR, true, true>();
testOne<CrossComparableR, true, false>();
testOne<CrossComparableR, false, true>();
return true;
}
int main(int, char**) {
test();
static_assert(test());
return 0;
}
|