C++ Logo

std-proposals

Advanced search

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

From: Giuseppe D'Angelo <giuseppe.dangelo_at_[hidden]>
Date: Sat, 28 Nov 2026 18:44:48 +0100
Il 28/11/26 15:11, Aleksandr Povaliaev via Std-Proposals ha scritto:
> I think that we can introduce a compile-time warning: if a Key class has
> a comparison operator defined, it should be used by default - otherwise
> a warning is shown.

But ... it *is* used by default? The default template argument for the
unordered_set's comparator is std::equal_to, which does use the type's
operator==.

The whole point of the comparator/hashing parameters is to allow extra
flexibility; in the case of comparisons, whether the underlying type
already offers an operator== (e.g. string) or not (e.g. span) is
completely irrelevant. The container allows you to choose the
comparison/hashing semantics that make sense for your specific use case.
What's wrong with *that*?

If you're instead complaining about the *engineering* of this feature,
namely the fact that the unordered containers have two template
arguments that *must be in sync*, but since they're different arguments
it's "easy"¹ to get them out of sync and run into UB, that's probably
fair, but

1) it needs a *thorough* proposal (= not a short email on this list, but
an email *with the proposal*) discussing on what is *exactly* the design
that you want (not "broadly speaking" -- all the nitty gritty the details);

2) how do you plan to make this deisgn coexist with existing code
without breaking it in any way shape or form (API and ABI);

3) if it's really worth to put it in the stdlib considering that you
could "easily" have something like this in your code:

> template <typename Key, typename Value, typename EqualityAndHasher = equal_and_hash<Key>>
> using my_unordered_map = std::unordered_map<Key, Value, typename EqualityAndHasher::Equality, typename EqualityAndHasher::Hasher>;




My 2 c,


¹ [cit. needed], although it's easy to see the parallel with "pass
pointer and size" vs. "use span"
--
Giuseppe D'Angelo

Received on 2026-11-28 17:44:58