根据标准,不良形式的程序需要什么样的C ++编译器? [英] What is the C++ compiler required to do with ill-formed programs according to the Standard?

查看:141
本文介绍了根据标准,不良形式的程序需要什么样的C ++编译器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++ 03标准将格式良好的程序(1.3.14 [defns.well.formed])定义为


根据语法规则,可诊断语义规则和单一定义规则(3.2)构建的C ++程序


它还将不合格的程序(1.3.4 [defns.ill.formed])定义为


输入到不是格式良好的程序(1.3.14)的C ++实现


if X then the program is ill-formed例如(2.13.1 / 3):



但是我还没有找到什么类型的翻译单元C ++实现需要与不良形式的程序。



假设我有一个错误的程序。现在是什么?



是C ++实现需要做什么具体的当遇到一个不成形的程序或C ++实现行为只是未定义?

解决方案


是C ++实现需要做特定的事情时遇到一个不成形的程序或C ++实现行为undefined?


如果标准没有另外指定,实现应发出诊断消息警告)。但是,对于某些违规情况,标准明确指定不需要诊断。在这种情况下,程序是错误的,但实现不需要告诉用户 - 通常,因为这样做在一般情况下太困难了。



关于单一定义规则,例如,参见C ++ 11标准的第3.2 / 4段:


每个程序都应包含一个定义在该程序中使用odr的每个非内联函数或变量
; 无需诊断


有关违反规则的实施要求, 2指定:





没有违反本国际标准的规则,合格的实施
在其资源限制内,接受并正确执行该程序。



- 如果程序包含违反任何可诊断规则
中描述的结构的出现,本标准作为有条件支持,当实现不支持该结构,
合格的实施应发布至少一个诊断消息



- 如果程序包含违反不需要诊断的规则,


同样相关的是第1.4 / 1段,它解释了什么是在上述段落中的可诊断规则


可诊断的规则集包括所有语法和语义规则在此国际标准中除了
之外,对于包含无需诊断或描述为
的明确符号的规则,导致未定义的行为


所以总结一下:如果一个错误的程序包含一个可诊断的违规未明确指定无需诊断,符合的实施应发出诊断。


C++03 Standard defines well-formed program (1.3.14 [defns.well.formed]) as

a C++ program constructed according to the syntax rules, diagnosable semantic rules, and the One Definition Rule (3.2)

It further defines an ill-formed program (1.3.4 [defns.ill.formed]) as

input to a C++ implementation that is not a well-formed program (1.3.14)

and the Standard is full of statements such as "if X then the program is ill-formed" for example (2.13.1/3):

A program is ill-formed if one of its translation units contains an integer literal that cannot be represented by any of the allowed types.

Yet I haven't found what the C++ implementation is required to do with ill-formed programs.

Suppose I have an ill-formed program. Now what?

Is the C++ implementation required to do something specific when it encounters an ill-formed program or is the C++ implementation behavior just undefined?

解决方案

Is the C++ implementation required to do something specific when it encounters an ill-formed program or is the C++ implementation behavior just undefined?

If the Standard does not specify otherwise, an implementation should emit a diagnostic message (error or warning). However, for some violations the Standard explicitly specifies that no diagnostic is required. In this case, the program is ill-formed, but implementations are not required to tell the user - usually, because doing so in the general case would be too difficult.

Concerning the One Definition Rule, for example, see Paragraph 3.2/4 of the C++11 Standard:

Every program shall contain exactly one definition of every non-inline function or variable that is odr-used in that program; no diagnostic required.

Regarding the requirements on implementations when encountering a violation of a rule, Paragraph 1.4/2 specifies:

[...]

— If a program contains no violations of the rules in this International Standard, a conforming implementation shall, within its resource limits, accept and correctly execute that program.

If a program contains a violation of any diagnosable rule or an occurrence of a construct described in this Standard as "conditionally-supported" when the implementation does not support that construct, a conforming implementation shall issue at least one diagnostic message.

— If a program contains a violation of a rule for which no diagnostic is required, this International Standard places no requirement on implementations with respect to that program.

Also relevant is Paragraph 1.4/1, which explains what is meant by "diagnosable rules" in the Paragraph quoted above:

The set of diagnosable rules consists of all syntactic and semantic rules in this International Standard except for those rules containing an explicit notation that "no diagnostic is required" or which are described as resulting in "undefined behavior."

So to sum it up: if an ill-formed program contains a diagnosable violation for which the Standard does not explicitly specify "no diagnostic required", conforming implementations should emit a diagnostic.

这篇关于根据标准,不良形式的程序需要什么样的C ++编译器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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