<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3597: Unsigned integer types don't model advanceable</title>
<meta property="og:title" content="Issue 3597: Unsigned integer types don't model advanceable">
<meta property="og:description" content="C++ library issue. Status: C++23">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3597.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="3597"><a href="lwg-defects.html#3597">3597</a>. Unsigned integer types don't model <code><i>advanceable</i></code></h3>
<p><b>Section:</b> 25.6.4.2 <a href="https://wg21.link/range.iota.view">[range.iota.view]</a> <b>Status:</b> <a href="lwg-active.html#C++23">C++23</a>
 <b>Submitter:</b> Jiang An <b>Opened:</b> 2021-09-23 <b>Last modified:</b> 2023-11-22</p>
<p><b>Priority: </b>3
</p>
<p><b>View all other</b> <a href="lwg-index.html#range.iota.view">issues</a> in [range.iota.view].</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>
Unsigned integer types satisfy <code><i>advanceable</i></code>, but don't model it, since
</p>
<blockquote><p>
every two values of an unsigned integer type are reachable from each other, and
modular arithmetic is performed on unsigned integer types,
</p></blockquote>
<p>
which makes the last three bullets of the semantic requirements of <code><i>advanceable</i></code> 
(25.6.4.2 <a href="https://wg21.link/range.iota.view">[range.iota.view]</a>/5) can't be satisfied, and some (if not all) uses of 
<code>iota_view</code>s of unsigned integer types ill-formed, no diagnostic required.
<p/>
Some operations that are likely to expect the semantic requirements of <code><i>advanceable</i></code> 
behave incorrectly for unsigned integer types. E.g. according to 25.6.4.2 <a href="https://wg21.link/range.iota.view">[range.iota.view]</a>/6 
and 25.6.4.2 <a href="https://wg21.link/range.iota.view">[range.iota.view]</a>/16, <code>std::ranges::iota_view&lt;std::uint8_t, std::uint8_t&gt;(std::uint8_t(1)).size()</code> 
is well-defined IMO, because
</p>
<blockquote><p>
<code>Bound()</code> is <code>std::uint8_t(0)</code>, which is reachable from <code>std::uint8_t(1)</code>, and
not modeling <code><i>advanceable</i></code> shouldn't affect the validity, as both <code>W</code> and 
<code>Bound</code> are integer types.
</p></blockquote>
<p>
However, it returns <code>unsigned(-1)</code> on common implementations (where <code>sizeof(int) &gt; sizeof(std::uint8_t))</code>, 
which is wrong.
<p/>
Perhaps the semantic requirements of <code><i>advanceable</i></code> should be adjusted, 
and a refined definition of reachability in 25.6.4 <a href="https://wg21.link/range.iota">[range.iota]</a> is needed to avoid reaching 
<code>a</code> from <code>b</code> when <code>a &gt; b</code> (the iterator type is also affected).
</p>

<p><i>[2021-10-14; Reflector poll]</i></p>

<p>
Set priority to 3 after reflector poll.
</p>
<p><i>[Tim Song commented:]</i></p>

<p>
The advanceable part of the issue is NAD.
This is no different from NaN and <code>totally_ordered</code>,
see 16.3.2.3 <a href="https://wg21.link/structure.requirements">[structure.requirements]</a>/8.
</p>
<p>
The part about <code>iota_view&lt;uint8_t, uint8_t&gt;(1)</code> is simply this:
when we added "When <code>W</code> and <code>Bound</code> model ..." to
25.6.4.2 <a href="https://wg21.link/range.iota.view">[range.iota.view]</a>/8,
we forgot to add its equivalent to the single-argument constructor.
We should do that.
</p>

<p><i>[2022-10-12; Jonathan provides wording]</i></p>


<p><i>[2022-10-19; Reflector poll]</i></p>

<p>
Set status to "Tentatively Ready" after seven votes in favour in reflector poll.
</p>

<p><i>[2022-11-12 Approved at November 2022 meeting in Kona. Status changed: Voting &rarr; WP.]</i></p>



<p id="res-3597"><b>Proposed resolution:</b></p>
<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.6.4.2 <a href="https://wg21.link/range.iota.view">[range.iota.view]</a> as indicated:</p>
<blockquote>
<pre>constexpr explicit iota_view(W value);</pre>
<blockquote>
<p>-6-
<i>Preconditions</i>: <code>Bound</code> denotes <code>unreachable_sentinel_t</code>
or <code>Bound()</code> is reachable from <code>value</code>.
<ins>
When <code>W</code> and <code>Bound</code> model <code>totally_ordered_with</code>,
then <code>bool(value &lt;= Bound())</code> is <code>true</code>.
</ins>
</p>
<p>-7-
<i>Effects</i>: Initializes <code><i>value_</i></code> with <code>value</code>.
</p>
</blockquote>

<pre>constexpr iota_view(type_identity_t&lt;W&gt; value, type_identity_t&lt;Bound&gt; bound);</pre>
<blockquote>
<p>-8-
<i>Preconditions</i>: <code>Bound</code> denotes <code>unreachable_sentinel_t</code>
or <code>bound</code> is reachable from <code>value</code>.
When <code>W</code> and <code>Bound</code> model <code>totally_ordered_with</code>,
then <code>bool(value &lt;= bound)</code> is <code>true</code>.
</p>
<p>-9-
<i>Effects</i>: Initializes <code><i>value_</i></code> with <code>value</code>
and <code><i>bound_</i></code> with <code>bound</code>.
</p>
</blockquote>

</blockquote>
</li>

</ol>






</body>
</html>
