<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3783: views::common may not be a range adaptor object</title>
<meta property="og:title" content="Issue 3783: views::common may not be a range adaptor object">
<meta property="og:description" content="C++ library issue. Status: New">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3783.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="3783"><a href="lwg-active.html#3783">3783</a>. <code>views::common</code> may not be a range adaptor object</h3>
<p><b>Section:</b> 24.5.5.1 <a href="https://wg21.link/common.iterator">[common.iterator]</a>, 24.5.5.6 <a href="https://wg21.link/common.iter.cmp">[common.iter.cmp]</a> <b>Status:</b> <a href="lwg-active.html#New">New</a>
 <b>Submitter:</b> Hewill Kang <b>Opened:</b> 2022-09-18 <b>Last modified:</b> 2022-10-12</p>
<p><b>Priority: </b>3
</p>
<p><b>View other</b> <a href="lwg-index-open.html#common.iterator">active issues</a> in [common.iterator].</p>
<p><b>View all other</b> <a href="lwg-index.html#common.iterator">issues</a> in [common.iterator].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#New">New</a> status.</p>
<p><b>Discussion:</b></p>
<p>
<a href="https://wg21.link/P2325R3" title=" Views should not be required to be default constructible">P2325R3</a> makes <code>input_or_output_iterator</code> no longer require <code>default_initializable</code>, 
which means that the template parameter <code>I</code> of <code>common_iterator</code> no longer requires 
<code>default_initializable</code>.
<p/>
In this case, since <code>common_iterator</code> itself cannot be default-constructed, it can never be a valid 
sentinel even if it can be compared to itself. Furthermore, this also makes <code>views::common</code> return a 
non-<code>range</code> even if it is well-formed (<a href="https://godbolt.org/z/dn4c7M57W">online example</a>):
</p>
<blockquote><pre>
#include &lt;ranges&gt;
#include &lt;vector&gt;

int main() {
  std::vector&lt;int&gt; v;
  auto r = std::views::counted(std::back_inserter(v), 3);
  auto cr = r | std::views::common;
  static_assert(std::ranges::range&lt;decltype(cr)&gt;); // <span style="color:red;font-weight:bolder">failed</span>
}
</pre></blockquote>
<p>
which causes <code>views::common</code> to be unable to convert a <code>range</code> into a <code>view</code>, 
making it not a valid range adaptor.
<p/>
I think <code>common_iterator</code> should always be <code>default_initializable</code>, 
which makes it eligible to be a legitimate sentinel.
<p/>
The proposed resolution provides a default constructor for <code>common_iterator</code> when <code>I</code> is 
not <code>default_initializable</code>, in which case constructs the <code>variant</code> with an alternative type 
of <code>S</code>.
</p>

<p><i>[2022-09-28; Reflector poll]</i></p>

<p>
Set priority to 3 after reflector poll.
</p>
<p>
"The P/R means that sometimes the variant containers an iterator and sometimes
contains a sentinel, depending on whether the iterator is default constructible.
Always constructing a sentinel would be more consistent."
</p>



<p id="res-3783"><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 24.5.5.1 <a href="https://wg21.link/common.iterator">[common.iterator]</a>, class template <code>common_iterator</code> synopsis, as indicated:</p>

<blockquote>
<pre>
namespace std {
  template&lt;input_or_output_iterator I, sentinel_for&lt;I&gt; S&gt;
    requires (!same_as&lt;I, S&gt; &amp;&amp; copyable&lt;I&gt;)
  class common_iterator {
  public:
    constexpr common_iterator() requires default_initializable&lt;I&gt; = default;
    <ins>constexpr common_iterator();</ins>
    [&hellip;]
  };
  [&hellip;]
}
</pre>
</blockquote>
</li>

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

<blockquote>
<pre>
<ins>constexpr common_iterator();</ins>
</pre>
<blockquote>
<p>
<ins>-?- <i>Effects</i>: Initializes <code>v_</code> as if by <code>v_{in_place_type&lt;S&gt;}</code>.</ins>
</p>
</blockquote>
<pre>
constexpr common_iterator(I i);
</pre>
<blockquote>
<p>
-1- <i>Effects</i>: Initializes <code>v_</code> as if by <code>v_{in_place_type&lt;I&gt;, std::move(i)}</code>.
</p>
</blockquote>
</blockquote>
</li>

</ol>





</body>
</html>
