Switch-case for user provided datatypes
This issue has been nagging me for a long time. Assuming I'm solely interested in getting switches to work for strings (std::string), would it make sense to the people here to propose something more general aka custom data types in switches or would std::string would be sufficient? I'm asking because using std::map or hashing every item in the case statement doesn't make sense to me. Moreover, this is a "basic" enough task that would help almost every C++ dev and I'm pretty sure a lot of shops have something that emulates this functionality internally.
Currently, even with all C++ features that I can think of, the following is the best I can do to get a switch case on custom datatype (assuming constexpr std::hash<T>::operator(const T&))
switch(auto hash, name = std::tuple{std::hash<std::string>{}, getUserName()};
hash(name)) {
case hash("cpp11"): std::cout << "Looks promising"; break;
default:
case hash("cpp14"): std::cout << "But feels boring";
}
If switch were to take an optional lambda that would change the landscape significantly:
switch(auto name = getUserName(); name; std::hash<std::string>{}) {
case "cpp11": std::cout << "Looks cleaner"; break;
default:
case "cpp14": std::cout << "Beginners can understand it properly";
}
That's of course a pretty lame duck syntax, since it fails to be easy to understand when there's no declaration:
switch(name; hash) { // both variables exist
case "cpp11": std::cout << "It might be possible to make a different overload"; break;
default:
case "cpp14": std::cout << "Treating 'init-statement; condition' as different from 'condition; functor' in language-spec";
}
I saw that something similar was possible with the proposed Pattern Matching Syntax (c++23), however, this isn't really pattern matching and C# has shows that language support for at least "string-literals" is possible and can be faster than if statement chain External link to an old webpage.
Even if there's no support for custom-data types, would it make sense to have C#esque support for string literals in switch statements? Or support for any data structure by using std::hash (making it implicit and removing the need for std::hash if the data type can just be cast into std::size_t, the return type of std::hash)?
Or no? Let's keep using enum and maps and if ladders.....
Posted Dec 9, 2019, 8:06 AM UTC
Hashing alone seems like an unreliable method. What if two strings have the same hash?
I don't think
switchwill change. We'll just have to wait for pattern matching.Well, you could detect the hash collisions at compile time, since I'd expect every case to be a compile time constant.
You might have a hash collision with the string that you're switching on.
I wouldn't want str="snow" to print "Boring!" just because "snow" and "rain" happens to have the same hash value.
Ah, true. I didn't think of that case. Although I think switching on the hash first and then doing a vlaue comparison would still have a runtime benefit in a lot of cases. Although for small switches it is probably most performant to switch on the first character, that differs and then doing a full comparison or something like that.
Yeah, but this becomes tedious if you need to do it by hand.
The C# implementation linked does a hash then an operator==() to check.
Obviously the compiler can detect two of the case values having the same hash at compile-time.
This would give a compile-time error for the identical case values, but you are right. The std::hash implementation is implementation-dependent, so this is not portable code.
No, since "snow" is not one of the case values.
That's the implicit price of a hash function. You can use 2 hash to reduce the chances but never remove it. Though even with 1, the chances are pretty low but non existent.
Yeah, and any collisions are detected and reported at compile time so there is a way the compiler can add a salt to get the perfect hash function to prevent collisions. We already do this in our implementation (detection and custom salt, not integration with compiler)