Is a C++ compiler allowed to emit different machine code compiling the same program?

前端 未结 4 1529
夕颜
夕颜 2020-12-06 13:17

Consider a situation. We have some specific C++ compiler, a specific set of compiler settings and a specific C++ program.

We compile that specific programs with that

4条回答
  •  情深已故
    2020-12-06 13:42

    According to the as-if rule in the standard, as long as a conforming program (e.g., no undefined behavior) cannot tell the difference, the compiler is allowed to do whatever it wants. In other words, as long as the program produces the same output, there is no restriction in the standard prohibiting this.

    From a practical point of view, I wouldn't use a compiler that does this to build production software. I want to be able to recompile a release made two years ago (with the same compiler, etc) and produce the same machine code. I don't want to worry that the reason I can't reproduce a bug is that the compiler decided to do something slightly different today.

提交回复
热议问题