<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3453: Generic code cannot call ranges::advance(i, s)</title>
<meta property="og:title" content="Issue 3453: Generic code cannot call ranges::advance(i, s)">
<meta property="og:description" content="C++ library issue. Status: C++23">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3453.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="3453"><a href="lwg-defects.html#3453">3453</a>. Generic code cannot call <code>ranges::advance(i, s)</code></h3>
<p><b>Section:</b> 24.4.4.2 <a href="https://wg21.link/range.iter.op.advance">[range.iter.op.advance]</a>, 24.3.4.7 <a href="https://wg21.link/iterator.concept.sentinel">[iterator.concept.sentinel]</a> <b>Status:</b> <a href="lwg-active.html#C++23">C++23</a>
 <b>Submitter:</b> Casey Carter <b>Opened:</b> 2020-06-18 <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>
The specification of the iterator &amp; sentinel overload of <code>ranges::advance</code> in 24.4.4.2 <a href="https://wg21.link/range.iter.op.advance">[range.iter.op.advance]</a> reads:
</p>
<blockquote style="border-left: 3px solid #ccc;padding-left: 15px;">
<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>Preconditions:</i> <code>[i, bound)</code> denotes a range.
<p/>
-4- <i>Effects:</i>
</p>
<ol style="list-style-type: none">
<li><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></li>
<li><p>(4.2) &mdash; [&hellip;]</p></li>
</ol>
</blockquote>

<p>
The assignment optimization in bullet 4.1 is just fine for callers with concrete types who can decide whether 
or not to call <code>advance</code> depending on the semantics of the assignment performed. However, since this assignment 
operation isn't part of the <code>input_or_output_iterator</code> or <code>sentinel_for</code> requirements its semantics 
are unknown for arbitrary types. Effectively, generic code is forbidden to call this overload of <code>advance</code> when 
<code>assignable_from&lt;I&amp;, S&gt;</code> is satisfied and non-generic code must tread lightly. This seems to 
make the library dangerously unusable.

We can correct this problem by either:
</p>
<ol style="list-style-type: upper-alpha">
<li><p>Making the assignment operation in question an optional part of the <code>sentinel_for</code> concept with well-defined 
semantics. This concept change should be relatively safe given that <code>assignable_from&lt;I&amp;, S&gt;</code> 
requires <code>common_reference_with&lt;const I&amp;, const S&amp;&gt;</code>, which is very rarely satisfied 
inadvertently.</p></li>
<li><p>Requiring instead <code>same_as&lt;I, S&gt;</code> to trigger the assignment optimization in bullet 4.1 above. 
<code>S</code> is <code>semiregular</code>, so <code>i = std::move(s)</code> is certainly well-formed (and has well-defined 
semantics thanks to <code>semiregular</code>) when <code>I</code> and <code>S</code> are the same type. The optimization will 
not apply in as many cases, but we don't need to make a scary concept change, either.</p></li>
</ol>

<p><i>[2020-06-26; Reflector prioritization]</i></p>

<p>
Set priority to 2 after reflector discussions.
</p>
<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>
<p>
Wording for both <b>Option A</b> and <b>Option B</b> are provided.
</p>

<p><b>Option A</b>:</p>

<ol>
<li><p>Modify 24.3.4.7 <a href="https://wg21.link/iterator.concept.sentinel">[iterator.concept.sentinel]</a> as indicated:</p>

<blockquote>
<pre>
template&lt;class S, class I&gt;
  concept sentinel_for =
    semiregular&lt;S&gt; &amp;&amp;
    input_or_output_iterator&lt;I&gt; &amp;&amp;
    <i>weakly-equality-comparable-with</i>&lt;S, I&gt;; <i>// See 18.5.4 <a href="https://wg21.link/concept.equalitycomparable">[concept.equalitycomparable]</a></i>
</pre>
<blockquote>
<p>
-2- Let <code>s</code> and <code>i</code> be values of type <code>S</code> and <code>I</code> such that <code>[i, s)</code> denotes a range. 
Types <code>S</code> and <code>I</code> model <code>sentinel_for&lt;S, I&gt;</code> only if
</p>
<ol style="list-style-type: none">
<li><p>(2.1) &mdash; <code>i == s</code> is well-defined.</p></li>
<li><p>(2.2) &mdash; If <code>bool(i != s)</code> then <code>i</code> is dereferenceable and <code>[++i, s)</code> denotes a range.</p></li>
<li><p><ins>(2.?) &mdash; <code>assignable_from&lt;I&amp;, S&gt;</code> is either modeled or not satisfied.</ins></p></li>
</ol>
</blockquote>
</blockquote>
</li>
</ol>

<p><b>Option B</b>:</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>
<blockquote>
<p>
-3- <i>Preconditions:</i> <code>[i, bound)</code> denotes a range.
<p/>
-4- <i>Effects:</i>
</p>
<ol style="list-style-type: none">
<li><p>(4.1) &mdash; If <code>I</code> and <code>S</code> model <code><del>assignable_from&lt;I&amp;, 
S&gt;</del><ins>same_as&lt;I, S&gt;</ins></code>, equivalent to <code>i = std::move(bound)</code>.</p></li>
<li><p>(4.2) &mdash; [&hellip;]</p></li>
</ol>
</blockquote>
</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-3453"><b>Proposed resolution:</b></p>
<p>
This wording is relative to <a href="https://wg21.link/n4861">N4861</a>. 
</p>

<ol>
<li><p>Modify 24.3.4.7 <a href="https://wg21.link/iterator.concept.sentinel">[iterator.concept.sentinel]</a> as indicated:</p>

<blockquote>
<pre>
template&lt;class S, class I&gt;
  concept sentinel_for =
    semiregular&lt;S&gt; &amp;&amp;
    input_or_output_iterator&lt;I&gt; &amp;&amp;
    <i>weakly-equality-comparable-with</i>&lt;S, I&gt;; <i>// See 18.5.4 <a href="https://wg21.link/concept.equalitycomparable">[concept.equalitycomparable]</a></i>
</pre>
<blockquote>
<p>
-2- Let <code>s</code> and <code>i</code> be values of type <code>S</code> and <code>I</code> such that <code>[i, s)</code> denotes a range. 
Types <code>S</code> and <code>I</code> model <code>sentinel_for&lt;S, I&gt;</code> only if
</p>
<ol style="list-style-type: none">
<li><p>(2.1) &mdash; <code>i == s</code> is well-defined.</p></li>
<li><p>(2.2) &mdash; If <code>bool(i != s)</code> then <code>i</code> is dereferenceable and <code>[++i, s)</code> denotes a range.</p></li>
<li><p><ins>(2.?) &mdash; <code>assignable_from&lt;I&amp;, S&gt;</code> is either modeled or not satisfied.</ins></p></li>
</ol>
</blockquote>
</blockquote>
</li>
</ol>






</body>
</html>
