This commit is contained in:
2025-06-14 15:29:01 +03:00
parent 3a28c3299f
commit 4c8e3f4caf
3 changed files with 462 additions and 8 deletions

View File

@@ -131,4 +131,45 @@ template <typename T>
using mcc_func_arg1_t = typename mcc_func_traits<T>::arg1_t;
namespace details
{
// compile-time hash for type
// (from https://stackoverflow.com/questions/56292104/hashing-types-at-compile-time-in-c17-c2a)
// WARNING: it does not work for unnamed struct!!!
template <typename T>
static consteval size_t Hash()
{
size_t result{};
#ifdef _MSC_VER
for (const auto& c : __FUNCSIG__)
#else // GCC and clang
for (const auto& c : __PRETTY_FUNCTION__)
#endif
(result ^= c) <<= 1;
return result;
}
} // namespace details
template <typename T>
static constexpr size_t mcc_type_hash = details::Hash<T>();
static constexpr size_t mcc_hash_combine(size_t lhs, size_t rhs)
{
constexpr size_t v_const = sizeof(size_t) >= 8 ? 0x517cc1b727220a95 : 0x9e3779b9;
lhs ^= rhs + v_const + (lhs << 6) + (lhs >> 2);
return lhs;
}
template <typename T1, typename T2>
static constexpr size_t mcc_type_pair_hash()
{
return mcc_hash_combine(mcc_type_hash<T1>, mcc_type_hash<T2>);
}
} // namespace mcc::traits