<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2266: vector and deque have incorrect insert requirements</title>
<meta property="og:title" content="Issue 2266: vector and deque have incorrect insert requirements">
<meta property="og:description" content="C++ library issue. Status: C++17">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2266.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="2266"><a href="lwg-defects.html#2266">2266</a>. <code>vector</code> and <code>deque</code> have incorrect <code>insert</code> requirements</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> Ahmed Charles <b>Opened:</b> 2013-05-17 <b>Last modified:</b> 2017-07-30</p>
<p><b>Priority: </b>2
</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>
According to Table 100 in n3485 23.2.4 <a href="https://wg21.link/sequence.reqmts">[sequence.reqmts]</a>/4 the notes for the expression <code>a.insert(p,i,j)</code>
say:
</p>

<blockquote><p>
<i>Requires:</i> <code>T</code> shall be <code>EmplaceConstructible</code>
into <code>X</code> from <code>*i</code>. For <code>vector</code>, if the iterator
does not meet the forward iterator requirements (24.2.5), <code>T</code> shall also be
<code>MoveInsertable</code> into <code>X</code> and <code>MoveAssignable</code>.
<p/>
Each iterator in the range <code>[i,j)</code> shall be dereferenced exactly once.
<p/>
<i>pre:</i> <code>i</code> and <code>j</code> are not iterators into <code>a</code>.
<p/>
Inserts copies of elements in <code>[i, j)</code> before <code>p</code>
</p></blockquote>

<p>
There are two problems with that wording: First, the special constraints for <code>vector</code>, that are expressed to be valid for
forward iterators only, are necessary for all iterator categories. Second, the same special constraints are needed for <code>deque</code>, too. 
</p>

<p><i>[2013-10-05, Stephan T. Lavavej comments and provides alternative wording]</i></p>

<p>
In Chicago, we determined that the original proposed resolution was correct, except that it needed additional requirements.  
When <code>vector</code> <code>insert(p, i, j)</code> is called with input-only iterators, it can't know how many elements will be inserted, 
which is obviously problematic for insertion anywhere other than at the end. Therefore, implementations typically append elements 
(geometrically reallocating), followed by <code>rotate()</code>. Given forward+ iterators, some implementations append and 
<code>rotate()</code> when they determine that there is sufficient capacity. Additionally, <code>deque</code> <code>insert(p, i, j)</code> is 
typically implemented with prepending/appending, with a possible call to <code>reverse()</code>, followed by a call to <code>rotate()</code>.  
Note that <code>rotate()</code>'s requirements are strictly stronger than <code>reverse()</code>'s.
<p/>
Therefore, when patching Table 100, we need to add <code>rotate()</code>'s requirements. Note that this does not physically affect code 
(implementations were already calling <code>rotate()</code> here), and even in Standardese terms it is barely noticeable &mdash; if an 
element is <code>MoveInsertable</code> and <code>MoveAssignable</code> then it is almost certainly <code>MoveConstructible</code> and swappable.  
However, this patch is necessary to be strictly correct.
</p>

<p>
Previous resolution from Ahmed Charles:
</p>
<blockquote class="note">
<ol>
<li><p>Change Table 100 as indicated:</p>
<blockquote>
<table border="1">
<caption>Table 100 &mdash; Sequence container requirements (in addition to container) (continued)</caption>
<tr>
<th>Expression</th>
<th>Return type</th>
<th>Assertion&#47;note pre-&#47;post-condition</th>
</tr>

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

<tr>
<td>
<code>a.insert(p,i,j)</code>
</td>
<td>
<code>iterator</code>
</td>
<td>
<i>Requires:</i> <code>T</code> shall be <code>EmplaceConstructible</code>
into <code>X</code> from <code>*i</code>. For <code>vector</code> <ins>and <code>deque</code></ins>, <del>if the iterator
does not meet the forward iterator requirements (24.2.5),</del> <code>T</code> shall also be
<code>MoveInsertable</code> into <code>X</code> and <code>MoveAssignable</code>.<br/>
Each iterator in the range <code>[i,j)</code> shall be dereferenced exactly once.<br/>
<i>pre:</i> <code>i</code> and <code>j</code> are not iterators into <code>a</code>.<br/>
Inserts copies of elements in <code>[i, j)</code> before <code>p</code>
</td>
</tr>

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

</table>
</blockquote>

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

<p><i>[2014-02-15 post-Issaquah session : move to Tentatively Ready]</i></p>

<p>
Pablo: We might have gone too far with the fine-grained requirements. Typically these things come in groups.
</p>
<p>
Alisdair: I think the concepts folks assumed we would take their guidance.
</p>
<p>
Move to Tentatively Ready.
</p>



<p id="res-2266"><b>Proposed resolution:</b></p>

<ol>
<li><p>Change Table 100 as indicated:</p>
<blockquote>
<table border="1">
<caption>Table 100 &mdash; Sequence container requirements (in addition to container) (continued)</caption>
<tr>
<th>Expression</th>
<th>Return type</th>
<th>Assertion&#47;note pre-&#47;post-condition</th>
</tr>

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

<tr>
<td>
<code>a.insert(p,i,j)</code>
</td>
<td>
<code>iterator</code>
</td>
<td>
<i>Requires:</i> <code>T</code> shall be <code>EmplaceConstructible</code> into <code>X</code><br/> 
from <code>*i</code>. For <code>vector</code> <ins>and <code>deque</code></ins>, <del>if the iterator</del><br/>
<del>does not meet the forward iterator requirements (24.2.5),</del> <code>T</code> shall also be<br/>
<code>MoveInsertable</code> into <code>X</code><ins>, <code>MoveConstructible</code>,</ins><br/> 
<del>and</del> <code>MoveAssignable</code><ins>, and swappable (16.4.4.3 <a href="https://wg21.link/swappable.requirements">[swappable.requirements]</a>)</ins>.<br/>
Each iterator in the range <code>[i,j)</code> shall be dereferenced exactly once.<br/>
<i>pre:</i> <code>i</code> and <code>j</code> are not iterators into <code>a</code>.<br/>
Inserts copies of elements in <code>[i, j)</code> before <code>p</code>
</td>
</tr>

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

</table>
</blockquote>

</li>
</ol>






</body>
</html>
