<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3387: &sect;[range.reverse.view] reverse_view&lt;V&gt; unintentionally requires range&lt;const V&gt;</title>
<meta property="og:title" content="Issue 3387: &sect;[range.reverse.view] reverse_view&lt;V&gt; unintentionally requires range&lt;const V&gt;">
<meta property="og:description" content="C++ library issue. Status: C++20">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3387.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++20">C++20</a> status.</em></p>
<h3 id="3387"><a href="lwg-defects.html#3387">3387</a>. &sect;[range.reverse.view] <code>reverse_view&lt;V&gt;</code> unintentionally requires <code>range&lt;const V&gt;</code></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#C++20">C++20</a>
 <b>Submitter:</b> Patrick Palka <b>Opened:</b> 2020-02-04 <b>Last modified:</b> 2021-02-25</p>
<p><b>Priority: </b>0
</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#C++20">C++20</a> status.</p>
<p><b>Discussion:</b></p>
<p>
<code>reverse_view&lt;V&gt;</code> requires <code>bidirectional_range&lt;V&gt;</code>, but not <code>range&lt;const V&gt;</code>, 
which means that <code>iterator_t&lt;const V&gt;</code> might be an invalid type. The return types of the 
<code>begin() const</code> and <code>end() const</code> overloads make use of <code>iterator_t&lt;const V&gt;</code> 
in a non-SFINAE context, which means that instantiating <code>reverse_view&lt;X&gt;</code> is ill-formed unless 
<code>range&lt;const X&gt;</code> is satisfied.
<p/>
Code like <code>x | views::filter(p) | views::reverse</code> fails to compile because 
<code>const filter_view&lt;&hellip;&gt;</code> does not model <code>range</code>, so 
<code>iterator_t&lt;const filter_view&lt;&hellip;&gt;&gt;</code> is invalid.
<p/>
Either <code>range&lt;const V&gt;</code> needs to be in the class' <code>requires</code>-clause, or the return types 
of the <code>const</code>-qualified <code>begin()</code> and <code>end()</code> need to delay use of 
<code>iterator_t&lt;const V&gt;</code> until <code>range&lt;const V&gt;</code> is known to be satisfied.
<p/>
Giving these overloads an <code>auto</code> return type means the type is determined when the member is called. 
The constraint <code>common_range&lt;const V&gt;</code> appropriately restricts the selection of these overloads, 
so they can only be called when the type is valid. This is what <code>cmcstl2</code> does. <code>range-v3</code> 
makes the <code>begin() const</code> and <code>end() const</code> members into function templates, so that they 
are SFINAE contexts.
</p>

This is related to <a href="lwg-defects.html#3347" title="std::pair&lt;T, U&gt; now requires T and U to be less-than-comparable (Status: C++20)">3347</a><sup><a href="https://cplusplus.github.io/LWG/issue3347" title="Latest snapshot">(i)</a></sup>.
<p><i>[2020-02 Prioritized as IMMEDIATE Monday morning in Prague]</i></p>



<p id="res-3387"><b>Proposed resolution:</b></p>
<p>This wording is relative to <a href="https://wg21.link/n4849">N4849</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>
<blockquote>
<pre>
namespace std::ranges {
  template&lt;view V&gt;
    requires bidirectional_range&lt;V&gt;
  class reverse_view : public view_interface&lt;reverse_view&lt;V&gt;&gt; {
    [&hellip;]
    constexpr reverse_iterator&lt;iterator_t&lt;V&gt;&gt; begin();
    constexpr reverse_iterator&lt;iterator_t&lt;V&gt;&gt; begin() requires common_range&lt;V&gt;;
    constexpr <del>reverse_iterator&lt;iterator_t&lt;const V&gt;&gt;</del><ins>auto</ins> begin() const
      requires common_range&lt;const V&gt;;  
    
    constexpr reverse_iterator&lt;iterator_t&lt;V&gt;&gt; end();
    constexpr <del>reverse_iterator&lt;iterator_t&lt;const V&gt;&gt;</del><ins>auto</ins> end() const
      requires common_range&lt;const V&gt;;  
    [&hellip;]
  };
  [&hellip;]
}
</pre>
[&hellip;]
</blockquote>
<blockquote>
<pre>
constexpr reverse_iterator&lt;iterator_t&lt;V&gt;&gt; begin() requires common_range&lt;V&gt;;
constexpr <del>reverse_iterator&lt;iterator_t&lt;const V&gt;&gt;</del><ins>auto</ins> begin() const
  requires common_range&lt;const V&gt;;
</pre>
<blockquote>
<p>
-5- <i>Effects:</i> Equivalent to: <code>return make_reverse_iterator(ranges::end(base_));</code>
</p>
</blockquote>
<pre>
constexpr reverse_iterator&lt;iterator_t&lt;V&gt;&gt; end();
constexpr <del>reverse_iterator&lt;iterator_t&lt;const V&gt;&gt;</del><ins>auto</ins> end() const
  requires common_range&lt;const V&gt;;
</pre>
<blockquote>
<p>
-6- <i>Effects:</i> Equivalent to: <code>return make_reverse_iterator(ranges::begin(base_));</code>
</p>
</blockquote>
</blockquote>
</blockquote>
</li>
</ol>




</body>
</html>
