Why does multiple inheritance increase the size of the object despite the bases being empty?

后端 未结 2 773
一个人的身影
一个人的身影 2021-01-17 09:06

Given this code:

#include 

struct A {

};

struct B {

};

struct C {

};

struct E : A {
    int field;
};

struct F : A, B {
    int field         


        
2条回答
  •  余生分开走
    2021-01-17 09:38

    Visual Studio 2015 Update 2 added support for Empty Base Class Optimization. However, as Updates are supposed to be layout-compatible between them, the optimization is not on by default; you need to manually ask for it using __declspec(empty_bases).

    There's more information in VC's blog: https://blogs.msdn.microsoft.com/vcblog/2016/03/30/optimizing-the-layout-of-empty-base-classes-in-vs2015-update-2-3/

    This will eventually be the default once they release a major compiler version update, where they're allowed to break binary compatibility.

提交回复
热议问题