Thursday, November 18, 2010

Empty Base Class Optimization (EBCO)

Given the following classes:

   class Base
   { }; 

   class Derived : public Base 
   { }; 
   class DerivedDerived : public Derived
   { };

If your compile implements the Empty Base Class Optimization (EBCO) and the sizeof(Base) == 1, then the sizeof(DerivedDerived) is 1.

This optimization is allowed as long as none of the same subobjects have the same address.

Reference: C++ Templates: The complete guide by David Vandevoorde and Nicolai M. Josuttis. Addison-Wesley, 2003, pp. 289-292.

No comments:

Post a Comment