Consider the following code :
#include <iostream>
#include <type_traits>
class Base
{
public: static int f() {return 42;}
};
class Derived : public Base
{
protected: int x;
};
class NotDerived
{
protected: int x;
};
int main()
{
std::cout<<sizeof(Base)<<std::endl;
std::cout<<sizeof(Derived)<<std::endl;
std::cout<<sizeof(NotDerived)<<std::endl;
return 0;
}
With g++ 4.7 -O3, it prints :
1
4
4
and if I understand that well, it means that Empty Base Class Optimization is enabled.
But my question concerns the runtime overhead : is there any overhead creating (and destructing) a Derived object compared to a NotDerived object due to the fact that Derived should construct/destruct the corresponding Base object ?
While the standard makes no guarantees to that there I would consider a compiler that did something different in those cases slightly defective.
There is literally nothing to be done to initialize the base: no memory has to be initialized, no virtual call mechanism has to be set up. No code should be generated for it.
However, you should always check some assembly in a non-trivial setting if this is really important to you.