<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3727: reverse_iterator/common_iterator's operator-&gt; should not require the underlying iterator's operator-&gt; to be a const member function</title>
<meta property="og:title" content="Issue 3727: reverse_iterator/common_iterator's operator-&gt; should not require the underlying iterator's operator-&gt; to be a const member function">
<meta property="og:description" content="C++ library issue. Status: NAD">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3727.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#NAD">NAD</a> status.</em></p>
<h3 id="3727"><a href="lwg-closed.html#3727">3727</a>. <code>reverse_iterator</code>/<code>common_iterator</code>'s <code>operator-&gt;</code> should not require the underlying iterator's <code>operator-&gt;</code> to be a <code>const</code> member function</h3>
<p><b>Section:</b> 24.5.1.6 <a href="https://wg21.link/reverse.iter.elem">[reverse.iter.elem]</a>, 24.5.5.4 <a href="https://wg21.link/common.iter.access">[common.iter.access]</a> <b>Status:</b> <a href="lwg-active.html#NAD">NAD</a>
 <b>Submitter:</b> Hewill Kang <b>Opened:</b> 2022-06-27 <b>Last modified:</b> 2022-11-30</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#NAD">NAD</a> status.</p>
<p><b>Discussion:</b></p>
<p>
For non-pointer types, <code>reverse_iterator::operator-&gt;</code> requires that the <code>Iterator</code> 
must have an <code>operator-&gt;()</code> with <code>const</code>-qualifier, whereas in the <i>Effects</i> clause, 
it always invokes the non-<code>const</code> object's <code>operator-&gt;()</code>.
<p/>
<code>common_iterator::operator-&gt;</code> also requires that <code>I::operator-&gt;()</code> must be 
<code>const</code>-qualified, which seems reasonable since the return type of <code>get&lt;I&gt;(v_)</code> 
is <code>const I&amp;</code>. However, LWG <a href="lwg-defects.html#3672" title="common_iterator::operator-&gt;() should return by value (Status: C++23)">3672</a><sup><a href="https://cplusplus.github.io/LWG/issue3672" title="Latest snapshot">(i)</a></sup> makes <code>common_iterator::operator-&gt;()</code> 
always return a value, which makes it unnecessary to detect the constness of <code>I::operator-&gt;()</code>, 
because it will be invoked with a non-<code>const</code> returned object anyway.
<p/>
I think we should remove this constraint as I don't see the benefit of doing that.
Constraining iterator's <code>operator-&gt;()</code> to be <code>const</code> and finally invoking non-<code>const</code> 
overload doesn't feel right to me either. In <code>&lt;ranges&gt;</code>, the exposition-only constraint 
<code><i>has-arrow</i></code> (25.5.2 <a href="https://wg21.link/range.utility.helpers">[range.utility.helpers]</a>) for <code>operator-&gt;()</code> does not 
require that the underlying iterator's <code>operator-&gt;()</code> to be <code>const</code>, we should make them 
consistent, and I believe this relaxation of constraints can bring some value.
<p/>
<b>Daniel:</b>
<p/>
This issue's second part of the resolution actually depends on <a href="lwg-defects.html#3672" title="common_iterator::operator-&gt;() should return by value (Status: C++23)">3672</a><sup><a href="https://cplusplus.github.io/LWG/issue3672" title="Latest snapshot">(i)</a></sup> being applied. But
note that the reference wording below is still <a href="https://wg21.link/N4910" title=" Working Draft, Standard for Programming Language C++">N4910</a>.
</p>

<p><i>[2022-08-23; Reflector poll: NAD]</i></p>

<p>
Implicit variations apply to those <i>requires-expressions</i>,
so calling as non-const (and rvalue) is fine.
The PR actually loses that property and makes those overloads truly underconstrained.
Motivation for relaxing it is vague.
As for consistency, we should fix has-arrow instead.
</p>

<p><i>[2022-11-30 LWG telecon. Status changed: Tentatively NAD &rarr; NAD.]</i></p>



<p id="res-3727"><b>Proposed resolution:</b></p>
<p>
This wording is relative to <a href="https://wg21.link/N4910" title=" Working Draft, Standard for Programming Language C++">N4910</a>.
</p>

<ol>
<li><p>Modify 24.5.1.6 <a href="https://wg21.link/reverse.iter.elem">[reverse.iter.elem]</a> as indicated:</p>

<blockquote>
<pre>
constexpr pointer operator-&gt;() const
  requires (is_pointer_v&lt;Iterator&gt; ||
            requires(<del>const</del> Iterator i) { i.operator-&gt;(); });
</pre>
<blockquote>
<p>
-2- <i>Effects</i>:
</p>
<ol style="list-style-type: none">
<li><p>(2.1) &mdash; If <code>Iterator</code> is a pointer type, equivalent to: <code>return prev(current);</code></p></li>
<li><p>(2.2) &mdash; Otherwise, equivalent to: <code>return prev(current).operator-&gt;();</code></p>
</li>
</ol>
</blockquote>
</blockquote>
</li>

<li><p>Modify 24.5.5.4 <a href="https://wg21.link/common.iter.access">[common.iter.access]</a> as indicated:</p>

<blockquote>
<pre>
constexpr decltype(auto) operator-&gt;() const
  requires <i>see below</i>;
</pre>
  <blockquote>
  <p>
  -3- The expression in the <i>requires-clause</i> is equivalent to:
  </p>
  <blockquote><pre>
  indirectly_readable&lt;const I&gt; &amp;&amp;
  (requires(<del>const</del> I<del>&amp;</del> i) { i.operator-&gt;(); } ||
  is_reference_v&lt;iter_reference_t&lt;I&gt;&gt; ||
  constructible_from&lt;iter_value_t&lt;I&gt;, iter_reference_t&lt;I&gt;&gt;)
  </pre></blockquote>
  <p>
  -4- <i>Preconditions</i>: <code>holds_alternative&lt;I&gt;(v_)</code> is <code>true</code>.
  <p/>
  -5- <i>Effects</i>:
  </p>
  <ol style="list-style-type: none">
  <li><p>(5.1) &mdash; If <code>I</code> is a pointer type or if <del>the expression 
  <code>get&lt;I&gt;(v_).operator-&gt;()</code> is 
  well-formed</del><ins><code>requires(I i) { i.operator-&gt;(); }</code> is <code>true</code></ins>, 
  equivalent to: <code>return get&lt;I&gt;(v_);</code>  
  </p></li>
  <li><p>(5.2) &mdash; Otherwise, if <code>iter_reference_t&lt;I&gt;</code> is a reference type, equivalent to:</p>
  <blockquote><pre>
  auto&amp;&amp; tmp = *get&lt;I&gt;(v_);
  return addressof(tmp);
  </pre></blockquote>
  </li>
  <li><p>(5.3) &mdash; Otherwise, equivalent to: <code>return <i>proxy</i>(*get&lt;I&gt;(v_));</code> where 
  <code><i>proxy</i></code> is the exposition-only class:</p>
  <blockquote><pre>
  class <i>proxy</i> {
    iter_value_t&lt;I&gt; keep_;
    constexpr <i>proxy</i>(iter_reference_t&lt;I&gt;&amp;&amp; x)
      : keep_(std::move(x)) {}
  public:
    constexpr const iter_value_t&lt;I&gt;* operator-&gt;() const noexcept {
      return addressof(keep_);
    }
  };
  </pre></blockquote>
  </li>
  </ol>
  <p>
  [&hellip;]
  </p>
</blockquote>
</blockquote>
</li>
</ol>





</body>
</html>
