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 22:44:25 +0300
*Below* ...

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

> On Sunday, 29 November 2026 10:12:34 Pacific Standard Time Aleksandr
> Povaliaev
> wrote:
> > [[[[[Below]]]]]....
>
> Please use a good email client for mailing lists.
>
> > [[[[[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>(...) { ...}
> >
> > }
>
> No, you shouldn't. That will get automatically rejected in any project I
> have
> ever been a part of, for putting an #include inside a namespace
> declaration.
> It works, but it's uncommon and very fragile - what happens if somewhere
> else
> you don't do it?
>
> More importantly, what if this isn't the C Standard Library, but some
> other
> project that might suddenly decide to offer C++ API? And if they do that,
> they
> may provide the operator==.
>

*Alex* You asked about importing C POD originally. The code snippet above
is a workaround on how to introduce comparison operator == and hash for
such structures. It should be done in one place.
If it is a C++ structure and comparison operator == is provided then we
should use it - this is what I am talking about: *IF THERE IS COMPARISON
OPERATOR DEFINED FOR A PARTICULAR CLASS, IT SHOULD BE USED ANYWHERE
INCLUDING UNORDERED_SET ARGUMENTS - OTHERWISE IT SHOULD BE A COMPILE-TIME
WARNING!*


>
> In any case, unless you want to make the above mandatory -- which you
> won't
> and which would be rejected even faster than the std::unordered_set change
> we're discussing-not-proposing -- then your other idea won't be acceptable
> either.
>
> > > > 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
>
> Rejected. My opinion is the one that matters.
>
> Plus, it's an ODR violation to declare the operator again, even if you
> couldn't see the declaration this other library's declaration.
>
> Ground rule: you must not declare operators and std::hash for types you
> don't
> own. So how do you use those types in a hash, map, or set?
>
> > [[[[[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.
>
> And I'm pretty sure they already knew that before the warning. What value
> is
> the warning then?
>
> > > 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>;
>
> That will never be accepted in any project. First of all, there are no
> traits
> for translation. At best what you could do is set up a wrapper around the
> same
> char_traits that do exactly the same thing, but are a different type. I
> don't
> think that's a good design: what's the purpose of different types that do
> exactly the same?
>

*Alex* char_traits std::basic_string argument is used to specify
comparison, coping and length calculation. Since comparison is differently
implemented for different languages, it should be a different char_trais
for any particular structure.


> Second, strings are meant to be interchangeable, despite the language. How
> would you print / format / display different languages' strings? Are you
> going
> to require that a font-formatting code be templated and inline so that it
> can
> take any basic_string specialisation?
>

*Alex* it is too far away from the current discussion topic


>
> Third, language selection is a runtime decision. Please stop and think
> about
> how you'd write code that supports all current 350 languages in the CLDR
> database (plus any regional and script variants) that the user may select.
> You've come up with this idea to support your argument, but you have not
> thought it through. That probably means you don't have a good idea of your
> problem in the first place.
>

*Alex* You either use wstring or a string. In the second case, you have
only 256 characters and the second page (> 127 hex) of them is completely
dependent on a locale. I am not talking about the system locale which is
currently being used, I am talking about the language locale. So, these 256
characters are compared differently for different languages. Since every
string might hold a string in a particular language (English, French or
German or others), they all should use their own char_traits.
Otherwise - you're to use unicodes and wstring.


>
> Fourth, this boat also sailed 25 years ago. Unless this is very dangerous
> content (such as std::auto_ptr was), it's not going to change. And it's
> not.
>

*Alex* Now, there is a variant possibility. Once a variant variable is used
as an unordered_set Key, it becomes very uneasy to specify CompPred
"in-place". So, once a variant<Class1, Class2, ..., ClassN> variable is
used as an unordered_set Key, you should use Class-i own comparison and
hash operators. Each of these Class-es should have a comparison operator ==
and hash defined.


>
> > and every trait will implement a different case-insensitive comparison
> > mechanism.
>
> std::char_traits has no case-insensitive comparison function.
>

*Alex* Please, ask "Google":
*template< class CharT, class Traits = std::char_traits<CharT>, class
Allocator = std::allocator<CharT>
> class basic_string;*
*Traits. Defines character operations such as comparison, copying, and
length calculation.*


>
> > 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};
>
> (2) is pure nonsense; see above.
>
> --
> Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
> Principal Engineer - Intel Data Center - Platform & Sys. Eng.
>

Received on 2026-11-29 19:44:41