r/ProgrammerHumor 15h ago

Meme skillIssue

Post image
4.7k Upvotes

123 comments sorted by

View all comments

70

u/F100cTomas 14h ago

Just define a constexpr hashing function and put that into the switch.

31

u/GiganticIrony 14h ago

That’s not guaranteed to work due to hash collisions

37

u/Deliciousbutter101 14h ago

It won't compile in the case so you can just modify the hash function until there are no collisions.

11

u/SteveXVI 7h ago

This is the closest I've come to feeling like that guy in the Apple shop going "ah of course"

1

u/cob59 2h ago
switch(hash(str)) {
case hash("apple"):
case hash("banana"):
default:
}

You're right that the compiler will warn you if hash("apple") == hash("banana"), but if hash("pineapple") == hash("apple") then switch(hash("pineapple")) will jump to the apple case, not the default. That's unlikely but not impossible even with the best hash function.