gpt4 book ai didi

c++ - 在C++标准中,格式正确意味着代码可以编译吗?

转载 作者:行者123 更新时间:2023-12-01 15:07:50 26 4
gpt4 key购买 nike

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

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



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

最佳答案

格式正确的程序可能具有不确定的行为。
它在注释中,因此在技术上没有权威,但是似乎打算终止编译(或按标准要求的“翻译”)在可能的UB范围内:

[intro.defs]

undefined behavior

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.

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



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

[implemits]

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.



此外,编译器可以具有并且确实具有错误。格式正确只是意味着符合标准的编译器应对其进行编译(在上述限制内)。有问题的编译器不一定符合标准。

最后,标准文档本身是 not perfect。如果对于规则的含义存在分歧,则程序可能在一种解释下格式正确,而在另一种解释下格式错误。
如果一个编译器与程序员或另一个编译器不同意,则它可能无法编译被认为是由另一方编写的格式正确的程序。

关于c++ - 在C++标准中,格式正确意味着代码可以编译吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62409721/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com