To add the standard quote:
expr.add#5
When two pointer expressions
PandQare subtracted, the type of the result is an implementation-defined signed integral type; this type shall be the same type that is defined asstd::ptrdiff_tin the<cstddef>header ([support.types]).
(5.1)
IfPandQboth evaluate to null pointer values, the result is 0.(5.2)
Otherwise, ifPandQpoint to, respectively, elementsx[i]andx[j]of the same array objectx, the expressionP - Qhas the valuei−j.(5.3)
Otherwise, the behavior is undefined.
[ Note: If the valuei−jis not in the range of representable values of typestd::ptrdiff_t, the behavior is undefined.
— end note
]
(5.1) does not apply as the pointers are not nullptrs. (5.2) does not apply because the pointers are not into the same array. So, we are left with (5.3) – UB.