For this question, no polymorphism shall be involved, i.e. no virtual methods, no virtual base classes. Just in case it matters, my case does not involve any of those.
Assume I have a class Derived which has an unambiguous accessible parent of type Base, with no polymorphism (no virtual methods, no virtual base classes), but possibly involving indirect and/or multiple inheritance.
Assume further I have a valid pointer Derived *derived
(points to an object of type Derived or a subclass thereof).
In this case, I believe static_cast<Base*>(derived) is valid
(results in a valid usable pointer). When the ancestry chain between Base and Derived involves multiple inheritance, this static_cast might imply pointer adjustments to locate the Base instance within the Derived instance. To do that, the compiler needs to know the inheritance chain, which he does in this case. However, if an intermediate cast to void * is inserted, that inheritance chain information is hidden from the compiler. For which inheritance chain is such a static cast valid nonetheless? I expect one of the following:
- None at all? Accessing a
static_castfrom void pointer is undefined behaviour unless the pointer really points to the exact type. - For all chains without multiple-inheritance? Then, the compiler could guarantee that
Baseis always at the start ofDerived- but what says the standard? - For all chains where
Baseis found within the first parent class of all intermediate multiple inheritance chains? Maybe the start ofBaseandDerivedstill matches? - Always?
static_castto void pointer could always adjust to the start of the very first parent, andstatic_castfrom void pointer undo that adjustment. But with multiple inheritance, the "very first parent" is not necessarily a parent of all parents.