Created
February 13, 2015 10:38
-
-
Save Xipiryon/9df988aaefb2de798f5c to your computer and use it in GitHub Desktop.
Compile-time hash of a litteral string (requires C++11)
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
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