Can we just have struct inheritence already?

Manu turkeyman at gmail.com
Thu Jun 13 06:43:44 UTC 2019


On Wed, Jun 12, 2019 at 11:29 PM Manu <turkeyman at gmail.com> wrote:
>
> On Wed, Jun 12, 2019 at 10:25 PM Walter Bright via Digitalmars-d
> <digitalmars-d at puremagic.com> wrote:
> >
> > On 6/11/2019 11:19 AM, Manu wrote:
> > >> Can I ask again, in a different way, why do you need the 0 size?
> > >
> > > To handle base structs with no members.
> >
> > For the 3rd time, why do you need it to have 0 size? I don't know what you mean
> > by "handling" it.
>
> I hate wasting 8 bytes needlessly. Almost every struct I write is
> carefully packed and aligned to 16, 32, or 64 bytes and allocated in
> pools... that's 50%, 25%, or 12.5% memory wastage.
> There's also ABI compatibility with existing C++ where this is
> super-common. Consider that user-supplied <Allocator> is a base of
> every STL container, that's one case where I've had to deploy the
> static if hack a lot. Those are absolutely not polymorphic types, and
> you couldn't make that mistake no matter how hard you squinted. They
> sometimes use a helper called a 'packed pair' in the STL, which is
> like std::pair (obviously another not-a-polymorphic-type), but where
> one or the other element can be zero-sized and not waste memory.
> It's an aggregation tool.

What are we so afraid of? I don't understand the resistance to this
*at all*... people talk about slicing, which is a pretty worthless
baseline argument; it's totally possible in C++ and I've never heard a
single case of anyone ever having that problem in my life, at work, at
home, or even on the internet (yes, I'm sure I would find if I
deliberately went looking). I'm only aware the concept has a name
because I've seen it written in a book somewhere. It's 99.9999%
not-even-remotely-a-problem, it's only a 'theoretical' problem.

What if we dis-allowed implicit casting structs to base type? I mean,
nothing else about it is polymorphic in nature... not-allowing
implicit casting to base type would not cause problems in any use I
can imagine.

This should obviously be a compile error:
  struct A { int x; }
  struct B : A { int y; }
  B b;
  A a = b; // <- obviously an error


More information about the Digitalmars-d mailing list