Skip to content

Instantly share code, notes, and snippets.

@aesophor
Created May 31, 2023 14:31
Show Gist options
  • Save aesophor/5c413ae886c0d3d8c64d5a26565032a8 to your computer and use it in GitHub Desktop.
Save aesophor/5c413ae886c0d3d8c64d5a26565032a8 to your computer and use it in GitHub Desktop.
C++17 Tuple Hash
template <typename T, typename... Ts>
constexpr void hash_combine(size_t &seed, const T &t, const Ts &...ts) {
seed ^= std::hash<T>{}(t) + 0x9e3779b97f4a7c15 + (seed << 6) + (seed >> 2);
if constexpr (sizeof...(ts) > 0) {
hash_combine(seed, ts...);
}
}
template <typename T>
struct tuple_hash {
size_t operator()(const T &t) const {
size_t seed{0};
std::apply([&](const auto &...ts) {
(hash_combine(seed, ts), ...);
}, t);
return seed;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment