<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3614: iota_view::size and the most negative signed integer values</title>
<meta property="og:title" content="Issue 3614: iota_view::size and the most negative signed integer values">
<meta property="og:description" content="C++ library issue. Status: New">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3614.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="3614"><a href="lwg-active.html#3614">3614</a>. <code>iota_view::size</code> and the most negative signed integer values</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#New">New</a>
 <b>Submitter:</b> Jiang An <b>Opened:</b> 2021-10-01 <b>Last modified:</b> 2021-10-14</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#New">New</a> status.</p>
<p><b>Discussion:</b></p>
<p>
According to 25.6.4.2 <a href="https://wg21.link/range.iota.view">[range.iota.view]</a>/15, when both <code>W</code> and <code>Bound</code> are 
integer-like, the expression in the return statement uses <code>-<i>value_</i></code> and <code>-<i>bound_</i></code>. 
These operations result in undefined behavior when <code>-</code> is applied to the most negative integer 
value of a promoted type.
<p/>
I believe that we can simply convert <code><i>value_</i></code> and <code><i>bound_</i></code> to the return type 
(<code><i>make-unsigned-like-t</i>&lt;common_type_t&lt;W, Bound&gt;&gt;</code>) and then perform the subtraction. 
Such method should give the same results with UB eliminated.
<p/>
Additionally, if we decide that <code>iota_view&lt;uint8_t, uint8_t&gt;(uint8_t(1)).size()</code> is well-defined 
(LWG <a href="lwg-defects.html#3597" title="Unsigned integer types don't model advanceable (Status: C++23)">3597</a><sup><a href="https://cplusplus.github.io/LWG/issue3597" title="Latest snapshot">(i)</a></sup>), it should give the correct result. We can truncate the result to fit the type <code>W</code>.
</p>

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

<p>
Set priority to 3 after reflector poll.
</p>



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

<blockquote class="note">
<p>
[<i>Drafting Note:</i> Two mutually exclusive options are prepared, depicted below by <b>Option A</b> and 
<b>Option B</b>, respectively.] 
</p>
</blockquote>

<p>
<b>Option A:</b> Just fixes the most negative values
</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 auto size() const requires <i>see below</i>;
</pre>
<blockquote>
<p>
-15- <i>Effects:</i> Equivalent to:
</p>
<blockquote><pre>
if constexpr (<i>is-integer-like</i>&lt;W&gt; &amp;&amp; <i>is-integer-like</i>&lt;Bound&gt;) <ins>{</ins>
  <del>return (<i>value_</i> &lt; 0)
    ? ((<i>bound_</i> &lt; 0)
      ? <i>to-unsigned-like</i>(-<i>value_</i>) - <i>to-unsigned-like</i>(-<i>bound_</i>)
      : <i>to-unsigned-like</i>(<i>bound_</i>) + <i>to-unsigned-like</i>(-<i>value_</i>))
    : <i>to-unsigned-like</i>(<i>bound_</i>) - <i>to-unsigned-like</i>(<i>value_</i>);</del>
  <ins>using UC = <i>make-unsigned-like-t</i>&lt;common_type_t&lt;W, Bound&gt;&gt;;
  return UC(<i>bound_</i>) - UC(<i>value_</i>);</ins>
<ins>}</ins> else
  return <i>to-unsigned-like</i>(<i>bound_</i> - <i>value_</i>);
</pre></blockquote>
<p>
-16- <i>Remarks:</i> [&hellip;]
</p>
</blockquote>
</blockquote>
</li>
</ol>

<p>
<b>Option B:</b> Also fixes pathological cases involving unsigned-integer-like types
</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 auto size() const requires <i>see below</i>;
</pre>
<blockquote>
<p>
-15- <i>Effects:</i> Equivalent to:
</p>
<blockquote><pre>
if constexpr (<i>is-integer-like</i>&lt;W&gt; &amp;&amp; <i>is-integer-like</i>&lt;Bound&gt;) <ins>{</ins>
  <del>return (<i>value_</i> &lt; 0)
    ? ((<i>bound_</i> &lt; 0)
      ? <i>to-unsigned-like</i>(-<i>value_</i>) - <i>to-unsigned-like</i>(-<i>bound_</i>)
      : <i>to-unsigned-like</i>(<i>bound_</i>) + <i>to-unsigned-like</i>(-<i>value_</i>))
    : <i>to-unsigned-like</i>(<i>bound_</i>) - <i>to-unsigned-like</i>(<i>value_</i>);</del>
  <ins>using UC = <i>make-unsigned-like-t</i>&lt;common_type_t&lt;W, Bound&gt;&gt;;
  if constexpr (<i>is-signed-integer-like</i>&lt;W&gt;)
    return UC(<i>bound_</i>) - UC(<i>value_</i>);
  else
    return UC(W(UC(<i>bound_</i>) - UC(<i>value_</i>)));</ins>
<ins>}</ins> else
  return <i>to-unsigned-like</i>(<i>bound_</i> - <i>value_</i>);
</pre></blockquote>
<p>
-16- <i>Remarks:</i> [&hellip;]
</p>
</blockquote>
</blockquote>
</li>
</ol>





</body>
</html>
