C++ Logo

std-proposals

Advanced search

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

From: Zhao YunShan <dou1984_at_[hidden]>
Date: Sun, 25 May 2025 22:02:41 +0800 (CST)
After reading [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?



Received on 2025-05-25 14:02:49