Mathias Gaunard
On Sun, 10 Nov 2019 at 14:16, Phil Endecott via Boost
wrote: Specifically thinking about strings, there are numerous ways that it could be done. Starting with
Even for integers there are multiple ways it could be done. It's up to the compiler to decide what is best.
Yes, it's important to remember that anything that converts to a conventional switch statement (for example, by hashing the string) then relies on how the compiler chooses to implement it. There are probably different implementations for sparse vs. dense case values, for example. If the compiler turns a sparse switch statement into an if-else-if chain, you might as well have an if-else-if chain in the first place (in terms of performance).
(5) hashing, and then checking:
switch (hash(s)) { case "aa"HASH: if (s=="aa") f(); break; case "ab"HASH: if (s=="ab") g(); break; case "xy"HASH: if (s=="xy") h(); break; }
That code doesn't really need to check, if your hashes collide the compiler will reject the code.
You need to check if s might not match any of the case labels but might collide with them. Regards, Phil.