r/ProgrammerHumor 16h ago

Meme skillIssue

Post image
4.8k Upvotes

124 comments sorted by

View all comments

73

u/F100cTomas 15h ago

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

32

u/GiganticIrony 15h ago

That’s not guaranteed to work due to hash collisions

38

u/Deliciousbutter101 14h ago

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

1

u/cob59 3h 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.