Is there any problem in jumping into if(false) block?

233 views Asked by At

I've read already a couple times (e.g. here Compiler: What if condition is always true / false) that any decent c++ compiler will opt-out something like

if(false)
{
 ...
}

But what if there is an intentional jump into this if(false) block. I'm having something like this in mind

#include <iostream>

void func(int part){
    switch (part) {
    case 0:{
        if(false)
            case 1:{std::cout << "hello" << std::endl;}
        break;
    }
    default:
        break;
    }
}

int main()
{
    func(0);
    func(1);
    return 0;
}

Is any decent c++ compiler going to respect the jump or will there eventually going to be some problems with opting-out?

1

There are 1 answers

3
hyde On BEST ANSWER

The code doesn't appear to be Undefined Behavior. Therefore any optimizations are not allowed to produce any effects which would affect the behavior of the code.

Note: Related to this kind of code, one thing you are not allowed to do is "goto" over definitions of local variables. But this code doesn't do that, so no problem.

Another note: If you have this kind of code in a "real" (not toy, experiment, obfuscation exercise etc) program, you should really refactor it into something which doesn't elicit quite so many WTFs from anybody reading the code.