在C ++标准中,格式正确意味着代码可以编译吗? [英] In the C++ standard does well-formed means that the code compiles?

查看:149
本文介绍了在C ++标准中,格式正确意味着代码可以编译吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++标准将well-formed programs定义为

The C++ standards defines well-formed programs as

根据语法规则构造的C ++程序,可诊断 语义规则和一定义规则

C ++ program constructed according to the syntax rules, diagnosable semantic rules, and the one-definition rule

我想知道是否所有格式正确的程序都可以编译(如果不是这样,哪种类型的错误会导致格式良好的程序与可编译问题之间的区别).例如,包含歧义错误的程序是否被认为格式正确?

I am wondering if all well-formed program compile or not (if it is not the case, what types of error make the difference between a well-formed program and a compilable problem). For example would a program containing ambiguity errors considered as well-formed?

推荐答案

格式正确的程序可能具有不确定的行为.

A well-formed program can have undefined behaviour.

它在注释中,因此在技术上没有权威,但是似乎打算终止编译(或如标准所称的翻译")在可能的UB范围内:

It's in a note, and thus not technically authoritative, but it seems that it is intention that termination of compilation (or "translation" as the standard calls it) is within the scope of possible UB:

[intro.defs]

[intro.defs]

未定义的行为

本文档不要求其行为的行为
[注意:当本文档省略行为的任何明确定义或程序使用错误的构造或错误的数据时,可能会出现未定义的行为.

behavior for which this document imposes no requirements
[ Note: Undefined behavior may be expected when this document 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.

对常量表达式的求值永远不会表现出在本文档的[intro]至[cpp]中明确指定为undefined的行为([expr.const]). —尾注 ]

Evaluation of a constant expression never exhibits behavior explicitly specified as undefined in [intro] through [cpp] of this document ([expr.const]). — end note ]


还有一些实际的实现限制:


There are also practical implementation limits:

[允许]

由于计算机是有限的,因此C ++实现不可避免地会限制它们可以成功处理的程序的大小.
每个实现应在已知的地方记录这些限制.该文档可能会引用存在的固定限制,例如如何根据可用资源来计算可变限制,或者说固定限制不存在或未知.

Because computers are finite, C++ implementations are inevitably limited in the size of the programs they can successfully process.
Every implementation shall document those limitations where known. This documentation may cite fixed limits where they exist, say how to compute variable limits as a function of available resources, or say that fixed limits do not exist or are unknown.


此外,编译器可以具有并且确实具有错误.格式正确的简单表示标准编译器应对其进行编译(在上述限制内).有问题的编译器不一定符合标准.


Furthermore, compilers can have, and do have bugs. Well-formed simply means the a standard conforming compiler should compile it (within the limitations mentioned above). A buggy compiler does not necessarily conform to the standard.

最后,标准文档本身是不够完善.如果对规则的含义存在分歧,则程序可能在一种解释下格式正确,而在另一种解释下格式错误.

Lastly, the standard document itself is not perfect. If there is disagreement about what the rules mean, then it is possible for a program to be well-formed under one interpretation, and ill-formed under another interpretation.

如果一个编译器与程序员或另一个编译器不同意,则它可能无法编译被认为是由另一方编写的格式正确的程序.

If a compiler disagrees with the programmer or another compiler, then it might fail to compile a program that is believed to be well-formed by the other party.

这篇关于在C ++标准中,格式正确意味着代码可以编译吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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