C++ Logo

std-proposals

Advanced search

[std-proposals] switch class

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Fri, 13 Feb 2026 10:30:35 +0000
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?

Received on 2026-02-13 10:28:59