C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Q: invariant for unordered_set and unordered_map "hash" and "comp" argument

From: Aleksandr Povaliaev <apovalyaev_at_[hidden]>
Date: Sun, 29 Nov 2026 21:12:34 +0300
[[[[[Below]]]]]....

вс, 29 нояб. 2026 г. в 19:24, Thiago Macieira <thiago_at_[hidden]>:

> On Saturday, 28 November 2026 21:37:46 Pacific Standard Time Aleksandr
> Povaliaev wrote:
> > > All C structures will come without equality operators. Should they all
> be
> > > banned from being used as keys in std::map, unordered_map, sets, etc.?
> >
> > [[[Alex]]] There is no "C POD structure" entity to the C++ program. Since
> > all the structures and classes are declared at once in the header files,
> > they all (including "C POD structure") become C++ class while being
> > included.
> > You should specify an external operator == (POD, POD) and std::hash<POD>
> > "near" the C POD structure declaration, in the same namespace or so.
>
> How much luck have you had in asking C library developers to add
> operator== to
> their headers? Sometimes we're lucky they don't name their struct members
> or
> function parameters "class" and "new".
>

[[[[[Alex]]]]] You should probably use the following code construction:

namespace PlainPODs {

#include <PlainPODs.h>

operator == (const POD& left, const POD & right) { ... }

template<>
std::hash<POD>(...) { ...}

}


>
> The point is that if you don't own the class, you shouldn't declare an
> operator== or std::hash for it. It's possible a later version of this
> library
> will declare those, and now your program fails to compile because of the
> redeclaration.
>
> > AGAIN: I already wrote this example, but would like to repeat it. Please
> > consider the case when you're importing a dynamic and static library
> where
> > a class is declared but its internal comparison == (POD) operator
> > implementation is hidden. So, the class does have a comparison operator
> > declared, but you don't see its implementation.
>
> And how should one use such a class?
>

[[[[Alex]]]] See code snippet above - the same approach


>
> > [[[[Alex]]]] You should introduce another structure which contains only
> > these two fields: struct short_sockaddr_in6 {
> > in_port_tsin6_port;
> > struct in6_addr sin6_addr; };
>
> I agree that would be a cleaner design, one I would request of any
> colleague.
> But my personal or project preferences do not need to apply to everyone.
> There's a difference between our opinion on what we think is a superior
> design
> and what MUST be for everyone. I don't think we need to impose this
> requirement.
>

[[[[[Alex]]]]] Almost all use-cases discussed are the result of not using
default operator == and std::hash<>.
So, a compile time warning just informs the user about possible
consequences.


>
> > otherwise you just storring trash. And such design will reflect the real
> > state of things: "you are not storing sockaddr_in6 objects, you're
> storing
> > only these two fields information and all the other fields information
> > will be lost all together with all the other objects having the same two
> > fields value - you'll get only 1 object inserted to std::unordered_set
> > instead of 10 or 100".>
> > And again when using std::unordered_map, there is a very erroneous case
> > "(key1, value2") which I described before.
>
> You're making an assumption here. First, it's not trash if you must store
> it
> anyway, it's not trash.
>

[[[[[Alex]]]]] You can use std::unordered_multiset, but you will see
performance degradation. And even in case of using std::unordered_multiset,
you will use memory inefficiently, since there are a lot of duplications.
The right design is use
associative container which provides the following reflection (port, addr)
-> list of (family, flow_id, scope_id). It will be both
effective and fast


>
> Second, as I said above: if you meant for this equivalence to exist, then
> you
> must accept the replacement or non-replacement of equivalent but not
> identical
> keys. If you don't, then yes this is an error in the program. You're
> assuming
> that all such cases are errors and therefore any comparators that are not
> the
> type's operator== are wrong. That assumption is incorrect.
>

[[[[[Alex]]]]] Nop. In case of std::string you should use
using eng_string = std::basic_string<eng_traits>;
using fre_string = std::basic_string<fre_traits>;
using ger_string = std::basic_string<ger_traits>;
and every trait will implement a different case-insensitive comparison
mechanism. So, you won't needed:
(1) to specify CompPred each time, you're going to use STL associative
container;
(2) to store somewhere a flag specifying what particular language does a
string belongs to (int eng_flag = {English, French, German};


>
>
>
> --
> Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
> Principal Engineer - Intel Data Center - Platform & Sys. Eng.
>

Received on 2026-11-29 18:12:52