C++ Logo

std-discussion

Advanced search

Re: Proposal for switch statement extension

From: Jefferson Carpenter <jeffersoncarpenter2_at_[hidden]>
Date: Thu, 10 Sep 2026 17:16:35 -0500
Actually the two are more or less the same. I think it would be much
more readable to allow a statement-seq ending with a retrun statement
to provide the value like a lambda expression than to try to construct
matchers and extractors for whatever intermediate values you need.

On Thu, Sep 10, 2026 at 5:07 PM Jefferson Carpenter
<jeffersoncarpenter2_at_[hidden]> wrote:
>
> I definitely agree that matchers and extractors should not be language
> features. Implementing these is like trying to invert a function.
>
> If the inspect expression is non-void, can a statement-seq be used
> with a return statement to implement the value?
>
> - Jefferson
>
> On Thu, Sep 10, 2026 at 4:30 PM Jefferson Carpenter
> <jeffersoncarpenter2_at_[hidden]> wrote:
> >
> > That looks good.
> >
> > On Thu, Sep 10, 2026 at 3:58 PM Jens Maurer <jens.maurer_at_[hidden]> wrote:
> > >
> > >
> > > Please read the pattern matching proposal:
> > >
> > > P1371 Pattern Matching
> > > https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/p1371r3.pdf
> > >
> > > Jens
> > >
> > >
> > > On 9/10/26 21:05, Jefferson Carpenter via Std-Discussion wrote:
> > > > Hi all,
> > > >
> > > > I'd like to propose some "syntactic sugar" that ties the switch
> > > > statement language feature to the std::variant standard library type.
> > > > The idea is to make the process of visiting a variant much cleaner and
> > > > simpler.
> > > >
> > > > Basically it has to support the following code transformation:
> > > >
> > > > switch (x) { // Assume x is of type std::variant<MyClass, int>
> > > > case (MyClass myclass): { a(myclass); break; }
> > > > case (int n): { b(n); break; }
> > > > }
> > > >
> > > > to the following:
> > > >
> > > > struct Unnamed {
> > > > void operator()(MyClass const& myclass) { a(myclass); }
> > > > void operator()(int const& n) { b(n); }
> > > > };
> > > > Unnamed{}(x);
> > > >
> > > > so that the code jumped to from each case is transformed into a
> > > > function call operator overload. A braced-enclosed code block ending
> > > > in a break statement may be required after each case "label" even when
> > > > there are no variables declared in the body or if fallthrough were
> > > > desired so that the code that must be moved is outlined and easy to
> > > > move.
> > > >
> > > > This could potentially be extended to other non-std::variant types if
> > > > there were a mechanism like std::get, but I haven't explored this at
> > > > all.
> > > >
> > > >
> > > > This would be very useful to me, as I've written a lot of
> > > > programming-language-like programs and use std::variant extensively to
> > > > represent expression trees.
> > > >
> > > > It would make C++ much more viable for programs like this to have the
> > > > code for visitors easier to read and type than calls to std::visit.
> > > >
> > > >
> > > > Warm Regards,
> > > > Jefferson Carpenter
> > >

Received on 2026-09-10 22:49:36