<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 4008: &sect;[range.utility.conv.to] ranges::to may cause infinite recursion if range_value_t&lt;C&gt; 
is a non-move-constructible range</title>
<meta property="og:title" content="Issue 4008: &sect;[range.utility.conv.to] ranges::to may cause infinite recursion if range_value_t&lt;C&gt; 
is a non-move-constructible range">
<meta property="og:description" content="C++ library issue. Status: New">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue4008.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="4008"><a href="lwg-active.html#4008">4008</a>. &sect;[range.utility.conv.to] <code>ranges::to</code> may cause infinite recursion if <code>range_value_t&lt;C&gt;</code> 
is a non-move-constructible range</h3>
<p><b>Section:</b> 25.5.7.2 <a href="https://wg21.link/range.utility.conv.to">[range.utility.conv.to]</a> <b>Status:</b> <a href="lwg-active.html#New">New</a>
 <b>Submitter:</b> S. B. Tam <b>Opened:</b> 2023-11-08 <b>Last modified:</b> 2024-03-11</p>
<p><b>Priority: </b>3
</p>
<p><b>View other</b> <a href="lwg-index-open.html#range.utility.conv.to">active issues</a> in [range.utility.conv.to].</p>
<p><b>View all other</b> <a href="lwg-index.html#range.utility.conv.to">issues</a> in [range.utility.conv.to].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#New">New</a> status.</p>
<p><b>Discussion:</b></p>
<p>
25.5.7.2 <a href="https://wg21.link/range.utility.conv.to">[range.utility.conv.to]</a>/2 says:
</p>
<blockquote>
<ol style="list-style-type: none">
<li><p>(2.1) &mdash; If <code>C</code> does not satisfy <code>input_range</code> or 
<code>convertible_to&lt;range_reference_t&lt;R&gt;, range_value_t&lt;C&gt;&gt;</code> is <code>true</code>:</p>
<ol style="list-style-type: none">
<li><p>[&hellip;]</p></li>
</ol></li>
<li><p>(2.2) &mdash; Otherwise, if <code>input_range&lt;range_reference_t&lt;R&gt;&gt;</code> is <code>true</code>:
</p>
<blockquote>
<pre>
to&lt;C&gt;(r | views::transform([](auto&amp;&amp; elem) {
  return to&lt;range_value_t&lt;C&gt;&gt;(std::forward&lt;decltype(elem)&gt;(elem));
}), std::forward&lt;Args&gt;(args)...);
</pre>
</blockquote>
</li>
<li><p>[&hellip;]</p></li>
</ol>
</blockquote>
<p>
That is, if <code>range_reference_t&lt;R&gt;</code> is not convertible to <code>range_value_t&lt;C&gt;</code>, and 
<code>range_reference_t&lt;R&gt;</code> is an input range, <code>views::transform</code> is applied to convert the 
inner range to <code>range_value_t&lt;C&gt;</code> (through <code>to&lt;range_value_t&lt;C&gt;&gt;</code>), and then 
the transformed range is converted to <code>C</code> (through <code>to&lt;C&gt;</code>).
<p/>
Consider:
</p>
<blockquote><pre>
#include &lt;ranges&gt;

struct ImmovableRange {
  ImmovableRange(int*, int*);
  ImmovableRange(ImmovableRange&amp;&amp;) = delete;

  int* begin();
  int* end();
};

struct C {
  ImmovableRange* begin();
  ImmovableRange* end();
};

using R = int[1][2];

