<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 4055: &sect;[iterator.operations] std::distance is missing a precondition</title>
<meta property="og:title" content="Issue 4055: &sect;[iterator.operations] std::distance is missing a precondition">
<meta property="og:description" content="C++ library issue. Status: New">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue4055.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="4055"><a href="lwg-active.html#4055">4055</a>. &sect;[iterator.operations] <code>std::distance</code> is missing a precondition</h3>
<p><b>Section:</b> 24.4.3 <a href="https://wg21.link/iterator.operations">[iterator.operations]</a> <b>Status:</b> <a href="lwg-active.html#New">New</a>
 <b>Submitter:</b> Jan Schultke <b>Opened:</b> 2024-02-25 <b>Last modified:</b> 2024-03-12</p>
<p><b>Priority: </b>4
</p>
<p><b>View other</b> <a href="lwg-index-open.html#iterator.operations">active issues</a> in [iterator.operations].</p>
<p><b>View all other</b> <a href="lwg-index.html#iterator.operations">issues</a> in [iterator.operations].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#New">New</a> status.</p>
<p><b>Discussion:</b></p>
<p>
<code>std::distance</code> for random access iterators is defined in terms of 
<code>(last - first)</code> (24.4.3 <a href="https://wg21.link/iterator.operations">[iterator.operations]</a> p5) for 
<i>Cpp17RandomAccessIterators</i>.
<p/>
However, this subtraction is not guaranteed to be well-defined
(24.3.5.7 <a href="https://wg21.link/random.access.iterators">[random.access.iterators]</a>, Table [tab:randomaccessiterator]):
</p>
<blockquote>
<table border="1">
<caption>Table 91: <i>Cpp17RandomAccessIterator</i> requirements (in addition to 
<i>Cpp17BidirectionalIterator</i>) [tab:randomaccessiterator]</caption>
<tr align="center">
<th>Expression</th>
<th>Return type</th>
<th>Operational semantics</th>
<th>Assertion&#47;note<br/>pre-&#47;post-condition</th>
</tr> 

<tr>
<td colspan="4" align="center">
<code>[&hellip;]</code>
</td>
</tr>

<tr>
<td>
<code>b - a</code>
</td>
<td>
<code>difference_type</code>
</td>
<td>
<code>return n;</code>
</td>
<td>
<i>Preconditions</i>: there exists a<br/>
value <code>n</code> of type <code>difference_type</code><br/> 
such that <code>a + n == b</code>.<br/>
<code>b == a + (b - a)</code>.
</td>
</tr>

</table>

</blockquote>
<p>
For example, pointer subtraction is undefined if the result isn't representable 
as <code>std::ptrdiff_t</code>, and user-defined types with random access iterators 
aren't required to have a difference which is always representable by 
<code>difference_type</code>.
<p/>
<code>std::distance(&amp;a, &amp;b)</code> can't be well-defined when 
<code>&amp;b - &amp;a</code> is not, so <code>std::distance</code> is missing a precondition.
</p>

<p><i>[2024-03-12; Reflector poll]</i></p>

<p>
Set priority to 4 after reflector poll.
</p>
<p>
The proposed change is wrong, the new wording only associates with the second
condition, but should also apply when "<code>last</code> is reachable from
<code>first</code>".
</p>

<p><strong>Previous resolution [SUPERSEDED]:</strong></p>
<blockquote class="note">

<p>
This wording is relative to <a href="https://wg21.link/N4971" title=" Working Draft, Programming Languages — C++">N4971</a>.
</p>

<ol>
<li><p>Modify 24.4.3 <a href="https://wg21.link/iterator.operations">[iterator.operations]</a> as indicated:</p>

<blockquote>
<pre>
template&lt;class InputIterator&gt;
  constexpr typename iterator_traits&lt;InputIterator&gt;::difference_type
    distance(InputIterator first, InputIterator last);
</pre>
<blockquote>
<p>
-4-<i>Preconditions</i>: <code>last</code> is reachable from <code>first</code>, or <code>InputIterator</code> 
meets the <i>Cpp17RandomAccessIterator</i> requirements<ins>,</ins> <del>and</del> <code>first</code> 
is reachable from <code>last</code> <ins>, and 
<code>typename iterator_traits&lt;InputIterator&gt;::difference_type</code> can represent the result of 
this function call</ins>.
<p/>
-5- <i>Effects</i>: If <code>InputIterator</code> meets the <i>Cpp17RandomAccessIterator</i> 
requirements, returns <code>(last - first)</code>; otherwise, increments <code>first</code> until <code>last</code> 
is reached and returns the number of increments.
</p>
</blockquote>
</blockquote>
</li>
</ol>
</blockquote>

<p><i>[2024-03-12; Jonathan provides improved wording]</i></p>




<p id="res-4055"><b>Proposed resolution:</b></p>

<p>
This wording is relative to <a href="https://wg21.link/N4971" title=" Working Draft, Programming Languages — C++">N4971</a>.
</p>

<ol>
<li><p>Modify 24.4.3 <a href="https://wg21.link/iterator.operations">[iterator.operations]</a> as indicated:</p>

<blockquote>
<pre>
template&lt;class InputIterator&gt;
  constexpr typename iterator_traits&lt;InputIterator&gt;::difference_type
    distance(InputIterator first, InputIterator last);
</pre>
<blockquote>
<p>
-4-<i>Preconditions</i>: <code>last</code> is reachable from <code>first</code>, or <code>InputIterator</code> 
meets the <i>Cpp17RandomAccessIterator</i> requirements and <code>first</code> 
is reachable from <code>last</code>.
<ins>The return type can represent the result.</ins>
<p/>
-5- <i>Effects</i>: If <code>InputIterator</code> meets the <i>Cpp17RandomAccessIterator</i> 
requirements, returns <code>(last - first)</code>; otherwise, increments <code>first</code> until <code>last</code> 
is reached and returns the number of increments.
</p>
</blockquote>
</blockquote>
</li>
</ol>






</body>
</html>
