Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
C++11 compile-time hash of literal strings.
#include <cstdint>
namespace detail
{
// FNV-1a 32bit hashing algorithm.
constexpr std::uint32_t fnv1a_32(char const* s, std::size_t count)
{
return ((count ? fnv1a_32(s, count - 1) : 2166136261u) ^ s[count]) * 16777619u;
}
} // namespace detail
constexpr std::uint32_t operator"" _hash(char const* s, std::size_t count)
{
return detail::fnv1a_32(s, count);
}
constexpr std::uint32_t check = "0123456789ABCDEF"_hash;
int main()
{
// Compare against precomputed value.
static_assert(check == 141695047u, "bad hash value");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.