4 ms·
And in C++14 you can do away with the recursion and just use a `for` loop. Compile-time hashing of strings can be used to create a string-switch: constexpr
by Kristine1975 11y ago
And in C++14 you can do away with the recursion and just use a `for` loop.
Compile-time hashing of strings can be used to create a string-switch:
constexpr unsigned int hash(const char* string)
{
// hash the string
...
}
And then:
switch(hash(some_string))
{
case hash("Hacker News"):
break;
case hash("Y-Combinator"):
break;
default:
break;
}
If there is a collision, you get a compile-time error due to a duplicate `case`.
- lokedhs 11y agoHow can you get a compile-time error if some_string has a hash collision?
- Kristine1975 11y agoGood point. You only get a compile-time error if the strings in the switch collide with each other.