I am trying to get a string-based switch expression to work in C using a hash function. I've been able to get it to work with clean syntax using 'constexpr' with Clang/LLVM turned to C++, even though the code is C.
However, there are of course odd side effects of having it compile as C++, like lack of void* implicit casting which becomes really awkward.
So the question is how to solve this dilemma (without slapping the C11 committee upside their head for why this wasn't added to the C spec)
- Is there a way to get constexpr option turned on with C?
- Is there a way to get implicit void* casting turned on with C++?
- Is there another clean way to code this in C11/C99 that doesn't require recalculating hashes?
Here is my current example code:
constexpr uint64 cHash(char const* text, uint64 last_value = basis)
{
return *str ? cHash(text+1, (*text ^ last_value) * prime) : last_value;
}
void SwitchFunction(char const* text)
{
switch(Hash(text))
{
case cHash("first"):
break;
case cHash("second"):
break;
case cHash("third"):
break;
default:
break;
}
}