|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | +<head> |
| 4 | +<meta charset="utf-8"> |
| 5 | +<title>Issue 4237: The standard library iterator adaptor does not handle iterator_category correctly</title> |
| 6 | +<meta property="og:title" content="Issue 4237: The standard library iterator adaptor does not handle iterator_category correctly"> |
| 7 | +<meta property="og:description" content="C++ library issue. Status: New"> |
| 8 | +<meta property="og:url" content="https://cplusplus.github.io/LWG/issue4237.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="4237"><a href="lwg-active.html#4237">4237</a>. The standard library iterator adaptor does not handle <code>iterator_category</code> correctly</h3> |
| 66 | +<p><b>Section:</b> 24.5.3.3 <a href="https://wg21.link/const.iterators.iterator">[const.iterators.iterator]</a>, 25.7 <a href="https://wg21.link/range.adaptors">[range.adaptors]</a> <b>Status:</b> <a href="lwg-active.html#New">New</a> |
| 67 | + <b>Submitter:</b> Hewill Kang <b>Opened:</b> 2025-03-27 <b>Last modified:</b> 2025-03-29</p> |
| 68 | +<p><b>Priority: </b>Not Prioritized |
| 69 | +</p> |
| 70 | +<p><b>View all issues with</b> <a href="lwg-status.html#New">New</a> status.</p> |
| 71 | +<p><b>Discussion:</b></p> |
| 72 | +<p> |
| 73 | +Currently, <code>basic_const_iterator</code>, and several range adaptors such as |
| 74 | +<code>filter_view</code>'s iterators provide <code>iterator_category</code> only when the |
| 75 | +underlying iterator models <code>forward_iterator</code>, implying that they expect those |
| 76 | +iterators should have a valid <code>iterator_category</code>. |
| 77 | +</p> |
| 78 | +<p> |
| 79 | +However, this is incorrect because being a <code>forward_iterator</code> does not |
| 80 | +necessarily mean it is a <i>Cpp17InputIterator</i>, it just means that it <i>probably</i> |
| 81 | +meets the syntactic requirements of <i>Cpp17InputIterator</i>. |
| 82 | +</p> |
| 83 | +<p> |
| 84 | +Any iterator that specializes <code>iterator_traits</code> and provides only |
| 85 | +<code>iterator_concept</code> without <code>iterator_category</code> is not a |
| 86 | +<i>Cpp17InputIterator</i>, for example, <code>common_iterator</code> with a |
| 87 | +<code>difference_type</code> of integer-class type. |
| 88 | +</p> |
| 89 | +<p> |
| 90 | +In this case, instantiating these iterator adaptors will result in a hard error because the |
| 91 | +<code>iterator_category</code> they expect does not exist. The following illustrates the |
| 92 | +problem (<a href="https://godbolt.org/z/j7x8bKzo4">demo</a>): |
| 93 | +</p> |
| 94 | +<blockquote><pre> |
| 95 | +#include <iterator> |
| 96 | +#include <ranges> |
| 97 | + |
| 98 | +int main() { |
| 99 | + auto r = std::views::iota(0ULL) |
| 100 | + | std::views::take(5) |
| 101 | + | std::views::common; |
| 102 | + |
| 103 | + static_assert(std::ranges::forward_range<decltype(r)>); |
| 104 | + |
| 105 | + std::basic_const_iterator ci(r.begin()); // <span style="color:red;font-weight:bolder">'iterator_category': is not a member of 'std::iterator_traits'</span> |
| 106 | + |
| 107 | + auto f = r | std::views::filter([](auto) { return true; }); |
| 108 | + auto b = f.begin(); // <span style="color:red;font-weight:bolder">'iterator_category': is not a member of 'std::iterator_traits'</span> |
| 109 | +} |
| 110 | +</pre></blockquote> |
| 111 | +<p> |
| 112 | +I believe that checking if the underlying iterator is a <code>forward_iterator</code> is not an appropriate |
| 113 | +mechanism to provide <code>iterator_category</code>, but rather checking if its <code>iterator_traits</code> |
| 114 | +specialization provides <code>iterator_category</code>. |
| 115 | +</p> |
| 116 | +<p> |
| 117 | +This issue is somewhat related to LWG <a href="lwg-active.html#3763" title="Should range adaptor iterators only provide iterator_category when its |
| 118 | +difference_type is not an integer-class type? (Status: New)">3763</a><sup><a href="https://cplusplus.github.io/LWG/issue3763" title="Latest snapshot">(i)</a></sup>, which is a further consideration after LWG |
| 119 | +<a href="lwg-defects.html#3749" title="common_iterator should handle integer-class difference types (Status: WP)">3749</a><sup><a href="https://cplusplus.github.io/LWG/issue3749" title="Latest snapshot">(i)</a></sup> has been resolved. |
| 120 | +</p> |
| 121 | + |
| 122 | + |
| 123 | +<p id="res-4237"><b>Proposed resolution:</b></p> |
| 124 | + |
| 125 | + |
| 126 | + |
| 127 | + |
| 128 | + |
| 129 | +</body> |
| 130 | +</html> |
0 commit comments