Skip to content

Instantly share code, notes, and snippets.

@Xipiryon
Created February 13, 2015 10:38
Show Gist options
  • Save Xipiryon/9df988aaefb2de798f5c to your computer and use it in GitHub Desktop.
Save Xipiryon/9df988aaefb2de798f5c to your computer and use it in GitHub Desktop.
Compile-time hash of a litteral string (requires C++11)
constexpr unsigned int const_hash(const char* str)
{
return *str ? static_cast<unsigned int>(*str) + 33 * const_hash(str+1) : 5381;
}
constexpr unsigned int operator "" _hash(const char* str, size_t)
{
return const_hash(str);
}
int main()
{
unsigned int hash_value = "hash me"_hash;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment