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
This is undefined behaviour, but (funny enough) if you would have used reinterpret_cast instead of static_cast, you would cast that demon away.
[expr.reinterpret.cast]/7
An object pointer can be explicitly converted to an object pointer of a different type. When a prvalue v of object pointer type is converted to the object pointer type “pointer to
cv T”, the result isstatic_cast<cv T*>(static_cast<cv void*>(v)).
As noted by user Angew, this "requires a particular internal representation which ensures that static_cast<void*>(d) == static_cast<void*>(a) when a == d".
This is expressed by [class.mem]/22 to 26:
[class.mem]/26
If a standard-layout class object has any non-static data members, its address is the same as the address of its first non-static data member if that member is not a bit-field. Its address is also the same as the address of each of its base class subobjects.
So if GetType() of Animal returns the value of a non-static data member from the common initial sequence of Animal and Dog, the behavior is defined.
Those requirements are met when dealing with simple inheritance and default-aligned objects.
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.