<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3830: reverse_view should not cache when ranges::next has constant time complexity</title>
<meta property="og:title" content="Issue 3830: reverse_view should not cache when ranges::next has constant time complexity">
<meta property="og:description" content="C++ library issue. Status: New">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3830.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="3830"><a href="lwg-active.html#3830">3830</a>. <code>reverse_view</code> should not cache when <code>ranges::next</code> has constant time complexity</h3>
<p><b>Section:</b> 25.7.21.2 <a href="https://wg21.link/range.reverse.view">[range.reverse.view]</a> <b>Status:</b> <a href="lwg-active.html#New">New</a>
 <b>Submitter:</b> Hewill Kang <b>Opened:</b> 2022-11-14 <b>Last modified:</b> 2022-11-30</p>
<p><b>Priority: </b>3
</p>
<p><b>View all other</b> <a href="lwg-index.html#range.reverse.view">issues</a> in [range.reverse.view].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#New">New</a> status.</p>
<p><b>Discussion:</b></p>
<p>
In order to ensure that <code>begin</code> has an amortized constant time, when the underlying range is not a 
common range, <code>reverse_view</code> always caches the result of <code>ranges::next</code>.
<p/>
However, for some non-common ranges, incrementing its <code>begin</code> to <code>end</code> still guarantees constant time, 
for example:
</p>
<blockquote><pre>
#include &lt;ranges&gt;
#include &lt;vector&gt;
#include &lt;list&gt;

int main() {
  std::vector v{42};
  auto x = std::ranges::subrange(std::counted_iterator(v.begin(), 1), std::default_sentinel)
         | std::views::reverse;
  (void) x.begin(); // <span style="color:red;font-weight:bolder">still caches end iterator in MSVC-STL</span>

  std::list l{42};
  auto y = std::ranges::subrange(l.cbegin(), l.end())
         | std::views::reverse;
  (void) y.begin(); // <span style="color:red;font-weight:bolder">still caches end iterator in both libstdc++ and MSVC-STL</span>
}
</pre></blockquote>
<p>
In the above example, although neither <code>subrange</code> is a common range, applying <code>ranges::next</code> 
to their iterator-sentinel pairs is still constant time, in this case, there's no need to introduce a cache for 
<code>reverse_view</code> to store the results. We shouldn't pay for things we don't need to use.
</p>

<p><i>[2022-11-30; Reflector poll]</i></p>

<p>
Set priority to 3 after reflector poll.
</p>
<p>
"NAD as specified, the <i>Remarks</i> don't need to be precise, they already
allow caching to be omitted if not needed. But we could still enable const
overloads of <code>begin</code> for cases like these."
</p>
<p>
"NAD, no need to complicate constraints for such contrived examples.
We don't care about random access, sized, non-common ranges like the first case.
Any change here should be a paper covering all adaptors, not a piecemeal issue."
</p>



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

<ol>
<li><p>Modify 25.7.21.2 <a href="https://wg21.link/range.reverse.view">[range.reverse.view]</a> as indicated:</p>

<blockquote>
<pre>
constexpr reverse_iterator&lt;iterator_t&lt;V&gt;&gt; begin();
</pre>
<blockquote>
<p>
-2- <i>Returns</i>:
</p>
<blockquote><pre>
make_reverse_iterator(ranges::next(ranges::begin(<i>base_</i>), ranges::end(<i>base_</i>)))
</pre></blockquote>
<p>
-3- <i>Remarks</i>: In order to provide the amortized constant time complexity required by the <code>range</code> concept, 
this function caches the result within the <code>reverse_view</code> for use on subsequent calls <ins>when both 
<code>assignable_from&lt;I&amp;, S&gt;</code> and <code>random_access_iterator&lt;I&gt; &amp;&amp; 
sized_sentinel_for&lt;S, I&gt;</code> are <code>false</code>, where <code>I</code> is <code>iterator_t&lt;V&gt;</code> and 
<code>S</code> is <code>sentinel_t&lt;V&gt;</code></ins>.
</p>
</blockquote>
</blockquote>
</li>
</ol>






</body>
</html>
