<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2033: Preconditions of reserve, shrink_to_fit, and resize functions</title>
<meta property="og:title" content="Issue 2033: Preconditions of reserve, shrink_to_fit, and resize functions">
<meta property="og:description" content="C++ library issue. Status: C++14">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2033.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++14">C++14</a> status.</em></p>
<h3 id="2033"><a href="lwg-defects.html#2033">2033</a>. Preconditions of <code>reserve</code>, <code>shrink_to_fit</code>, and <code>resize</code> functions</h3>
<p><b>Section:</b> 23.3.13.3 <a href="https://wg21.link/vector.capacity">[vector.capacity]</a>, 23.3.5.3 <a href="https://wg21.link/deque.capacity">[deque.capacity]</a> <b>Status:</b> <a href="lwg-active.html#C++14">C++14</a>
 <b>Submitter:</b> Nikolay Ivchenkov <b>Opened:</b> 2011-02-20 <b>Last modified:</b> 2016-01-28</p>
<p><b>Priority: </b>Not Prioritized
</p>
<p><b>View other</b> <a href="lwg-index-open.html#vector.capacity">active issues</a> in [vector.capacity].</p>
<p><b>View all other</b> <a href="lwg-index.html#vector.capacity">issues</a> in [vector.capacity].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#C++14">C++14</a> status.</p>
<p><b>Discussion:</b></p>
<p>
I have several questions with regard to the working paper N3225 (C++0x working draft):
</p>
<ol>
<li><p>
Where the working draft specifies preconditions for <code>shrink_to_fit</code>
member function of <code>std::vector</code> and <code>std::deque</code>?
</p></li>
<li><p>
Where the working draft specifies preconditions for '<code>void reserve(size_type n)</code>' 
member function of <code>std::vector</code>?
</p></li>
<li><p>
Does a call to '<code>void resize(size_type sz)</code>' of <code>std::vector</code> require
the element type to be <code>DefaultConstructible</code>? If yes, why such
requirement is not listed in the <i>Requires</i> paragraph?
</p></li>
<li><p>
Does a call to '<code>void resize(size_type sz)</code>' of <code>std::vector</code> require
the element type to be <code>MoveAssignable</code> because the call <code>erase(begin() + sz, end())</code> 
mentioned in the <i>Effects</i> paragraph would require the element type to be <code>MoveAssignable</code>?
</p></li>
<li><p>
Why <code>CopyInsertable</code> requirement is used for '<code>void resize(size_type sz)</code>' of <code>std::vector</code> 
instead of <code>MoveInsertable</code> requirement?
</p></li>
</ol>

<p><i>[2011-06-12: Daniel comments and provides wording]</i></p>

<p>According to my understanding of the mental model of vector (and to some parts for deque) the 
some requirements are missing in the standard as response to above questions:</p>
<ol>
<li>The preconditions of <code>shrink_to_fit</code> for both <code>std::vector</code> and <code>std::deque</code>
should impose the <code>MoveInsertable</code> requirements. The reason for this is, that these containers
can host move-only types. For a container type <code>X</code> the C++03 idiom <code>X(*this).swap(*this)</code>
imposes the <code>CopyInsertable</code> requirements which would make the function call ill-formed,
which looks like an unacceptable restriction to me. Assuming the committee decides to support the
move-only case, further wording has to be added for the situation where such a move-only type could
throw an exception, because this can leave the object in an unspecified state. This seems consistent
with the requirements of <code>reserve</code>, which seems like a very similar function to me (for
<code>vector</code>). And this brings us smoothly to the following bullet:
</li>

<li><p>I agree that we are currently missing to specify the preconditions of the <code>reserve</code> function.
My interpretation of the mental model of this function is that it should work for move-only types, which
seems to be supported by the wording used in 23.3.13.3 <a href="https://wg21.link/vector.capacity">[vector.capacity]</a> p2:
</p>
<blockquote><p>
[&hellip;] If an exception is thrown other than by the move constructor of a non-CopyInsertable type, 
there are no effects.
</p></blockquote>
<p>
Given this statement, the appropriate requirement is <code>MoveInsertable</code> into the <code>vector</code>.
</p>
</li>

<li>I agree that <code>vector::resize(size_type)</code> misses to list the <code>DefaultConstructible</code>
requirements.
</li>

