Here's an example of a hang of values ββdue to godliness with a base class and EBO :
#include <cassert> #include <iostream> struct Base {}; // empty class struct Derived1 : Base { public: int i; }; int main() { // the size of any object of empty class type is at least 1 assert(sizeof(Base) == 1); // empty base optimization applies assert(sizeof(Derived1) == sizeof(int)); Base objBase; Derived1 objDerived; objDerived.i = 42; Base& refToobjDerived = objDerived; char buf[sizeof(Base)]; // 1 std::memcpy(buf, &objBase, sizeof(Base)); // copy objBase to buf // might do something with buf.. std::memcpy(&refToobjDerived, buf, sizeof(Base)); // EBO! I'm overwriting the int value! std::cout << objDerived.i; // Screwed }
Example
If you create a base class nontrivially copied , the value will not be affected.
Another problem with dedicated hvd may be an additional addition at the end of the base class, which is used to store data belonging to derivatives.
Marco A.
source share