C++ Logo

std-proposals

Advanced search

Re: [std-proposals] A draft for modern switch

From: Sebastian Wittmeier <wittmeier_at_[hidden]>
Date: Wed, 21 May 2025 09:51:44 +0200
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_at_[hidden]> Gesendet:Mi 21.05.2025 04:15 Betreff:Re: [std-proposals] A draft for modern switch An:Nikolay Mihaylov <nmmm_at_[hidden]>; CC:Zhao YunShan <dou1984_at_[hidden]>; std-proposals_at_[hidden];   switch is a concept familiar to all programmers. Most languages use switch with strings, including JS, Golang, PHP, etc. match seems a bit unusual in comparison.   Indeed, using a constexpr hash function can construct a perfect hash, but my idea is to have the compiler handle this task rather than doing it manually myself.     Regards   

Received on 2025-05-21 07:59:14