Permalink
Browse files

Hash enums with underlying type instead of int

Thanks to jameyboor
  • Loading branch information...
1 parent c1f3424 commit a3b20b248e2e26e41bd03edb8cf2e024bcf35ec8 @Rochet2 Rochet2 committed Feb 1, 2016
Showing with 9 additions and 4 deletions.
  1. +9 −4 BindingMap.h
View
@@ -274,18 +274,23 @@ class hash_helper
return seed;
}
- template <typename T>
+ template <typename T, typename std::enable_if<std::is_enum<T>::value>::type* = nullptr>
+ static inline result_type hash(T const & t)
+ {
+ return std::hash<typename std::underlying_type<T>::type>()(t);
+ }
+
+ template <typename T, typename std::enable_if<!std::is_enum<T>::value>::type* = nullptr>
static inline result_type hash(T const & t)
{
- // Possibly convert int to std::underlying_type or find another way
- using Hasher = typename std::conditional< std::is_enum<T>::value, std::hash<int>, std::hash<T> >::type;
- return Hasher()(t);
+ return std::hash<T>()(t);
}
private:
template <typename T>
static inline void _hash_combine(result_type& seed, T const & v)
{
+ // from http://www.boost.org/doc/libs/1_40_0/boost/functional/hash/hash.hpp
seed ^= hash(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
}

0 comments on commit a3b20b2

Please sign in to comment.