C++ Logo

std-discussion

Advanced search

Re: std::hash

From: Marcin Jaczewski <marcinjaczewski86_at_[hidden]>
Date: Sat, 25 Mar 2023 00:08:20 +0100
pt., 24 mar 2023 o 23:35 Chris Ryan <chrisr98008_at_[hidden]> napisaƂ(a):
>
> `std::vector<bool>` is a specialized implementation that is done differently from the other vectors & types because of the need to do the underlying bit packing.
> Other types come out in the wash on their own.
>

My point is that I need more hash for `std::vector<int>` than for
`std::vector<bool>` as it is a bit a black sheep of containers.
Writing hash for `int` it is not so hard as we can easily find
hundreds of examples on StackOverflow or Boost.
But if it is so easy why do standard not provide it? or at least some
basic building blocks.

I was consider using some non trivial logic that would rely on
`std::hash` but finding that standard lacks basic tools for it,
I now need spend day or two to find some replacement "should I believe
in code of this student from StackOverflow?",
 "how correctly implement hash combine?", "what library should I grab
for hash functions?".
Lot of time wasted that could be solved by standard at least in the
basic version that will be better than some random code
copy-pasted from QA sites.

This is a bit 22-catch, if implementation is easy, why then standard
do not have it?
 If implementation is hard, how could I even correctly implement this
myself, should it not be part of standard?

> Chris++;
>
> On Fri, Mar 24, 2023 at 2:14 PM Marcin Jaczewski via Std-Discussion <std-discussion_at_[hidden]> wrote:
>>
>> I recently looked at `std::unordered_map` and noticed a bit of a
>> bizarre handling of `std::hash` in the standard library.
>> Why of all types that could be useful, does the standard define only
>> `std::vector<bool>`?
>> Where `std::tuple` or other vectors are left unusable.
>>
>> What is the reason for standard not defining
>> `std::hash<std::vector<T>>` if `T` has a hash?
>> And why is `bool` the only exception there? What was the motivation for this?
>> https://eel.is/c++draft/vector.bool#pspc-7
>> --
>> Std-Discussion mailing list
>> Std-Discussion_at_[hidden]
>> https://lists.isocpp.org/mailman/listinfo.cgi/std-discussion

Received on 2023-03-24 23:08:33