<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 766: Inconsistent exception guarantees between ordered and unordered associative containers</title>
<meta property="og:title" content="Issue 766: Inconsistent exception guarantees between ordered and unordered associative containers">
<meta property="og:description" content="C++ library issue. Status: CD1">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue766.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="766"><a href="lwg-defects.html#766">766</a>. Inconsistent exception guarantees between ordered and unordered associative containers</h3>
<p><b>Section:</b> 23.2 <a href="https://wg21.link/container.requirements">[container.requirements]</a>, 23.2.8.2 <a href="https://wg21.link/unord.req.except">[unord.req.except]</a> <b>Status:</b> <a href="lwg-active.html#CD1">CD1</a>
 <b>Submitter:</b> Ion Gazta&ntilde;aga <b>Opened:</b> 2007-12-22 <b>Last modified:</b> 2016-01-28</p>
<p><b>Priority: </b>Not Prioritized
</p>
<p><b>View all other</b> <a href="lwg-index.html#container.requirements">issues</a> in [container.requirements].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#CD1">CD1</a> status.</p>
<p><b>Discussion:</b></p>
<p>
23.2 <a href="https://wg21.link/container.requirements">[container.requirements]</a>p10 states:
</p>

<blockquote>
<p>
Unless otherwise specified (see 23.2.2.3 and 23.2.5.4) all container types defined in this clause meet the following
additional requirements:
</p>
<ul>

<li>[...]</li>

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

</ul>
</blockquote>

<p>
23.3.5.4 <a href="https://wg21.link/deque.modifiers">[deque.modifiers]</a> and 23.3.13.5 <a href="https://wg21.link/vector.modifiers">[vector.modifiers]</a> offer
additional guarantees for <code>deque</code>/<code>vector insert()</code> and
<code>erase()</code> members. However, 23.2 <a href="https://wg21.link/container.requirements">[container.requirements]</a> p10 
does not mention 23.2.8.2 <a href="https://wg21.link/unord.req.except">[unord.req.except]</a> that specifies exception 
safety guarantees for unordered containers. In addition,  
23.2.8.2 <a href="https://wg21.link/unord.req.except">[unord.req.except]</a> p1 offers the following guaratee for
<code>erase()</code>:
</p>

<blockquote><p>
No <code>erase()</code> function throws an exception unless that exception
is thrown by the container's Hash or Pred object (if any).
</p></blockquote>

<p>
Summary:
</p>

<p>
According to 23.2 <a href="https://wg21.link/container.requirements">[container.requirements]</a> p10 no
<code>erase()</code> function should throw an exception unless otherwise
specified. Although does not explicitly mention 23.2.8.2 <a href="https://wg21.link/unord.req.except">[unord.req.except]</a>, this section offers additional guarantees
for unordered containers, allowing <code>erase()</code> to throw if
predicate or hash function throws.
</p>

<p>
In contrast, associative containers have no exception safety guarantees
section so no <code>erase()</code> function should throw, <em>including
<code>erase(k)</code></em> that needs to use the predicate function to
perform its work. This means that the predicate of an associative
container is not allowed to throw.
</p>

<p>
So:
</p>

<ol>
<li>
<code>erase(k)</code> for associative containers is not allowed to throw. On
the other hand, <code>erase(k)</code> for unordered associative containers
is allowed to throw.
</li>
<li>
<code>erase(q)</code> for associative containers is not allowed to throw. On
the other hand, <code>erase(q)</code> for unordered associative containers
is allowed to throw if it uses the hash or predicate.
</li>
<li>
To fulfill 1), predicates of associative containers are not allowed to throw.
Predicates of unordered associative containers are allowed to throw.
</li>
<li>
2) breaks a widely used programming pattern (flyweight pattern) for
unordered containers, where objects are registered in a global map in
their constructors and unregistered in their destructors. If <code>erase(q)</code> is
allowed to throw, the destructor of the object would need to rethrow the
exception or swallow it, leaving the object registered.
</li>
</ol>


<p id="res-766"><b>Proposed resolution:</b></p>
<p>
Create a new sub-section of 23.2.7 <a href="https://wg21.link/associative.reqmts">[associative.reqmts]</a> (perhaps 
[associative.req.except]) titled "Exception safety guarantees".
</p>

<blockquote>
<p>
1 For associative containers, no <code>clear()</code> function throws an exception.
<code>erase(k)</code> does not throw an exception unless that exception is thrown by
the container's Pred object (if any).
</p>

<p>
2 For associative containers, if an exception is thrown by any operation
from within an <code>insert()</code> function inserting a single element, the
<code>insert()</code> function has no effect.
</p>

<p>
3 For associative containers, no <code>swap</code> function throws an exception
unless that exception is thrown by the copy constructor or copy
assignment operator of the container's Pred object (if any).
</p>
</blockquote>

<p>
Change 23.2.8.2 <a href="https://wg21.link/unord.req.except">[unord.req.except]</a>p1:
</p>

<blockquote><p>
For unordered associative containers, no <code>clear()</code> function
throws an exception. <del>No</del> <code>erase(<ins>k</ins>)</code>
<del>function</del> <ins>does not</ins> throw<del>s</del> an exception
unless that exception is thrown by the container's Hash or Pred object
(if any).
</p></blockquote>

<p>
Change 23.2 <a href="https://wg21.link/container.requirements">[container.requirements]</a> p10 to add references to new sections:
</p>

<blockquote><p>
Unless otherwise specified (see [deque.modifiers]<ins>,</ins>
<del>and</del> [vector.modifiers]<ins>, [associative.req.except],
[unord.req.except]</ins>) all container types defined in this clause meet
the following additional requirements:
</p></blockquote>

<p>
Change 23.2 <a href="https://wg21.link/container.requirements">[container.requirements]</a> p10 referring to <code>swap</code>:
</p>

<blockquote>
<ul>
<li>
no <code>swap()</code> function throws an exception<del> unless that exception is thrown
by the copy constructor or assignment operator of the container's
Compare object (if any; see [associative.reqmts])</del>.
</li>
</ul>
</blockquote>






</body>
</html>
