C++ Logo

std-proposals

Advanced search

Re: [std-proposals] switch class

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Sun, 22 Feb 2026 14:08:50 +0000
On Fri, Feb 13, 2026 at 10:50 AM Peter Bindels wrote:
>
> 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.



I have finished implementing "switch class" in the GNU g++ compiler.
Here's a GodBolt to try it out:

      https://godbolt.org/z/bnn94EGrE

In the right-hand pane, you can edit the command line argument from
'frog' to something else to see what effect it has.

The "switch class" statement is written as follows:

    switch class ( string_view(argv[1]) )
    {
    default:
        cout << "default\n";
    case "frog":
        cout << "frog\n";
    case "monkey":
        cout << "monkey\n";
    case "chocolate":
    case Func():
        ;
    }

And essentially the compiler treats it as though you had written the following:

    switch ( 0 )
    {
    case 0:
      auto &&expr = string_view(argv[1]);

      if ( expr == "frog" ) goto __case_frog ;
      if ( expr == "monkey" ) goto __case_monkey ;
      if ( expr == "chocolate" ) goto __case_chocolate;
      if ( expr == Func() ) goto __case_Func ;
      goto __case_default;

      __case_default:
          cout << "default\n";
      __case_frog:
          cout << "frog\n";
      __case_monkey:
          cout << "monkey\n";
      __case_chocolate:
      __case_Func:
        ;
    }

Note that this means that 'Func' will never get invoked if any of the
first three cases match.

Arthur pointed out to me about writing tests when adding new features
to compilers . . . I've added about 7 or 8 features now to my patched
GNU compiler, but I think I've broken some of the pre-exisiting tests.
So before I can get started on writing tests, I need to go through
those 7 or 8 features and fix the tests I broke. So I won't adding new
compiler features for a while until I've corrected all the broken
tests.

Received on 2026-02-22 14:07:26