<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 4303: std::decay_t in the specification of ranges::distance is problematic</title>
<meta property="og:title" content="Issue 4303: std::decay_t in the specification of ranges::distance is problematic">
<meta property="og:description" content="C++ library issue. Status: New">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue4303.html">
<meta property="og:type" content="website">
<meta property="og:image" content="http://cplusplus.github.io/LWG/images/cpp_logo.png">
<meta property="og:image:alt" content="C++ logo">
<style>
  p {text-align:justify}
  li {text-align:justify}
  pre code.backtick::before { content: "`" }
  pre code.backtick::after { content: "`" }
  blockquote.note
  {
    background-color:#E0E0E0;
    padding-left: 15px;
    padding-right: 15px;
    padding-top: 1px;
    padding-bottom: 1px;
  }
  ins {background-color:#A0FFA0}
  del {background-color:#FFA0A0}
  table.issues-index { border: 1px solid; border-collapse: collapse; }
  table.issues-index th { text-align: center; padding: 4px; border: 1px solid; }
  table.issues-index td { padding: 4px; border: 1px solid; }
  table.issues-index td:nth-child(1) { text-align: right; }
  table.issues-index td:nth-child(2) { text-align: left; }
  table.issues-index td:nth-child(3) { text-align: left; }
  table.issues-index td:nth-child(4) { text-align: left; }
  table.issues-index td:nth-child(5) { text-align: center; }
  table.issues-index td:nth-child(6) { text-align: center; }
  table.issues-index td:nth-child(7) { text-align: left; }
  table.issues-index td:nth-child(5) span.no-pr { color: red; }
  @media (prefers-color-scheme: dark) {
     html {
        color: #ddd;
        background-color: black;
     }
     ins {
        background-color: #225522
     }
     del {
        background-color: #662222
     }
     a {
        color: #6af
     }
     a:visited {
        color: #6af
     }
     blockquote.note
     {
        background-color: rgba(255, 255, 255, .10)
     }
  }
</style>
</head>
<body>
<hr>
<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>
<h3 id="4303"><a href="lwg-active.html#4303">4303</a>. <code class='backtick'>std::decay_t</code> in the specification of <code class='backtick'>ranges::distance</code> is problematic</h3>
<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>
 <b>Submitter:</b> Jiang An <b>Opened:</b> 2025-07-24 <b>Last modified:</b> 2025-08-29</p>
<p><b>Priority: </b>4
</p>
<p><b>View all other</b> <a href="lwg-index.html#range.iter.op.distance">issues</a> in [range.iter.op.distance].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#New">New</a> status.</p>
<p><b>Discussion:</b></p>
<p>
This is discovered when implementing the resolution LWG <a href="lwg-defects.html#4242" title="ranges::distance does not work with volatile iterators (Status: WP)">4242</a><sup><a href="https://cplusplus.github.io/LWG/issue4242" title="Latest snapshot">(i)</a></sup>. 
Per LWG <a href="lwg-defects.html#4242" title="ranges::distance does not work with volatile iterators (Status: WP)">4242</a><sup><a href="https://cplusplus.github.io/LWG/issue4242" title="Latest snapshot">(i)</a></sup>, it is intended to allow <code class='backtick'>ranges::distance</code> to handle 
<code class='backtick'>volatile</code>-qualified iterator values. 
However, the uses of <code class='backtick'>decay_t</code> (established per 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>) are still problematic, 
because when <code>sized_sentinel_for&lt;S, decay_t&lt;I&gt;&gt;</code> is modeled, there's no 
semantic or syntactic requirement that <code class='backtick'>S</code> shall work with volatile-qualified <code class='backtick'>I</code>.
<p/>
If we implement the constraint as is, there will still be some underconstrained cases. E.g. 
When the <code class='backtick'>operator==</code> or <code class='backtick'>operator-</code> intendedly rejects volatile-qualified iterators. And 
even when they accept volatile-qualified iterators, the additional semantic requirements 
imposed by <code>sized_sentinel_for&lt;S, decay_t&lt;I&gt;&gt;</code> are still undesired.
<p/>
I think we should only decay arrays and keep <code class='backtick'>volatile</code> for non-array arguments.
</p>

<p><i>[2025-08-29; Reflector poll]</i></p>

<p>
Set priority to 4 after reflector poll.
</p>



<p id="res-4303"><b>Proposed resolution:</b></p>
<p>
This wording is relative to <a href="https://wg21.link/N5014">N5014</a>.
</p>

<ol>

<li><p>Modify 24.2 <a href="https://wg21.link/iterator.synopsis">[iterator.synopsis]</a>, header <code>&lt;iterator&gt;</code> synopsis, as indicated:</p>

<blockquote>
<pre>
[&hellip;]
namespace std: {
  [&hellip;]
  // <i>24.4.4.3 <a href="https://wg21.link/range.iter.op.distance">[range.iter.op.distance]</a>, ranges::distance</i>
 
  <ins>template&lt;class T&gt;
    using <i>distance-iterator-t</i> =               // <i>exposition only</i>
      conditional_t&lt;is_array_v&lt;remove_reference_t&lt;T&gt;&gt;,
        decay_t&lt;T&gt;, remove_const_t&lt;remove_reference_t&lt;T&gt;&gt;&gt;;
</ins>  
  template&lt;class I, sentinel_for&lt;I&gt; S&gt;
    requires (!sized_sentinel_for&lt;S, I&gt;)
    constexpr iter_difference_t&lt;I&gt; distance(I first, S last); // <i>freestanding</i>
  template&lt;class I, sized_sentinel_for&lt;<del>decay_t</del><ins><i>distance-iterator-t</i></ins>&lt;I&gt;&gt; S&gt;
    constexpr iter_difference_t&lt;<del>decay_t</del><ins><i>distance-iterator-t</i></ins>&lt;I&gt;&gt; distance(I&amp;&amp; first, S last); // <i>freestanding</i>
  template&lt;range R&gt;
    constexpr range_difference_t&lt;R&gt; distance(R&amp;&amp; r); // <i>freestanding</i>
  [&hellip;]
}
</pre>
</blockquote>
</li>

<li><p>Modify 24.4.4.3 <a href="https://wg21.link/range.iter.op.distance">[range.iter.op.distance]</a> as indicated:</p>

<blockquote>
<pre>
template&lt;class I, sized_sentinel_for&lt;<del>decay_t</del><ins><i>distance-iterator-t</i></ins>&lt;I&gt;&gt; S&gt;
  constexpr iter_difference_t&lt;<del>decay_t</del><ins><i>distance-iterator-t</i></ins>&lt;I&gt;&gt; distance(I&amp;&amp; first, S last);
</pre>
<blockquote>
<p>
-3- <i>Effects</i>: Equivalent to:
</p>
<blockquote><pre>
if constexpr (!is_array_v&lt;remove_reference_t&lt;I&gt;&gt;)
  return last - first;
else
  return last - static_cast&lt;decay_t&lt;I&gt;&gt;(first);
</pre></blockquote>
</blockquote>
</blockquote>
</li>
</ol>





</body>
</html>
