G++ accepts this code and it behaves as I'd expect it to:
#include <cassert>
void example (int value, bool condition) {
switch (value) {
case 0:
if (condition) {
case 1:
assert(condition || value == 1);
} else {
assert(!condition && value == 0);
}
assert(value == 0 || value == 1);
}
}
int main () {
example(0, false);
example(1, false);
example(0, true);
example(1, true);
}
Maybe this is a silly basic question but, code smell aside, is it valid C++ to put a case
label inside an if...else
block, and will all well-behaved compilers correctly generate code that will jump over the else
block when entered through case 1
?
As far as C++ is concerned (draft N3936):
What you are doing is technically ok, of course that doesn't mean you should.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With