Empty Derivative Optimization

Most C ++ programmers are aware of the empty base class optimizer, like idiom . What happens to empty child classes? for example

class EmptyBase {
    int i;
};

template<typename T>
class Derived : T { 
};

std::cout << sizeof(Derived<EmptyBase>); // Is there a standard verdic on this?

Similarly, EBO should be EDO, which states that since the derived class does not provide more members and does not introduce any virtual parameterizing type , it should not require more memory. Given various situations in which something like this might be apper (multiple inheritance, single inheritance ...):

  • Is such an optimization standard / possible?
  • If so, what are the mechanics of this optimization , are they similar to EBO ?

. , , .

+4
1

" ". , (. 1.8):

[A] . .

[...] , , . [...] , ; .

( 9):

. : .

, "" : :

struct A {}; struct B : A { int x; };             // "equivalent" to { int }

struct X { int a; }; struct Y : X {};             // "equivalent" to { int }

B b; Y y;. , b, A - b (.. &static_cast<A&>(b)), b.x , , y, X - y, y.a - .

, , :

struct S {}; struct T { S s; };                   // "equivalent" to { char }

"equivalent" , .

:

struct Foo { int x; char a; };                    // { int, char, char[3] }

struct Bar : Foo { short q; };                    // { int, char, char, short }

sizeof(int) == 4 sizeof(short) == 2. sizeof(Foo) == 8, sizeof(Bar) 8, .


9.2/13:

(non-union) ( 11) , . (11). , ; (10.3) (10.1).

, 9.2/10 , , " ". , , , " ", ​​ b y .

+5

Source: https://habr.com/ru/post/1527374/


All Articles