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

南楼画角 提交于 2019-12-22 06:33:36

问题


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:


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.



来源:https://stackoverflow.com/questions/58631602/is-there-any-problem-in-jumping-into-iffalse-block

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!