<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2968: Inconsistencies between basic_string reserve and vector/unordered_map/unordered_set reserve functions</title>
<meta property="og:title" content="Issue 2968: Inconsistencies between basic_string reserve and vector/unordered_map/unordered_set reserve functions">
<meta property="og:description" content="C++ library issue. Status: Resolved">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2968.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#Resolved">Resolved</a> status.</em></p>
<h3 id="2968"><a href="lwg-defects.html#2968">2968</a>. Inconsistencies between <code>basic_string reserve</code> and <code>vector/unordered_map/unordered_set reserve</code> functions</h3>
<p><b>Section:</b> 27.4.3.5 <a href="https://wg21.link/string.capacity">[string.capacity]</a> <b>Status:</b> <a href="lwg-active.html#Resolved">Resolved</a>
 <b>Submitter:</b> Andrew Luo <b>Opened:</b> 2017-05-30 <b>Last modified:</b> 2020-09-06</p>
<p><b>Priority: </b>3
</p>
<p><b>View all other</b> <a href="lwg-index.html#string.capacity">issues</a> in [string.capacity].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#Resolved">Resolved</a> status.</p>
<p><b>Discussion:</b></p>
<p>
According to 27.4.3.5 <a href="https://wg21.link/string.capacity">[string.capacity]</a> paragraph 11:
</p>
<blockquote>
<p>
-11- <i>Effects:</i> After <code>reserve()</code>, <code>capacity()</code> is greater or equal to the argument of <code>reserve</code>. 
[<i>Note:</i> Calling <code>reserve()</code> with a <code>res_arg</code> argument less than <code>capacity()</code> is in effect a 
non-binding shrink request. A call with <code>res_arg &lt;= size()</code> is in effect a non-binding shrink-to-fit 
request. &mdash; <i>end note</i>]
</p>
</blockquote>
<p>
A call to <code>basic_string</code>'s <code>reserve</code> function with <code>res_arg &lt;= size()</code> is taken as a non-binding 
request to shrink the capacity, whereas for <code>vector</code> (and similarly for <code>unordered_map</code> and <code>unordered_set</code>)
according to 23.3.13.3 <a href="https://wg21.link/vector.capacity">[vector.capacity]</a> p3:
</p>
<blockquote>
<p>
-3- <i>Effects:</i> A directive that informs a <code>vector</code> 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>
</blockquote>
<p>
The problem here is that the different behavior makes it that writing template code where the template argument type is a 
container type (for example <code>std::string</code> or <code>std::vector&lt;char&gt;</code>) calls to <code>reserve</code> can have 
different meaning depending on which container type the template is instantiated with. It might be a minor issue but 
it would be nice to fix the inconsistency. I ran into an issue around this when I was porting code from MSVC++ to G++ 
(For <code>basic_string</code>, MSVC++'s STL implementation, based on Dinkumware, ignores the call if <code>res_arg &lt; capacity()</code> 
whereas GCC's STL implementation, libstdc++ will actually shrink the string. For the code I wrote this caused a huge 
performance issue since we were reallocating the entire string with every call to <code>reserve</code>. Of course we could have 
worked around it by doing the <code>res_arg &lt; capacity()</code> check ourselves, but I think this inconsistency in the 
standard isn't desirable).
<p/>
My proposal is to change 27.4.3.5 <a href="https://wg21.link/string.capacity">[string.capacity]</a> paragraph 11 to read:
</p>
<blockquote>
<p>
-11- <i>Effects:</i> After <code>reserve()</code>, <code>capacity()</code> is greater or equal to the argument of <code>reserve</code> 
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>.
</p>
</blockquote>
<p>
I realize that this causes the <code>basic_string::reserve</code> to no longer have the secondary property of shrinking, 
but this is what <code>shrink_to_fit</code> is for.
</p>

<p><i>[2017-07 Toronto Monday issue prioritization]</i></p>

<p>Priority 3; status to LEWG</p>

<p><i>[2018-3-17 Resolved by <a href="https://wg21.link/P0966">P0966</a>, which was adopted in Jacksonville.]</i></p>



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

<ol>
<li>
<p>Edit 27.4.3.5 <a href="https://wg21.link/string.capacity">[string.capacity]</a> as indicated:</p>

<blockquote>
<pre>
void reserve(size_type res_arg=0);
</pre>
<blockquote>
<p>
-10- The member function <code>reserve()</code> is a directive that informs a <code>basic_string</code> object of a planned change
in size, so that it can manage the storage allocation accordingly.
<p/>
-11- <i>Effects:</i> After <code>reserve()</code>, <code>capacity()</code> is greater or equal to the argument of <code>reserve</code><ins>,
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></ins>. 
<del>[<i>Note:</i> Calling <code>reserve()</code> with a <code>res_arg</code> argument less than <code>capacity()</code> is in effect a 
non-binding shrink request. A call with <code>res_arg &lt;= size()</code> is in effect a non-binding shrink-to-fit 
request. &mdash; <i>end note</i>]</del>
<p/>
-12- <i>Throws:</i> <code>length_error</code> if <code>res_arg &gt; max_size()</code>.
</p>
</blockquote>
</blockquote>
</li>
</ol>





</body>
</html>
