C++ Logo

std-discussion

Advanced search

Re: Proposal for switch statement extension

From: Jefferson Carpenter <jeffersoncarpenter2_at_[hidden]>
Date: Sun, 13 Sep 2026 06:30:58 -0500
On Sun, Sep 13, 2026 at 12:16 AM Michael Park <mcypark_at_[hidden]> wrote:
>
> > complete with a return statement
>
> With immediately-invoked-lambdas, you literally get a return statement that yields the value
> as the result of match. With do expressions, you get do_return to yield the value as
> the result of match and return returns from the surrounding function which is a very useful
> functionality.

This is the key point for me. It's not going to be good enough to use
immediately-invoked lambdas. If you mean for instance

match (x) {
  case { MyClass myclass } => ([&](){
    return myclass.num;
  })()
}

that's a lot of extra punctuation to type just to get a scope. I'd
like to use this feature more like a switch statement as I intend to
introduce new variables and compute things before returning the
result.

Maybe your "match" feature is not what I'm looking for Michael, but if
it did allow me the bare minimum of typing curly braces { and }
instead of the strings "([&](){" and "})()" then I think it would
almost be viable.


> > It needs to let me bind at least a single variable (the value inside the variant)
>
> { MyClass myclass } and { int n } are each binding the value inside the variant.

This is also too much typing. The { and } occur in every case.

This is because "match" is for general pattern matching, not for
visiting variants specifically. It's good at things like matching
optional "{} => ..." and "{a} => ..." and other specific algebraic
data types, but std::variant isn't actually a regular sum type, it's
implemented using a struct. So if you use a pattern matching feature
for inspecting values, you naturally have to unpack it using curly
braces (unless you add even more features so that you can elide them).


Thanks,
Jefferson

Received on 2026-09-13 12:03:59