Can I override std::hash?
Asked Answered
M

2

9

I can replace the actual implementation of std::hash with my own definition of std::hash in C++ 11 ?

I mean from my codebase, without touching the standard library.

I can't see any use for virtual function/polymorphism in this case, so I suppose that I can't alter the definition of std::hash anyway ?

Mercurial answered 6/8, 2013 at 11:29 Comment(3)
You can provide a specialization for your own types.Secession
@Secession yes, I know that, but I was interested in a global change.Mercurial
Possible duplicate of How to specialize std::hash<Key>::operator() for user-defined type in unordered containers?Deposition
A
8

Yes it's okay, and you don't have to modify the standard library in any way, just use template specialization:

namespace std
{
    template<>
    struct hash<YourSpecialType>
    {
        // ...
    };
}
Amble answered 6/8, 2013 at 11:32 Comment(5)
@Mercurial Ah, then no. Then you have fork the standard library.Amble
@user2485710: Global replacement of the hash function doesn't make much if any sense. What would it accomplish?Secession
@Secession changing the hash function used by any structure in just one move.Mercurial
@Mercurial How do you implement std::hash for any arbitrary type in C++? (hint: it doesn't work)Unspotted
You can implement type with a different name, like struct HashInt{}, and specify it where use it, e.g unordered_map<int, int, HashInt>Casein
T
9

You can specialise hash for specific types. See here and here e.g. like this

namespace std {
  template <> struct hash<Foo>
  {
    size_t operator()(const Foo & x) const
    {
      /* your code here, e.g. "return hash<int>()(x.value);" */
    }
  };
}

If you think you can do better than the library implementors for existing versions you are either 1. wrong or 2. clever

Tellez answered 6/8, 2013 at 11:33 Comment(3)
3. lazy , so I can get a new hash function without writing X new types.Mercurial
"X new types"? Just write a myHash function and use that instead, but that's probably not lazy - you will have to read Knuth for hours first to get it right.Tellez
the lazy part was about "writing" indeed :D but thanks for the reference to the creator of Tex and other bozillion of algorithms and books.Mercurial
A
8

Yes it's okay, and you don't have to modify the standard library in any way, just use template specialization:

namespace std
{
    template<>
    struct hash<YourSpecialType>
    {
        // ...
    };
}
Amble answered 6/8, 2013 at 11:32 Comment(5)
@Mercurial Ah, then no. Then you have fork the standard library.Amble
@user2485710: Global replacement of the hash function doesn't make much if any sense. What would it accomplish?Secession
@Secession changing the hash function used by any structure in just one move.Mercurial
@Mercurial How do you implement std::hash for any arbitrary type in C++? (hint: it doesn't work)Unspotted
You can implement type with a different name, like struct HashInt{}, and specify it where use it, e.g unordered_map<int, int, HashInt>Casein

© 2022 - 2024 — McMap. All rights reserved.