<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3474: Nesting join_views is broken because of CTAD</title>
<meta property="og:title" content="Issue 3474: Nesting join_views is broken because of CTAD">
<meta property="og:description" content="C++ library issue. Status: C++23">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3474.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="3474"><a href="lwg-defects.html#3474">3474</a>. Nesting <code>join_view</code>s is broken because of CTAD</h3>
<p><b>Section:</b> 25.7.14 <a href="https://wg21.link/range.join">[range.join]</a> <b>Status:</b> <a href="lwg-active.html#C++23">C++23</a>
 <b>Submitter:</b> Barry Revzin <b>Opened:</b> 2020-08-04 <b>Last modified:</b> 2023-11-22</p>
<p><b>Priority: </b>Not Prioritized
</p>
<p><b>View all other</b> <a href="lwg-index.html#range.join">issues</a> in [range.join].</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>
Let's say I had a range of range of ranges and I wanted to recursively flatten it. That would involve 
repeated invocations of <code>join</code>. But this doesn't work:
</p>
<blockquote><pre>
std::vector&lt;std::vector&lt;std::vector&lt;int&gt;&gt;&gt; nested_vectors = {
  {{1, 2, 3}, {4, 5}, {6}},
  {{7},       {8, 9}, {10, 11, 12}},
  {{13}}
};
auto joined = nested_vectors | std::views::join | std::views::join;
</pre></blockquote>
<p>
The expectation here is that the <code>value_type</code> of joined is <code>int</code>, but it's actually 
<code>vector&lt;int&gt;</code> &mdash; because the 2nd invocation of <code>join</code> ends up just copying 
the first. This is because <code>join</code> is specified to do:
</p>
<blockquote><p>
The name <code>views::join</code> denotes a range adaptor object (25.7.2 <a href="https://wg21.link/range.adaptor.object">[range.adaptor.object]</a>). 
Given a subexpression <code>E</code>, the expression <code>views::join(E)</code> is expression-equivalent to 
<code>join_view{E}</code>.
</p></blockquote>
<p>
And <code>join_view{E}</code> for an <code>E</code> that's already a specialization of a <code>join_view</code> 
just gives you the same <code>join_view</code> back. Yay CTAD. We need to do the same thing with <code>join</code> 
that we did with <code>reverse</code> in <a href="https://wg21.link/p1252">P1252</a>. We can do that either in 
exposition (<b>Option A</b>) my modifying 25.7.14.1 <a href="https://wg21.link/range.join.overview">[range.join.overview]</a> p2
</p>
<blockquote><p>
The name <code>views::join</code> denotes a range adaptor object (25.7.2 <a href="https://wg21.link/range.adaptor.object">[range.adaptor.object]</a>). Given 
a subexpression <code>E</code>, the expression <code>views::join(E)</code> is expression-equivalent to 
<code>join_view<ins>&lt;views::all_t&lt;decltype((E))&gt;&gt;</ins>{E}</code>.
</p>
</blockquote>
<p>
Or in code (<b>Option B</b>) add a deduction guide to 25.7.14.2 <a href="https://wg21.link/range.join.view">[range.join.view]</a>:
</p>
<blockquote><pre>
  template&lt;class R&gt;
    explicit join_view(R&amp;&amp;) -&gt; join_view&lt;views::all_t&lt;R&gt;&gt;;

  <ins>template&lt;class V&gt;
    explicit join_view(join_view&lt;V&gt;) -&gt; join_view&lt;join_view&lt;V&gt;&gt;;</ins>

</pre></blockquote>

<p><i>[2020-08-21; Issue processing telecon: Option A is Tentatively Ready]</i></p>

<p><strong>Previous resolution [SUPERSEDED]:</strong></p>
<blockquote class="note">
<p>
This wording is relative to <a href="https://wg21.link/n4861">N4861</a>.
</p>
<blockquote class="note">
<p>
[<i>Drafting Note:</i> Two mutually exclusive options are prepared, depicted below by <b>Option A</b> and 
<b>Option B</b>, respectively.] 
</p>
</blockquote>

<p>
<b>Option A:</b>
</p>
<ol>
<li><p>Modify 25.7.14.1 <a href="https://wg21.link/range.join.overview">[range.join.overview]</a> as indicated:</p>

<blockquote>
<p>
-2- <code>The name views::join</code> denotes a range adaptor object (25.7.2 <a href="https://wg21.link/range.adaptor.object">[range.adaptor.object]</a>). 
Given a subexpression <code>E</code>, the expression <code>views::join(E)</code> is expression-equivalent to 
<code>join_view<ins>&lt;views::all_t&lt;decltype((E))&gt;&gt;</ins>{E}</code>.
</p>
</blockquote>
</li>

</ol>

<p>
<b>Option B:</b>
</p>
<ol>
<li><p>Modify 25.7.14.2 <a href="https://wg21.link/range.join.view">[range.join.view]</a> as indicated:</p>

<blockquote>
<pre>
namespace std::ranges {
  [&hellip;]
  
  template&lt;class R&gt;
    explicit join_view(R&amp;&amp;) -&gt; join_view&lt;views::all_t&lt;R&gt;&gt;;
  
  <ins>template&lt;class V&gt;
    explicit join_view(join_view&lt;V&gt;) -&gt; join_view&lt;join_view&lt;V&gt;&gt;;</ins>
}
</pre>
</blockquote>
</li>

</ol>
</blockquote>
<p><i>[2020-11-09 Approved In November virtual meeting. Status changed: Tentatively Ready &rarr; WP.]</i></p>



<p id="res-3474"><b>Proposed resolution:</b></p>
<p>
This wording is relative to <a href="https://wg21.link/n4861">N4861</a>.
</p>

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

<blockquote>
<p>
-2- <code>The name views::join</code> denotes a range adaptor object (25.7.2 <a href="https://wg21.link/range.adaptor.object">[range.adaptor.object]</a>). 
Given a subexpression <code>E</code>, the expression <code>views::join(E)</code> is expression-equivalent to 
<code>join_view<ins>&lt;views::all_t&lt;decltype((E))&gt;&gt;</ins>{E}</code>.
</p>
</blockquote>
</li>

</ol>





</body>
</html>
