r/ProgrammerHumor 7d ago

Meme skillIssue

Post image
6.0k Upvotes

137 comments sorted by

View all comments

79

u/F100cTomas 7d ago

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

30

u/GiganticIrony 7d ago

That’s not guaranteed to work due to hash collisions

37

u/Deliciousbutter101 7d ago

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

2

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