|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | +<head> |
| 4 | +<meta charset="utf-8"> |
| 5 | +<title>Issue 4242: ranges::distance does not work with volatile iterators</title> |
| 6 | +<meta property="og:title" content="Issue 4242: ranges::distance does not work with volatile iterators"> |
| 7 | +<meta property="og:description" content="C++ library issue. Status: New"> |
| 8 | +<meta property="og:url" content="https://cplusplus.github.io/LWG/issue4242.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="4242"><a href="lwg-active.html#4242">4242</a>. <code class='backtick'>ranges::distance</code> does not work with volatile iterators</h3> |
| 66 | +<p><b>Section:</b> 24.4.4.3 <a href="https://wg21.link/range.iter.op.distance">[range.iter.op.distance]</a> <b>Status:</b> <a href="lwg-active.html#New">New</a> |
| 67 | + <b>Submitter:</b> Hewill Kang <b>Opened:</b> 2025-04-12 <b>Last modified:</b> 2025-04-13</p> |
| 68 | +<p><b>Priority: </b>Not Prioritized |
| 69 | +</p> |
| 70 | +<p><b>View all other</b> <a href="lwg-index.html#range.iter.op.distance">issues</a> in [range.iter.op.distance].</p> |
| 71 | +<p><b>View all issues with</b> <a href="lwg-status.html#New">New</a> status.</p> |
| 72 | +<p><b>Discussion:</b></p> |
| 73 | +<p> |
| 74 | +After LWG <a href="lwg-defects.html#3664" title="LWG 3392 broke std::ranges::distance(a, a+3) (Status: C++23)">3664</a><sup><a href="https://cplusplus.github.io/LWG/issue3664" title="Latest snapshot">(i)</a></sup>, <code class='backtick'>ranges::distance</code> computes the distance between <code class='backtick'>last</code> and <code class='backtick'>first</code> |
| 75 | +by returning <code>last - static_cast<const decay_t<I>&>(first)</code> when the |
| 76 | +two are subtractable. However, this will cause a hard error if <code>first</code> is volatile-qualified |
| 77 | +(<a href="https://godbolt.org/z/5M3d9rMjE">demo</a>): |
| 78 | +</p> |
| 79 | +<blockquote><pre> |
| 80 | +#include <iterator> |
| 81 | + |
| 82 | +int main() { |
| 83 | + int arr[] = {1, 2, 3}; |
| 84 | + int* volatile ptr = arr; |
| 85 | + // return std::distance(ptr, arr + 3); // this is ok |
| 86 | + return std::ranges::distance(ptr, arr + 3); // <span style="color:#C80000;font-weight:bold">hard error</span> |
| 87 | +} |
| 88 | +</pre></blockquote> |
| 89 | +<p> |
| 90 | +The resolution changes the <i>Effects</i> of LWG <a href="lwg-defects.html#3664" title="LWG 3392 broke std::ranges::distance(a, a+3) (Status: C++23)">3664</a><sup><a href="https://cplusplus.github.io/LWG/issue3664" title="Latest snapshot">(i)</a></sup> from "cute" to "noncute". |
| 91 | +</p> |
| 92 | + |
| 93 | + |
| 94 | +<p id="res-4242"><b>Proposed resolution:</b></p> |
| 95 | +<p> |
| 96 | +This wording is relative to <a href="https://wg21.link/N5008">N5008</a>. |
| 97 | +</p> |
| 98 | + |
| 99 | +<ol> |
| 100 | + |
| 101 | +<li><p>Modify 24.4.4.3 <a href="https://wg21.link/range.iter.op.distance">[range.iter.op.distance]</a> as indicated:</p> |
| 102 | + |
| 103 | +<blockquote> |
| 104 | +<pre> |
| 105 | +template<class I, sized_sentinel_for<decay_t<I>> S> |
| 106 | + constexpr iter_difference_t<decay_t<I>> ranges::distance(I&& first, S last); |
| 107 | +</pre> |
| 108 | +<blockquote> |
| 109 | +<p> |
| 110 | +-3- <i>Effects</i>: Equivalent to: |
| 111 | +</p> |
| 112 | +<pre> |
| 113 | +<ins>if constexpr (!is_array_v<remove_reference_t<I>>) |
| 114 | + return last - first; |
| 115 | +else</ins> |
| 116 | + return last - static_cast<<del>const</del> decay_t<I><del>&</del>>(first); |
| 117 | +</pre> |
| 118 | +</blockquote> |
| 119 | +</blockquote> |
| 120 | + |
| 121 | +</li> |
| 122 | + |
| 123 | +</ol> |
| 124 | + |
| 125 | + |
| 126 | + |
| 127 | + |
| 128 | + |
| 129 | +</body> |
| 130 | +</html> |
0 commit comments