<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3286: ranges::size is not required to be valid after a call to ranges::begin on an input range</title>
<meta property="og:title" content="Issue 3286: ranges::size is not required to be valid after a call to ranges::begin on an input range">
<meta property="og:description" content="C++ library issue. Status: C++20">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3286.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="3286"><a href="lwg-defects.html#3286">3286</a>. <code>ranges::size</code> is not required to be valid after a call to <code>ranges::begin</code> on an input range</h3>
<p><b>Section:</b> 25.7.10.2 <a href="https://wg21.link/range.take.view">[range.take.view]</a>, 25.5.4.2 <a href="https://wg21.link/range.subrange.ctor">[range.subrange.ctor]</a> <b>Status:</b> <a href="lwg-active.html#C++20">C++20</a>
 <b>Submitter:</b> Eric Niebler <b>Opened:</b> 2019-09-10 <b>Last modified:</b> 2021-02-25</p>
<p><b>Priority: </b>0
</p>
<p><b>View all other</b> <a href="lwg-index.html#range.take.view">issues</a> in [range.take.view].</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>
On an input (but not forward) range, <code>begin(rng)</code> is not required to be an equality-preserving 
expression (25.4.2 <a href="https://wg21.link/range.range">[range.range]</a>/3.3). If the range is <em>also</em> sized, then it is not valid 
to call <code>size(rng)</code> after <code>begin(rng)</code> (25.4.4 <a href="https://wg21.link/range.sized">[range.sized]</a>/2.2). In several 
places in the ranges clause, this precondition is violated. A trivial re-expression of the effects 
clause fixes the problem.
</p>

<p><i>[2019-10-12 Issue Prioritization]</i></p>

<p>Status to Tentatively Ready and priority to 0 after seven positive votes on the reflector.</p>


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

<ol>
<li><p>Modify 25.7.10.2 <a href="https://wg21.link/range.take.view">[range.take.view]</a>, class template <code>take_view</code> synopsis, as indicated:</p>

<blockquote>
<pre>
namespace std::ranges {
  template&lt;view V&gt;
  class take_view : public view_interface&lt;take_view&lt;V&gt;&gt; {
  private:
    [&hellip;]
  public:
    [&hellip;]
    constexpr auto begin() requires (!<i>simple-view</i>&lt;V&gt;) {
      if constexpr (sized_range&lt;V&gt;) {
        if constexpr (random_access_range&lt;V&gt;)
          return ranges::begin(base_);
        else <ins>{
          auto sz = size();</ins>
          return counted_iterator{ranges::begin(base_), <del>size()</del><ins>sz</ins>};
        <ins>}</ins>
      } else
        return counted_iterator{ranges::begin(base_), count_};
    }

    constexpr auto begin() const requires range&lt;const V&gt; {
      if constexpr (sized_range&lt;const V&gt;) {
        if constexpr (random_access_range&lt;const V&gt;)
          return ranges::begin(base_);
        else <ins>{
          auto sz = size();</ins>
          return counted_iterator{ranges::begin(base_), <del>size()</del><ins>sz</ins>};
        <ins>}</ins>
      } else
        return counted_iterator{ranges::begin(base_), count_};
    }
    
    [&hellip;]
  };
  [&hellip;]
}
</pre>
</blockquote>
</li>

<li><p>Modify 25.5.4.2 <a href="https://wg21.link/range.subrange.ctor">[range.subrange.ctor]</a> as indicated:</p>

<blockquote>
<pre>
template&lt;<i>not-same-as</i>&lt;subrange&gt; R&gt;
  requires forwarding-range&lt;R&gt; &amp;&amp;
    convertible_to&lt;iterator_t&lt;R&gt;, I&gt; &amp;&amp; convertible_to&lt;sentinel_t&lt;R&gt;, S&gt;
constexpr subrange(R&amp;&amp; r) requires (!StoreSize || sized_range&lt;R&gt;);
</pre>
<blockquote>
<p>
-6- <i>Effects:</i> Equivalent to:
<ol style="list-style-type: none">
<li><p>(6.1) &mdash; If <code>StoreSize</code> is <code>true</code>, <code>subrange{<del>ranges::begin(r), 
ranges::end(r)</del><ins>r</ins>, ranges::size(r)}</code>.</p></li>
<li><p>(6.2) &mdash; Otherwise, <code>subrange{ranges::begin(r), ranges::end(r)}</code>.</p></li>
</ol>
</p>
</blockquote>
</blockquote>
</li>
</ol>





</body>
</html>
