I have read in few different places that using C++11'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.
Best Answer
This is a little bit late, but I succeeded in implementing a compile-time CRC32 function with the use of
constexpr
. The problem with it is that at the time of writing, it only works with GCC and not MSVC nor Intel compiler.Here is the code snippet:
CrcVal01
is equal to 0x335CC04AHope this will help you!