switch( variable )
{
case 0: case 1: case 2: case 3: case 4:
{
// ...
break;
}
case 5: case 6:
{
// ...
break;
}
default:
{
// ...
}
}
switch( variable )
{
case 0 .. 4:
{
// ...
}
case 5, 6:
{
// ...
}
default:
{
// ...
}
}
There's only so much elaboration that could be added to the switch
statement before it stops offering any meaningful advantage in
convenience or readability over just manually writing the equivalent
sequence of if-elses.
std::string x = "AF-56";
int k = 90;
enum YesNo{ NO,YES } answer;
switch ( answer, k, x.substr(0,2) ) {
case NO, default : // enters here if answer is NO and not check any of the others
break;
case YES, 10r.80r, default : // enters here if answer is yes and if k is in range 10-90, forget the third parameter
// using UDL 'r' suffix and dot operator we may create a range that can compares thru operator == to a scalar
break;
case YES, 81, "XY" : // no surprises here
break;
case YES, 82r.Inf, "XZ" : // the range now is from 82 to infinity
break;
case YES, 90, "AA"r."AX"r : // ranges can be of std::strings too ( or const char* ), why not ?
// This is actually the case that will be reached by (YES, 90, "AF")
break;
case default:
}
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/574320af-c1ae-4676-883c-ae4e91fcd874%40isocpp.org.--
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-pr...@isocpp.org.
We can only add silently with case 5, 6:
I would very much like to see some changes to switch.I'd like something like:switch break (x) // cases break by default;{case 1: whatever();case 2: whatever();
}I'd also like, for at least this variation of switch, for a case to be a scope by default, such that is ok:So that variables can be declared without requiring more braces.switch break (x) // cases break by default;{case 1: int z = 1; whatever(z); // not an errorcase 2: whatever();
}I think this would make most switch statements a lot less verbose.It seems a lot of the flexibility of the classic switch just isn't required.So this switch could be used when it's not and the existing switch can be used when it is.switch break seems easy to teach to me. So why not?
> 1. There's no need for break, there's no fallthrough. Advantage: we can
> easily "continue" and "break" loops from inside the "switch".
Disadvantage: you can't do this:
case FOO:
...some code...
[[fallthrough]];
case BAR:
...more code...
break;
And yes, I've actually written code like that before.
--
Matthew
How do I get fallthru on case 1 to 2?etc();case 4:bar();case 3:foo();case 2:case 1:break by default is fine, how do I opt-out within switch break, or is it all or nothing?switch break(x)
{
}
switch( someinteger )
{
case 581: case 523: case 462: case 521: case 463: case 522: case 461: case 448: case 468: case 586:
case 509: case 481: case 510: case 472: case 473: case 493: case 595: case 484: case 430: case 453:
case 452: case 446: case 454: case 590: case 569: case 537: case 538: case 570: case 449: case 513:
case 520: case 519: case 487: case 417: case 406: case 556: case 444: case 557:
return 1;}
switch( someinteger )
{
case 581, 523, 462, 521, 463, 522, 461, 448, 468, 586, 509, 481, 510, 472, 473, 493, 595, 484, 430, 453,
452, 446, 454, 590, 569, 537, 538, 570, 449, 513, 520, 519, 487, 417, 406, 556, 444, 557:
return 1;
}
// inside some function
for( unsigned int i = 0; i < liLength; i ++ ){ switch( lsString[ i ] ) { case '0': case '1': case '2': ...etc... case 'a': case 'b': ... etc ... case 'X': case 'Y': case 'Z': case '[': case ']': case '(': case ')': case '$': case '@': case '.': case '_': case '=': break;
default: return false; }}
// inside some function
for( new i = 0; i < liLength; i ++ )
{
switch( lsString[ i ] )
{
case '0' .. '9', 'a' .. 'z', 'A' .. 'Z', '[', ']', '(', ')', '$', '@', '.', '_', '=': { }
default: return 0;
}
}
On quarta-feira, 9 de agosto de 2017 10:08:14 PDT Tony V E wrote:
> So you have a pointer s, which is just an int, right?
> And all the cases are compile/link time constants...
> I'm not sure that was valid C, but it worked. Many years ago.
>
> Tony
Something like this?
switch ("A") {
case "A":
return 0;
case "B":
return 1;
default:
return 2;
}
It doesn't compile in C: https://godbolt.org/g/m9Asnk (Clang) &
https://godbolt.org/g/Uy1pKW (GCC)
Even if it did, what do you think this would return? I'd say it's equally
correct to return 0 or 2.
On quarta-feira, 9 de agosto de 2017 13:17:07 PDT Tony V E wrote:
> yep, basically.
>
> Once upon a time some compiler compiled that, and I agree, I wouldn't
> guarantee what it returned.
> (I think the idea was that hitting the "A" case was an optimization, and
> case default was to do a strcmp())
Like multicharacter literals?
int v = 'abcd';
GCC and ICC warn about that by default. Clang only warns at -Wpedantic. MSVC
does not warn even at -W4.
That's not a switch, that's an if-else chain masquerading as a switch.
I would strongly
suggest adding a pattern-matching mechanism and leaving switch alone; switch has
semantics that don't fit into general matching, and other expectations
that a general
matching mechanism doesn't have, including recommendations against using switch
because switch does fall-through and a general matching mechanism
probably shouldn't.
std::string x = "AF-56";
enum YesNo{ NO,YES } answer = YesNo.YES;
switch megazord ( answer; int k = 90; x.substr(0,2) ) {
case NO; default : // enters here if answer is NO and not check any of the others
// Code
case YES; 10r.100r; "[_a-zA-Z][_a-zA-Z0-9]{0,30}"regex : // ranges and regular expressions, nice !
// Code
case YES; 10r.100r; "AA"r."AX"r : // ranges can be of std::strings too ( or const char* ), why not ?
// Code
case default:
}
How do I get fallthru on case 1 to 2?etc();case 4:bar();case 3:foo();case 2:case 1:break by default is fine, how do I opt-out within switch break, or is it all or nothing?switch break(x)
{
}