<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 755: std::vector and std:string lack explicit shrink-to-fit operations</title>
<meta property="og:title" content="Issue 755: std::vector and std:string lack explicit shrink-to-fit operations">
<meta property="og:description" content="C++ library issue. Status: CD1">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue755.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#CD1">CD1</a> status.</em></p>
<h3 id="755"><a href="lwg-defects.html#755">755</a>. <code>std::vector</code> and <code>std:string</code> lack explicit shrink-to-fit operations</h3>
<p><b>Section:</b> 23.3.13.3 <a href="https://wg21.link/vector.capacity">[vector.capacity]</a>, 27.4.3.5 <a href="https://wg21.link/string.capacity">[string.capacity]</a> <b>Status:</b> <a href="lwg-active.html#CD1">CD1</a>
 <b>Submitter:</b> Beman Dawes <b>Opened:</b> 2007-10-31 <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#CD1">CD1</a> status.</p>
<p><b>Discussion:</b></p>
<p>
A <code>std::vector</code> can be shrunk-to-fit via the swap idiom:
</p>

<blockquote><pre>
vector&lt;int&gt; v;
...
v.swap(vector&lt;int&gt;(v));  // shrink to fit
</pre>
<blockquote><p>
or:
</p></blockquote>
<pre>
vector&lt;int&gt;(v).swap(v);  // shrink to fit
</pre>
<blockquote><p>
or:
</p></blockquote>
<pre>
swap(v, vector&lt;int&gt;(v));  // shrink to fit
</pre>
</blockquote>

<p>
A non-binding request for shrink-to-fit can be made to a <code>std::string</code> via:
</p>

<blockquote><pre>
string s;
...
s.reserve(0);
</pre></blockquote>

<p>
Neither of these is at all obvious to beginners, and even some
experienced C++ programmers are not aware that shrink-to-fit is
trivially available.
</p>
<p>
Lack of explicit functions to perform these commonly requested
operations makes vector and string less usable for non-experts. Because
the idioms are somewhat obscure, code readability is impaired. It is
also unfortunate that two similar vector-like containers use different
syntax for the same operation.
</p>
<p>
The proposed resolution addresses these concerns. The proposed function
takes no arguments to keep the solution simple and focused.
</p>


<p id="res-755"><b>Proposed resolution:</b></p>
<p>
To Class template basic_string 27.4.3 <a href="https://wg21.link/basic.string">[basic.string]</a> synopsis,
Class template vector 23.3.13 <a href="https://wg21.link/vector">[vector]</a> synopsis, and Class
vector&lt;bool&gt; 23.3.14 <a href="https://wg21.link/vector.bool">[vector.bool]</a> synopsis, add:
</p>

<blockquote><pre>    
void shrink_to_fit();
</pre></blockquote>

<p>
To basic_string capacity 27.4.3.5 <a href="https://wg21.link/string.capacity">[string.capacity]</a> and vector
capacity 23.3.13.3 <a href="https://wg21.link/vector.capacity">[vector.capacity]</a>, add:
</p>

<blockquote>
<pre>
void shrink_to_fit();
</pre>
<blockquote><p>
<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>]
</p></blockquote>
</blockquote>

<p><i>[
<a href="lwg-defects.html#850" title="Should shrink_to_fit apply to std::deque? (Status: CD1)">850</a><sup><a href="https://cplusplus.github.io/LWG/issue850" title="Latest snapshot">(i)</a></sup> has been added to deal with this issue with respect to <code>deque</code>.
]</i></p>






</body>
</html>
