Unfortunately, I know of no way to do exactly what you want.
Are there any restrictions you can put on the strings? Like number of characters? If you can limit it to 1-8 characters, you can do something like this:
template <char Ch1, char Ch2 = '\0', char Ch3 = '\0', char Ch4 = '\0', char Ch5 = '\0', char Ch6 = '\0', char Ch7 = '\0', char Ch8 = '\0'>
struct string_hash {
static const uint64_t value =
(static_cast<uint64_t>(Ch1) << 56) |
(static_cast<uint64_t>(Ch2) << 48) |
(static_cast<uint64_t>(Ch3) << 40) |
(static_cast<uint64_t>(Ch4) << 32) |
(static_cast<uint64_t>(Ch5) << 24) |
(static_cast<uint64_t>(Ch6) << 16) |
(static_cast<uint64_t>(Ch7) << 8) |
(Ch8);
};
which basically, at compile time stuff up to 8
characters into a uint64_t
. Usage would look like this:
const uint64_t x = string_hash<'T', 'e', 's', 't'>::value
This will create a compile time numeric value (can be used in a switch
and all that goodness) unique to each string 1-8 chars long. Unfortunately, the only big downside is that you can't write it as a string literal, you need to write it as a list of char
s