Interesting syntax usage I just discovered:
In case you missed it, note the "if(false)" at the end of the "case 0:"
#include <iostream>
void fn(int x) {
switch(x) {
case 0:
std::cout << "execute only if x == 0" << std::endl;
if(false)
case 1:
{
std::cout << "execute only if x == 1" << std::endl;
}
std::cout << "execute if x == 0 or x == 1" << std::endl;
break;
case 2:
std::cout << "execute only if x == 2" << std::endl;
break;
}
}
int main(int argc, char* argv[]) {
fn(0);
std::cout << "*****" << std::endl;
fn(1);
std::cout << "*****" << std::endl;
fn(2);
return 0;
}
In case you missed it, note the "if(false)" at the end of the "case 0:"
renji@shrine:~/tmp$ g++ test.cpp && ./a.out
execute only if x == 0
execute if x == 0 or x == 1
*****
execute only if x == 1
execute if x == 0 or x == 1
*****
execute only if x == 2
Something loosely similar to the trickery used in Duff's Device, I guess. This can be used when 2 cases of a switch have different initial code and the same common code, although using it in production code will decrease readability of the code.