<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3482: drop_view's const begin should additionally require sized_range</title>
<meta property="og:title" content="Issue 3482: drop_view's const begin should additionally require sized_range">
<meta property="og:description" content="C++ library issue. Status: C++23">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3482.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#C++23">C++23</a> status.</em></p>
<h3 id="3482"><a href="lwg-defects.html#3482">3482</a>. <code>drop_view</code>'s <code>const begin</code> should additionally require <code>sized_range</code></h3>
<p><b>Section:</b> 25.7.12.2 <a href="https://wg21.link/range.drop.view">[range.drop.view]</a> <b>Status:</b> <a href="lwg-active.html#C++23">C++23</a>
 <b>Submitter:</b> Casey Carter <b>Opened:</b> 2020-08-31 <b>Last modified:</b> 2023-11-22</p>
<p><b>Priority: </b>0
</p>
<p><b>View other</b> <a href="lwg-index-open.html#range.drop.view">active issues</a> in [range.drop.view].</p>
<p><b>View all other</b> <a href="lwg-index.html#range.drop.view">issues</a> in [range.drop.view].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#C++23">C++23</a> status.</p>
<p><b>Discussion:</b></p>
<p>
When the underlying range models both <code>random_access_range</code> and <code>sized_range</code>, a 
<code>drop_view</code> can easily calculate its first iterator in <code>&#x1d4aa;</code>(1) as by the 
underlying range's first iterator  plus the minimum of the number of elements to drop and the 
size of the underlying range. In this case <code>drop_view::begin</code> need not "cache the result 
within the <code>drop_view</code> for use on subsequent calls" "in order to provide the amortized 
constant-time complexity required by the <code>range</code> concept" (25.7.12.2 <a href="https://wg21.link/range.drop.view">[range.drop.view]</a>/4). 
However, <code>drop_view::begin() const</code> does not require <code>sized_range</code>, it requires 
only <code>random_access_range</code>. There's no way to implementing what amounts to a requirement 
that calls to <code>begin</code> after the first must be <code>&#x1d4aa;</code>(1) without memoization.
<p/>
Performing memoization in a <code>const</code> member function in a manner consistent with 
16.4.6.10 <a href="https://wg21.link/res.on.data.races">[res.on.data.races]</a> is impossible without some kind of thread synchronization. It 
is not the intended design for anything in current Range library to require such implementation 
heroics, we typically fall back to mutable-only iteration to avoid thread synchronization concerns. 
(Note that both <code>range-v3</code> and <code>cmcstl2</code> handle <code>drop_view::begin() const</code> 
incorrectly by performing <code>&#x1d4aa;</code>(<code><i>N</i></code>) lookup of the first iterator on each call to 
<code>begin</code>, which is consistent with 16.4.6.10 <a href="https://wg21.link/res.on.data.races">[res.on.data.races]</a> but fails to meet the 
complexity requirements imposed by the <code>range</code> concept.) We should fall back to mutable-only 
iteration here as well when the underlying range is not a <code>sized_range</code>.
<p/>
For <code>drop_view</code>, changing the constraints on the <code>const</code> overload of <code>begin</code> 
also requires changing the constraints on the non-<code>const</code> overload. The non-<code>const begin</code> 
tries to constrain itself out of overload resolution when the <code>const</code> overload would be valid 
if the underlying range models the exposition-only <code><i>simple-view</i></code> concept. (Recall that 
<code>T</code> models simple-view iff <code>T</code> models <code>view</code>, <code>const T</code> models <code>range</code>, 
and <code>T</code> and <code>const T</code> have the same iterator and sentinel types.) Effectively this means 
the constraints on the non-<code>const</code> overload must require either that the underlying range fails 
to model <code><i>simple-view</i></code> or that the constraints on the <code>const</code> overload would not 
be satisfied. So when we add a new <code>sized_range</code> requirement to the <code>const</code> overload, 
we must also add its negation to the mutable overload. (The current form of the constraint on the 
mutable <code>begin</code> overload is <code>!(<i>simple-view</i>&lt;V&gt; &amp;&amp; random_access_range&lt;V&gt;)</code> 
instead of <code>!(<i>simple-view</i>&lt;V&gt; &amp;&amp; random_access_range&lt;const V&gt;)</code> because 
of an unstated premise that <code>V</code> and <code>const V</code> should both have the same category when 
both are ranges. Avoiding this unstated premise would make it easier for future readers to grasp what's 
happening here; we should formulate our new constraints in terms of <code>const V</code> instead of <code>V</code>.)
</p>

<p><i>[2020-09-29; Reflector discussions]</i></p>

<p>
Status to Tentatively Ready and priority to 0 after five positive votes on the reflector.
</p>

<p><i>[2020-11-09 Approved In November virtual meeting. Status changed: Tentatively Ready &rarr; WP.]</i></p>



<p id="res-3482"><b>Proposed resolution:</b></p>
<p>
This wording is relative to <a href="https://wg21.link/n4861">N4861</a>.
</p>

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

<blockquote>
<blockquote>
<pre>
namespace std::ranges {
  template&lt;view V&gt;
  class drop_view : public view_interface&lt;drop_view&lt;V&gt;&gt; {
  public:
    [&hellip;]
    constexpr auto begin()
      requires (!(<i>simple-view</i>&lt;V&gt; &amp;&amp; 
        random_access_range&lt;<ins>const</ins> V&gt; <ins>&amp;&amp; sized_range&lt;const V&gt;</ins>));
    constexpr auto begin() const
      requires random_access_range&lt;const V&gt; <ins>&amp;&amp; sized_range&lt;const V&gt;</ins>;    
    [&hellip;]
  };
}
</pre>
</blockquote>
[&hellip;]
<pre>
constexpr auto begin()
  requires (!(<i>simple-view</i>&lt;V&gt; &amp;&amp; 
    random_access_range&lt;<ins>const</ins> V&gt; <ins>&amp;&amp; sized_range&lt;const V&gt;</ins>));
constexpr auto begin() const
  requires random_access_range&lt;const V&gt; <ins>&amp;&amp; sized_range&lt;const V&gt;</ins>;
</pre>
<blockquote>
<p>
-3- <i>Returns:</i> <code>ranges::next(ranges::begin(<i>base</i>_), <i>count</i>_, ranges::end(<i>base</i>_))</code>.
<p/>
-4- <i>Remarks:</i> In order to provide the amortized constant-time complexity required by the 
<code>range</code> concept when <code>drop_view</code> models <code>forward_range</code>, the first overload 
caches the result within the <code>drop_view</code> for use on subsequent calls. [<i>Note:</i> Without 
this, applying a <code>reverse_view</code> over a <code>drop_view</code> would have quadratic iteration 
complexity. &mdash; <i>end note</i>]
</p>
</blockquote>
</blockquote>
</li>

</ol>




</body>
</html>
