It should be, if not a "must."
Behavior by definition from ISO C (the corresponding definition is not found in ISO C ++, but it should somehow apply):
3.4
1 behavior
appearance or action
And UB:
WG21 / N4527
1.3.25 [defns.undefined]
undefined behavior
for which this International Standard does not impose requirements [Note: Undefined behavior can be expected if there is no explicit definition of behavior in this International Standard or when a program uses an erroneous construction or erroneous data. Acceptable Undefined behavior varies from completely ignoring the situation with unpredictable results, maintaining during the translation or execution of the program in a documented manner specific to the environment (with or without a diagnostic message), to stop translating or executing (with the delivery of a diagnostic message). Many erroneous software constructs do not generate Undefined behavior; they must be diagnosed. -end note]
Despite the "behave during translation" above, the word "behavior" used by ISO C ++ mainly refers to program execution.
WG21 / N4527
1.9 Program Execution [intro.execution]
1 The semantic descriptions in this International Standard define a parameterized non-deterministic abstract machine. There are no requirements for the structure of the corresponding implementations in this International Standard. In particular, they do not need to copy or emulate the structure of an abstract machine. Rather, appropriate implementations are needed to emulate (only) the observed behavior of an abstract machine, as described below .5
2 Some aspects and operations of an abstract machine are described in this International Standard as an implementation (for example, sizeof(int) ). They make up the parameters of an abstract machine. Each implementation should include documentation describing its characteristics and behavior in these relationships .6 Such documentation should determine the instance of the abstract machine that corresponds to this implementation (referred to as the “corresponding instance” below).
3 Some other aspects and operations of an abstract machine are described in this International Standard as undefined (for example, evaluating expressions in a new-initializer if the allocation function does not allocate memory (5.3.4)). Where possible, this International Standard defines a set of acceptable behaviors. They define the non-deterministic aspects of an abstract machine. Thus, an abstract machine instance can have more than one possible execution for a given program and a given input.
4 Some other operations are described in this International Standard as Undefined (for example, the effect of trying to modify a const object). [Note. This International Standard does not establish the requirements for the behavior of programs containing Undefined behavior. -end note]
5 The corresponding implementation executing a well-formed program should produce the same observable behavior as one of the possible executions of the corresponding instance of an abstract machine with the same program and the same input. However, if any such execution contains an Undefined operation, this international standard does not require the implementation of this program to be executed with this input (even with respect to operations preceding the first Undefined operation).
5) This provision is sometimes called the “as is” rule, because the implementation can ignore any requirement of this International Standard if the result is as if this requirement were met, as far as possible it is determined by the observed behavior of the program. For example, the actual implementation should not evaluate part of the expression if it can infer that its value is not used and that no side effects affecting the observed behavior of the program are produced.
6) Conditionally supported constructs and locale-specific behavior are also included in this documentation. See 1.4.
It is clear that the behavior of Undefined was caused by a specific language construct used incorrectly or in a non-portable way (which does not comply with the standard). However, the standard does not mention anything about which specific piece of code in the program will call it. In other words, “with Undefined behavior” is a property (of conformity) of the entire executable program, and not its smaller parts .
The standard could give a stronger guarantee to make the behavior correct as soon as some specific code is not executed, only when there is a way to precisely match the C ++ code with the corresponding behavior. It is difficult (if not impossible) without a detailed semantic model of implementation. In short, the operational semantics given by the above abstract machine model are not enough to achieve a stronger guarantee . But in any case, the ISO C ++ will never be a JVMS or ECMA-335. And I do not expect that there will be a complete set of formal semantics describing the language.
The key issue here is the meaning of "execution." Some people think that “executing a program” means starting a program. This is not entirely true. Note that the representation of the program running in the abstract machine is not specified. (Also note that “this international standard does not require an implementation conformance structure.”) The code executed here may literally be C ++ code (not necessarily machine code or some other form of intermediate code that is not specified by the standard at all). This effectively allows you to use the main language as an interpreter, an online partial evaluator, or some other monsters that translate C ++ code on the fly. As a result, there is actually no way to completely separate the translation phases (defined by ISO C ++ [lex.phases]) completely before the execution process without knowledge of specific implementations. Therefore, it is necessary to resolve the UB that occurs during translation when it is too difficult to specify portable, well-defined behavior.
In addition to the above problems, perhaps for most ordinary users, one (non-technical) reason is enough: you just don’t need to provide a stronger guarantee, resolve bad code and win one of the (most likely most important) aspects of the utility UB itself: to encourage quickly discarding some (unnecessarily) intolerable smelly code effortlessly to “fix” them, which will ultimately be in vain.
Additional notes:
Some words are copied and restored from one of my answers to this comment .