Compile time string hashing

Posted by Caspin on Stack Overflow See other posts from Stack Overflow or by Caspin
Published on 2010-01-21T18:08:14Z Indexed on 2010/05/13 12:24 UTC
Read the original article Hit count: 347

Filed under:
|
|
|

I have read in few different places that using c++0x's new string literals it might be possible to compute a string's hash at compile time. However, no one seems to be ready to come out and say that it will be possible or how it would be done.

  • Is this possible?
  • What would the operator look like?

I'm particularly interested use cases like this.

void foo( const std::string& value )
{
   switch( std::hash(value) )
   {
      case "one"_hash: one(); break;
      case "two"_hash: two(); break;
      /*many more cases*/
      default: other(); break;
   }
}

Note: the compile time hash function doesn't have to look exactly as I've written it. I did my best to guess what the final solution would look like, but meta_hash<"string"_meta>::value could also be a viable solution.

© Stack Overflow or respective owner

Related posts about c++

Related posts about c++0x