Boost logo

Boost :

From: Vinnie Falco (vinnie.falco_at_[hidden])
Date: 2024-12-07 01:03:50


On Fri, Dec 6, 2024 at 4:21 PM Peter Dimov via Boost <boost_at_[hidden]>
wrote:

> A std::hash specialization isn't a good way to do it because you can't pass
> an initial seed, which is a poor practice in 2025.
>

std::hash only requires DefaultConstructible, CopyConstructible, and
Destructible. And it doesn't say anything about not also being
constructible with additional parameters. Couldn't this be made to work:

    struct std::hash<MyT> {
      hash( std::size_t seed );
      ...

All std containers allow construction from an instance of a suitable hash
function, and they take ownership by copy:

https://en.cppreference.com/w/cpp/container/unordered_map/unordered_map

It does look like specializations of std::hash for user-defined types can
construct from seeds. Disclaimer: I did not actually read the text of the
standard, and only looked at cppreference.com.

Thanks


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk