Created
May 31, 2023 14:31
-
-
Save aesophor/5c413ae886c0d3d8c64d5a26565032a8 to your computer and use it in GitHub Desktop.
C++17 Tuple Hash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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