Skip to content

Commit ee57e44

Browse files
committed
New issue from Hewill: "to_input_view::iterator cannot be compared to its const sentinel"
1 parent b93bfba commit ee57e44

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

xml/issue4226.xml

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?xml version='1.0' encoding='utf-8' standalone='no'?>
2+
<!DOCTYPE issue SYSTEM "lwg-issue.dtd">
3+
4+
<issue num="4226" status="New">
5+
<title><code>to_input_view::<i>iterator</i></code> cannot be compared to its `const` sentinel</title>
6+
<section>
7+
<sref ref="[range.to.input.view]"/>
8+
</section>
9+
<submitter>Hewill Kang</submitter>
10+
<date>15 Mar 2025</date>
11+
<priority>99</priority>
12+
13+
<discussion>
14+
<p>
15+
`to_input_view` was recently added to the working draft by <paper num="P3137R3"/>.
16+
<p/>
17+
Consider: (<a href="https://godbolt.org/z/7s1hPscrG">demo</a>):
18+
</p>
19+
<blockquote><pre>
20+
#include &lt;ranges&gt;
21+
22+
int main() {
23+
auto r = std::views::single(0)
24+
| std::views::chunk(1)
25+
| std::views::to_input;
26+
27+
r.begin() == std::as_const(r).end(); // <span style="color:red;font-weight:bolder">#1, error</span>
28+
r.begin() == r.cend(); // <span style="color:red;font-weight:bolder">#2, error</span>
29+
}
30+
</pre></blockquote>
31+
<p>
32+
In <code>#1</code>, <code>r.begin()</code> returns <code>to_input_view&lt;chunk_view&gt;::<i>iterator</i>&lt;false&gt;</code>,
33+
while the latter returns <code>chunk_view::<i>iterator</i>&lt;true&gt;</code>.
34+
Since the former can only be compared with <code>chunk_view::<i>iterator</i>&lt;false&gt;</code> that cannot
35+
be converted from <code>chunk_view::<i>iterator</i>&lt;true&gt;</code>, the two are incomparable.
36+
<p/>
37+
This can be fixed by adding the following overload to <code>to_input_view::<i>iterator</i></code>:
38+
</p>
39+
<blockquote><pre>
40+
template&lt;bool OtherConst = !Const&gt;
41+
requires sentinel_for&lt;sentinel_t&lt;<i>maybe-const</i>&lt;OtherConst, V&gt;&gt;, iterator_t&lt;<i>Base</i>&gt;&gt;
42+
friend constexpr bool operator==(const <i>iterator</i>&amp; x, const sentinel_t&lt;<i>maybe-const</i>&lt;OtherConst, V&gt;&gt;&amp; y)
43+
{ return x.<i>current_</i> == y; }
44+
</pre></blockquote>
45+
<p>
46+
Unfortunately, it still doesn't resolve <code>#2</code>, because <code>r.cend()</code> returns
47+
<code>basic_const_iterator&lt;chunk_view::iterator&lt;true&gt;&gt;</code>, which cannot be compared to any
48+
non-copyable iterators as its <code>operator==(const S&amp; s)</code> requires <code>S</code> to be a
49+
sentinel type, which rules out <code>to_input_view::<i>iterator</i></code>, so the constraint is not satisfied.
50+
<p/>
51+
I believe we may need to introduce a custom sentinel class for <code>to_input_view</code>.
52+
</p>
53+
</discussion>
54+
55+
<resolution>
56+
</resolution>
57+
58+
</issue>

0 commit comments

Comments
 (0)