<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2831: Equality can be defined when Hash function objects have different behaviour</title>
<meta property="og:title" content="Issue 2831: Equality can be defined when Hash function objects have different behaviour">
<meta property="og:description" content="C++ library issue. Status: Resolved">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2831.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#Resolved">Resolved</a> status.</em></p>
<h3 id="2831"><a href="lwg-defects.html#2831">2831</a>. Equality can be defined when <code>Hash</code> function objects have different behaviour</h3>
<p><b>Section:</b> 23.2.8 <a href="https://wg21.link/unord.req">[unord.req]</a> <b>Status:</b> <a href="lwg-active.html#Resolved">Resolved</a>
 <b>Submitter:</b> Daniel James <b>Opened:</b> 2016-11-24 <b>Last modified:</b> 2020-09-06</p>
<p><b>Priority: </b>Not Prioritized
</p>
<p><b>View other</b> <a href="lwg-index-open.html#unord.req">active issues</a> in [unord.req].</p>
<p><b>View all other</b> <a href="lwg-index.html#unord.req">issues</a> in [unord.req].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#Resolved">Resolved</a> status.</p>
<p><b>Discussion:</b></p>
<p>
In 23.2.8 <a href="https://wg21.link/unord.req">[unord.req]</a> paragraph 12, it says that the behaviour of <code>operator==</code> is undefined unless the 
<code>Hash</code> and <code>Pred</code> function objects respectively have the same behaviour. This makes comparing containers 
with randomized hashes with different seeds undefined behaviour, but I think that's a valid use case. It's not much 
more difficult to support it when the <code>Hash</code> function objects behave differently. I did a little testing and 
both libstdc++ and libc++ appear to support this correctly.
<p/>
I suggest changing the appropriate sentence in 23.2.8 <a href="https://wg21.link/unord.req">[unord.req]</a> paragraph 12: "The behavior of a program that 
uses <code>operator==</code> or <code>operator!=</code> on unordered containers is undefined unless the <del><code>Hash</code> and 
</del><code>Pred</code> function <del>objects respectively have</del><ins>object has</ins> the same behavior for both 
containers and the equality comparison operator for Key is a <code>refinement</code>"
</p>

<p><i>[2017-01-27 Telecon]</i></p>

<p>This is a design issue; send to LEWG</p>

<p><i>[2018-3-17 Resolved by <a href="https://wg21.link/P0809R0">P0809R0</a>, which was adopted in Jacksonville.]</i></p>



<p id="res-2831"><b>Proposed resolution:</b></p>
<p>
This wording is relative to <a href="https://wg21.link/n4618">N4618</a>.
</p>
<ol>
<li><p>Change 23.2.8 <a href="https://wg21.link/unord.req">[unord.req]</a> as indicated:</p>
<blockquote>
<p>
-12- Two unordered containers <code>a</code> and <code>b</code> compare equal if <code>a.size() == b.size()</code> and, 
for every equivalent-key group <code>[Ea1, Ea2)</code> obtained from <code>a.equal_range(Ea1)</code>, there exists 
an equivalent-key group <code>[Eb1, Eb2)</code> obtained from <code>b.equal_range(Ea1)</code>, such that 
<code>is_permutation(Ea1, Ea2, Eb1, Eb2)</code> returns <code>true</code>. For <code>unordered_set</code> and 
<code>unordered_map</code>, the complexity of <code>operator==</code> (i.e., the number of calls to the <code>==</code>
operator of the <code>value_type</code>, to the predicate returned by <code>key_eq()</code>, and to the hasher returned 
by <code>hash_function()</code>) is proportional to <code><i>N</i></code> in the average case and to <code><i>N</i><sup>2</sup></code> 
in the worst case, where <code><i>N</i></code> is <code>a.size()</code>. For <code>unordered_multiset</code> and 
<code>unordered_multimap</code>, the complexity of <code>operator==</code> is proportional to 
&sum; <code><i>E<sub>i</sub></i><sup>2</sup></code> in the average case and to <code><i>N</i><sup>2</sup></code> in the worst case, 
where <code><i>N</i></code> is a.size(), and <code><i>E<sub>i</sub></i></code> is the size of the <code><i>i</i></code>th 
equivalent-key group in <code>a</code>. However, if the respective elements of each corresponding pair of equivalent-key 
groups <code><i>Ea<sub>i</sub></i></code> and <code><i>Eb<sub>i</sub></i></code> are arranged in the same order (as is commonly 
the case, e.g., if <code>a</code> and <code>b</code> are unmodified copies of the same container), then the average-case 
complexity for <code>unordered_multiset</code> and <code>unordered_multimap</code> becomes proportional to <code><i>N</i></code> 
(but worst-case complexity remains &#x1d4aa;(<code><i>N</i><sup>2</sup></code>), e.g., for a pathologically bad hash function).
The behavior of a program that uses <code>operator==</code> or <code>operator!=</code> on unordered containers is undefined 
unless the <del><code>Hash</code> and</del> <code>Pred</code> function object<del>s respectively have</del><ins>has</ins> 
the same behavior for both containers and the equality comparison operator for <code>Key</code> is a refinement(footnote 258) 
of the partition into equivalent-key groups produced by <code>Pred</code>.
</p>
</blockquote>
</li>
</ol>





</body>
</html>
