Date: Sun, 29 Nov 2026 08:23:55 -0800
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".
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]]]] 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.
> 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.
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.
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".
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]]]] 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.
> 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.
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.
-- Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org Principal Engineer - Intel Data Center - Platform & Sys. Eng.
Received on 2026-11-29 16:24:05
