Why no default hash for C++ POD structs?

Viewed 3468

I want to use a POD struct as a hash key in a map, e.g.

struct A { int x; int y; };
std::unordered_map<A, int> my_map;

but I can't do this, since no hash function is auto-generatable for such structs.

  • Why does the C++ standard not require a default hash for a POD struct?
  • Why do compilers (specifically, GCC 4.x / 5.x) offer such a hash, even if the standard doesn't mandate one?
  • How can I generate a hash function, using a template, in a portable way, for all of my POD structures (I'm willing to make semantic assumptions if necessary)?
3 Answers
Related