<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3386: elements_view needs its own sentinel type</title>
<meta property="og:title" content="Issue 3386: elements_view needs its own sentinel type">
<meta property="og:description" content="C++ library issue. Status: C++20">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3386.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="3386"><a href="lwg-defects.html#3386">3386</a>. <code>elements_view</code> needs its own <code>sentinel</code> type</h3>
<p><b>Section:</b> 25.7.23 <a href="https://wg21.link/range.elements">[range.elements]</a> <b>Status:</b> <a href="lwg-active.html#C++20">C++20</a>
 <b>Submitter:</b> Tim Song <b>Opened:</b> 2020-02-07 <b>Last modified:</b> 2021-02-25</p>
<p><b>Priority: </b>1
</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>elements_view</code> is effectively a specialized version of <code>transform_view</code>.
The latter has a custom <code>sentinel</code> type, and so should <code>elements_view</code>.
<p/>
In particular, it should not use the underlying range's sentinel directly, for that sentinel
could encode a generic predicate that is equally meaningful for the adapted range. Consider a
range <code>[i, s)</code> whose <code>value_type</code> is <code>pair&lt;array&lt;int, 2&gt;, long&gt;</code>,
where <code>s</code> is a generic sentinel that checks if the second element (for this range in particular,
the <code>long</code>) is zero:
</p>
<blockquote>
<pre>
struct S {
  friend bool operator==(input_iterator auto const&amp; i, S) /* additional constraints */
  { return get&lt;1&gt;(*i) == 0; }
};
</pre>
</blockquote>
<p>
If we adapt <code>[i, s)</code> with <code>views::keys</code>, then the resulting adapted range would have
surprising behavior when used with <code>S{}</code>: even though it's nominally a range of
<code>array&lt;int, 2&gt;</code>, when its iterator is used with the sentinel <code>S{}</code> 
it doesn't actually check the second element of the array, but the <code>long</code> that's not even
part of the <code>value_type</code>:
</p>
<blockquote>
<pre>
void algo(input_range auto&amp;&amp; r) /* constraints */{
  // We want to stop at the first element of the range r whose second element is zero.
  for (auto&amp;&amp; x : subrange{ranges::begin(r), S{}})
  {
    std::cout &lt;&lt; get&lt;0&gt;(x);
  }
}

using P = pair&lt;array&lt;int, 2&gt;, long&gt;;
vector&lt;P&gt; vec =  {
    { {0, 1}, 1L },
    { {1, 0}, 1L },
    { {2, 2}, 0L }
};
   
subrange r{vec.begin(), S{}};              // range with two elements: {0, 1}, {1, 0}

algo(r | views::keys);                     // checks the long, prints '01'
algo(r | views::transform(&amp;P::first));     // checks the second element of the array, prints '0'
</pre>
</blockquote>
<p>
This is an API break since it changes the return type of <code>end()</code>, so it should be
fixed before we ship C++20.
</p>

<p><i>[2020-02 Prioritized as P1 Monday morning in Prague]</i></p>

<p><i>[2020-06-11 Voted into the WP in Prague. Status changed: New &rarr; WP.]</i></p>



<p id="res-3386"><b>Proposed resolution:</b></p>
<p>The proposed wording is contained in <a href="https://wg21.link/p1994r0">P1994R0</a>.</p>





</body>
</html>
