<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2698: Effect of assign() on iterators/pointers/references</title>
<meta property="og:title" content="Issue 2698: Effect of assign() on iterators/pointers/references">
<meta property="og:description" content="C++ library issue. Status: C++17">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2698.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="2698"><a href="lwg-defects.html#2698">2698</a>. Effect of <code>assign()</code> on iterators/pointers/references</h3>
<p><b>Section:</b> 23.2.4 <a href="https://wg21.link/sequence.reqmts">[sequence.reqmts]</a> <b>Status:</b> <a href="lwg-active.html#C++17">C++17</a>
 <b>Submitter:</b> Tim Song <b>Opened:</b> 2016-04-25 <b>Last modified:</b> 2017-07-30</p>
<p><b>Priority: </b>0
</p>
<p><b>View other</b> <a href="lwg-index-open.html#sequence.reqmts">active issues</a> in [sequence.reqmts].</p>
<p><b>View all other</b> <a href="lwg-index.html#sequence.reqmts">issues</a> in [sequence.reqmts].</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>
The sequence container requirements table says nothing about the effect of <code>assign()</code> on iterators, 
pointers or references into the container. Before LWG <a href="lwg-defects.html#2209" title="assign() overspecified for sequence containers (Status: C++14)">2209</a><sup><a href="https://cplusplus.github.io/LWG/issue2209" title="Latest snapshot">(i)</a></sup> (and LWG <a href="lwg-defects.html#320" title="list::assign overspecified (Status: CD1)">320</a><sup><a href="https://cplusplus.github.io/LWG/issue320" title="Latest snapshot">(i)</a></sup> for <code>std::list</code>), 
<code>assign()</code> was specified as "erase everything then <code>insert</code>", which implies wholesale invalidation from the 
"erase everything" part. With that gone, the blanket "no invalidation" wording in 
23.2.2 <a href="https://wg21.link/container.requirements.general">[container.requirements.general]</a>/12 would seem to apply, which makes absolutely no sense.
<p/>
The proposed wording below simply spells out the invalidation rule implied by the previous "erase everything" wording.
</p>

<p><i>[2016-05 Issues Telecon]</i></p>

<p>
This is related to <a href="lwg-closed.html#2256" title="On vector iterator invalidation (Status: NAD)">2256</a><sup><a href="https://cplusplus.github.io/LWG/issue2256" title="Latest snapshot">(i)</a></sup>
</p>


<p id="res-2698"><b>Proposed resolution:</b></p>
<p>
This wording is relative to N4582.
</p>
<ol>
<li><p>In 23.2.4 <a href="https://wg21.link/sequence.reqmts">[sequence.reqmts]</a>, edit Table 107 (Sequence container requirements) as indicated:</p>

<blockquote>
<table border="1">
<caption>Table 107 &mdash; Sequence container requirements (in addition to container)</caption>
<tr>
<th>Expression</th>
<th>Return type</th>
<th>Assertion&#47;note<br/>pre-&#47;post-condition</th>
</tr>
<tr>
<td colspan="3" align="center">
<code>[&hellip;]</code>
</td>
</tr>
<tr>
<td>
<code>a.assign(i, j)</code>
</td>
<td><code>void</code></td>
<td>
<i>Requires</i>: <code>T</code> shall be <code>EmplaceConstructible</code> into <code>X</code> from <code>*i</code> and assignable from 
<code>*i</code>.<br/>For <code>vector</code>, if the iterator does not meet the forward iterator requirements (24.2.5),<br/> 
<code>T</code> shall also be <code>MoveInsertable</code> into <code>X</code>.<br/> Each iterator in the range <code>[i, j)</code> shall be 
dereferenced exactly once. <br/>pre: <code>i</code>, <code>j</code> are not iterators into <code>a</code>.<br/> Replaces elements 
in <code>a</code> with a copy of <code>[i, j)</code>.<ins><br/>Invalidates all references, pointers and iterators referring to the 
elements of <code>a</code>.<br/>For <code>vector</code> and <code>deque</code>, also invalidates the past-the-end iterator.</ins>
</td>
</tr>
<tr>
<td colspan="3" align="center">
<code>[&hellip;]</code>
</td>
</tr>
<tr>
<td>
<code>a.assign(n, t)</code>
</td>
<td><code>void</code></td>
<td>
<i>Requires</i>: <code>T</code> shall be <code>CopyInsertable</code> into <code>X</code> and <code>CopyAssignable</code>.<br/>
pre: <code>t</code> is not a reference into <code>a</code>.<br/> Replaces elements in <code>a</code> with <code>n</code> 
copies of <code>t</code>.<ins><br/>Invalidates all references, pointers and iterators referring to the elements of 
<code>a</code>.<br/>
For <code>vector</code> and <code>deque</code>, also invalidates the past-the-end iterator.</ins>
</td>
</tr>
</table>
</blockquote>
</li>
</ol>





</body>
</html>
