<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 386: Reverse iterator's operator[] has impossible return type</title>
<meta property="og:title" content="Issue 386: Reverse iterator's operator[] has impossible return type">
<meta property="og:description" content="C++ library issue. Status: CD1">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue386.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="386"><a href="lwg-defects.html#386">386</a>. Reverse iterator's operator[] has impossible return type</h3>
<p><b>Section:</b> 24.5.1.6 <a href="https://wg21.link/reverse.iter.elem">[reverse.iter.elem]</a> <b>Status:</b> <a href="lwg-active.html#CD1">CD1</a>
 <b>Submitter:</b> Matt Austern <b>Opened:</b> 2002-10-23 <b>Last modified:</b> 2021-06-06</p>
<p><b>Priority: </b>Not Prioritized
</p>
<p><b>View all other</b> <a href="lwg-index.html#reverse.iter.elem">issues</a> in [reverse.iter.elem].</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  [reverse.iter.op-=], <code>reverse_iterator&lt;&gt;::operator[]</code>
is specified as having a return type of <code>reverse_iterator::reference</code>,
which is the same as <code>iterator_traits&lt;Iterator&gt;::reference</code>.
(Where <code>Iterator</code> is the underlying iterator type.)</p>

<p>The trouble is that <code>Iterator</code>'s own operator[] doesn't
  necessarily have a return type
  of <code>iterator_traits&lt;Iterator&gt;::reference</code>.   Its
  return type is merely required to be convertible
  to <code>Iterator</code>'s value type.  The return type specified for
  reverse_iterator's operator[] would thus appear to be impossible.</p>

<p>With the resolution of issue <a href="lwg-closed.html#299" title="Incorrect return types for iterator dereference (Status: NAD Editorial)">299</a><sup><a href="https://cplusplus.github.io/LWG/issue299" title="Latest snapshot">(i)</a></sup>, the type of
  <code>a[n]</code> will continue to be required (for random access
  iterators) to be convertible to the value type, and also <code>a[n] =
  t</code> will be a valid expression.  Implementations of
  <code>reverse_iterator</code> will likely need to return a proxy from
  <code>operator[]</code> to meet these requirements. As mentioned in the
  comment from Dave Abrahams, the simplest way to specify that
  <code>reverse_iterator</code> meet this requirement to just mandate
  it and leave the return type of <code>operator[]</code> unspecified.</p>



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

<p>In 24.5.1.3 <a href="https://wg21.link/reverse.iter.requirements">[reverse.iter.requirements]</a> change:</p>

<blockquote>
<pre>
reference operator[](difference_type n) const;
</pre>
</blockquote>

<p>to:</p>

<blockquote>
<pre>
<b><i>unspecified</i></b> operator[](difference_type n) const; // see 24.3.5.7 <a href="https://wg21.link/random.access.iterators">[random.access.iterators]</a>
</pre>
</blockquote>




<p><i>[
Comments from Dave Abrahams: IMO we should resolve 386 by just saying
    that the return type of reverse_iterator's operator[] is
    unspecified, allowing the random access iterator requirements to
    impose an appropriate return type.  If we accept 299's proposed
    resolution (and I think we should), the return type will be
    readable and writable, which is about as good as we can do.
]</i></p>






</body>
</html>