<li>Unfortunately the current specification in terms of <code>erase</code> implies the <code>MoveAssignable</code>
requirements. I don't think that this implication is intended. This function requires "append" and 
"pop-back" effects, respectively, where the former can be realized in terms of <code>MoveInsertable</code> 
requirements. The same fix in regard to using <code>pop_back</code> instead of <code>erase</code> is necessary 
for the two argument overload of <code>resize</code> as well (no <code>MoveAssignable</code> is required).
</li>

<li>The <code>CopyInsertable</code> requirement is incorrect and should be <code>MoveInsertable</code> instead.</li>
</ol>

<p>In addition to above mentioned items, the proposed resolution adds a linear complexity bound for 
<code>shrink_to_fit</code> and attempts to resolve the related issue <a href="lwg-defects.html#2066" title="Missing specification of vector::resize(size_type) (Status: Resolved)">2066</a><sup><a href="https://cplusplus.github.io/LWG/issue2066" title="Latest snapshot">(i)</a></sup>.</p>

<p><i>[
2011 Bloomington
]</i></p>


<p>
Move to Ready.
</p>

<p>
Note for editor: we do not normally refer to 'linear <i>time</i>' for complexity requirements, but there
is agreement that any clean-up of such wording is editorial.
</p>



<p id="res-2033"><b>Proposed resolution:</b></p>
<p>This wording is relative to the FDIS.</p>
<ol>
<li><p>Edit 23.3.5.3 <a href="https://wg21.link/deque.capacity">[deque.capacity]</a> as indicated [Remark: The suggested change of p4 is
not redundant, because <code>CopyInsertable</code> is not necessarily a refinement of <code>MoveInsertable</code>
in contrast to the fact that <code>CopyConstructible</code> is a refinement of <code>MoveConstructible</code>]:</p>

<blockquote><pre>
void resize(size_type sz);
</pre><blockquote><p>
-1- <i>Effects</i>: If <code>sz &lt;= size()</code>, equivalent to <del><code>erase(begin() + sz, 
end());</code></del><ins>calling <code>pop_back()</code> <code>size() - sz</code> times</ins>. If 
<code>size() &lt; sz</code>, appends <code>sz - size()</code> value-initialized elements to the sequence.
<p/>
-2- Requires: <code>T</code> shall be <ins><code>MoveInsertable</code> into <code>*this</code> and</ins> <code>DefaultConstructible</code>.
</p></blockquote></blockquote>

<blockquote><pre>
void resize(size_type sz, const T&amp; c);
</pre><blockquote><p>
-3- <i>Effects</i>: <ins>If <code>sz &lt;= size()</code>, equivalent to calling <code>pop_back()</code> 
<code>size() - sz</code> times. If <code>size() &lt; sz</code>, appends <code>sz - size()</code> copies of <code>c</code> 
to the sequence.</ins>
</p><blockquote><pre>
<del>if (sz &gt; size())
  insert(end(), sz-size(), c);
else if (sz &lt; size())
  erase(begin()+sz, end());
else
  ; <i>// do nothing</i></del>
</pre></blockquote>
<p>
-4- <i>Requires</i>: <code>T</code> shall be <ins><code>MoveInsertable</code> into <code>*this</code> and</ins> 
<code>CopyInsertable</code> into <code>*this</code>.
</p></blockquote></blockquote>

<blockquote><pre>
void shrink_to_fit();
</pre><blockquote><p>
<ins>-?- <i>Requires</i>: <code>T</code> shall be <code>MoveInsertable</code> into <code>*this</code>.</ins>
<p/>
<ins>-?- <i>Complexity</i>: Takes at most linear time in the size of the sequence.</ins>
<p/>
-5- <i>Remarks</i>: <code>shrink_to_fit</code> is a non-binding request to reduce memory use <ins>but does
not change the size of the sequence</ins>. [ <i>Note</i>: The request is non-binding to allow latitude 
for implementation-specific optimizations. &mdash; <i>end note</i> ]
</p></blockquote></blockquote>
</li>

<li><p>Edit 23.3.13.3 <a href="https://wg21.link/vector.capacity">[vector.capacity]</a> as indicated including edits that also resolve <a href="lwg-defects.html#2066" title="Missing specification of vector::resize(size_type) (Status: Resolved)">2066</a><sup><a href="https://cplusplus.github.io/LWG/issue2066" title="Latest snapshot">(i)</a></sup> 
[Remark: The combined listing of <code>MoveInsertable</code> and <code>CopyInsertable</code> before p12 is not redundant, 
because <code>CopyInsertable</code> is not necessarily a refinement of <code>MoveInsertable</code> in contrast to the 
fact that <code>CopyConstructible</code> is a refinement of <code>MoveConstructible</code>]:</p>

