Policy based implementation and multiple inheritance class size

While implementing a policy based class, I've noticed a strange behaviour on VC++ 8 concerning the size of a class inheriting from more than one class.

Consider the following example where class CSize inherits from 4 empty classes :

class A {};
class B {};
class C {};
class D {};

class CSize : public A, public B, public C, class D {};

In this sample, sizeof(CSize ) == 3, while I was expecting 1.

As I could not explain it, I've tried the exactly same code on GCC 3.4.4 to see if it was a compiler specific behaviour. GCC gives my expected result with a size of 1.

What is the reason for that behaviour which really degrades policy based implementations

Many thanks

Guillaume



Answer this question

Policy based implementation and multiple inheritance class size

  • 風清揚

    The reason really is binary compatibility. Microsoft guarantees that the same class declaration yields the same class layout in different versions of the compiler. This is important for scenarios where you have third-party static library built with a different version.

    I doubt that we will ever see support for EBO in the MI case for IA32. However, they might have got it right for x64 or at least IA-64.

    -hg


  • Bessa

    It seems to me that it is an interesting feature for many modern C++ patterns. Even after a few reads about EBCO, I cannot see why Microsoft don't do that optimization (binary compatibility and ABI does not really make sense to me). Could you tell me more about the technical reasons behind this lack

    I'm afraid that a linear inheritance will complicate a lot my implementation (even using Boost). Do you know if there are any plans to support EBCO in future releases or something that could help

    Many thanks

    Guillaume


  • ?封

    The reason is binary compatibility. Microsoft screwed up when the designed the ABI (i.e. they don't do empty base optimization for multiple inheritance).

    Of course, you can always try to generate a single-inheritance chain (you may want to take a look at Boost MPL - specifically inherit_linearly)

    -hg


  • Policy based implementation and multiple inheritance class size