Last active
September 18, 2023 03:46
-
-
Save Little-Ki/d53383ebcd8415aeb06fe50f31fd9945 to your computer and use it in GitHub Desktop.
[C++ Template] Calculate hash by type name
This file contains hidden or 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
#pragma once | |
template <typename T> | |
constexpr size_t hash_type() | |
{ | |
size_t result{}; | |
#ifdef _MSC_VER | |
#define F __FUNCSIG__ | |
#else | |
#define F __PRETTY_FUNCTION__ | |
#endif | |
for (const auto &c : F) | |
(result ^= c) <<= 1; | |
return result; | |
}; | |
template<typename ...Args> | |
constexpr size_t hash_types() { | |
size_t result{}; | |
constexpr size_t size = sizeof...(Args); | |
constexpr size_t hash[size] = { hash_type<Args>() ... }; | |
for (const auto &c : hash) (result ^= c) <<= 1; | |
return result; | |
}; | |
template<> | |
constexpr size_t hash_types() { return hash_type<void>(); } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment