|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | +<head> |
| 4 | +<meta charset="utf-8"> |
| 5 | +<title>Issue 4249: The past end issue for lazy_split_view</title> |
| 6 | +<meta property="og:title" content="Issue 4249: The past end issue for lazy_split_view"> |
| 7 | +<meta property="og:description" content="C++ library issue. Status: New"> |
| 8 | +<meta property="og:url" content="https://cplusplus.github.io/LWG/issue4249.html"> |
| 9 | +<meta property="og:type" content="website"> |
| 10 | +<meta property="og:image" content="http://cplusplus.github.io/LWG/images/cpp_logo.png"> |
| 11 | +<meta property="og:image:alt" content="C++ logo"> |
| 12 | +<style> |
| 13 | + p {text-align:justify} |
| 14 | + li {text-align:justify} |
| 15 | + pre code.backtick::before { content: "`" } |
| 16 | + pre code.backtick::after { content: "`" } |
| 17 | + blockquote.note |
| 18 | + { |
| 19 | + background-color:#E0E0E0; |
| 20 | + padding-left: 15px; |
| 21 | + padding-right: 15px; |
| 22 | + padding-top: 1px; |
| 23 | + padding-bottom: 1px; |
| 24 | + } |
| 25 | + ins {background-color:#A0FFA0} |
| 26 | + del {background-color:#FFA0A0} |
| 27 | + table.issues-index { border: 1px solid; border-collapse: collapse; } |
| 28 | + table.issues-index th { text-align: center; padding: 4px; border: 1px solid; } |
| 29 | + table.issues-index td { padding: 4px; border: 1px solid; } |
| 30 | + table.issues-index td:nth-child(1) { text-align: right; } |
| 31 | + table.issues-index td:nth-child(2) { text-align: left; } |
| 32 | + table.issues-index td:nth-child(3) { text-align: left; } |
| 33 | + table.issues-index td:nth-child(4) { text-align: left; } |
| 34 | + table.issues-index td:nth-child(5) { text-align: center; } |
| 35 | + table.issues-index td:nth-child(6) { text-align: center; } |
| 36 | + table.issues-index td:nth-child(7) { text-align: left; } |
| 37 | + table.issues-index td:nth-child(5) span.no-pr { color: red; } |
| 38 | + @media (prefers-color-scheme: dark) { |
| 39 | + html { |
| 40 | + color: #ddd; |
| 41 | + background-color: black; |
| 42 | + } |
| 43 | + ins { |
| 44 | + background-color: #225522 |
| 45 | + } |
| 46 | + del { |
| 47 | + background-color: #662222 |
| 48 | + } |
| 49 | + a { |
| 50 | + color: #6af |
| 51 | + } |
| 52 | + a:visited { |
| 53 | + color: #6af |
| 54 | + } |
| 55 | + blockquote.note |
| 56 | + { |
| 57 | + background-color: rgba(255, 255, 255, .10) |
| 58 | + } |
| 59 | + } |
| 60 | +</style> |
| 61 | +</head> |
| 62 | +<body> |
| 63 | +<hr> |
| 64 | +<p><em>This page is a snapshot from the LWG issues list, see the <a href="lwg-active.html">Library Active Issues List</a> for more information and the meaning of <a href="lwg-active.html#New">New</a> status.</em></p> |
| 65 | +<h3 id="4249"><a href="lwg-active.html#4249">4249</a>. The past end issue for <code class='backtick'>lazy_split_view</code></h3> |
| 66 | +<p><b>Section:</b> 25.7.16.3 <a href="https://wg21.link/range.lazy.split.outer">[range.lazy.split.outer]</a> <b>Status:</b> <a href="lwg-active.html#New">New</a> |
| 67 | + <b>Submitter:</b> Hewill Kang <b>Opened:</b> 2025-04-26 <b>Last modified:</b> 2025-04-27</p> |
| 68 | +<p><b>Priority: </b>Not Prioritized |
| 69 | +</p> |
| 70 | +<p><b>View other</b> <a href="lwg-index-open.html#range.lazy.split.outer">active issues</a> in [range.lazy.split.outer].</p> |
| 71 | +<p><b>View all other</b> <a href="lwg-index.html#range.lazy.split.outer">issues</a> in [range.lazy.split.outer].</p> |
| 72 | +<p><b>View all issues with</b> <a href="lwg-status.html#New">New</a> status.</p> |
| 73 | +<p><b>Discussion:</b></p> |
| 74 | +<p> |
| 75 | +Consider (<a href="https://godbolt.org/z/eeMe8aTqv">demo</a>): |
| 76 | +</p> |
| 77 | +<blockquote><pre> |
| 78 | +#include <print> |
| 79 | +#include <ranges> |
| 80 | +#include <sstream> |
| 81 | + |
| 82 | +int main() { |
| 83 | + std::istringstream is{"1 0 2 0 3"}; |
| 84 | + auto r = std::views::istream<int>(is) |
| 85 | + | std::views::lazy_split(0) |
| 86 | + | std::views::stride(2); |
| 87 | + std::println("{}", r); // should print [[1], [3]] |
| 88 | +} |
| 89 | +</pre></blockquote> |
| 90 | +<p> |
| 91 | +The above leads to SIGSEGV in libstdc++, the reason is that we are iterating over the nested |
| 92 | +range as: |
| 93 | +</p> |
| 94 | +<blockquote><pre> |
| 95 | +for (auto&& inner : r) { |
| 96 | + for (auto&& elem : inner) { |
| 97 | + // […] |
| 98 | + } |
| 99 | +} |
| 100 | +</pre></blockquote> |
| 101 | +<p> |
| 102 | +which is disassembled as: |
| 103 | +</p> |
| 104 | +<blockquote><pre> |
| 105 | +auto outer_it = r.begin(); |
| 106 | +std::default_sentinel_t out_end = r.end(); |
| 107 | +for(; outer_it != out_end; ++outer_it) { |
| 108 | + auto&& inner_r = *outer_it; |
| 109 | + auto inner_it = inner_r.begin(); |
| 110 | + std::default_sentinel_t inner_end = inner_r.end(); |
| 111 | + for(; inner_it != inner_end; ++inner_it) { |
| 112 | + auto&& elem = *inner_it; |
| 113 | + // […] |
| 114 | + } |
| 115 | +} |
| 116 | +</pre></blockquote> |
| 117 | +<p> |
| 118 | +Since <code class='backtick'>inner_it</code> and <code class='backtick'>output_it</code> actually update the same iterator, |
| 119 | +when we back to the outer loop, <code>lazy_split_view::<i>outer-iterator</i></code> |
| 120 | +is now equal to <code class='backtick'>default_sentinel</code>, which makes <code class='backtick'>output_it</code> reach the end, |
| 121 | +so <code class='backtick'>++outer_it</code> will increment the iterator past end, triggering the assertion. |
| 122 | +</p> |
| 123 | +<p> |
| 124 | +Note that this <a href="https://godbolt.org/z/6Tx61eeq8">also happens</a> in MSVC-STL |
| 125 | +when <code class='backtick'>_ITERATOR_DEBUG_LEVEL</code> is turned on. |
| 126 | +</p> |
| 127 | +<p> |
| 128 | +It seems that extra flags are needed to fix this issue because <code class='backtick'>output_it</code> should not |
| 129 | +be considered to reach the end when we back to the outer loop. |
| 130 | +</p> |
| 131 | + |
| 132 | + |
| 133 | +<p id="res-4249"><b>Proposed resolution:</b></p> |
| 134 | +<p> |
| 135 | +This wording is relative to <a href="https://wg21.link/N5008">N5008</a>. |
| 136 | +</p> |
| 137 | + |
| 138 | +<ol> |
| 139 | + |
| 140 | +<li><p>Modify 25.7.16.3 <a href="https://wg21.link/range.lazy.split.outer">[range.lazy.split.outer]</a> as indicated:</p> |
| 141 | + |
| 142 | +<blockquote> |
| 143 | +<blockquote> |
| 144 | +<pre> |
| 145 | +namespace std::ranges { |
| 146 | + template<input_range V, forward_range Pattern> |
| 147 | + requires view<V> && view<Pattern> && |
| 148 | + indirectly_comparable<iterator_t<V>, iterator_t<Pattern>, ranges::equal_to> && |
| 149 | + (forward_range<V> || <i>tiny-range</i><Pattern>) |
| 150 | + template<bool Const> |
| 151 | + struct lazy_split_view<V, Pattern>::<i>outer-iterator</i> { |
| 152 | + private: |
| 153 | + using <i>Parent</i> = <i>maybe-const</i><Const, lazy_split_view>; // <i>exposition only</i> |
| 154 | + using <i>Base</i> = <i>maybe-const</i><Const, V>; // <i>exposition only</i> |
| 155 | + <i>Parent</i>* <i>parent_</i> = nullptr; // <i>exposition only</i> |
| 156 | + |
| 157 | + iterator_t<<i>Base</i>> <i>current_</i> = iterator_t<<i>Base</i>>(); // <i>exposition only, present only</i> |
| 158 | + // <i>if V models forward_range</i> |
| 159 | + |
| 160 | + bool <i>trailing_empty_</i> = false; // <i>exposition only</i> |
| 161 | + <ins>bool <i>has_next_</i> = false; // <i>exposition only, present only</i></ins> |
| 162 | + <ins>// <i>if forward_range<<i>V</i>> is false</i></ins> |
| 163 | + public: |
| 164 | + […] |
| 165 | + }; |
| 166 | +} |
| 167 | +</pre> |
| 168 | +</blockquote> |
| 169 | +[…] |
| 170 | +<pre> |
| 171 | +constexpr explicit <i>outer-iterator</i>(<i>Parent</i>& parent) |
| 172 | + requires (!forward_range<<i>Base</i>>); |
| 173 | +</pre> |
| 174 | +<blockquote> |
| 175 | +<p> |
| 176 | +-2- <i>Effects:</i> Initializes <code><i>parent_</i></code> with <code class='backtick'>addressof(parent)</code> <ins>and |
| 177 | +<code><i>has_next_</i></code> with <code><i>current</i> != ranges::end(<i>parent_</i>-><i>base_</i>)</code></ins>. |
| 178 | +</p> |
| 179 | +</blockquote> |
| 180 | +[…] |
| 181 | +<pre> |
| 182 | +constexpr <i>outer-iterator</i>& operator++(); |
| 183 | +</pre> |
| 184 | +<blockquote> |
| 185 | +<p> |
| 186 | +-6- <i>Effects:</i> Equivalent to: |
| 187 | +</p> |
| 188 | +<blockquote><pre> |
| 189 | +const auto end = ranges::end(<i>parent_</i>-><i>base_</i>); |
| 190 | +if (<i>current</i> == end) { |
| 191 | + <i>trailing_empty_</i> = false; |
| 192 | + <ins>if constexpr (!forward_range<V>) |
| 193 | + <i>has_next_</i> = false;</ins> |
| 194 | + return <code>*this</code>; |
| 195 | +} |
| 196 | +const auto [pbegin, pend] = subrange{<i>parent_</i>-><i>pattern_</i>}; |
| 197 | +if (pbegin == pend) ++<i>current</i>; |
| 198 | +else if constexpr (<i>tiny-range</i><Pattern>) { |
| 199 | + <i>current</i> = ranges::find(std::move(<i>current</i>), end, *pbegin); |
| 200 | + if (<i>current</i> != end) { |
| 201 | + ++<i>current</i>; |
| 202 | + if (<i>current</i> == end) |
| 203 | + <i>trailing_empty_</i> = true; |
| 204 | + } |
| 205 | +} |
| 206 | +else { |
| 207 | + do { |
| 208 | + auto [b, p] = ranges::mismatch(<i>current</i>, end, pbegin, pend); |
| 209 | + if (p == pend) { |
| 210 | + <i>current</i> = b; |
| 211 | + if (<i>current</i> == end) |
| 212 | + <i>trailing_empty_</i> = true; |
| 213 | + break; <i>// The pattern matched; skip it</i> |
| 214 | + } |
| 215 | + } while (++<i>current</i> != end); |
| 216 | +} |
| 217 | +<ins>if constexpr (!forward_range<V>) |
| 218 | + if (<i>current</i> == end) |
| 219 | + <i>has_next_</i> = false;</ins> |
| 220 | +return *this; |
| 221 | +</pre></blockquote> |
| 222 | +</blockquote> |
| 223 | +[…] |
| 224 | +<pre> |
| 225 | +friend constexpr bool operator==(const <i>outer-iterator</i>& x, default_sentinel_t); |
| 226 | +</pre> |
| 227 | +<blockquote> |
| 228 | +<p> |
| 229 | +-8- <i>Effects:</i> Equivalent to: |
| 230 | +</p> |
| 231 | +<blockquote><pre> |
| 232 | +<ins>if constexpr (!forward_range<V>) |
| 233 | + return !x.<i>has_next_</i> && !x.<i>trailing_empty_</i>; |
| 234 | +else</ins> |
| 235 | + return x.<i>current</i> == ranges::end(x.<i>parent_</i>-><i>base_</i>) && !x.<i>trailing_empty_</i>; |
| 236 | +</pre></blockquote> |
| 237 | +</blockquote> |
| 238 | +</blockquote> |
| 239 | + |
| 240 | +</li> |
| 241 | + |
| 242 | +</ol> |
| 243 | + |
| 244 | + |
| 245 | + |
| 246 | + |
| 247 | + |
| 248 | +</body> |
| 249 | +</html> |
0 commit comments