<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 940: std::distance</title>
<meta property="og:title" content="Issue 940: std::distance">
<meta property="og:description" content="C++ library issue. Status: Resolved">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue940.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#Resolved">Resolved</a> status.</em></p>
<h3 id="940"><a href="lwg-defects.html#940">940</a>. <code>std::distance</code></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#Resolved">Resolved</a>
 <b>Submitter:</b> Thomas <b>Opened:</b> 2008-12-14 <b>Last modified:</b> 2016-01-28</p>
<p><b>Priority: </b>Not Prioritized
</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#Resolved">Resolved</a> status.</p>
<p><b>Discussion:</b></p>

<p><b>Addresses UK 270</b></p>

<p>
Regarding the <code>std::distance</code> - function, 24.4.3 <a href="https://wg21.link/iterator.operations">[iterator.operations]</a>
p.4 says:
</p>
<blockquote><p>
Returns the number of increments or decrements needed to get from first to last.
</p></blockquote>
<p>
This sentence is completely silent about the sign of the return value.
24.4.3 <a href="https://wg21.link/iterator.operations">[iterator.operations]</a> p.1 gives more information about the
underlying operations, but again no inferences about the sign can be made.
Strictly speaking, that is taking that sentence literally, I think this
sentence even implies a positive return value in all cases, as the
number of increments or decrements is clearly a ratio scale variable,
with a natural zero bound.
</p>
<p>
Practically speaking, my implementations did what common sense and
knowledge based on pointer arithmetic forecasts, namely a positive sign
for increments (that is, going from <code>first</code> to <code>last</code> by <code>operator++</code>), and a
negative sign for decrements (going from <code>first</code> to <code>last</code> by <code>operator--</code>).
</p>
<p>
Here are my two questions:
</p>
<p>
First, is that paragraph supposed to be interpreted in the way what I
called 'common sense', that is negative sign for decrements ? I am
fairly sure that's the supposed behavior, but a double-check here in
this group can't hurt.
</p>
<p>
Second, is the present wording (2003 standard version - no idea about
the draft for the upcoming standard) worth an edit to make it a bit more
sensible, to mention the sign of the return value explicitly ?
</p>

<p><i>[
Daniel adds:
]</i></p>


<blockquote>
<p>
My first thought was that resolution <a href="lwg-closed.html#204" title="distance(first, last) when &quot;last&quot; is before &quot;first&quot; (Status: NAD)">204</a><sup><a href="https://cplusplus.github.io/LWG/issue204" title="Latest snapshot">(i)</a></sup> would already cover the
issue report, but it seems that current normative wording is in
contradiction to that resolution:
</p>

<p>
Referring to
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2798.pdf">N2798</a>,
24.4.3 <a href="https://wg21.link/iterator.operations">[iterator.operations]</a>/ p.4 says:
</p>

<blockquote><p>
<i>Effects:</i> Returns the number of increments or decrements needed to get
from <code>first</code> to <code>last</code>.
</p></blockquote>

<p>
IMO the part " or decrements" is in contradiction to p. 5 which says
</p>

<blockquote><p>
<i>Requires:</i> <code>last</code> shall be reachable from <code>first</code>.
</p></blockquote>

<p>
because "reachable" is defined in 24.3.4 <a href="https://wg21.link/iterator.concepts">[iterator.concepts]</a>/7 as
</p>

<blockquote><p>
An iterator <code>j</code> is called reachable from an iterator <code>i</code> if and only if
there is a finite sequence of applications of the expression <code>++i</code> that makes 
<code>i == j</code>.[..]
</p></blockquote>

<p>
Here is wording that would be consistent with this definition of "reachable":
</p>

<p>
Change 24.4.3 <a href="https://wg21.link/iterator.operations">[iterator.operations]</a> p4 as follows:
</p>

<blockquote><p>
<i>Effects:</i> Returns the number of increments <del>or decrements</del>
needed to get from <code>first</code> to <code>last</code>.
</p></blockquote>

</blockquote>

<p>
Thomas adds more discussion and an alternative view point
<a href="http://groups.google.com/group/comp.std.c++/browse_thread/thread/e8e46dcda0a5d797#">here</a>.
</p>

<p><i>[
Summit:
]</i></p>


<blockquote><p>
The proposed wording below was verbally agreed to.  Howard provided.
</p></blockquote>

<p><i>[
Batavia (2009-05):
]</i></p>

<blockquote>
<p>
Pete reports that a recent similar change has been made
for the <code>advance()</code> function.
</p>
<p>
We agree with the proposed resolution.
Move to Tentatively Ready.
</p>
</blockquote>

<p><i>[
2009-07 Frankfurt
]</i></p>


<blockquote><p>
Moved from Tentatively Ready to Open only because the wording needs to be
tweaked for concepts removal.
</p></blockquote>

<p><i>[
2009-07 Frankfurt:
]</i></p>


<blockquote><p>
Leave Open pending arrival of a post-Concepts WD.
</p></blockquote>

<p><i>[
2009-10-14 Daniel provided de-conceptified wording.
]</i></p>


<p><i>[
2009-10 Santa Cruz:
]</i></p>


<blockquote><p>
Move to Ready, replacing the Effects clause in the proposed wording with
"If InputIterator meets the requirements of random access iterator then
returns (last - first), otherwise returns the number of increments
needed to get from first to list.".
</p></blockquote>

<p><i>[
2010 Pittsburgh:
]</i></p>


<blockquote><p>
Moved to <del>NAD Editorial</del><ins>Resolved</ins>.  Rationale added below.
</p></blockquote>



<p><b>Rationale:</b></p>
<p>
Solved by <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2010/n3066.html">N3066</a>.
</p>


<p id="res-940"><b>Proposed resolution:</b></p>
<ol>
<li>
<p>
Change 24.3.5.7 <a href="https://wg21.link/random.access.iterators">[random.access.iterators]</a>, Table 105 as indicated [This change is not
essential but it simplifies the specification] for the row with expression "<code>b - a</code>"
and the column Operational semantics:
</p>

<blockquote><pre>
<del>(a &lt; b) ? </del>distance(a,b)
<del>: -distance(b,a)</del>
</pre></blockquote>
</li>

<li>
<p>
Change 24.4.3 <a href="https://wg21.link/iterator.operations">[iterator.operations]</a>/4+5 as indicated:
</p>

<blockquote><pre>
template&lt;class InputIterator&gt;
  typename iterator_traits&lt;InputIterator&gt;::difference_type
    distance(InputIterator first, InputIterator last);
</pre>
<blockquote>
<p>
4 <i>Effects:</i> <ins>If <code>InputIterator</code> meets the requirements
of random access iterator then returns <code>(last - first)</code>,
otherwise</ins> <del>R</del><ins>r</ins>eturns the number of increments
<del>or decrements</del> needed to get from <code>first</code> to
<code>last</code>.
</p>

<p>
5 <i>Requires:</i> <ins>If <code>InputIterator</code> meets the requirements
of random access iterator then <code>last</code> shall be reachable from
<code>first</code> or <code>first</code> shall be reachable from <code>last</code>,
otherwise</ins> <code>last</code> shall be reachable from <code>first</code>.
</p>
</blockquote>
</blockquote>
</li>
</ol>









</body>
</html>
