<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 1332: Let Hash objects throw!</title>
<meta property="og:title" content="Issue 1332: Let Hash objects throw!">
<meta property="og:description" content="C++ library issue. Status: C++11">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue1332.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#C++11">C++11</a> status.</em></p>
<h3 id="1332"><a href="lwg-defects.html#1332">1332</a>. Let Hash objects throw!</h3>
<p><b>Section:</b> 16.4.4.5 <a href="https://wg21.link/hash.requirements">[hash.requirements]</a> <b>Status:</b> <a href="lwg-active.html#C++11">C++11</a>
 <b>Submitter:</b> Daniel Kr&uuml;gler <b>Opened:</b> 2010-03-26 <b>Last modified:</b> 2025-03-13</p>
<p><b>Priority: </b>Not Prioritized
</p>
<p><b>View all other</b> <a href="lwg-index.html#hash.requirements">issues</a> in [hash.requirements].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#C++11">C++11</a> status.</p>
<p><b>Discussion:</b></p>
<p>
The currently added <code>Hash</code> requirements demand in Table 40 &mdash; <code>Hash</code>
requirements [hash]:
</p>

<blockquote>
<table border="1">

<caption>Table 40 &mdash; Hash requirements [hash]</caption>

<tr>
<th>Expression</th>
<th>Return type</th>
<th>Requirement</th>
</tr>

<tr>
<td><code>h(k)</code></td>
<td><code>size_t</code></td>
<td>Shall not throw exceptions. [..]</td>
</tr>

</table>
</blockquote>

<p>
While it surely is a generally accepted idea that hash function objects
<i>should</i> not throw exceptions, this basic constraint for such a fundamental
requirement set does neither match the current library policy nor real world
cases:
</p>

<ol>
<li>
There are little known situations where a swap or move operation may throw an
exception and in some popular domains such functions are <em>required</em> not
to throw. But the library invested already efforts for good reasons to require
"working" container implementations in the presence of throwing move or swap
operations, see e.g. 23.2.7.2 <a href="https://wg21.link/associative.reqmts.except">[associative.reqmts.except]</a>, 23.2.8.2 <a href="https://wg21.link/unord.req.except">[unord.req.except]</a>.
</li>

<li>
The container library is already specified to cope with potentially throwing
comparers, predicates, <i>and</i> hash function objects, see above.
</li>

<li>
<p>
The new definition goes beyond the original hash requirements as specified
by SGI library in regard to the exception requirement:
</p>
<blockquote><p>
<a href="https://www.boost.org/sgi/stl/HashFunction.html">https://www.boost.org/sgi/stl/HashFunction.html</a>
</p></blockquote>
</li>

<li>
There are indeed real-world examples of potentially throwing hash functions,
typically when the proxy pattern is used and when the to-be hashed proxied
instance is some <i>volatile</i> object, e.g. a file or internet resource, that
might suddenly be unavailable at the time of hashing.
</li>

<li>
With the new <code>noexcept</code> language facility libraries can still take
advantage of no-throw guarantees of hasher functions with stricter guarantees.
</li>
</ol>

<p>
Even though the majority of all known move, swap, and hash functions won't throw
and in some cases <em>must</em> not throw, it seems like unnecessary
over-constraining the definition of a Hash functor not to propagate exceptions
in any case and it contradicts the general principle of C++ to impose such a
requirement for this kind of fundamental requirement.
</p>

<p><i>[
2010-11-11 Daniel asks the working group whether they would prefer a replacement
for the second bullet of the proposed resolution (a result of discussing this
with Alberto) of the form:
]</i></p>


<p>
Add to 22.10.19 <a href="https://wg21.link/unord.hash">[unord.hash]</a>/1 a new bullet:
</p>

