<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3855: tiny-range is not quite right</title>
<meta property="og:title" content="Issue 3855: tiny-range is not quite right">
<meta property="og:description" content="C++ library issue. Status: New">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3855.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="3855"><a href="lwg-active.html#3855">3855</a>. <code><i>tiny-range</i></code> is not quite right</h3>
<p><b>Section:</b> 25.7.16.2 <a href="https://wg21.link/range.lazy.split.view">[range.lazy.split.view]</a> <b>Status:</b> <a href="lwg-active.html#New">New</a>
 <b>Submitter:</b> Hewill Kang <b>Opened:</b> 2023-01-07 <b>Last modified:</b> 2025-04-28</p>
<p><b>Priority: </b>4
</p>
<p><b>View other</b> <a href="lwg-index-open.html#range.lazy.split.view">active issues</a> in [range.lazy.split.view].</p>
<p><b>View all other</b> <a href="lwg-index.html#range.lazy.split.view">issues</a> in [range.lazy.split.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>
Currently, <code>lazy_split_view</code> supports input range when the element of the pattern is less than or equal to 1. 
In order to ensure this condition at compile time, <code><i>tiny-range</i></code> constrains the type <code>R</code> to model 
<code>sized_range</code> and requires <code>(remove_reference_t&lt;R&gt;::size() &lt;= 1)</code> to be a constant expression.
<p/>
However, modeling a <code>sized_range</code> does not guarantee that <code>ranges::size</code> will be evaluated by <code>R::size()</code>.
For example, when <code>disable_sized_range&lt;R&gt;</code> is specialized to <code>true</code> or <code>R::size()</code> returns a non-integer-like type, 
<code>ranges::size</code> can still compute the size by subtracting the iterator-sentinel pair when both satisfy <code>sized_sentinel_for</code>.
<p/>
Since the <code>lazy_split_view</code>'s iterator uses <code>R::size()</code> to get the constant value of the pattern, 
we must ensure that this is indeed how <code>ranges::size</code> is calculated. Also, I think we can simplify 
<code><i>tiny-range</i></code> with <code>bool_constant</code> in a way similar to LWG <a href="lwg-defects.html#3150" title="UniformRandomBitGenerator should validate min and max (Status: C++20)">3150</a><sup><a href="https://cplusplus.github.io/LWG/issue3150" title="Latest snapshot">(i)</a></sup>, which removes the 
introduction of <code><i>require-constant</i></code>.
</p>

<p><i>[2023-02-01; Reflector poll]</i></p>

<p>
Set priority to 4 after reflector poll.
Only matters for pathological types.
Maybe use <code>requires bool_constant&lt;ranges::size(r) &lt;= 1&gt;</code>.
</p>

<p><strong>Previous resolution [SUPERSEDED]:</strong></p>
<blockquote class="note">

<p>
This wording is relative to <a href="https://wg21.link/N4917" title=" Working Draft, Standard for Programming Language C++">N4917</a>.
</p>

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

<blockquote>
<blockquote>
<pre>
namespace std::ranges {
  <del>template&lt;auto&gt; struct <i>require-constant</i>; <i>// exposition only</i></del>

  <del>template&lt;class R&gt;
  concept <i>tiny-range</i> = <i>// exposition only</i>
     sized_range&lt;R&gt; &amp;&amp;
     requires { typename <i>require-constant</i>&lt;remove_reference_t&lt;R&gt;::size()&gt;; } &amp;&amp;
     (remove_reference_t&lt;R&gt;::size() &lt;= 1);</del>
   
  template&lt;input_range V, forward_range Pattern&gt;
    requires view&lt;V&gt; &amp;&amp; view&lt;Pattern&gt; &amp;&amp;
             indirectly_comparable&lt;iterator_t&lt;V&gt;, iterator_t&lt;Pattern&gt;, ranges::equal_to&gt; &amp;&amp;
             (forward_range&lt;V&gt; || <i>tiny-range</i>&lt;Pattern&gt;)
  class lazy_split_view : public view_interface&lt;lazy_split_view&lt;V, Pattern&gt;&gt; {
    [&hellip;]
  };
  [&hellip;]
}
</pre>
</blockquote>
<pre><ins>
template&lt;class R&gt;
concept <i>tiny-range</i> = <i>// exposition only</i>
   sized_range&lt;R&gt; &amp;&amp;
   requires { requires bool_constant&lt;(remove_reference_t&lt;R&gt;::size() &lt;= 1)&gt;::value; };
</ins></pre>
<blockquote>
<p><ins>
-?- Given an lvalue <code>r</code> of type <code>remove_reference_t&lt;R&gt;</code>, <code>R</code> models <code><i>tiny-range</i></code> only if
<code>ranges::size(r)</code> is evaluated by <code>remove_reference_t&lt;R&gt;::size()</code>.
</ins></p>
</blockquote>
<pre>
constexpr lazy_split_view(V base, Pattern pattern);
</pre>
<blockquote>
<p>
-1- <i>Effects</i>: : Initializes <code><i>base_</i></code> with <code>std::move(base)</code>, and <code><i>pattern_</i></code> with 
<code>std::move(pattern)</code>.
</p>
</blockquote>
</blockquote>
</li>

</ol>
</blockquote>

<p><i>[2025-04-27, Hewill provides alternative wording]</i></p>



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

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

<blockquote class="note">
<p>
[<i>Drafting note:</i> This benefits from <a href="https://wg21.link/P2280" title=" Using unknown references in constant expressions">P2280</a> that a call to a
member function of a non-constexpr object can be a constant expression if it does 
not actually access the member.
<p/>
This would make <code>views::lazy_split(r, span&lt;int, 0&gt;{})</code> 
well-formed, which can be seen as an enhancement.]
</p>
</blockquote>

<blockquote>
<pre>
namespace std::ranges {
  <del>template&lt;auto&gt; struct <i>require-constant</i>;                       // <i>exposition only</i></del>

  template&lt;class R&gt;
  concept <i>tiny-range</i> =                                          // <i>exposition only</i>
    sized_range&lt;R&gt; &amp;&amp;
    <ins>requires (R&amp; r) { requires bool_constant&lt;ranges::size(r) &lt;= 1&gt;::value; }</ins>
    <del>requires { typename <i>require-constant</i>&lt;remove_reference_t&lt;R&gt;::size()>; } &amp;&amp;
    (remove_reference_t&lt;R&gt;::size() &lt;= 1)</del>;

  template&lt;input_range V, forward_range Pattern&gt;
    requires view&lt;V&gt; &amp;&amp; view&lt;Pattern&gt; &amp;&amp;
             indirectly_comparable&lt;iterator_t&lt;V&gt;, iterator_t&lt;Pattern&gt;, ranges::equal_to&gt; &amp;&amp;
             (forward_range&lt;V&gt; || <i>tiny-range</i>&lt;Pattern&gt;)
  class lazy_split_view::view_interface&lt;lazy_split_view&lt;V, Pattern&gt;&gt; {
    [&hellip;]
  };
  [&hellip;]
}
</pre>
</blockquote>
</li>

<li><p>Modify 25.7.16.5 <a href="https://wg21.link/range.lazy.split.inner">[range.lazy.split.inner]</a> as indicated:</p>

<blockquote class="note">
<p>
[<i>Drafting note:</i>
We can't use <code>if constexpr (ranges::size(<i>i_</i>.<i>parent_</i>-&gt;<i>pattern_</i>) == 0)</code> 
here because it is not a constant expression, and it seems more intuitive to just use 
<code>ranges::empty</code> combined with runtime <code>if</code> which is always well-formed. 
Note that the PR does not seek the aggressive optimization that minimizes the instantiation as this is 
not the intent of the current design (for example, <code><i>outer-iterator</i>&amp; operator++()</code> 
can be specialized for the case where <code>Pattern::size() == 0</code> to save some O(1) comparisons), 
library implementations are free to optimize as it pleases.]
</p>
</blockquote>

<blockquote>
<pre>
constexpr <i>inner-iterator</i>&amp; operator++();
</pre>
<blockquote>
<p>
-5- <i>Effects:</i> Equivalent to:
</p>
<blockquote><pre>
<i>incremented_</i> = true;
if constexpr (!forward_range&lt;<i>Base</i>&gt;) {
  <ins>if (ranges::empty(<i>i_</i>.<i>parent_</i>-&gt;<i>pattern_</i>))</ins>
  <del>if constexpr (Pattern::size() == 0) {</del>
    return *this;
  }
}
++<i>i_</i>.<i>current</i>;
return *this;
</pre></blockquote>
</blockquote>
</blockquote>
</li>

</ol>






</body>
</html>
