V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
iOS 开发实用技术导航
NSHipster 中文版
http://nshipster.cn/
cocos2d 开源 2D 游戏引擎
http://www.cocos2d-iphone.org/
CocoaPods
http://cocoapods.org/
Google Analytics for Mobile 统计解决方案
http://code.google.com/mobile/analytics/
WWDC
https://developer.apple.com/wwdc/
Design Guides and Resources
https://developer.apple.com/design/
Transcripts of WWDC sessions
http://asciiwwdc.com
Cocoa with Love
http://cocoawithlove.com/
Cocoa Dev Central
http://cocoadevcentral.com/
NSHipster
http://nshipster.com/
Style Guides
Google Objective-C Style Guide
NYTimes Objective-C Style Guide
Useful Tools and Services
Charles Web Debugging Proxy
Smore
ShikiSuen
V2EX  ›  iDev

Cpp 有没有 case when 这样的语句?

  •  
  •   ShikiSuen · 2023-01-31 19:39:22 +08:00 · 2522 次点击
    这是一个创建于 451 天前的主题,其中的信息可能已经有所发展或是发生改变。

    Swift 有 case _ where (bool):,CSharp 有 case var _ when (bool):,那么 Cpp 该怎么写呢?

    (假设讨论的是 Cpp17 ,如果 Cpp17 之后的某个版本才可以的话也可以讨论。)

    ysc3839
        1
    ysc3839  
       2023-01-31 20:27:03 +08:00 via Android
    判断类型?编译期用 if constexpr std::is_same_v 。运行期用 std::variant 和 std::holds_alternative 。
    ShikiSuen
        2
    ShikiSuen  
    OP
       2023-01-31 23:55:16 +08:00
    @ysc3839 欸?这两个在 switch 语句当中能用吗?

    P.S.: 我这个讨论串问的是在 switch case 当中的使用情形。
    fatestigma
        3
    fatestigma  
       2023-02-01 08:31:19 +08:00
    感觉像是说 Pattern Matching ,C++23 的一个提案: https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/p1371r3.pdf

    inspect (v) {
    pattern1 if (cond1) => { stmt1 }
    pattern2 => { stmt2 }
    // ...
    };
    ShikiSuen
        4
    ShikiSuen  
    OP
       2023-02-01 16:43:05 +08:00
    @fatestigma 感谢您的分享。
    看来 C++ 在语言特性上真的落后太多,到现在才想到要引入这种改进。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1029 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 19:33 · PVG 03:33 · LAX 12:33 · JFK 15:33
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.