是编译器的行为未定义,与未定义的行为? [英] Is the behaviour of the compiler undefined, with Undefined Behaviour?

查看:204
本文介绍了是编译器的行为未定义,与未定义的行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我回答此< a href =http://stackoverflow.com/questions/32132574/does-undefined-behavior-really-permit-anything-to-happen>问题,我写道:


首先,需要注意的是,它不仅是用户程序的行为未定义,而是编译器的行为未定义。

First, it is important to note that it is not only the behaviour of the user program that is undefined, it is the behaviour of the compiler that is undefined.

但有在评论中有不同意见,因此我想在这里提出问题:

But there was disagreement in a comment, so I want to ask the question here:

如果源代码包含未定义的行为,它只是被翻译的机器代码的行为是未定义的,或者是编译器的行为未定义?

标准定义抽象机器的行为(1.9):

The standard defines the behaviour of an abstract machine (1.9):


这个国际标准中的语义描述定义了一个参数化的非确定性抽象
机器。本国际标准不要求符合实施的结构。
特别地,它们不需要复制或模拟抽象机的结构。相反,符合
实现需要模拟(仅)抽象机器的可观察行为,如下面的
所述。

The semantic descriptions in this International Standard define a parameterized nondeterministic abstract machine. This International Standard places no requirement on the structure of conforming implementations. In particular, they need not copy or emulate the structure of the abstract machine. Rather, conforming implementations are required to emulate (only) the observable behavior of the abstract machine as explained below.

也许问题是,如果编译器是该机器的一部分,如果是,如果该部分允许以未定义的方式行为?

Maybe the question is if the compiler is a part of that machine, and if yes, if that part is allowed to behave in an undefined way?

这个问题的一个更实际的版本是:

假设编译器在所有控制路径上找到UB时会崩溃或不产生输出,就像在这个程序中:

A more practical version of this question would be:
Assume a compiler would crash or not produce any output when it finds UB on all control paths, like in this program:

int main() {
    complex_things_without_UB();
    int x = 42;
    x = x++;  //UB here
    return x;
}

但是否则会始终产生正确的二进制文件。这仍然是一个符合标准的编译器?

but otherwise it would always produce correct binaries. Would this still be a standard-compliant compiler?

推荐答案

C ++标准定义了代码的行为,编译器。因此,引用编译器的未定义行为并没有什么意义 - 它从来没有被很好地定义。唯一的要求是它生成符合代码的标准指南的实现。 如何它是一个实现细节。

The C++ standard defines behavior for code, it doesn't define behavior for the compiler. As such, it doesn't really make sense to refer to undefined behavior of the compiler -- it was never well-defined to begin with. The only requirement is that it produces an implementation that conforms to the standard guidelines for the code. How it does this is an implementation detail.

这篇关于是编译器的行为未定义,与未定义的行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