I'm trying to use an enum class as a key in an unordered_map. But this fails to compile:
1 2 3 4 5 6 7 8 9 10 11
#include <unordered_map>
enumclass E
{
V
};
int main()
{
std::unordered_map<E, int> m;
}
First it did compile but failed to link but only if I actually used operator[] (Not sure if I used the same compile options both time). I tried upgrading my version of gcc and now this code doesn't even compile. It says that std::hash is not specialized for type E.
> I guess I just have to specialize std::hash for my enum classes
Required only if we want to use a custom hash algorithm - something different hash from std::hash<int>. Otherwise, we can just specify std::hash<int> as a non-defaulted template parameter: