Any computation is possible at compile-time with C++ template metafunctions. Therefore, I was considering, if the following were possible:
void my_function(char const* string_ptr)
{
switch (hash_function(string_ptr))
{
case hash_metafunction<"yoohooo">::value:
...
break;
case hash_metafunction<"woooooo">::value:
...
break;
...
}
}
Can you give leads as to where to find code (library) for both the hash function and template metafunction. If none such library exists, can you give hints on how I might roll the template metafunction myself? I am particularly worried about the char const* parameter to the template metafunction. Maybe some preprocessor magic is possible?
How about a
constexprfunction? Of course implementing that hash could be a pain. You’ll have something like this:The
hash_metafunctionfunction would be executed on compile-time.Edit: This is a naive implementation, which basically converts the input string to a
uint64_t:Live demo here.
Edit: I’ve implemented a compile time MD5, you can find the source code here. In order to use it, do the following:
This prints out the hash: “b8b4e2be16d2b11a5902b80f9c0fe6d6”.