But now I'm wondering why the standard has to make any pronouncements about division by zero in the first place.
The standard makes no pronouncements about division by zero. That's what "undefined behavior" means. Whenever a situation arises in which the execution of a program for a given set of inputs would have behavior that is either explicitly or implicitly not defined by the standard, then the standard does not make any demands of the C++ implementation (compiler + standard library) at all with regards to that program under theses inputs and specifying the behavior of C++ implementations with regards to given programs in their source code form is the only thing that the standard does.
Saying explicitly that division by zero has undefined behavior is 100% equivalent to just not saying what the result of division by zero should be. Making it explicit only assures the reader that this is intentional and avoids misunderstandings.
isn't division by zero more of a run-time concern?
Yes, it is a run-time concern, which is why the term "undefined behavior" is used. For analogous situations involving only compile-time questions regardless of program inputs, the standard uses "ill-formed, no diagnostic required" instead.
If the standard doesn't specify how a given program should behave at run-time, then that means that the behavior of the program is undefined. There is also "unspecified" which the standard uses to allow for multiple possible, but locally confined, behaviors and "implementation-defined" for "unspecified" behavior that should additionally be documented by the C++ implementation (and is typically expected to be consistent).
It is not like the standard specifies that the compiler should translate e.g. a /
on float
to a machine instruction representing division of floating point numbers of appropriate size and then leaves the exact behavior to the particular machine.
The standard specifies how a given program would behave for give inputs in terms on an abstract machine that has nothing to do with any real CPU and only specifies that C++ implementations should translate/run programs in such a way that observable behavior (e.g. IO) of well-formed programs matches those that the described abstract machine would have. As far as the standard is concerned there doesn't even need to be a compiler translating to some CPU's instruction set. C++ could be interpreted.
Especially if the compiler is unlikely to be able to detect it in most cases without evaluating the denominator (I think this is what happens in the example above). Especially if the compiler is unlikely to be able to detect it in most cases without evaluating the denominator (I think this is what happens in the example above).
Because the standard doesn't define the behavior, the compiler does not need to consider at all whether or not the program contains it. If the compiler wants to do some analysis (to whatever degree) in order to warn about it, it can, but that has nothing to do with the standard. There is no imperative for the standard to diagnose undefined behavior. There are no imperatives on undefined behavior at all. But a user of the compiler will expect it to warn about easily recognizable mistakes.
From what I understand, division by zero is undefined behaviour in the C++ standard and the reason for the difference between res1 and res2 is due to my machine implementing IEEE 754 for double, which requires division by zero to return Inf or -Inf.
Because the C++ standard says that it is undefined, the compiler can do anything without its conformance with the C++ standard being impacted. It doesn't matter what the CPU implements, if the compiler wants to it can ignore that and still optimize under the assumption that floating point division by zero never happens. And even if the CPU does not support it, the compiler could define division by zero to have some well-defined meaning. But of course, if the compiler wants to be IEEE 754 conforming, then it must assure that division by zero behaves as that specifies and it may rely on the CPU's behavior to implement it.