fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. constexpr unsigned long djb2_hash_impl( const char* text, unsigned long prev_hash )
  5. {
  6. return text[0] == '\0' ? prev_hash : djb2_hash_impl( &text[1], prev_hash * 33 ^ static_cast<unsigned long>(text[0]) );
  7. }
  8.  
  9. constexpr unsigned long djb2_hash( const char* text )
  10. {
  11. return djb2_hash_impl( text, 5381 );
  12. }
  13.  
  14. enum EXAMPLE
  15. {
  16. VAL = djb2_hash( "Hello World" )
  17. };
  18.  
  19. int main() {
  20. auto hello_hash = djb2_hash( "Hello World" );
  21.  
  22. switch( hello_hash )
  23. {
  24. case djb2_hash( "Hello World" ):
  25. cout << "Hash matched with value " << hello_hash << endl;
  26. break;
  27. default:
  28. cout << "Hash did not match" << endl;
  29. }
  30.  
  31. cout << "And the enum has value of " << VAL << endl;
  32. return 0;
  33. }
Success #stdin #stdout 0s 3344KB
stdin
Standard input is empty
stdout
Hash matched with value 903737989
And the enum has value of 903737989