It is true that undefined behavior can only occur at run time (for example, dereferencing a pointer that is null). In other cases, the program may be statically "poorly formed, no diagnostics required" (for example, if you add an explicit specialization for the template after it has already been used), which has the same effect: although you cannot argue internally as your program will behave.
Compilers can use UB to βoptimizeβ code generation aggressively. In your case, the compiler sees that the second branch will call UB (I assume it is known statically, even if you did not specify it), and therefore it can assume that this branch is never taken, because it is indistinguishable: If you entered the second branch , then the behavior will be undefined, and it will behave the way you entered the first branch. That way, the compiler can simply view the entire path of the code, which will lead to UB as dead and delete it.
There is no way to prove that something is wrong.
source share