If I do a cast from a Base to a Derived type, but the Base type isn\'t an instance of derived type, but only use the result if it is, do I get undefined behaviour?
H
The cast itself has undefined behaviour. Quoting C++17 (n4659) [expr.static.cast] 8.2.10/11:
A prvalue of type “pointer to cv1
B”, whereBis a class type, can be converted to a prvalue of type “pointer to cv2D”, whereDis a class derived (Clause 13) fromB, if cv2 is the same cv-qualification as, or greater cv-qualification than, cv1. ... If the prvalue of type “pointer to cv1B” points to aBthat is actually a subobject of an object of typeD, the resulting pointer points to the enclosing object of typeD. Otherwise, the behavior is undefined.