c++ - Empty derived optimization -
Most C ++ programmers are aware of empty base class customizations / as with empty hair sections what happens? For example
class EmptyBase {int i; }; Template & lt; Typename T & gt; Category received: T {}; Std :: cout & lt; & Lt; Sizeof (derived & lt; emptybase & gt;); // Is this a standard verdic on? Similarly, according to eBay, there should be an EDO that because a derived class does not provide any more members, nor does its virtual people present it for its parametrising type , It should not require more memory - Is such a compatibility standard / possible?
- If so, what is the mechanism of such optimization , are they equal?
Note: Using class templates that are derived from their parameter type, is quite specific. Subject is wasting space in such circumstances
In the standard "empty base class" case is not real . Rather, it says (CF 1.8):
[A] The most derived object will have a zero-zero size and one or more bytes of storage will be captured. Base class sub-objects can be zero size.
And:
There is no object [...] base class sub-subject of zero size, know that the first byte of that object The address of who is at it. There can be only one address of two objects [...] if someone is sub-subjective of another, or if the base class of at least one zero square is sub-subject and these are of different types; Otherwise, they will have specific addresses.
and (9):
Full objects and class-type member sub-subjects will be the size of a nozer Footnote: Base Class Subject is not so constrained.
It never says that only empty bases are used for any type of layout change and leaves enough room to "squeeze" the layout: for example For: struct A {}; Structure B: A {int x; }; // {int} straight X {int a; "Equivalent" for; }; Struct Y: X {}; "Equivalent" for // {int}
BB,
and YY; Consider
. It is possible that the address of b
, a
-subobject's b
(i.e and static_cast & lt; a & gt; ; (B)
) and bx
is the same, and so on y
, X
-suboject y The address of
and ya
are the same.
The only thing that does not work is:
struct S {}; Structure T {SS; }; // "equal" {char}
to "equivalent"
I mean the most sensible, space-saving implementation.
A more interesting case is the following:
struct foo {int x; Four a; }; // {int, char, char [3]} straight bar: Foo {short q; }; // {int, char, char, short}
This example considers sizeof (int) == 4
and sizeof (short) == 2
. For alignment reasons we have sizeof (Foo) == 8
, but despite having more data members size (bar)
also 8
is.
Data members of non (non-union) class were allocated with equal access control (Section 11) so that the subsequent members Have a high address within a class object. The order of allocation of non-static data members with different access control is unspecified (11). Due to implementation alignment requirements, two adjacent members can not be allocated immediately after each other; Therefore, there may be a need for a place to manage Virtual Functions (10.3) and Virtual Base Class (10.1). Finally, 9.2 / 10 says that there is no padding at the beginning of standard-layout classes, because their address is equivalent to their "initial member" address as standard- The layout requires that all the bases be either empty or the most derived class has no data member, this means that the standard-layout class should be of a type of "empty base" Employ optimization, and my B
and Dry prefix
layout is really mandatory.
Comments
Post a Comment