Can code that will never be executed invoke undefined behavior?

后端 未结 9 1570
予麋鹿
予麋鹿 2020-11-27 17:52

The code that invokes undefined behavior (in this example, division by zero) will never get executed, is the program still undefined behavior?

int main(void)         


        
9条回答
  •  有刺的猬
    2020-11-27 18:19

    I think it still is undefined behavior, but I can't find any evidence in the standard to support or deny me.

    I think the program does not invoke undefined behavior.

    Defect Report #109 addresses a similar question and says:

    Furthermore, if every possible execution of a given program would result in undefined behavior, the given program is not strictly conforming. A conforming implementation must not fail to translate a strictly conforming program simply because some possible execution of that program would result in undefined behavior. Because foo might never be called, the example given must be successfully translated by a conforming implementation.

提交回复
热议问题