<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2519: Iterator operator-= has gratuitous undefined behaviour</title>
<meta property="og:title" content="Issue 2519: Iterator operator-= has gratuitous undefined behaviour">
<meta property="og:description" content="C++ library issue. Status: C++17">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2519.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++17">C++17</a> status.</em></p>
<h3 id="2519"><a href="lwg-defects.html#2519">2519</a>. Iterator <code>operator-=</code> has gratuitous undefined behaviour</h3>
<p><b>Section:</b> 24.3.5.7 <a href="https://wg21.link/random.access.iterators">[random.access.iterators]</a> <b>Status:</b> <a href="lwg-active.html#C++17">C++17</a>
 <b>Submitter:</b> Hubert Tong  <b>Opened:</b> 2015-07-15 <b>Last modified:</b> 2017-07-30</p>
<p><b>Priority: </b>2
</p>
<p><b>View all other</b> <a href="lwg-index.html#random.access.iterators">issues</a> in [random.access.iterators].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#C++17">C++17</a> status.</p>
<p><b>Discussion:</b></p>
<p>
In subclause 24.3.5.7 <a href="https://wg21.link/random.access.iterators">[random.access.iterators]</a>, Table 110, the operational semantics for the expression "<code>r -= n</code>" 
are defined as
</p>
<blockquote><pre>
return r += -n;
</pre></blockquote>
<p>
Given a <code>difference_type</code> of a type <code>int</code> with range [-32768, 32767], if the value of <code>n</code> is -32768, 
then the evaluation of <code>-n</code> causes undefined behaviour (Clause 5 [expr] paragraph 4).
<p/>
The operational semantics may be changed such that the undefined behaviour is avoided.
<p/>
<b>Suggested wording:</b>
<p/>
Replace the operational semantics for "<code>r -= n</code>" with:
</p>
<blockquote><pre>
{ 
  difference_type m = n;
  if (m >= 0)
    while (m--)
      --r;
  else
    while (m++)
      ++r;
  return r; 
}
</pre></blockquote>
<p>
Jonathan Wakely:
<p/>
I'm now convinced we <em>don't</em> want to change the definition of <code>-=</code> and
instead we should explicitly state the (currently implicit)
precondition that <code>n != numeric_limits&lt;difference_type&gt;::min()</code>.
</p>

<p><i>[2016-08, Chicago]</i></p>

<p>Monday PM: Move to Tentatively Ready</p>


<p id="res-2519"><b>Proposed resolution:</b></p>
<p>
This wording is relative to N4527.
</p>

<ol>
<li><p>Change Table 110 "Random access iterator requirements (in addition to
bidirectional iterator)" as indicated:</p>
<blockquote>
<table border="1">
<caption>Table 110 &mdash; Random access iterator requirements (in addition to
bidirectional iterator)</caption>
<tr>
<th align="center">Expression</th>
<th align="center">Return type</th>
<th align="center">Operational<br/>semantics</th>
<th align="center">Assertion&#47;note<br/>pre-&#47;post-condition</th>
</tr>

<tr>
<td colspan="4" align="center">
<code>&hellip;</code>
</td>
</tr>

<tr>
<td>
<code>r -= n</code>
</td>

<td>
<code>X&amp;</code>
</td>

<td>
<code>return r += -n;</code>
</td>

<td>
<ins>pre: the absolute value of <code>n</code> is in the range of representable values of <code>difference_type</code>.</ins>
</td>
</tr>

<tr>
<td colspan="4" align="center">
<code>&hellip;</code>
</td>
</tr>

</table>
</blockquote>
</li>
</ol>





</body>
</html>
