Date: Sat, 28 Nov 2026 01:04:32 +0300
[[Below]]...
пт, 27 нояб. 2026 г. в 23:43, Jonathan Wakely <cxx_at_[hidden]>:
>
>
> On Fri, 27 Nov 2026, 17:32 Александр Поваляев, <apovalyaev_at_[hidden]>
> wrote:
>
>> If you are not using POD as a "Key" template argument, you should use the
>> class comparison operator.
>>
>> Every class being used as a "Key" template argument should have either
>> default ( = default) or a custom operator ==. If there is no default
>> comparison operator for a particular class (say, it is explicitly "=
>> deleted"), the class should not be used as Key, I guess.
>> If a class object might have either padding bits (as you wrote) unequal,
>> or there are, let's say, some pointers inside which might be pointing to
>> some other classes meaning-full objects, Default or Custom comparison
>> operator should be used. It is a principle of encapsulation. No
>> complicated logic (including comparison) should be exported to some
>> external "CompPred" functions.
>>
>> Almost the same logic might be provided and for a "Hash" template
>> argument - we have got "std::hash" and it might be also specified
>> explicitly. But let's assume we're going to use different hash functions
>> for different unordered_set instantiation, where "Key" argument is the
>> same, but somehow (I don't know use-case also) "Hash" functions should be
>> different.
>>
>> As for the tuple<A,B,C>. It is ok to a have bitwise comparison operator
>> and a hash function using only get<0>(tuple), A.
>>
>
> No it isn't.
>
[[Alex]] For tuple<A,B,C> bitwise comparison will work fine!
>
> What if A is std::string, a bitwise comparison would compare the unused
> bytes in the SSO buffer.
>
[[Alex]] In case of std::string - you should use the class comparison
operator (specified through char_traits or default) by design.
>
>
> It will work correctly.
>> It would be a logic error, if a hash function does use A,B and C, but a
>> comparison does only use A and B.
>>
>
> Well obviously. So don't do that.
>
>
>
>> I think it is a good time for another example! - let's imagine there is a
>> library (either static or dynamic), there is a header with a class
>> declaration and a linkable module (binary code) where all the class methods
>> (including comparison) are implemented.
>> You are to use the class somehow as a Key "unordered_set" template
>> argument and you need to invent somehow a hash function.
>> What will you do?
>> You will use every meaningful bit of the class attributes while
>> calculating hash value.
>>
>
> No, not always.
>
> It will be the only solution for this situation.
>>
>
> For this hypothetical situation you just created, maybe. Not every
> situation. If you can't see that, you lack imagination.
>
[[Alex]] I think there are many projects using "library import", when you
have a comparison class operator logic "hidden" and only header declaration
information is available.
>
> In other words, we will presume that this particular class has a "bitwise"
>> comparison operator, and we will use this presumption while designing a
>> hash function. (In case of erroneous padding presence, we should have skip
>> some bits - but I think you do understand the essence).
>>
>
> The essence is wrong.
>
[[Alex]] "Bitwise" comparison, I mean here, is that you are comparing every
meaningful bit (skipping padding).
>
>
>>
>> пт, 27 нояб. 2026 г. в 11:43, Jonathan Wakely <cxx_at_[hidden]>:
>>
>>>
>>>
>>> On Mon, 23 Nov 2026, 18:04 Александр Поваляев via Std-Proposals, <
>>> std-proposals_at_[hidden]> wrote:
>>>
>>>> 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.
>>>
>>>
>>>
>>>> пт, 20 нояб. 2026 г. в 23:50, Arthur O'Dwyer <arthur.j.odwyer_at_[hidden]
>>>> >:
>>>>
>>>>> On Fri, Nov 20, 2026 at 2:21 PM Александр Поваляев via Std-Proposals <
>>>>> std-proposals_at_[hidden]> wrote:
>>>>>
>>>>>> 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.
>>>>> https://eel.is/c++draft/unord.req#general-5.sentence-2
>>>>>
>>>>>> 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_at_[hidden]
>>>> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>>>>
>>>
пт, 27 нояб. 2026 г. в 23:43, Jonathan Wakely <cxx_at_[hidden]>:
>
>
> On Fri, 27 Nov 2026, 17:32 Александр Поваляев, <apovalyaev_at_[hidden]>
> wrote:
>
>> If you are not using POD as a "Key" template argument, you should use the
>> class comparison operator.
>>
>> Every class being used as a "Key" template argument should have either
>> default ( = default) or a custom operator ==. If there is no default
>> comparison operator for a particular class (say, it is explicitly "=
>> deleted"), the class should not be used as Key, I guess.
>> If a class object might have either padding bits (as you wrote) unequal,
>> or there are, let's say, some pointers inside which might be pointing to
>> some other classes meaning-full objects, Default or Custom comparison
>> operator should be used. It is a principle of encapsulation. No
>> complicated logic (including comparison) should be exported to some
>> external "CompPred" functions.
>>
>> Almost the same logic might be provided and for a "Hash" template
>> argument - we have got "std::hash" and it might be also specified
>> explicitly. But let's assume we're going to use different hash functions
>> for different unordered_set instantiation, where "Key" argument is the
>> same, but somehow (I don't know use-case also) "Hash" functions should be
>> different.
>>
>> As for the tuple<A,B,C>. It is ok to a have bitwise comparison operator
>> and a hash function using only get<0>(tuple), A.
>>
>
> No it isn't.
>
[[Alex]] For tuple<A,B,C> bitwise comparison will work fine!
>
> What if A is std::string, a bitwise comparison would compare the unused
> bytes in the SSO buffer.
>
[[Alex]] In case of std::string - you should use the class comparison
operator (specified through char_traits or default) by design.
>
>
> It will work correctly.
>> It would be a logic error, if a hash function does use A,B and C, but a
>> comparison does only use A and B.
>>
>
> Well obviously. So don't do that.
>
>
>
>> I think it is a good time for another example! - let's imagine there is a
>> library (either static or dynamic), there is a header with a class
>> declaration and a linkable module (binary code) where all the class methods
>> (including comparison) are implemented.
>> You are to use the class somehow as a Key "unordered_set" template
>> argument and you need to invent somehow a hash function.
>> What will you do?
>> You will use every meaningful bit of the class attributes while
>> calculating hash value.
>>
>
> No, not always.
>
> It will be the only solution for this situation.
>>
>
> For this hypothetical situation you just created, maybe. Not every
> situation. If you can't see that, you lack imagination.
>
[[Alex]] I think there are many projects using "library import", when you
have a comparison class operator logic "hidden" and only header declaration
information is available.
>
> In other words, we will presume that this particular class has a "bitwise"
>> comparison operator, and we will use this presumption while designing a
>> hash function. (In case of erroneous padding presence, we should have skip
>> some bits - but I think you do understand the essence).
>>
>
> The essence is wrong.
>
[[Alex]] "Bitwise" comparison, I mean here, is that you are comparing every
meaningful bit (skipping padding).
>
>
>>
>> пт, 27 нояб. 2026 г. в 11:43, Jonathan Wakely <cxx_at_[hidden]>:
>>
>>>
>>>
>>> On Mon, 23 Nov 2026, 18:04 Александр Поваляев via Std-Proposals, <
>>> std-proposals_at_[hidden]> wrote:
>>>
>>>> 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.
>>>
>>>
>>>
>>>> пт, 20 нояб. 2026 г. в 23:50, Arthur O'Dwyer <arthur.j.odwyer_at_[hidden]
>>>> >:
>>>>
>>>>> On Fri, Nov 20, 2026 at 2:21 PM Александр Поваляев via Std-Proposals <
>>>>> std-proposals_at_[hidden]> wrote:
>>>>>
>>>>>> 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.
>>>>> https://eel.is/c++draft/unord.req#general-5.sentence-2
>>>>>
>>>>>> 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_at_[hidden]
>>>> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>>>>
>>>
Received on 2026-11-27 22:04:49
