2012-07-21

Interesting syntax usage I just discovered:


#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.

6 comments:

Anonymous said...

This is, in both C and C++ a horrible implementation. There is no reason to do this, it looks like a bug you are trying to take advantage of and if you were writing code for me and intentionally produced this I would fire you.

Both C and C++ have tonnes of undefined behaviours, a great deal of which is documented and a GOOD engineer will look to remove this crap to ensure portability and solid maintenance. There is nothing interesting about screwing up the parser and producing code no one can read.

SPrabhu said...

+1 for Anonymous.

Relying on these undocumented quirks will backfire resulting in subtle bugs when they change this behaviour or you use a different compiler.

Unknown said...

I believe that this is perfectly allowed by the C/C++ standard. However.. it can be considered as a bad practise.

A nice implementation is the Duff machine though.

Unknown said...

Thanks for the feedback, Anonymous, Sachin and Riemer. I have updated the blog accordingly.
Regards,
-/renji

MGH said...

Oh my crap! I was actually able to mostly predict the output after staring at it for about 5 mins. The part that I was ambiguous about was the block after 'case 1:'.

Renji, for the most part, thought, I think that the world where such a hack would be appreciated is long gone. Perhaps I did experience a tail of it, when working on a device whose memory was limited to 256K :)

I'd put you on a PIP if I saw this code and you would be convinced that you needed to be on that PIP!

Cheers

--
mgh

MGH said...

Oh my crap! I was actually able to mostly predict the output after staring at it for about 5 mins. The part that I was ambiguous about was the block after 'case 1:'.

Renji, for the most part, thought, I think that the world where such a hack would be appreciated is long gone. Perhaps I did experience a tail of it, when working on a device whose memory was limited to 256K :)

I'd put you on a PIP if I saw this code and you would be convinced that you needed to be on that PIP!

Cheers

--
mgh