C++ Logo

std-proposals

Advanced search

Re: [std-proposals] A draft for modern switch

From: Dejan Milosavljevic <dmilos_at_[hidden]>
Date: Sun, 25 May 2025 18:48:15 +0400
There are numerous attempts to add lex. Even myself has tried too.
Adding a match as described will make a serious jump from a well known
switch.

What is lex see: https://en.wikipedia.org/wiki/Lex_(software) )

On Sun, May 25, 2025 at 6:02 PM Zhao YunShan via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> *After reading [**https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2025/p2688r5.html
> <https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2025/p2688r5.html> ],
> I have a few questions I’d like to get clarified.*
>
> *First, there is no regex (regular expression) matching, as match is a
> supperset of switch. For example:*
> string str = "";
> str match
> {
> case /^http:\/\/.+/:
> f1();
> break;
> case /^tcp:\/\/.+/:
> f2();
> break;
> default:
> break;
> }
>
> *Then, in what scenarios can matching tuples be used? **Can **switch be
> used as a replacement in this scenarios?*
>
> *Next,**How to Handle Multiple Type Operators When Matching Variants? For
> example:*
> *class X*
> *{*
> * operator int(){return 0;}*
> * operator std::string() { return ""; }*
> *};*
> *X x;*
> *x match {*
> * int: let i => printf("%d", i);*
> * std::string : let str => printf("%s", str.c_str());*
> *}*
>
> *Finally, when matching nested structures:*
> *using Command = variant<Quit, Move, Write, ChangeColor>;*
> *int DoCommand(Command& cmd);Command& GetCommand();If GetCommand is a
> function from a third-party library, how can my program determine which
> specific Command type is returned?*
>
>
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>

Received on 2025-05-25 14:48:33