C++ Logo

std-proposals

Advanced search

Re: [std-proposals] switch class

From: Peter Bindels <dascandy_at_[hidden]>
Date: Fri, 13 Feb 2026 11:50:45 +0100
You reinvented pattern matching.

Try reading up on the pattern matching proposals.

wg21.link/p2688
wg21.link/p1371
p0095, p1260, p1308, p2392, p2411, p3527, p3572, and many more. Also maybe the Hagenberg EWG wiki for the discussions there, and why it is not in 26.

Feb 13, 2026 11:29:01 AM Frederick Virchanza Gotham via Std-Proposals <std-proposals_at_[hidden]>:

> I've already started implementing "switch class" in the GNU g++ compiler.
>
> Basically when you write the following:
>
> std::string s("monkey");
> switch class ( s )
> {
> default:
> DoSomethingElse();
> case "frog":
> break;
> case "monkey":
> DoSomething();
> }
>
> It gets transformed into:
>
> std::string s("monkey");
> switch (0)
> {
> case 0:
> auto &&__sw = (s);
>
> if (__sw == "frog") goto __case1;
> if (__sw == "monkey") goto __case2;
> goto __default;
>
> __default:
> DoSomethingElse();
> __case1:
> break;
> __case2:
> DoSomething();
> }
>
> As you can see in the above code, each 'case <expr>' gets evaluated in
> order. So if 's' is equal to "frog", then the expression "monkey"
> never gets evaluated. The expression can be anything -- including the
> invocation of a function, for example:
>
> std::string s("monkey");
> switch class ( s )
> {
> case Func1():
> ReportFault();
> break;
> case Func2():
> DoSomething();
> }
>
> It will be a few more days before I have this working but I just
> thought I'd ask now if anyone thinks it should be tweaked in any way?
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2026-02-13 10:50:53