<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3563: keys_view example is broken</title>
<meta property="og:title" content="Issue 3563: keys_view example is broken">
<meta property="og:description" content="C++ library issue. Status: C++23">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3563.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++23">C++23</a> status.</em></p>
<h3 id="3563"><a href="lwg-defects.html#3563">3563</a>. <code>keys_view</code> example is broken</h3>
<p><b>Section:</b> 25.7.23.1 <a href="https://wg21.link/range.elements.overview">[range.elements.overview]</a> <b>Status:</b> <a href="lwg-active.html#C++23">C++23</a>
 <b>Submitter:</b> Barry Revzin <b>Opened:</b> 2021-05-29 <b>Last modified:</b> 2023-11-22</p>
<p><b>Priority: </b>3
</p>
<p><b>View all issues with</b> <a href="lwg-status.html#C++23">C++23</a> status.</p>
<p><b>Discussion:</b></p>
<p>
In 25.7.23.1 <a href="https://wg21.link/range.elements.overview">[range.elements.overview]</a> we have:
</p>
<blockquote><p>
<code>keys_view</code> is an alias for <code>elements_view&lt;views::all_t&lt;R&gt;, 0&gt;</code>, and
is useful for extracting keys from associative containers.
<p/>
[<i>Example 2:</i>
</p>
<blockquote><pre>
auto names = keys_view{historical_figures};
for (auto&amp;&amp; name : names) {
  cout &lt;&lt; name &lt;&lt; ' ';  <i>// prints Babbage Hamilton Lovelace Turing</i>
}
</pre></blockquote>
<p>
&mdash; <i>end example</i>]
</p>
</blockquote>
<p>
Where <code>keys_view</code> is defined as:
</p>
<blockquote><pre>
template&lt;class R&gt;
  using keys_view = elements_view&lt;views::all_t&lt;R&gt;, 0&gt;;
</pre></blockquote>
<p>
There's a similar example for <code>values_view</code>.
<p/>
But class template argument deduction cannot work here, <code>keys_view(r)</code> cannot deduce <code>R</code> &mdash;
it's a non-deduced context. At the very least, the example is wrong and should be rewritten as
<code>views::keys(historical_figures)</code>. Or, I guess, as
<code>keys_view&lt;decltype(historical_figures)&gt;(historical_figures)&gt;</code> (but really, not).
</p>

<p><i>[2021-05-29 Tim comments and provides wording]</i></p>

<p>
I have some ideas about making CTAD work for <code>keys_view</code> and
<code>values_view</code>, but current implementations of alias template CTAD are
not yet in a shape to permit those ideas to be tested.
What <em>is</em> clear, though, is that the current definitions of <code>keys_view</code>
and <code>values_view</code> can't possibly ever work for CTAD, because <code>R</code> is only
used in a non-deduced context and so they can never meet the "deducible" constraint in
12.2.2.9 <a href="https://wg21.link/over.match.class.deduct">[over.match.class.deduct]</a> p2.3.
<p/>
The proposed resolution below removes the <code>views::all_t</code> transformation
in those alias templates. This makes these aliases transparent enough to support CTAD out
of the box when a view is used, and any questions about deduction guides on
<code>elements_view</code> can be addressed later once the implementations become more mature.
This also makes them consistent with all the other views: you can't write
<code>elements_view&lt;map&lt;int, int&gt;&amp;, 0&gt;</code> or
<code>reverse_view&lt;map&lt;int, int&gt;&amp;&gt;</code>, so why do we allow
<code>keys_view&lt;map&lt;int, int&gt;&amp;&gt;</code>? It is, however, a breaking change,
so it is important that we get this in sooner rather than later.
<p/>
The proposed resolution has been implemented and tested.
</p>

<p><i>[2021-06-14; Reflector poll]</i></p>

<p>
Set priority to 3 after reflector poll in August.
Partially addressed by an
<a href="https://github.com/cplusplus/draft/pull/4659">editorial change</a>.
</p>

<p><strong>Previous resolution [SUPERSEDED]:</strong></p>
<blockquote class="note">
<p>
This wording is relative to <a href="https://wg21.link/n4885">N4885</a>.
</p>

