Dividing by zero in a constant expression

后端 未结 5 457
有刺的猬
有刺的猬 2021-02-02 06:47

My toy compiler crashes if I divide by zero in a constant expression:

int x = 1 / 0;

Is this behaviour allowed by the C and/or C++ standards?

5条回答
  •  梦毁少年i
    2021-02-02 07:08

    How the compiler should behave is unrelated to the value of the expression. The compiler should not crash. Period.

    I imagine that a pedantic implementation, given an expression like this, would compile to code that will execute 1/0 at run time, but I don't think that would be seen as a good feature.

    So the remaining space is that the compiler should decline to compile it, and treat it as some class of source code error.

提交回复
热议问题