Adding std::hash<std::tuple<...>> as a hash-combiner?

147 views
Skip to first unread message

Marc Mutz

unread,
May 15, 2017, 5:52:41 AM5/15/17
to std-pr...@isocpp.org
Hi,

While adding std::hash specialisations for Qt types, I ran into the (probably
well-known) problem that the library does not lend a particularly helping hand
for implementing your own. In particular, there's little-to-no support to hash
memory regions and to combine hash values.

The addition of a hash specialisation for std::string_view solves the first
problem, because I can now hash contiguous memory without a memory allocation.

To solve the second problem, I was thinking of using std::tie and hash the
tuple, but to my surprise, the standard does not provide a hash specialisation
for std::tuple.

I have two questions:

1. Why is that (the std doesn't provide hash specialisations for all of its
own types)?
2. Would it be acceptable to add a specialisation for std::tuple, so users
could use std::tie as a way to combine hash values?

I get that hashing is an open problem, and a can of worms. But I am left to
wonder why the third issue of modern C++ still appears to have no solution for
hash combining (and am skeptical whether hashing std::string_views is really
the intended way to hash raw memory).

Thanks,
Marc

--
Marc Mutz <marc...@kdab.com> | Senior Software Engineer
KDAB (Deutschland) GmbH & Co.KG, a KDAB Group Company
Tel: +49-30-521325470
KDAB - The Qt, C++ and OpenGL Experts

Vicente J. Botet Escriba

unread,
May 15, 2017, 6:04:08 PM5/15/17
to std-pr...@isocpp.org
Le 15/05/2017 à 11:51, Marc Mutz a écrit :
Hi,

While adding std::hash specialisations for Qt types, I ran into the (probably 
well-known) problem that the library does not lend a particularly helping hand 
for implementing your own. In particular, there's little-to-no support to hash 
memory regions and to combine hash values.
See
P0029R0 A Unified Proposal for Composable Hashing
The addition of a hash specialisation for std::string_view solves the first 
problem, because I can now hash contiguous memory without a memory allocation.

To solve the second problem, I was thinking of using std::tie and hash the 
tuple, but to my surprise, the standard does not provide a hash specialisation 
for std::tuple.

I have two questions:

1. Why is that (the std doesn't provide hash specialisations for all of its
   own types)?
product_type::hash function is missing in p0327r1 pending on p0029r0. I'll add it.

2. Would it be acceptable to add a specialisation for std::tuple, so users
   could use std::tie as a way to combine hash values?
I believe so.

I get that hashing is an open problem, and a can of worms. But I am left to 
wonder why the third issue of modern C++ still appears to have no solution for 
hash combining (and am skeptical whether hashing std::string_views is really 
the intended way to hash raw memory).

Vicente


Reply all
Reply to author
Forward
0 new messages