I'm just trying to understand the use-case.
A straight-forward way is to have a single "hash" function template argument.
"operator==" or "CompPred" is only needed for collision resolution policy.
The only way to guarantee the same hash function value of any two given keys (k1 and k2), is to have a bitwise comparison of k1 and k2.
This is nonsense. Lots of types do not allow bitwise comparison, e.g. because they're not trivially copyable, or have padding bits.
And it's common to have an object in an unordered set where only part of the object is salient to the hash code. Consider a type like a tuple<A,B,C> where the hash code is only based on the A element. Your comparison function would also have to only compare the first elements.
Even if all the types A, B, and C are trivially copyable, doing a bitwise comparison or just using the tuple's operator== would do the wrong thing.
(An alternative design would be to have a projection function which just gives you the salient parts of the object, so you would call proj(obj) which would return const A&, and you could hash that and compare that for equality. But we don't have that design either.)
Anyway, why does it matter how we will find an item inside the bucket, if there is a cure-all such as bitwise comparison.
There isn't.
Colleagues, good morning!
unordered_set container allos "hash" and "compare" functions specifications through template arguments. Something like this "unordered_set<string, hash<string>, HashCmp> a;". On the other hand there is neither invariant specified (in cppreference or any other relevant web resources) for these two functions ("hash" and "compare"),
Yes there is.
Two values k1 and k2 are considered equivalent if the container's key equality predicate pred(k1, k2) is valid and returns true when passed those values. If k1 and k2 are equivalent, the container's hash function shall return the same value for both.
(Your example code violates that "shall": strings "abc" and "def" are equivalent according to your HashCmp, but your hash<string> does not return the same value for both.)
HTH,
Arthur
--
Std-Proposals mailing list
Std-Proposals@lists.isocpp.org
https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals