This page is a snapshot from the LWG issues list, see the Library Active Issues List for more information and the meaning of Resolved status.
Hash
function objects have different behaviourSection: 23.2.8 [unord.req] Status: Resolved Submitter: Daniel James Opened: 2016-11-24 Last modified: 2020-09-06
Priority: Not Prioritized
View other active issues in [unord.req].
View all other issues in [unord.req].
View all issues with Resolved status.
Discussion:
In 23.2.8 [unord.req] paragraph 12, it says that the behaviour of operator==
is undefined unless the
Hash
and Pred
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 Hash
function objects behave differently. I did a little testing and
both libstdc++ and libc++ appear to support this correctly.
operator==
or operator!=
on unordered containers is undefined unless the Hash
and
Pred
function refinement
"
[2017-01-27 Telecon]
This is a design issue; send to LEWG
[2018-3-17 Resolved by P0809R0, which was adopted in Jacksonville.]
Proposed resolution:
This wording is relative to N4618.
Change 23.2.8 [unord.req] as indicated:
-12- Two unordered containers
a
andb
compare equal ifa.size() == b.size()
and, for every equivalent-key group[Ea1, Ea2)
obtained froma.equal_range(Ea1)
, there exists an equivalent-key group[Eb1, Eb2)
obtained fromb.equal_range(Ea1)
, such thatis_permutation(Ea1, Ea2, Eb1, Eb2)
returnstrue
. Forunordered_set
andunordered_map
, the complexity ofoperator==
(i.e., the number of calls to the==
operator of thevalue_type
, to the predicate returned bykey_eq()
, and to the hasher returned byhash_function()
) is proportional toN
in the average case and toN2
in the worst case, whereN
isa.size()
. Forunordered_multiset
andunordered_multimap
, the complexity ofoperator==
is proportional to ∑Ei2
in the average case and toN2
in the worst case, whereN
is a.size(), andEi
is the size of thei
th equivalent-key group ina
. However, if the respective elements of each corresponding pair of equivalent-key groupsEai
andEbi
are arranged in the same order (as is commonly the case, e.g., ifa
andb
are unmodified copies of the same container), then the average-case complexity forunordered_multiset
andunordered_multimap
becomes proportional toN
(but worst-case complexity remains 𝒪(N2
), e.g., for a pathologically bad hash function). The behavior of a program that usesoperator==
oroperator!=
on unordered containers is undefined unless theHash
andPred
function objects respectively havehas the same behavior for both containers and the equality comparison operator forKey
is a refinement(footnote 258) of the partition into equivalent-key groups produced byPred
.