<blockquote>
<p>
1 The unordered associative containers defined in Clause 23.5 use
specializations of the class template <code>hash</code>
as the default hash function. For all object types <code>Key</code> for which there
exists a specialization <code>hash&lt;Key&gt;</code>, the
instantiation <code>hash&lt;Key&gt;</code> shall:
</p>
<ul>
<li>
satisfy the <code>Hash</code> requirements (20.2.4), with <code>Key</code> as the
function call argument type, the <code>DefaultConstructible</code> requirements
(33), the <code>CopyAssignable</code> requirements (37),
</li>
<li>
be swappable (20.2.2) for lvalues,
</li>
<li>
provide two nested types <code>result_type</code> and <code>argument_type</code> which
shall be synonyms for <code>size_t</code> and <code>Key</code>, respectively,
</li>
<li>
satisfy the requirement that if <code>k1 == k2</code> is true, <code>h(k1) ==
h(k2)</code> is also true, where <code>h</code> is an object of type
<code>hash&lt;Key&gt;</code> and <code>k1</code> and <code>k2</code> are objects of type
<code>Key</code><ins>,</ins><del>.</del>
</li>
<li>
<ins>satisfy the requirement <code>noexcept(h(k)) == true</code>, where <code>h</code> is an object
of type <code>hash&lt;Key&gt;</code> and <code>k</code> is an object of type <code>Key</code>, unless 
<code>hash&lt;Key&gt;</code> is a user-defined specialization that depends on at least one user-defined type.</ins>
</li>
</ul>
</blockquote>



<p><i>[Batavia: Closed as NAD Future, then reopened. See the wiki for Tuesday.]</i></p>


<p id="res-1332"><b>Proposed resolution:</b></p>
<ol>
<li>
<p>
Change Table 26 &mdash; <code>Hash</code> requirements [tab:hash] as indicated:
</p>

<blockquote>
<table border="1">

<caption>Table 26 &mdash; <code>Hash</code> requirements [tab:hash]</caption>

<tr>
<th>Expression</th>
<th>Return type</th>
<th>Requirement</th>
</tr>

<tr>
<td><code>h(k)</code></td>
<td><code>size_t</code></td>
<td><del>Shall not throw exceptions.</del> [&hellip;]</td>
</tr>

</table>
</blockquote>
</li>

<li>
<p>
Add to 22.10.19 <a href="https://wg21.link/unord.hash">[unord.hash]</a> p. 1 a new bullet:
</p>

<blockquote>
<p>
1 The unordered associative containers defined in Clause 23.5 <a href="https://wg21.link/unord">[unord]</a> use
specializations of the class template <code>hash</code>
as the default hash function. For all object types <code>Key</code> for which there
exists a specialization <code>hash&lt;Key&gt;</code>, the
instantiation <code>hash&lt;Key&gt;</code> shall:
</p>
<ul>
<li>
satisfy the <code>Hash</code> requirements ([hash.requirements]), with <code>Key</code> as the
function call argument type, the <code>DefaultConstructible</code> requirements
(Table [defaultconstructible]), the <code>CopyAssignable</code> requirements (Table [copyassignable]),
</li>
<li>
be swappable ([swappable.requirements]) for lvalues,
</li>
<li>
provide two nested types <code>result_type</code> and <code>argument_type</code> which
shall be synonyms for <code>size_t</code> and <code>Key</code>, respectively,
</li>
<li>
satisfy the requirement that if <code>k1 == k2</code> is true, <code>h(k1) ==
h(k2)</code> is also true, where <code>h</code> is an object of type
<code>hash&lt;Key&gt;</code> and <code>k1</code> and <code>k2</code> are objects of type
<code>Key</code><ins>,</ins><del>.</del>
</li>
<li>
<ins>satisfy the requirement that the expression <code>h(k)</code>, where <code>h</code>
is an object of type <code>hash&lt;Key&gt;</code> and <code>k</code> is an object of
type <code>Key</code>, shall not throw an exception, unless
<code>hash&lt;Key&gt;</code> is a user-defined specialization that depends on at
least one user-defined type.</ins>
</li>
</ul>
</blockquote>
</li>
</ol>






</body>
</html>