<ol>
<li><p>Modify 25.2 <a href="https://wg21.link/ranges.syn">[ranges.syn]</a>, header <code>&lt;ranges&gt;</code> synopsis, as indicated:</p>

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

  <i>// 25.7.23 <a href="https://wg21.link/range.elements">[range.elements]</a>, elements view</i>
  template&lt;input_range V, size_t N&gt;
    requires <i>see below</i>
  class elements_view;

  template&lt;class T, size_t N&gt;
    inline constexpr bool enable_borrowed_range&lt;elements_view&lt;T, N&gt;&gt; = enable_borrowed_range&lt;T&gt;;

  template&lt;class R&gt;
    using keys_view = elements_view&lt;<del>views::all_t&lt;</del>R<del>&gt;</del>, 0&gt;;
  template&lt;class R&gt;
    using values_view = elements_view&lt;<del>views::all_t&lt;</del>R<del>&gt;</del>, 1&gt;;

[&hellip;]
}
</pre>
</blockquote>
</li>

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

<blockquote>
<p>
-3- <code>keys_view</code> is an alias for <code>elements_view&lt;<del>views::all_t&lt;</del>R<del>&gt;</del>, 0&gt;</code>,
and is useful for extracting keys from associative containers.
<p/>
[<i>Example 2</i>:
</p>
<blockquote>
<pre>
auto names = keys_view{<ins>views::all(</ins>historical_figures<ins>)</ins>};
for (auto&amp;&amp; name : names) {
  cout &lt;&lt; name &lt;&lt; ' '; <i>// prints Babbage Hamilton Lovelace Turing</i>
}
</pre>
</blockquote>
<p>
&mdash; <i>end example</i>]
<p/>
-4- <code>values_view</code> is an alias for <code>elements_view&lt;<del>views::all_t&lt;</del>R<del>&gt;</del>, 1&gt;</code>,
and is useful for extracting values from associative containers.
<p/>
[<i>Example 3</i>:
</p>
<blockquote>
<pre>
auto is_even = [](const auto x) { return x % 2 == 0; };
cout &lt;&lt; ranges::count_if(values_view{<ins>views::all(</ins>historical_figures<ins>)</ins>}, is_even); <i>// prints 2</i>
</pre>
</blockquote>
<p>
&mdash; <i>end example</i>]
</p>
</blockquote>
</li>

</ol>
</blockquote>
<p><i>[2021-06-18 Tim syncs wording to latest working draft]</i></p>

<p>
The examples have been editorially corrected, so the new wording simply changes the
definition of <code>keys_view</code> and <code>values_view</code>.
</p>

<p><i>[2021-09-20; Reflector poll]</i></p>

<p>
Set status to Tentatively Ready after six votes in favour during reflector poll.
</p>

<p><i>[2021-10-14 Approved at October 2021 virtual plenary. Status changed: Voting &rarr; WP.]</i></p>



<p id="res-3563"><b>Proposed resolution:</b></p>
<p>
This wording is relative to <a href="https://wg21.link/N4892" title=" Working Draft, Standard for Programming Language C++">N4892</a>.
</p>

<ol>
<li><p>Modify 25.2 <a href="https://wg21.link/ranges.syn">[ranges.syn]</a>, header <code>&lt;ranges&gt;</code> synopsis, as indicated:</p>

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

  <i>// 25.7.23 <a href="https://wg21.link/range.elements">[range.elements]</a>, elements view</i>
  template&lt;input_range V, size_t N&gt;
    requires <i>see below</i>
  class elements_view;

  template&lt;class T, size_t N&gt;
    inline constexpr bool enable_borrowed_range&lt;elements_view&lt;T, N&gt;&gt; = enable_borrowed_range&lt;T&gt;;

  template&lt;class R&gt;
    using keys_view = elements_view&lt;<del>views::all_t&lt;</del>R<del>&gt;</del>, 0&gt;;
  template&lt;class R&gt;
    using values_view = elements_view&lt;<del>views::all_t&lt;</del>R<del>&gt;</del>, 1&gt;;

[&hellip;]
}
</pre>
</blockquote>
</li>

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

<blockquote>
<p>
-3- <code>keys_view</code> is an alias for <code>elements_view&lt;<del>views::all_t&lt;</del>R<del>&gt;</del>, 0&gt;</code>,
and is useful for extracting keys from associative containers.
<p/>
[<i>Example 2</i>: &hellip; &mdash; <i>end example</i>]
<p/>
-4- <code>values_view</code> is an alias for <code>elements_view&lt;<del>views::all_t&lt;</del>R<del>&gt;</del>, 1&gt;</code>,
and is useful for extracting values from associative containers.
<p/>
[<i>Example 3</i>: &hellip; &mdash; <i>end example</i>]
</p>
</blockquote>
</li>

</ol>




</body>
</html>
