<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<meta name="generator" content="dascandy/fiets">
<title>
Container truncation </title>
  <style type="text/css">
body {
  margin: 5em;
  font-family: sans-serif;
  hyphens: auto;
  line-height: 1.35;
}
ul {
  padding-left: 2em;
}
h1, h2, h3, h4 {
  position: relative;
  line-height: 1;
}
h1.title {
}
h2.subtitle {
}
h1.toc a, h2.toc a, h3.toc a, h4.toc a {
  text-decoration: none;
  color: #000000;
}
h1.toc a:hover, h2.toc a:hover, h3.toc a:hover, h4.toc a:hover {
  text-decoration: underline;
}
a.self-link {
  position: absolute;
  top: 0;
  left: calc(-1 * (3.5rem - 26px));
  width: calc(3.5rem - 26px);
  height: 2em;
  text-align: center;
  border: none;
  transition: opacity .2s;
  opacity: .5;
  font-family: sans-serif;
  font-weight: normal;
  font-size: 83%;
}
a.self-link:hover { opacity: 1; }
a.self-link::before { content: "§"; }
span.identifier {
  font-style: italic;
}
span.special {
  color: #bf003f;
}
span.keyword {
  color: #0030cf;
}
span.comment {
  color: #00c000;
}
span.new {
  text-decoration: underline;
  background-color: #006e28;
}
div.code, span.code {
  font-family: Courier New, monospace;
  background-color: #e8e8e8;
  white-space: pre;
}
span.delete {
  text-decoration: line-through;
  background-color: #bf0303;
}
p.indent {
  margin-left: 50px;
}
p.quote {
  margin-left: 50px;
  border: 2px solid black;
  background-color: #f0f0e0;
}
table {
  border: 1px solid black;
  border-collapse: collapse;
  margin-left: auto;
  margin-right: auto;
  margin-top: 0.8em;
  text-align: left;
  hyphens: none; 
}
td, th {
  padding-left: 1em;
  padding-right: 1em;
  vertical-align: top;
}
th {
  border-bottom: 2px solid black;
  background-color: #f0f0f0;
}
</style>
</head>
<body>
<h1 class="title" style="text-align:center">Container truncation </h1><table><tbody><tr><td> Document # </td><td> D3526R0 </td></tr><tr><td> Date </td><td> 2024-11-22 </td></tr><tr><td> Project </td><td>	Programming Language C++ </td></tr><tr><td> Targeted subgroups </td><td> LEWG </td></tr><tr><td> Target release </td><td> C++26 </td></tr><tr><td> Reply-to </td><td> Peter Bindels &lt;dascandy@gmail.com&gt; </td></tr></tbody></table><h1 data-number="1" id="Abstract"><span class="header-section-number">1</span> Abstract<a href="#Abstract" class="self-link"></a></h1><p>std::vector (et al) have a function to change the number of stored allocations, called <span class="code">resize</span>. This may need to enlarge the container, which is an operation that cannot generally be done noexcept. Shrinking a container, however, can always be done noexcept, as it only destructs member objects using their noexcept-by-default destructor, and reducing the stored size to a lower number. This has benefits for code generation, where the compiler will be able to know that the truncation cannot throw without further analysis.</p><p>It enables truncating containers with types that are not default-constructible. Right now, if you have a container with non-default-constructible types, there is no way to truncate, other than to repeatedly pop_back(). The resulting compile error is not immediately obvious, as the mental model of the user is "I am only removing items since I truncate the container".</p><p>In addition, it helps code readability to be able to specify that a given code fragment will only reduce the size of a buffer, which in some contexts is a common operation.</p><h1 data-number="2" id="Wording"><span class="header-section-number">2</span> Wording<a href="#Wording" class="self-link"></a></h1><p>In the summaries of <span class="code">vector</span>, <span class="code">inplace_vector</span>, <span class="code">list</span>, <span class="code">forward_list</span>, <span class="code">deque</span> and <span class="code">vector<span class="special">&lt;</span><span class="keyword">bool</span><span class="special">&gt;</span></span>, add the function</p><code><div class="code">    <span class="keyword">constexpr</span> <span class="keyword">void</span>      truncate<span class="special">(</span>size_type sz<span class="special">)</span> <span class="keyword">noexcept</span><span class="special">;</span></div></code><p>In the corresponding explanation blocks following, add the following</p><code><div class="code"><span class="keyword">void</span> truncate<span class="special">(</span>size_type sz<span class="special">)</span> <span class="keyword">noexcept</span><span class="special">;</span>

expects<span class="special">:</span> T is MoveInsertable into <span class="special">`</span>deque<span class="special">`</span>

Precondition<span class="special">:</span> <span class="special">`</span>sz<span class="special">`</span> is less than <span class="keyword">or</span> equal to <span class="special">`</span>size<span class="special">()`</span>

effects<span class="special">:</span> Erases the last <span class="special">`</span>size<span class="special">()</span> <span class="special">-</span> sz<span class="special">`</span> elements from the sequence<span class="special">.</span></div></code></body></html>
