I've been working with some c++ lately and had previously been using static hash values for strings (similar to this from the Bitsquid blog) but the one thing that bothered me was the lack of ability to use them in switch statements, where I often want to use them. With
constexpr in c++11, I can.
I've been talking a bit with Alan Wolfe (@Atrix256) and he has been posting a good series of exploring code vs data, and compile time evaluation on his blog. You should read it, if that interests you.
I had been meaning to convert my fnv1a hash functions into
constexpr but haven't had enough reason to yet, but the recent posts from Alan spurred me on to just do it, and it was surprisingly straight forward. I found a few really old examples of people trying it but for some reason they were all a mess and wrapped in classes...
Since I had to go digging to find a nice simple version originally, here are the implementations of the hash function I am using below along with the
constexpr version. The license is public domain or an equivalent (attribution would be nice but not necessary).
See the assembly output on Compiler Explorer.
Gist on Github
If you have any thoughts, feel free to send them my way: