9

As pointed out in answers to Generic hash for tuples in unordered_map / unordered_set and elsewhere, Boost has an implementation of boost::hash<tuple<string, string>>.

So, we can do things like:

#include <boost/functional/hash.hpp>
#include <boost/unordered_map.hpp>

std::tuple<string, string> t;
size_t bh = boost::hash<tuple<string, string>>{}(t);
boost::unordered_map<tuple<string, string>, int> bm;

but not:

size_t sh = std::hash<tuple<string, string>>{}(t); // error C2338: The C++ Standard doesn't provide a hash for this type.
std::unordered_map<tuple<string, string>, int> sm; // same error

Why didn't this make it into the C++ standard?

Extra points for references with a rationale given by the committee or other experts.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
nielses
  • 150
  • 1
  • 6
  • 13
    Because they couldn't agree on the right way to do it. Still debating it these days... – Marc Glisse Jul 09 '21 at 16:29
  • 3
    The obvious choice for a tuple is to hash each element and combine the hashes. Mathematically combining hashes is still an interesting topic. – Richard Critten Jul 09 '21 at 16:34
  • Some additional reflections in the comments to [this answer](https://stackoverflow.com/questions/2590677/how-do-i-combine-hash-values-in-c0x/2595226#2595226). – nielses Jul 09 '21 at 16:46
  • 5
    A good **nine** papers attempting to standardize this can be seen [here](http://bajamircea.github.io/coding/cpp/2017/06/09/unordered-hash.html#references) – Drew Dormann Jul 09 '21 at 16:50
  • 2
    Thanks, Drew Dormann. That [Unordered hash conumdrum](https://bajamircea.github.io/coding/cpp/2017/06/09/unordered-hash.html) article explains it well and has further references. So it could count as the accepted answer. – nielses Jul 09 '21 at 17:03
  • @nielses if DrewDromann doesn't write an answer you should write one yourself. It's encouraged to answer your own question as long as you write a good answer. In this case provide the links, summarize them and quote the most relevant parts. – bolov Jul 09 '21 at 23:24
  • If OP is 400k+ in rep I usually assume they know how the site works :) – sehe Jul 10 '21 at 07:33

0 Answers0