<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3306: ranges::advance violates its preconditions</title>
<meta property="og:title" content="Issue 3306: ranges::advance violates its preconditions">
<meta property="og:description" content="C++ library issue. Status: C++23">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3306.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="3306"><a href="lwg-defects.html#3306">3306</a>. <code>ranges::advance</code> violates its preconditions</h3>
<p><b>Section:</b> 24.4.4.2 <a href="https://wg21.link/range.iter.op.advance">[range.iter.op.advance]</a> <b>Status:</b> <a href="lwg-active.html#C++23">C++23</a>
 <b>Submitter:</b> Casey Carter <b>Opened:</b> 2019-10-27 <b>Last modified:</b> 2023-11-22</p>
<p><b>Priority: </b>2
</p>
<p><b>View all other</b> <a href="lwg-index.html#range.iter.op.advance">issues</a> in [range.iter.op.advance].</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>Recall that "<code>[i, s)</code> denotes a range" for an iterator <code>i</code> and sentinel <code>s</code>
means that either <code>i == s</code> holds, or <code>i</code> is dereferenceable and <code>[++i, s)</code>
denotes a range ( [iterator.requirements.genera]).
</p><p>
The three-argument overload <code>ranges::advance(i, n, bound)</code> is specified in
24.4.4.2 <a href="https://wg21.link/range.iter.op.advance">[range.iter.op.advance]</a> paragraphs 5 through 7. Para 5 establishes a precondition that
<code>[bound, i)</code> denotes a range when <code>n &lt; 0</code> (both <code>bound</code> and <code>i</code> must
have the same type in this case). When <code>sized_sentinel_for&lt;S, I&gt;</code> holds and
<code>n &lt; bound - i</code>, para 6.1.1 says that <code>ranges::advance(i, n, bound)</code> is equivalent
to <code>ranges::advance(i, bound)</code>. Para 3, however, establishes a precondition for
<code>ranges::advance(i, bound)</code> that <code>[i, bound)</code> denotes a range. <code>[bound, i)</code> and
<code>[i, bound)</code> cannot both denote ranges unless <code>i == bound</code>, which is not the case for
all calls that reach 6.1.1.
</p><p>
The call in para 6.1.1 wants the effects of either 4.1 - which really has no preconditions - or 4.2,
which is well-defined if either <code>[i, bound)</code> or <code>[bound, i)</code> denotes a range. Para 3's
stronger precondition is actually only required by Para 4.3, which increments <code>i</code> blindly
looking for <code>bound</code>. The straight-forward fix here seems to be to relax para 3's precondition
to only apply when 4.3 will be reached.
</p>

<p><i>[2019-11 Priority to 2 during Monday issue prioritization in Belfast]</i></p>


<p><i>[2020-08-21 Issue processing telecon: moved to Tentatively Ready]</i></p>

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



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

<ol>
<li><p>Modify 24.4.4.2 <a href="https://wg21.link/range.iter.op.advance">[range.iter.op.advance]</a> as indicated:</p>

<blockquote>
<pre>
template&lt;input_or_output_iterator I, sentinel_for&lt;I&gt; S&gt;
  constexpr void ranges::advance(I&amp; i, S bound);
</pre>
<p>
-3- <i>Expects:</i> <ins>Either <code>assignable_from&lt;I&amp;, S&gt; ||
sized_sentinel_for&lt;S, I&gt;</code> is modeled, or</ins> <code>[i, bound)</code> denotes a range.
</p><p>
-4- <i>Effects:</i>
</p><p>
(4.1) &mdash; If <code>I</code> and <code>S</code> model <code>assignable_from&lt;I&amp;, S&gt;</code>,
equivalent to <code>i = std::move(bound)</code>.
</p><p>
(4.2) &mdash; Otherwise, if <code>S</code> and <code>I</code> model <code>sized_sentinel_for&lt;S, I&gt;</code>,
equivalent to <code>ranges::advance(i, bound - i)</code>.
</p><p>
(4.3) &mdash; Otherwise, while <code>bool(i != bound)</code> is <code>true</code>, increments <code>i</code>.
</p>
</blockquote>
</li>
</ol>





</body>
</html>
