<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3829: as_rvalue_view::end should improve non-common case</title>
<meta property="og:title" content="Issue 3829: as_rvalue_view::end should improve non-common case">
<meta property="og:description" content="C++ library issue. Status: New">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3829.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="3829"><a href="lwg-active.html#3829">3829</a>. <code>as_rvalue_view::end</code> should improve non-common case</h3>
<p><b>Section:</b> 25.7.7.2 <a href="https://wg21.link/range.as.rvalue.view">[range.as.rvalue.view]</a> <b>Status:</b> <a href="lwg-active.html#New">New</a>
 <b>Submitter:</b> Hewill Kang <b>Opened:</b> 2022-11-13 <b>Last modified:</b> 2022-11-30</p>
<p><b>Priority: </b>3
</p>
<p><b>View all issues with</b> <a href="lwg-status.html#New">New</a> status.</p>
<p><b>Discussion:</b></p>
<p>
Currently, when the underlying range is not a common range, <code>as_rvalue_view::begin</code> and <code>end</code> return 
<code>move_iterator</code> and <code>move_sentinel</code> respectively, this seems reasonable since <code>move_sentinel</code> 
is a sentinel adaptor introduced by C++20 specifically for comparison with <code>move_iterator</code>.
<p/>
However, in the case where the sentinel type of the underlying type is an input iterator, this may lead to some 
performance issues, <a href="https://godbolt.org/z/8KGdqzzTK">consider</a>:
</p>  
<blockquote><pre>
#include &lt;list&gt;
#include &lt;ranges&gt;
  
int main() {
  std::list&lt;std::tuple&lt;int&amp;&gt;&gt; l;
  auto k = std::move(l) | std::views::keys;
  auto s = std::ranges::subrange(std::cbegin(k), std::end(k));
  (void) std::ranges::next(s.begin(), s.end()); // <span style="color:red;font-weight:bolder">constant time</span>
  auto r = s | std::views::as_rvalue;
  (void) std::ranges::next(r.begin(), r.end()); // <span style="color:red;font-weight:bolder">linear time</span>
}
</pre></blockquote>
<p>
The above <code>subrange</code> is constructed by the <code>elements_view::<i>iterator</i>&lt;true&gt;</code> and 
<code>elements_view::<i>iterator</i>&lt;false&gt;</code> pair, and since the former can be assigned by the latter, 
when we use <code>ranges::next</code> to increment the <code>begin</code> of <code>s</code> to its <code>end</code>, the 
<code>assignable_from</code> branch will be executed, so we get a constant-time complexity.
<p/>
However, when we apply <code>views::as_rvalue</code> to <code>s</code>, the <code>as_rvalue_view::end</code> will go 
into the non-common branch and return <code>move_sentinel</code>. And because <code>move_iterator</code> cannot be 
assigned by <code>move_sentinel</code>, <code>ranges::next</code> will successively increment the <code>begin</code> of <code>s</code> 
until its end, we get the linear-time complexity this time.
<p/>
I think it is more appropriate to return <code>move_iterator</code> for the above case, as this preserves the 
assignability, but also preserves the iterator operability that the original sentinel type has.
<p/>
Another benefit of doing this is that when the sentinel type of underlying range can be subtracted from its 
iterator type but does not model <code>sized_sentinel_for</code>, returning different <code>move_iterator</code> makes 
them still subtractable, because its <code>operator-</code> only constrain the <code>x.base() - y.base()</code> being 
well-formed.
<p/>
This also solves the issue of <code>as_rvalue_view</code> being a valid type but does not model a <code>range</code> in 
some cases, <a href="https://godbolt.org/z/GM5sMarqP">for example</a>:
</p>
<blockquote><pre>
#include &lt;ranges&gt;

int main() {
  std::move_iterator&lt;int*&gt; i;
  std::move_iterator&lt;const int*&gt; ci;
  std::ranges::subrange s(i, ci);
  std::ranges::as_rvalue_view r(s); // not failed
  static_assert(std::ranges::range&lt;decltype(r)&gt;); // <span style="color:red;font-weight:bolder">failed</span>
}
</pre></blockquote>
<p>
This is because currently, <code>as_rvalue_view</code> does not explicitly specify the template parameters of the returned 
<code>move_iterator</code> and <code>move_sentinel</code>, so based on CTAD, its <code>begin</code> will return 
<code>move_iterator(move_iterator(...))</code> which is still <code>move_iterator&lt;int*&gt;</code>, and its <code>end</code> will 
return <code>move_sentinel&lt;move_iterator&lt;const int*&gt;&gt;</code>. Those two types are not comparable, so <code>r</code> 
does not constitute a valid <code>range</code>.
<p/>
The proposed resolution is to return  <code>move_iterator</code> when the sentinel type of the underlying range models 
<code>input_iterator</code>.
</p>

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

<p>
Set priority to 3 after reflector poll.
</p>
<p>
"NAD, these examples seem entirely contrived. If not NAD, don't need the
<code>common_range</code> check if we are checking that the sentinel models
<code>input_iterator</code>."
</p>



<p id="res-3829"><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.7.2 <a href="https://wg21.link/range.as.rvalue.view">[range.as.rvalue.view]</a> as indicated:</p>

<blockquote><pre>
namespace std::ranges {
  template&lt;view V&gt;
    requires input_range&lt;V&gt;
  class as_rvalue_view : public view_interface&lt;as_rvalue_view&lt;V&gt;&gt; {
    [&hellip;]
    constexpr auto begin() requires (!<i>simple-view</i>&lt;V&gt;)
    { return move_iterator(ranges::begin(base_)); }
    constexpr auto begin() const requires range&lt;const V&gt;
    { return move_iterator(ranges::begin(base_)); }
 
    constexpr auto end() requires (!<i>simple-view</i>&lt;V&gt;) {
      if constexpr (common_range&lt;V&gt; <ins>|| input_iterator&lt;sentinel_t&lt;V&gt;&gt;</ins>) {
        return move_iterator(ranges::end(base_));
      } else {
        return move_sentinel(ranges::end(base_));
      }
    }
    constexpr auto end() const requires range&lt;const V&gt; {
      if constexpr (common_range&lt;const V&gt; <ins>|| input_iterator&lt;sentinel_t&lt;const V&gt;&gt;</ins>) {
        return move_iterator(ranges::end(base_));
      } else {
        return move_sentinel(ranges::end(base_));
      }
    }
    [&hellip;]
  };
  [&hellip;]
}
</pre></blockquote>
</li>
</ol>






</body>
</html>
