26

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

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

它进一步将格式错误的程序(1.3.4 [defns.ill.formed])定义为

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

并且标准中充满了诸如“如果 X 则程序格式错误”之类的语句,例如 (2.13.1/3):

如果程序的一个翻译单元包含不能由任何允许的类型表示的整数文字,则该程序是格式错误的。

然而,我还没有发现 C++ 实现对格式错误的程序有什么要求。

假设我有一个格式错误的程序。怎么办?

当遇到格式错误的程序时,C++ 实现是否需要做一些特定的事情,或者 C++ 实现行为只是未定义?

4

4 回答 4

16
于 2013-04-04T07:56:19.917 回答
4

Quoting [intro.compliance]§2:

  • 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.

I haven't found any other relevant passages in the standard. If we combine this with [defns.undefined]:

undefined behavior

behavior for which this International Standard imposes no requirements

[ Note: Undefined behavior may be expected when this International Standard omits any explicit definition of behavior or when a program uses an erroneous construct or erroneous data. Permissible undefined behavior ranges from ignoring the situation completely with unpredictable results, to behaving during translation or program execution in a documented manner characteristic of the environment (with or without the issuance of a diagnostic message), to terminating a translation or execution (with the issuance of a diagnostic message). Many erroneous program constructs do not engender undefined behavior; they are required to be diagnosed. -end note ]

I'd say we arrive at "issue a diagnostic message and further the behaviour is undefined," because the standard doesn't say anything more about it.

于 2013-04-04T07:55:08.023 回答
3

(首先,对不起我的英语)

§1.4.2 中的标准说:

如果程序包含违反任何可诊断规则[...],则符合要求的实现应发出至少一个诊断消息。

格式错误的程序的定义是“格式不正确的程序”(§1.3.9),格式正确的程序是(§1.3.26):

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

因此,格式错误的程序确实隐含地违反了“某些”规则。如果规则 R 具有以下结构:

如果一个程序具有属性 P,那么它就是一个格式错误的程序。

当程序具有这样的属性 P 时,它确实隐含地违反了规则(根据格式错误程序的定义),尽管尚不清楚哪个规则是被违反的,因为 R 本身没有(来自严格的逻辑观点)。

于 2014-02-05T23:40:31.367 回答
1

The only relevant quote I could find says that an implementation is required to diagnose an ill-formed program, but it can finish compiling it:

1.4 Implementation compliance [intro.compliance]

8) A conforming implementation may have extensions (including additional library functions), provided they do not alter the behavior of any well-formed program. Implementations are required to diagnose programs that use such extensions that are ill-formed according to this International Standard. Having done so, however, they can compile and execute such programs.

于 2013-04-04T07:53:20.790 回答