<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 1301: clear() and assignment</title>
<meta property="og:title" content="Issue 1301: clear() and assignment">
<meta property="og:description" content="C++ library issue. Status: NAD Editorial">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue1301.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#NAD_Editorial">NAD Editorial</a> status.</em></p>
<h3 id="1301"><a href="lwg-closed.html#1301">1301</a>. <code>clear()</code> and assignment</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#NAD_Editorial">NAD Editorial</a>
 <b>Submitter:</b> Nicolai Josuttis <b>Opened:</b> 2010-01-01 <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#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#NAD Editorial">NAD Editorial</a> status.</p>
<p><b>Discussion:</b></p>
<p>
I propose that <code>clear()</code> be defined to be equivalent to
<code>erase(begin(),end())</code> except not using copy or move of elements.
</p>

<blockquote>
<p>
To: C++ libraries mailing list<br/>
Message c++std-lib-26465
</p>

<p>
and specifiying as post: <code>size()==0</code> might also not be appropriate
because forward-Lists provide no <code>size()</code>, this it should be:
post: <code>empty()==true</code>
</p>

<p>
Bjarne Stroustrup schrieb/wrote:
</p>

<blockquote>
<p>
To: C++ libraries mailing list<br/>
Message c++std-lib-26458
</p>

<p>
in table 94 we define <code>clear()</code> as:
</p>

<blockquote><pre>
a.clear() void erase(begin(), end())
post: size() == 0
</pre></blockquote>

<p>
Now <code>erase</code> requires assignment (<code>MoveAssignable</code>) which makes
sense if we have to move an element, but why should that be required from
<code>clear()</code> where all elements are destroyed?
</p>
</blockquote>
</blockquote>

<p><i>[
2010-01-23 Alisdiar provides wording.
]</i></p>


<p><i>[
2010-01-30 Moved to Tentatively Ready after 5 positive votes on c++std-lib.
]</i></p>


<p><i>[
2010-01-30 Daniel opens:
]</i></p>


<blockquote>
<p>
First, I read the newly proposed spec for <code>clear()</code> that it does in
general <em>not</em> invalidate a previous past-the-end iterator value, but
<code>deque</code> says in 23.3.5.4 <a href="https://wg21.link/deque.modifiers">[deque.modifiers]</a> for the semantics of
<code>erase</code> that erasures at the end will invalidate the past-the-end
iterator. With removal of a direct binding between <code>clear()</code> and
<code>erase()</code> there seem to be some fixes necessary. One way to fix that
would be to mention in Table 94 that this "may also invalidate the past-the-end
iterator" and then to mention for all specific containers where this does not
happen, the exception, [1] e.g. in <code>std::vector</code>. <code>std::vector</code>
has no own specification of <code>clear()</code> and one aspect of the closed issue
<a href="lwg-active.html#1102" title="std::vector's reallocation policy still unclear (Status: Open)">1102</a><sup><a href="https://cplusplus.github.io/LWG/issue1102" title="Latest snapshot">(i)</a></sup> was to realize just that (indirectly via <code>erase</code>). IMO
we should now add an extra specification for <code>clear()</code>. Btw.:
<code>std::vector::erase</code> reads to me that it would invalidate previous
past-the-end values (and that seems correct in general).
</p>
<p>
Before I will provide explicit wording, I would like to
discuss these points.
</p>

<p>
[1] <code>std::list</code> does fortunately specify that clear does not invalidate
the past-the-end iterator.
</p>
</blockquote>

<p><i>[
2010-02-08 Moved to Tentatively NAD Editorial after 5 positive votes on c++std-lib.
Rationale added below.
]</i></p>




<p><b>Rationale:</b></p>
<p>
Solved as proposed by LWG <a href="lwg-defects.html#704" title="MoveAssignable requirement for container value type overly strict (Status: C++11)">704</a><sup><a href="https://cplusplus.github.io/LWG/issue704" title="Latest snapshot">(i)</a></sup>.
</p>


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

<p>
Change 23.2.2 <a href="https://wg21.link/container.requirements.general">[container.requirements.general]</a>/10:
</p>

<blockquote>
<p>
Unless otherwise specified (see 23.2.4.1, 23.2.5.1, 23.3.2.3, and 23.3.6.4) all
container types defined in this Clause meet the following additional
requirements:
</p>

<ul>
<li>
..
</li>

<li>
no <code>erase()</code>, <ins><code>clear()</code>,</ins> <code>pop_back()</code> or
<code>pop_front()</code> function throws an exception.
</li>

<li>
...
</li>
</ul>

</blockquote>

<p>
Replace the following words from Table 94 &mdash; Sequence container
requirements (in addition to container) in 23.2.4 <a href="https://wg21.link/sequence.reqmts">[sequence.reqmts]</a>:
</p>

<blockquote>
<table border="1">
<caption>Table 94 &mdash; Sequence container requirements (in addition to
container)</caption>
<tr>
<th>Expression</th>
<th>Return type</th>
<th>Assertion/note<br/>pre-/post-condition</th>
</tr>

<tr>
<td><code>a.clear()</code></td>
<td><code>void</code></td>
<td><del><code>erase(begin(), end())</code></del><br/>
<ins>Destroys all elements in the container a. Invalidates all references,
pointers, and iterators referring to the elements of <code>a</code> and may
invalidate the past-the-end iterator.</ins><br/>
post: <code><del>size() == 0</del> <ins>a.empty() == true</ins></code>.  </td>
</tr>
</table>
</blockquote>

<p>
Add a new paragraph after  [forwardlist.modifiers]/23:
</p>

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

<blockquote>
<p>
23 <i>Effects:</i> Erases all elements in the range <code>[begin(),end())</code>.
</p>
<p><ins>
<i>Remarks:</i> Does not invalidate past-the-end iterators.
</ins></p>
</blockquote>
</blockquote>






</body>
</html>