void test() {
  (void)std::ranges::to&lt;C&gt;(R{});
}
</pre></blockquote>
<p>
Here:
</p>
<ol>
<li><p><code>convertible_to&lt;range_reference_t&lt;R&gt;, range_value_t&lt;C&gt;&gt;</code> is <code>false</code>.</p></li>
<li><p><code>range_reference_t&lt;R&gt;</code> satisfies <code>input_range</code>.</p></li>
<li><p><code>range_reference_t&lt;R&gt;</code> can be converted to <code>range_value_t&lt;C&gt;</code> through 
<code>to&lt;range_value_t&lt;C&gt;&gt;</code>. (If it couldn't, an error would be produced immediately.)</p></li>
</ol>
<p>
So <code>to&lt;C&gt;</code> is called recursively, constructing <code>C</code> with the transformed range (whose 
<code>range_reference_t&lt;R&gt;</code> is the same as <code>range_value_t&lt;C&gt;</code>). For the construction 
from the transformed range:
</p>
<ol>
<li><p><code>range_reference_t&lt;R&gt;</code> and <code>range_value_t&lt;C&gt;</code> are both <code>ImmovableRange</code>.</p></li>
<li><p><code>convertible_to&lt;range_reference_t&lt;R&gt;, range_value_t&lt;C&gt;&gt;</code> (i.e. 
<code>convertible_to&lt;ImmovableRange, ImmovableRange&gt;</code>) is <code>false</code>.</p></li>
<li><p><code>range_reference_t&lt;R&gt;</code> (i.e. <code>ImmovableRange</code>) satisfies <code>input_range</code>.</p></li>
<li><p><code>range_reference_t&lt;R&gt;</code> can be converted to <code>range_value_t&lt;C&gt;</code> through 
<code>to&lt;range_value_t&lt;C&gt;&gt;</code>.</p></li>
</ol>
<p>
So <code>to&lt;C&gt;</code> is called recursively again, transforming the range for the second time. This time,
the transformation does not change any of the above four facts. As a result, <code>to&lt;C&gt;</code> is called 
yet again, leading to an infinite recursion.
<p/>
I believe this can be fixed by stop calling <code>to&lt;C&gt;</code> recursively when <code>range_reference_t&lt;R&gt;</code> 
is the same as <code>range_value_t&lt;C&gt;</code>.
</p>

<p><i>[2024-03-11; Reflector poll]</i></p>

<p>
Set priority to 3 after reflector poll.
</p>
<p>
"Do we want <code>same_as</code> or <code>!<em>different-from</em></code>?"
</p>



<p id="res-4008"><b>Proposed resolution:</b></p>
<p>
This wording is relative to <a href="https://wg21.link/N4964" title=" Working Draft, Programming Languages — C++">N4964</a>.
</p>

<ol>

<li><p>Modify 25.5.7.2 <a href="https://wg21.link/range.utility.conv.to">[range.utility.conv.to]</a> as indicated:</p>

<blockquote>
<pre>
template&lt;class C, input_range R, class... Args&gt; requires (!view&lt;C&gt;)
  constexpr C to(R&amp;&amp; r, Args&amp;&amp;... args);
</pre>
<blockquote>
<p>
-1- <i>Mandates</i>: <code>C</code> is a cv-unqualified class type.
</p>
<p>
-2- <i>Returns</i>: An object of type <code>C</code> constructed from the elements of <code>r</code> in the following manner:
</p>
<ol style="list-style-type: none">
<li><p>(2.1) &mdash; If <code>C</code> does not satisfy <code>input_range</code> or 
<code>convertible_to&lt;range_reference_t&lt;R&gt;, range_value_t&lt;C&gt;&gt;</code> is <code>true</code>:</p>
<ol style="list-style-type: none">
<li><p>[&hellip;]</p></li>
</ol></li>
<li><p>(2.2) &mdash; Otherwise, if <ins><code>same_as&lt;range_reference_t&lt;R&gt;, range_value_t&lt;C&gt;&gt;</code> 
is <code>false</code> and</ins> <code>input_range&lt;range_reference_t&lt;R&gt;&gt;</code> is <code>true</code>:
</p>
<blockquote>
<pre>
to&lt;C&gt;(r | views::transform([](auto&amp;&amp; elem) {
  return to&lt;range_value_t&lt;C&gt;&gt;(std::forward&lt;decltype(elem)&gt;(elem));
}), std::forward&lt;Args&gt;(args)...);
</pre>
</blockquote>
</li>
<li><p>(2.3) &mdash; Otherwise, the program is ill-formed.</p></li>
</ol>
</blockquote>
</blockquote>


</li>

</ol>





</body>
</html>
