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:18:28 +0300
[[[[[Below]]]]]...

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

> On Saturday, 28 November 2026 21:06:57 Pacific Standard Time Aleksandr
> Povaliaev wrote:
> > Correction!: "(key1, value2) argument" is about std:unordered_map not
> about
> > std::unordered set. I will elaborate a little bit on it: if you have
> (key1.
> > value1) already in place to std:unordered_set S structure, and you make
> > something like "S[key2] = value2" where key1 != key2, but CompPred(key1,
> > key2) is true, so you're overwriting the value and will get (key1,
> value2)
> > pair in place after all. It might be a possible source of error due to
> > unwanted (key1, value2) combination which might be used further, let's
> say
> > after "extract".
>
> If Comp(key1, key2) = true, then that's all that matters and the keys are
> equivalent for the purposes of the discussion. The container is expected
> to
> treat the two as equivalent and replace or not replace as key as such. The
> fact that there is another function that may return false is irrelevant.
> Don't
> bring it up.
>
> If someone is making a mistake doing that, it's their problem. I agree
> that it
> would be useful to detect the error and tell the user. Can you write an
> algorithm that detects when it's wrong? This feels more like a job for an
> AI
> assistant, not the compiler.
>

[[[[[Alex]]]]] You see!!! You do understand that such a design is not
perfect and for some reason you keep advocating it.
You started with the wrong decision of not using default comparison
operator == and at the next step you just run into a "not so good"
unordered_set structure, which "just stores unnecessary fields". And after
some more steps, the design will be even worther.
This is what I want to inform the developper by compile-time warnings,
since there is no any option to introduce a run-time assertion.


>
> > You wrote "the size of the key is the least of the problems" :DDDD
> > So if you're using only, let'say, 1% of Key object size for "CompPred"
> > comparison, and you still put other 99% of Key to the unordered_set
> > structure (99% is not used for Key comparison, but just occupies space in
> > memory) - it make 100 times more usage of the CPU cache.
>
> If you must store it, then you must store it and the cache usage is
> exactly
> what was expected.
>
> --
> Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
> Principal Engineer - Intel Data Center - Platform & Sys. Eng.
>

Received on 2026-11-29 18:18:43