Why can't we derive struct's?

Steven Schveighoffer schveiguy at gmail.com
Thu Dec 20 14:55:45 UTC 2018


On 12/20/18 3:15 AM, Manu wrote:
> On Wed, Dec 19, 2018 at 7:45 PM Walter Bright via Digitalmars-d
> <digitalmars-d at puremagic.com> wrote:
>>
>> On 12/19/2018 5:40 PM, Manu wrote:
>>   > static if (Base.tupleof.length == 0)
>>
>> static if (Base.sizeof == 0)
> 
> Empty struct has sizeof == 1 no? It certainly takes that many bytes as
> a member, and that 1 byte is naturally padded out to the alignment of
> the following member.

Yes, it does take 1 byte. I believe the reasoning was so it could have a 
unique address and be used in an array.

It's the same in C++ as well, according to the Internet (except for the 
base class optimization allowance). Note that C++ compilers aren't 
*required* to implement this.

The difference between inheritance and manual inheritance (i.e. alias 
this and a member), is that with true inheritance there is no member 
that has to have it's own distinct address, so it's allowed to take up 0 
space

But I would expect your boilerplate could be handled via mixin template 
pretty easily, then it becomes:

struct Derived
{
    mixin DeriveFrom!Base;
}

That it takes up the one alias this slot is a bummer, but really we 
should implement multiple-alias-this and that problem would be solved.

-Steve


More information about the Digitalmars-d mailing list