<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3797: elements_view insufficiently constrained</title>
<meta property="og:title" content="Issue 3797: elements_view insufficiently constrained">
<meta property="og:description" content="C++ library issue. Status: New">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3797.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="3797"><a href="lwg-active.html#3797">3797</a>. <code>elements_view</code> insufficiently constrained</h3>
<p><b>Section:</b> 25.7.23.2 <a href="https://wg21.link/range.elements.view">[range.elements.view]</a> <b>Status:</b> <a href="lwg-active.html#New">New</a>
 <b>Submitter:</b> Hui Xie <b>Opened:</b> 2022-10-21 <b>Last modified:</b> 2022-11-01</p>
<p><b>Priority: </b>2
</p>
<p><b>View all other</b> <a href="lwg-index.html#range.elements.view">issues</a> in [range.elements.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>
This issue came up when I tried to integrate the C++23 changes to tuple-like into <code>ranges::elements_view</code> 
in libc++. Given the following test:
</p>
<blockquote><pre>
Using SubRange = ranges::subrange&lt;MoveOnlyIter, Sent&gt;;
std::vector&lt;SubRange&gt; srs = ...;  // a vector of subranges
for(auto&amp;&amp; iter : srs | views::elements&lt;0&gt;){
}
</pre></blockquote>
<p>
The above code results in a hard error in deciding the <code>iterator_category</code> (The base is a random access range 
so it should exist). The immediate hard error complains that the following expression is invalid. 
</p>
<blockquote><pre>
std::get&lt;N&gt;(*current_);
</pre></blockquote>
<p>
Note that even if <code>iterator_category</code> does not complain, it will complain later when we dereference the iterator.
<p/>
Here are the declarations of the "get" overloads for <code>subrange</code>:
</p>
<blockquote><pre>
template&lt;size_t N, class I, class S, subrange_kind K&gt;
  requires ((N == 0 &amp;&amp; copyable&lt;I&gt;) || N == 1)
  constexpr auto get(const subrange&lt;I, S, K&gt;&amp; r);

template&lt;size_t N, class I, class S, subrange_kind K&gt;
  requires (N &lt; 2)
  constexpr auto get(subrange&lt;I, S, K&gt;&amp;&amp; r);
</pre></blockquote>
<p>
Note that the first overload requires <code>copyable&lt;I&gt;</code> which is <code>false</code> and 
the second overload requires an rvalue, which is also not the case. So we don't have a valid "get" in this case.
<p/>
But why does <code>elements_view</code> allow the instantiation in the first place? Let's look at its requirements:
</p>
<blockquote><pre>
template&lt;class T, size_t N&gt;
  concept <i>returnable-element</i> =                  <i>// exposition only</i>
    is_reference_v&lt;T&gt; || move_constructible&lt;tuple_element_t&lt;N, T&gt;&gt;;

template&lt;input_range V, size_t N&gt;
    requires view&lt;V&gt; &amp;&amp; has-tuple-element&lt;range_value_t&lt;V&gt;, N&gt; &amp;&amp;
             <i>has-tuple-element</i>&lt;remove_reference_t&lt;range_reference_t&lt;V&gt;&gt;, N&gt; &amp;&amp;
             <i>returnable-element</i>&lt;range_reference_t&lt;V&gt;, N&gt;
  class elements_view;
</pre></blockquote>
<p>
It passed the "<code>is_reference_v&lt;range_reference_t&lt;V&gt;&gt;</code>" requirement, because it is 
"<code>subrange&amp;</code>". Here the logic has an assumption: if the tuple-like is a reference, 
then we can always "<code>get</code>" and return a reference. This is not the case for <code>subrange</code>. 
<code>subrange</code>'s <code>get</code> always return by value.
</p>

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

<p>
Set priority to 2 after reflector poll.
</p>
<p>
"The actual issue is that <a href="https://wg21.link/P2165" title=" Compatibility between tuple, pair and tuple-like objects">P2165</a> broke
<code><i>has-tuple-element</i></code> for this case. We should unbreak it."
</p>



<p id="res-3797"><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>

<blockquote class="note">
<p>
[<i>Drafting Note:</i> Three mutually exclusive options are prepared, depicted below by <b>Option A</b>, 
<b>Option B</b>, and <b>Option C</b>, respectively.] 
</p>
</blockquote>

<p>
<b>Option A:</b> Properly disallow this case (preferred solution)
</p>

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

<blockquote>
<pre>
namespace std::ranges {
  [&hellip;]
  template&lt;class T, size_t N&gt;
  concept <i>returnable-element</i> =              <i>// exposition only</i>
    <ins>requires { std::get&lt;N&gt;(declval&lt;T&gt;()); } &amp;&amp;</ins>
    is_reference_v&lt;T&gt; || move_constructible&lt;tuple_element_t&lt;N, T&gt;&gt;;  
  [&hellip;]
}
</pre>
</blockquote>
</li>

</ol>

<p>
<b>Option B:</b> Relax <code>subrange</code>'s <code>get</code> to have more overloads. Since <code>subrange</code>'s 
non-<code>const begin</code> unconditionally moves the iterator (even for lvalue-reference), 
</p>
<blockquote><pre>
[[nodiscard]] constexpr I begin() requires (!copyable&lt;I&gt;);
Effects: Equivalent to: return std::move(<i>begin_</i>);
</pre></blockquote>
<p>
if we add more <code>get</code> overloads, it would work. The non-const lvalue-ref overload would work 
(and it also moves because non-<code>const</code> lvalue begin moves). This solution would make another way
to let <code>subrange</code>'s iterator in moved-from state, which is not good.
</p>

<ol>
<li><p>Modify 25.2 <a href="https://wg21.link/ranges.syn">[ranges.syn]</a> as indicated:</p>

<blockquote>
<pre>
[&hellip;]
namespace std::ranges {
  [&hellip;]

  template&lt;size_t N, class I, class S, subrange_kind K&gt;
    requires ((N == 0 &amp;&amp; copyable&lt;I&gt;) || N == 1)
    constexpr auto get(const subrange&lt;I, S, K&gt;&amp; r);

  template&lt;size_t N, class I, class S, subrange_kind K&gt;
    requires (N &lt; 2)
    constexpr auto get(subrange&lt;I, S, K&gt;&amp;&amp; r);
    
  <ins>template&lt;size_t N, class I, class S, subrange_kind K&gt;
    requires ((N == 0 &amp;&amp; constructible_from&lt;I, const I&amp;&amp;&gt;) || N == 1)
    constexpr auto get(const subrange&lt;I, S, K&gt;&amp;&amp; r);
  
  template&lt;size_t N, class I, class S, subrange_kind K&gt;
    requires (N &lt; 2)
    constexpr auto get(subrange&lt;I, S, K&gt;&amp; r);</ins>
}
[&hellip;]
</pre>
</blockquote>
</li>

</ol>

<p>
<b>Option C:</b> Make <code>subrange</code>'s get to return by reference. This seems to significantly 
change the <code>subrange</code>'s tuple protocol, which is not ideal.
</p>






</body>
</html>