<p>[&hellip;]</p>

<blockquote><pre>
void reserve(size_type n);
</pre><blockquote><p>
<ins>-?- <i>Requires</i>: <code>T</code> shall be <code>MoveInsertable</code> into <code>*this</code>.</ins>
<p/>
-2- <i>Effects</i>: A directive that informs a vector of a planned change in size, so that it can manage the storage
allocation accordingly. After <code>reserve()</code>, <code>capacity()</code> is greater or equal to the argument of reserve if
reallocation happens; and equal to the previous value of <code>capacity()</code> otherwise. Reallocation happens
at this point if and only if the current capacity is less than the argument of <code>reserve()</code>. If an exception
is thrown other than by the move constructor of a non-<code>CopyInsertable</code> type, there are no effects.
<p/>
-3- <i>Complexity</i>: It does not change the size of the sequence and takes at most linear time in the size of
the sequence.
<p/>
-4- <i>Throws</i>: <code>length_error</code> if <code>n &gt; max_size()</code>.[footnote 266]
<p/>
-5- <i>Remarks</i>: Reallocation invalidates all the references, pointers, and iterators referring to the elements
in the sequence. It is guaranteed that no reallocation takes place during insertions that happen after
a call to <code>reserve()</code> until the time when an insertion would make the size of the vector greater than
the value of <code>capacity()</code>.
</p></blockquote></blockquote>

<blockquote><pre>
void shrink_to_fit();
</pre><blockquote><p>
<ins>-?- <i>Requires</i>: <code>T</code> shall be <code>MoveInsertable</code> into <code>*this</code>.</ins>
<p/>
<ins>-?- <i>Complexity</i>: Takes at most linear time in the size of the sequence.</ins>
<p/>
-6- <i>Remarks</i>: <code>shrink_to_fit</code> is a non-binding request to reduce <code>capacity()</code> to <code>size()</code>. 
[ <i>Note</i>: The request is non-binding to allow latitude for implementation-specific optimizations. &mdash; <i>end note</i> ]
<ins>If an exception is thrown other than by the move constructor of a non-<code>CopyInsertable</code> <code>T</code> there 
are no effects.</ins>
</p></blockquote></blockquote>

<p>[&hellip;]</p>

<blockquote><pre>
void resize(size_type sz);
</pre><blockquote><p>
-9- <i>Effects</i>: If <code>sz &lt;= size()</code>, equivalent to <del><code>erase(begin() + sz, end());</code></del><ins>calling 
<code>pop_back()</code> <code>size() - sz</code> times</ins>. If <code>size() &lt; sz</code>, appends <code>sz - size()</code> 
value-initialized elements to the sequence.
<p/>
-10- <i>Requires</i>: <code>T</code> shall be <code><del>Copy</del><ins>Move</ins>Insertable</code> into <code>*this</code> 
<ins>and <code>DefaultConstructible</code></ins>.
<p/>
<ins>-??- <i>Remarks</i>: If an exception is thrown other than by the move constructor of a non-<code>CopyInsertable</code> 
<code>T</code> there are no effects.</ins>
</p></blockquote></blockquote>

<blockquote><pre>
void resize(size_type sz, const T&amp; c);
</pre><blockquote><p>
-11- <i>Effects</i>: <ins>If <code>sz &lt;= size()</code>, equivalent to calling <code>pop_back()</code> 
<code>size() - sz</code> times. If <code>size() &lt; sz</code>, appends <code>sz - size()</code> copies of <code>c</code> 
to the sequence.</ins>
</p><blockquote><pre>
<del>if (sz &gt; size())
  insert(end(), sz-size(), c);
else if (sz &lt; size())
  erase(begin()+sz, end());
else
  ; <i>// do nothing</i></del>
</pre></blockquote><p>
<ins>-??- <i>Requires</i>: <code>T</code> shall be <code>MoveInsertable</code> into <code>*this</code> and 
<code>CopyInsertable</code> into <code>*this</code>.</ins>
<p/>
-12- <i><del>Requires</del><ins>Remarks</ins></i>: If an exception is thrown other than by the move constructor of a 
non-<code>CopyInsertable</code> <code>T</code> there are no effects.
</p></blockquote></blockquote>

</li>

</ol>





</body>
</html>
