<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 685: reverse_iterator/move_iterator difference has invalid signatures</title>
<meta property="og:title" content="Issue 685: reverse_iterator/move_iterator difference has invalid signatures">
<meta property="og:description" content="C++ library issue. Status: CD1">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue685.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#CD1">CD1</a> status.</em></p>
<h3 id="685"><a href="lwg-defects.html#685">685</a>. reverse_iterator/move_iterator difference has invalid signatures</h3>
<p><b>Section:</b> 24.5.1.9 <a href="https://wg21.link/reverse.iter.nonmember">[reverse.iter.nonmember]</a>, 24.5.4.9 <a href="https://wg21.link/move.iter.nonmember">[move.iter.nonmember]</a> <b>Status:</b> <a href="lwg-active.html#CD1">CD1</a>
 <b>Submitter:</b> Bo Persson <b>Opened:</b> 2007-06-10 <b>Last modified:</b> 2021-06-06</p>
<p><b>Priority: </b>Not Prioritized
</p>
<p><b>View all issues with</b> <a href="lwg-status.html#CD1">CD1</a> status.</p>
<p><b>Discussion:</b></p>
<p>
In C++03 the difference between two <code>reverse_iterators</code>
</p>
<blockquote><pre>
ri1 - ri2
</pre></blockquote>
<p>
is possible to compute only if both iterators have the same base
iterator. The result type is the <code>difference_type</code> of the base iterator.
</p>
<p>
In the current draft, the operator is defined as  [reverse.iter.opdiff]
</p>
<blockquote><pre>
template&lt;class Iterator1, class Iterator2&gt;
typename reverse_iterator&lt;Iterator&gt;::difference_type
   operator-(const reverse_iterator&lt;Iterator1&gt;&amp; x,
                    const reverse_iterator&lt;Iterator2&gt;&amp; y);
</pre></blockquote>
<p>
The return type is the same as the C++03 one, based on the no longer
present <code>Iterator</code> template parameter.
</p>
<p>
Besides being slightly invalid, should this operator work only when
<code>Iterator1</code> and <code>Iterator2</code> has the same <code>difference_type</code>? Or should the
implementation choose one of them? Which one?
</p>
<p>
The same problem now also appears in <code>operator-()</code> for <code>move_iterator</code>
24.5.4.9 <a href="https://wg21.link/move.iter.nonmember">[move.iter.nonmember]</a>.
</p>


<p id="res-685"><b>Proposed resolution:</b></p>
<p>
Change the synopsis in 24.5.1.2 <a href="https://wg21.link/reverse.iterator">[reverse.iterator]</a>:
</p>

<blockquote>
<pre>
template &lt;class Iterator1, class Iterator2&gt;
  <del>typename reverse_iterator&lt;Iterator&gt;::difference_type</del> <ins>auto</ins> operator-(
    const reverse_iterator&lt;Iterator1&gt;&amp; x,
    const reverse_iterator&lt;Iterator2&gt;&amp; y)<ins> -&gt; decltype(y.current - x.current)</ins>;
</pre>
</blockquote>

<p>
Change  [reverse.iter.opdiff]:
</p>

<blockquote>
<pre>
template &lt;class Iterator1, class Iterator2&gt;
  <del>typename reverse_iterator&lt;Iterator&gt;::difference_type</del> <ins>auto</ins> operator-(
    const reverse_iterator&lt;Iterator1&gt;&amp; x,
    const reverse_iterator&lt;Iterator2&gt;&amp; y)<ins> -&gt; decltype(y.current - x.current)</ins>;
</pre>
<blockquote>
<p>
<i>Returns:</i> <code>y.current - x.current</code>.
</p>
</blockquote>
</blockquote>


<p>
Change the synopsis in 24.5.4.2 <a href="https://wg21.link/move.iterator">[move.iterator]</a>:
</p>

<blockquote>
<pre>
template &lt;class Iterator1, class Iterator2&gt;
  <del>typename move_iterator&lt;Iterator&gt;::difference_type</del> <ins>auto</ins> operator-(
    const move_iterator&lt;Iterator1&gt;&amp; x,
    const move_iterator&lt;Iterator2&gt;&amp; y)<ins> -&gt; decltype(x.base() - y.base())</ins>;
</pre>
</blockquote>

<p>
Change 24.5.4.9 <a href="https://wg21.link/move.iter.nonmember">[move.iter.nonmember]</a>:
</p>

<blockquote>
<pre>
template &lt;class Iterator1, class Iterator2&gt;
  <del>typename move_iterator&lt;Iterator&gt;::difference_type</del> <ins>auto</ins> operator-(
    const move_iterator&lt;Iterator1&gt;&amp; x,
    const move_iterator&lt;Iterator2&gt;&amp; y)<ins> -&gt; decltype(x.base() - y.base())</ins>;
</pre>
<blockquote>
<p>
<i>Returns:</i> <code>x.base() - y.base()</code>.
</p>
</blockquote>
</blockquote>

<p><i>[
Pre Bellevue:  This issue needs to wait until the <code>auto -&gt; return</code> language feature
goes in.
]</i></p>







</body>
</html>
