<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 1182: Unfortunate hash dependencies</title>
<meta property="og:title" content="Issue 1182: Unfortunate hash dependencies">
<meta property="og:description" content="C++ library issue. Status: C++11">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue1182.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="1182"><a href="lwg-defects.html#1182">1182</a>. Unfortunate hash dependencies</h3>
<p><b>Section:</b> 22.10.19 <a href="https://wg21.link/unord.hash">[unord.hash]</a> <b>Status:</b> <a href="lwg-active.html#C++11">C++11</a>
 <b>Submitter:</b> Alisdair Meredith <b>Opened:</b> 2009-07-28 <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#unord.hash">issues</a> in [unord.hash].</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><b>Addresses UK 324</b></p>

<p>
The implied library dependencies created by spelling out all the <code>hash</code>
template specializations in the <code>&lt;functional&gt;</code> synopsis are unfortunate. 
The potential coupling is greatly reduced if the <code>hash</code> specialization is
declared in the appropriate header for each library type, as it is much
simpler to forward declare the primary template and provide a single
specialization than it is to implement a <code>hash</code> function for a <code>string</code> or
<code>vector</code> without providing a definition for the whole <code>string/vector</code>
template in order to access the necessary bits.
</p>

<p>
Note that the proposed resolution purely involves moving the
declarations of a few specializations, it specifically does not make any
changes to 22.10.19 <a href="https://wg21.link/unord.hash">[unord.hash]</a>.
</p>

<p><i>[
2009-09-15 Daniel adds:
]</i></p>


<p>
I suggest to add to the current existing
proposed resolution the following items.
</p>

<ul>
<li>
<p>
Add to the very first strike-list of the currently suggested resolution
the following lines:
</p>

<blockquote><pre>
<del>template &lt;&gt; struct hash&lt;std::error_code&gt;;</del>
<del>template &lt;&gt; struct hash&lt;std::thread::id&gt;;</del>
</pre></blockquote>
</li>

<li>
<p>
Add the following declarations to 19.5 <a href="https://wg21.link/syserr">[syserr]</a>, header
<code>&lt;system_error&gt;</code> synopsis after // 19.5.4:
</p>

<blockquote><pre>
<ins>
// 19.5.x hash support
template &lt;class T&gt; struct hash;
template &lt;&gt; struct hash&lt;error_code&gt;;
</ins>
</pre></blockquote>
</li>

<li>
<p>
Add a new clause 19.5.X (probably after 19.5.4):
</p>

<blockquote>
<p><ins>
19.5.X Hash support [syserr.hash]
</ins></p>

<pre><ins>
template &lt;&gt; struct hash&lt;error_code&gt;;
</ins></pre>

<blockquote><p><ins>
An explicit specialization of the class template hash (22.10.19 <a href="https://wg21.link/unord.hash">[unord.hash]</a>)
shall be provided
for the type <code>error_code</code> suitable for using this type as key in
unordered associative
containers (23.5 <a href="https://wg21.link/unord">[unord]</a>).
</ins></p></blockquote>
</blockquote>
</li>

<li>
<p>
Add the following declarations to 32.4.3.2 <a href="https://wg21.link/thread.thread.id">[thread.thread.id]</a> just after the
declaration of
the comparison operators:
</p>

<blockquote><pre>
<ins>
template &lt;class T&gt; struct hash;
template &lt;&gt; struct hash&lt;thread::id&gt;;
</ins></pre></blockquote>
</li>

<li>
<p>
Add a new paragraph at the end of 32.4.3.2 <a href="https://wg21.link/thread.thread.id">[thread.thread.id]</a>:
</p>

<blockquote>
<pre><ins>
template &lt;&gt; struct hash&lt;thread::id&gt;;
</ins></pre>

<blockquote><p><ins>
An explicit specialization of the class template hash (22.10.19 <a href="https://wg21.link/unord.hash">[unord.hash]</a>)
shall be provided
for the type <code>thread::id</code> suitable for using this type as key in
unordered associative
containers (23.5 <a href="https://wg21.link/unord">[unord]</a>).
</ins></p></blockquote>
</blockquote>
</li>

<li>
Issue <a href="lwg-defects.html#889" title="thread::id comparisons (Status: Resolved)">889</a><sup><a href="https://cplusplus.github.io/LWG/issue889" title="Latest snapshot">(i)</a></sup> independently suggests moving the specialization
<code>std::hash&lt;std::thread::id&gt;</code> to header <code>&lt;thread&gt;</code>.
</li>
</ul>

<p><i>[
2009-11-13 Alisdair adopts Daniel's suggestion and the extended note from
<a href="lwg-defects.html#889" title="thread::id comparisons (Status: Resolved)">889</a><sup><a href="https://cplusplus.github.io/LWG/issue889" title="Latest snapshot">(i)</a></sup>.
]</i></p>


<p><i>[
2010-01-31 Alisdair: related to <a href="lwg-defects.html#1245" title="std::hash&lt;string&gt; &amp; co (Status: C++11)">1245</a><sup><a href="https://cplusplus.github.io/LWG/issue1245" title="Latest snapshot">(i)</a></sup> and <a href="lwg-defects.html#978" title="Hashing smart pointers (Status: C++11)">978</a><sup><a href="https://cplusplus.github.io/LWG/issue978" title="Latest snapshot">(i)</a></sup>.
]</i></p>


<p><i>[
2010-02-07 Proposed wording updated by Beman, Daniel, Alisdair and Ganesh.
]</i></p>


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




<p id="res-1182"><b>Proposed resolution:</b></p>
<p><i>Strike the following specializations declared in the <code>&lt;functional&gt;</code> 
synopsis p2 22.10 <a href="https://wg21.link/function.objects">[function.objects]</a> </i> </p>
<blockquote><pre>
<del>template &lt;&gt; struct hash&lt;std::string&gt;;</del>
<del>template &lt;&gt; struct hash&lt;std::u16string&gt;;</del>
<del>template &lt;&gt; struct hash&lt;std::u32string&gt;;</del>
<del>template &lt;&gt; struct hash&lt;std::wstring&gt;;</del>

<del>template &lt;&gt; struct hash&lt;std::error_code&gt;;</del>
<del>template &lt;&gt; struct hash&lt;std::thread::id&gt;;</del>
<del>template &lt;class Allocator&gt; struct hash&lt;std::vector&lt;bool, Allocator&gt; &gt;;</del>
<del>template &lt;std::size_t N&gt; struct hash&lt;std::bitset&lt;N&gt; &gt;;</del></pre>
</blockquote>
<p><i>Add the following at the end of 22.10.19 <a href="https://wg21.link/unord.hash">[unord.hash]</a>:</i></p>
<blockquote><pre>
<ins>template &lt;&gt; struct hash&lt;bool&gt;;
template &lt;&gt; struct hash&lt;char&gt;;
template &lt;&gt; struct hash&lt;signed char&gt;;
template &lt;&gt; struct hash&lt;unsigned char&gt;;
template &lt;&gt; struct hash&lt;char16_t&gt;;
template &lt;&gt; struct hash&lt;char32_t&gt;;
template &lt;&gt; struct hash&lt;wchar_t&gt;;
template &lt;&gt; struct hash&lt;short&gt;;
template &lt;&gt; struct hash&lt;unsigned short&gt;;
template &lt;&gt; struct hash&lt;int&gt;;
template &lt;&gt; struct hash&lt;unsigned int&gt;;
template &lt;&gt; struct hash&lt;long&gt;;
template &lt;&gt; struct hash&lt;long long&gt;;
template &lt;&gt; struct hash&lt;unsigned long&gt;;
template &lt;&gt; struct hash&lt;unsigned long long&gt;;
template &lt;&gt; struct hash&lt;float&gt;;
template &lt;&gt; struct hash&lt;double&gt;;
template &lt;&gt; struct hash&lt;long double&gt;;
template&lt;class T&gt; struct hash&lt;T*&gt;;</ins></pre>
  <p><ins>
  Specializations meeting the requirements of class template <code>hash</code> 22.10.19 <a href="https://wg21.link/unord.hash">[unord.hash]</a>.</ins></p>
</blockquote>
<p><i>Add the following declarations to 19.5 <a href="https://wg21.link/syserr">[syserr]</a>, header <code>&lt;system_error&gt;</code> 
synopsis after // 19.5.4: </i> </p>
<blockquote>
  <pre><ins>// [syserr.hash] hash support
template &lt;class T&gt; struct hash;
template &lt;&gt; struct hash&lt;error_code&gt;;</ins></pre>
</blockquote>
<p><i>Add a new clause 19.5.X (probably after 19.5.4): </i> </p>
<blockquote>
  <p><ins>19.5.X Hash support [syserr.hash] </ins></p>
  <pre><ins>template &lt;&gt; struct hash&lt;error_code&gt;;</ins></pre>
    <p><ins>Specialization meeting the requirements of class template <code>hash</code> 22.10.19 <a href="https://wg21.link/unord.hash">[unord.hash]</a>.</ins></p>
</blockquote>
<p><i>Add the following declarations to the synopsis of <code>&lt;string&gt;</code> in 27.4 <a href="https://wg21.link/string.classes">[string.classes]</a>
</i>
</p>
<blockquote><pre>
<ins>// [basic.string.hash] hash support
template &lt;class T&gt; struct hash;
template &lt;&gt; struct hash&lt;string&gt;;
template &lt;&gt; struct hash&lt;u16string&gt;;
template &lt;&gt; struct hash&lt;u32string&gt;;
template &lt;&gt; struct hash&lt;wstring&gt;;</ins></pre>
</blockquote>
<p><i>Add a new clause 21.4.X </i> </p>
<blockquote>
  <p><ins>21.4.X Hash support [basic.string.hash]></ins></p>
  <pre><ins>template &lt;&gt; struct hash&lt;string&gt;;
template &lt;&gt; struct hash&lt;u16string&gt;;
template &lt;&gt; struct hash&lt;u32string&gt;;
template &lt;&gt; struct hash&lt;wstring&gt;;</ins></pre>
    <p><ins>Specializations meeting the requirements of class template <code>hash</code> 22.10.19 <a href="https://wg21.link/unord.hash">[unord.hash]</a>.</ins></p>
</blockquote>
<p><i>Add the following declarations to the synopsis of <code>&lt;vector&gt;</code> in
23.3 <a href="https://wg21.link/sequences">[sequences]</a></i> </p>
<blockquote><pre>
<ins>// 21.4.x hash support
template &lt;class T&gt; struct hash;
template &lt;class Allocator&gt; struct hash&lt;vector&lt;bool, Allocator&gt;&gt;;</ins></pre>
</blockquote>
<p><i>Add a new paragraph to the end of 23.3.14 <a href="https://wg21.link/vector.bool">[vector.bool]</a> </i> </p>
<blockquote>
  <pre><ins>template &lt;class Allocator&gt; struct hash&lt;vector&lt;bool, Allocator&gt;&gt;;</ins></pre>
    <p><ins>Specialization meeting the requirements of class template <code>hash</code> 22.10.19 <a href="https://wg21.link/unord.hash">[unord.hash]</a>.</ins></p>
</blockquote>
<p><i>Add the following declarations to the synopsis of <code>&lt;bitset&gt;</code> in 22.9.2 <a href="https://wg21.link/template.bitset">[template.bitset]</a> </i> </p>
<blockquote>
  <pre><ins>// [bitset.hash] hash support
template &lt;class T&gt; struct hash;
template &lt;size_t N&gt; struct hash&lt;bitset&lt;N&gt; &gt;;</ins></pre>
</blockquote>
<p><i>Add a new subclause 20.3.7.X [bitset.hash] </i> </p>
<blockquote>
  <p><ins>20.3.7.X bitset hash support [bitset.hash]</ins></p>
  <pre><ins>template &lt;size_t N&gt; struct hash&lt;bitset&lt;N&gt; &gt;;</ins></pre>
    <p><ins>Specialization meeting the requirements of class template <code>hash</code> 22.10.19 <a href="https://wg21.link/unord.hash">[unord.hash]</a>.</ins></p>
</blockquote>
<p><i>Add the following declarations to 32.4.3.2 <a href="https://wg21.link/thread.thread.id">[thread.thread.id]</a> synopsis just after the 
declaration of the comparison operators: </i> </p>
<blockquote>
  <pre><ins>template &lt;class T&gt; struct hash;
template &lt;&gt; struct hash&lt;thread::id&gt;;</ins></pre>
</blockquote>
<p><i>Add a new paragraph at the end of 32.4.3.2 <a href="https://wg21.link/thread.thread.id">[thread.thread.id]</a>: </i> </p>
<blockquote>
  <pre><ins>template &lt;&gt; struct hash&lt;thread::id&gt;;</ins></pre>
  <p><ins>Specialization meeting the requirements of class template <code>hash</code> 22.10.19 <a href="https://wg21.link/unord.hash">[unord.hash]</a>.</ins></p>
</blockquote>

<p><i>Change Header &lt;typeindex&gt; synopsis 17.7.6 <a href="https://wg21.link/type.index.synopsis">[type.index.synopsis]</a> as 
indicated:</i></p>
<blockquote>
<pre>namespace std {
class type_index;
  <ins>// [type.index.hash] hash support</ins>
  template &lt;class T&gt; struct hash;
  template&lt;&gt; struct hash&lt;type_index&gt;<ins>;</ins> <del> : public unary_function&lt;type_index, size_t&gt; {
    size_t operator()(type_index index) const;
  }</del>
}</pre>
</blockquote>

<p><i>Change Template specialization hash&lt;type_index&gt;  [type.index.templ]
  as indicated:</i></p>

<blockquote>

  <p>20.11.4 <del>Template specialization hash&lt;type_index&gt; [type.index.templ]</del>
  <ins>Hash support [type.index.hash]</ins></p>

  <pre><del>size_t operator()(type_index index) const;</del></pre>
  <blockquote>
    <p><del><i>Returns:</i> <code>index.hash_code()</code></del></p>
  </blockquote>
  
  <pre><ins>template&lt;&gt; struct hash&lt;type_index&gt;;</ins></pre>
  <p><ins>Specialization meeting the requirements of class template <code>hash</code> [unord.hash]. 
  For an object <code>index</code> of type <code>type_index</code>, <code>hash&lt;type_index&gt;()(index)</code> 
  shall evaluate to the same value as <code>index.hash_code()</code>.</ins></p>
  
</blockquote>






</body>
</html>
