
Birch Grove is just 40 minutes from Bishkek. Although I was concerned about the strong, foggy weather, it was an amazing opportunity for photography!
C++ is a powerful language, and I genuinely love it, but sometimes, even in modern versions, it lacks some surprisingly simple features. One such missing feature is switch on
Why Doesn’t C++ Support switch on strings? Because “you only pay for what you use,” which is the standard C++ mantra. The switch statement in C++ relies on integral types. Under the hood, it works by converting the case values into jump table indices for efficient execution. But
case "topology":
// Handle network topology
break;
case "broadcast":
// Handle network broadcast
break;
}
So… how do we work around this? We need a way to turn strings into something
uint64_t hash = 0;
for (char chr : str) {
hash = (hash * 131) + chr;
}
return hash;
}
constexpr uint64_t operator"" _hash(const char* str, size_t len) {
return hash(std::string_view(str, len));
}
And, no magic, we have
case "topology"_hash: {
// Handle topology case
break;
}
case "broadcast"_hash: {
// Handle broadcast case
break;
}
default:
// Handle unknown case
break;
}
There are some downsides to using this approach, specifically regarding hash collisions. Hashing introduces a small, but non-zero, risk of collisions. The proposed hash function, which multiplies the current hash by 131 and adds each character, is sufficiently effective for this use case. It provides a fast, compile-time method to map short, distinct string literals to unique integer values.
The choice of 131 as a multiplier contributes to achieving a reasonable distribution of values, thus reducing the likelihood of collisions, especially for a limited set of keywords like “topology,” “broadcast,” and others. The number 131 is commonly used in simple hash functions because it is a prime number. This helps to spread out the hash values more evenly across the available space, minimizing clustering and improving distribution properties. Additionally, it is small enough to maintain fast calculations while ensuring sufficient entropy accumulation over short strings.
Since the hash is computed at compile-time (using
It’s ridiculous that in 2025, we still have to hack our way around something as basic as