Putting aside all the good practices against writing code like below, just from the language lawyer perspective, is the following snippet legal?
struct Base {
int f() { return 42; }
};
struct D1 : Base {};
struct D2 : Base {};
int main() {
D2 d2;
D1* ouch = reinterpret_cast<D1*>(&D2);
return ouch->f();
}
It seems to me that [class.mfct.non.static] is the most relevant clause and that it suggests it is ok, as we are calling a non-static member function for an object of a derived class.
Should it happen that the non-virtual case is indeed legal, does anything change if f or the inheritance is virtual?