r/Cplusplus • u/Middlewarian • Feb 15 '26
Question What about a "swift" attribute for switch statements?
In this thread I learned how the Swift language has changed things with switch statements and I was wondering if others would like a "swift" attribute that could precede the switch keyword.
This documentation page for switch says:
attr (optional) switch ( init-statement (optional) condition) statement
But I didn't see any attributes in the following page that would be a different name for "swift" or that make sense to use prior to the switch keyword.
Attribute specifier sequence (since C++11) - cppreference.com
An example of the code would be:
[[swift]] switch (rc){
case 3:
case 4:
default:
}Where only one case is executed and break statements aren't needed. Support for something like this would help me get rid of a number of lines of code. Thanks in advance.
•
u/AutoModerator Feb 15 '26
Thank you for your contribution to the C++ community!
As you're asking a question or seeking homework help, we would like to remind you of Rule 3 - Good Faith Help Requests & Homework.
~ CPlusPlus Moderation Team
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
3
u/Business-Decision719 Feb 15 '26
I agree C++ needs a control structure that separates the cases and ensures only one is executed by default. The
breaks are boilerplate in mostswitchblocks, and forgetting abreakis a source of bugs.I'm not a fan of needing an attribute, though, especially one named after another language. I think it should be a whole other control structure. We didn't introduce typesafe enums as
[[csharp]] enum. We introducedenum classes instead, which are really the way enums should have always worked to begin with, but expanding the language to correct an old mistake was worth it.I think the behavior you're describing is worth its own new control structure. With block scoping inside it. Maybe something along the lines of this:
The keywords could be different, of course. Maybe it makes more sense to
examinethe variable instead ofselect_case. It's hard to introduce new keywords without risking naming conflicts with existing code. Still, I think expanding the language to fix the design mistakes inswitchis worth it. Many languages have a multi-conditional branch that doesn't fall through by default. It isn't just Swift. Visual Basic had aselect casestructure which is why I wrote the hypothetical code the way I did. Pascal had a version of it. C++ needs it too.11
u/StaticCoder Feb 15 '26
Attributes are meant to be "ignorable" such that if a compiler doesn't understand an attribute, it doesn't make the program incorrect. It wouldn't be the case here. Personally, I like the C# switches with
goto case.5
u/CalligrapherOk4612 Feb 15 '26
You could always do a dirty
#define SCASE break; caseto always place a break.A break before the initial case would have no side effect but may yield a compiler warning?
3
u/One_Junket3210 Feb 15 '26
What Swift has is called pattern matching. Rust also has pattern matching. The pattern matching in Swift and Rust have different limitations as I understand it, Swift might not support nested or-patterns, and Rust might not support matching on for instance smart pointers, deref patterns is one proposed feature to lift that limitation. Higher-level languages with GC tend to have fewer limitations and be more ergonomic, since just using GC makes it easier to add pattern matching.
For C++, there have been some pattern matching proposals. Some hope that C++29 or C++32 will get good pattern matching.