Wiadomość napisana przez Sebastian Wittmeier via Std-Proposals <std-proposals@lists.isocpp.org> w dniu 21 maj 2025, o godz. 09:59:
AW: [std-proposals] A draft for modern switch First what is wrong with the existing suggestions?
Here you have a comparison of three proposals:https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2025/p2688r5.html#matching-strings
Many languages call their "switch" differently.
You still have not specified, whether you suggest that the compiler can assume
- the given string is one of the options or
- the given string can be any string
- the given string can be any string only if a default case is provided
Depending on it, those shorter hash solutions or comparing the first few characters would work or not.
Normally switch is a simple function / logic. If it now could compare more complex objects, this should be clearly defined.
All other programming languages allowing switch (or which name they chose) to do string comparisons, do a full (non-optimized to the selection) comparison, as if you do a chain of if/else.
If this is what you also propose, then perhaps you should extend it to any custom type, too. Why should strings be something special?
MyObject o(42);
...
switch (o) {
case o2:
...
break;
case MyObject(3):
...
break;
case MyObject(5):
...
break;
default:
...
}
-----Ursprüngliche Nachricht-------
Von: Zhao YunShan via Std-Proposals <std-proposals@lists.isocpp.org>
Gesendet: Mi 21.05.2025 04:15
Betreff: Re: [std-proposals] A draft for modern switch
An: Nikolay Mihaylov <nmmm@nmmm.nu>;
CC: Zhao YunShan <dou1984@163.com>; std-proposals@lists.isocpp.org;
switchis a concept familiar to all programmers. Most languages useswitchwith strings, including JS, Golang, PHP, etc.matchseems a bit unusual in comparison.
Indeed, using a
constexprhash function can construct a perfect hash, but my idea is to have the compiler handle this task rather than doing it manually myself.
Regards
Std-Proposals mailing list
Std-Proposals@lists.isocpp.org
https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals