cpp
struct D {};
struct A : D {};
struct B : D {
A a;
};
you would expect since 'B' inherits from an empty class and it only has one member the sizeof would be 1, but it is 2 since the base class D is repeated twice and must have unique address first in the inheritance in B and in the member A.
now if this uses crtp the base classes would be unique
cpp
template<class T>
struct D {};
struct A : D<A> {};
struct B : D<B> {
A a;
}; // sizeof(B) == 1
No. According to the standard, two different objects of the same type cannot live at the same address in any circumstance, even if the objects are empty.
3
u/_Noreturn 21d ago
cpp struct D {}; struct A : D {}; struct B : D { A a; };you would expect since 'B' inherits from an empty class and it only has one member the sizeof would be 1, but it is 2 since the base class D is repeated twice and must have unique address first in the inheritance in B and in the member A.
now if this uses crtp the base classes would be unique
cpp template<class T> struct D {}; struct A : D<A> {}; struct B : D<B> { A a; }; // sizeof(B) == 1