The followings code shows a difference behavior when upcast a derived class to its base.
#include <cstdio>
struct base {
int value;
};
template <typename T> struct wrapper: base, T {
};
struct empty {};
struct nonempty { int value; };
template <typename T>
int ofst(wrapper<T> *t) { return (char *) ((T *) t) - (char *) t; }
template <typename T>
int ofst2(wrapper<T> *t) { return (char *) static_cast<T*>(t) - (char *) t; }
int main() {
wrapper<empty> empty;
wrapper<nonempty> nonempty;
printf("%d %dn", ofst(&empty), ofst(&nonempty)); // give 0, 4
printf("%d %dn", ofst2(&empty), ofst2(&nonempty)); // also give 0, 4
}
Can someone gives me the reference that explains this result?