C++ Logo

std-proposals

Advanced search

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

From: Jeremy Rifkin <rifkin.jer_at_[hidden]>
Date: Thu, 22 May 2025 13:00:24 -0500
> 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.

Switch is older, and used in several languages. But pattern matching is by
no means unusual or rare. It's featured in C#, Rust, Python, Scala, Swift,
etc.

Usual or otherwise, it's pretty inarguably better language design and also
easier to teach than switch footguns.

P.s.: You're still sending emails with text inverted and that makes them
hard to read.

Jeremy

On Tue, May 20, 2025 at 9:15 PM Zhao YunShan via Std-Proposals <
std-proposals_at_[hidden]> wrote:

>
> 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
>
>
> At 2025-05-21 01:42:45, "Nikolay Mihaylov" <nmmm_at_[hidden]> wrote:
>
> my 5 cents
>
> The idea of switch is not to make the life of the programmer easier, the
> idea is to make a jump table so this can be compiled in 2-3 instructions.
>
> There is a new control flow statement called "match". lots of languages
> uses it already:
> https://www.php.net/manual/en/control-structures.match.php
>
> btw, you can use switch with strings, kind of, using constexpr hash
> function and assuming not hash collisions (manual perfect hash)
>
>
> https://stackoverflow.com/questions/650162/why-cant-the-switch-statement-be-applied-to-strings/46711735#46711735
>
> Regards
>
> Nikolay
>
> On Tue, May 20, 2025 at 8:29 PM Zhihao Yuan via Std-Proposals <
> std-proposals_at_[hidden]> wrote:
>
>> Are you looking for
>> https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3627.html ?
>>
>> --
>> Zhihao Yuan, ID lichray
>> The best way to predict the future is to invent it.
>> _______________________________________________
>>
>> On Tuesday, May 20th, 2025 at 4:38 AM, Zhao YunShan via Std-Proposals <
>> std-proposals_at_[hidden]> wrote:
>>
>>
>> In C++, the switch statement is a fundamental control-flow construct
>> originally designed to work only with integer types (int, char, enum,
>> etc.). However, in real-world development, programmers often need to handle
>> string-based (std::string) branching logic. While the standard syntax
>> does not natively support strings, well-structured design patterns can
>> still leverage switch-like behavior to replace lengthy if...else if chains,
>> significantly improving code readability and conciseness.
>>
>> #include <iostream>
>> #include <string>
>> int main()
>> {
>> std::string router;
>> switch (router)
>> {
>> case "cpp":
>> std::cout << "cpp router selected.";
>> break;
>> case "hpp":
>> std::cout << "hpp router selected.";
>> break;
>> default:
>> std::cout << "Unknown router type.";
>> break;
>> }
>> return 0;
>> }
>>
>>
>> By leveraging compile-time static analysis, switch statements can be
>> optimized into various efficient data structures:
>>
>> * Jump tables* for few cases,
>> * Balanced binary search* for many branches,
>> * Hash tables* where supported.
>>
>> This ensures optimal time complexity (*O(1)* to *O(log n)*) even with
>> large branch sets.
>>
>>
>> --
>> Std-Proposals mailing list
>> Std-Proposals_at_[hidden]
>> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>>
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>

Received on 2025-05-22 18:00:42