|
| 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 <ranges> |
| 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<chunk_view>::<i>iterator</i><false></code>, |
| 33 | +while the latter returns <code>chunk_view::<i>iterator</i><true></code>. |
| 34 | +Since the former can only be compared with <code>chunk_view::<i>iterator</i><false></code> that cannot |
| 35 | +be converted from <code>chunk_view::<i>iterator</i><true></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<bool OtherConst = !Const> |
| 41 | + requires sentinel_for<sentinel_t<<i>maybe-const</i><OtherConst, V>>, iterator_t<<i>Base</i>>> |
| 42 | +friend constexpr bool operator==(const <i>iterator</i>& x, const sentinel_t<<i>maybe-const</i><OtherConst, V>>& 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<chunk_view::iterator<true>></code>, which cannot be compared to any |
| 48 | +non-copyable iterators as its <code>operator==(const S& 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