Can we just have struct inheritence already?

Jonathan M Davis newsgroup.d at jmdavisprog.com
Tue Jun 11 05:05:57 UTC 2019


On Monday, June 10, 2019 5:00:40 PM MDT H. S. Teoh via Digitalmars-d wrote:
> On Mon, Jun 10, 2019 at 06:35:16PM -0400, Nick Sabalausky (Abscissa) via 
Digitalmars-d wrote:
> > On 6/10/19 12:03 AM, Nicholas Wilson wrote:
> > > On Monday, 10 June 2019 at 03:08:26 UTC, Walter Bright wrote:
> > > > On 6/9/2019 12:40 PM, Vladimir Panteleev wrote:
> > > > > Structs are always at least 1 byte in size, even when they
> > > > > contain no fields. This causes unnecessary bloat for reasons
> > > > > that I never understood.
> > > >
> > > > The reason is so that each struct instance is at a unique address.
> > > >
> > > > There are some rules in C++ where sometimes a struct with no
> > > > fields occupies 1 byte, sometimes 0 bytes. I don't recall what
> > > > they are at the moment.
> > >
> > > There is a (proposal?) for a no unique address attribute to
> > > disappear the address of structs the take up no room.
> >
> > Would it really be a problem for the addresses of zero-byte structs to
> > just simply be null?
>
> Probably, if people expect:
>
>   struct S {}
>   S s;
>   assert(&s !is null);
>
> to work.
>
> TBH, though, expecting different instances of a 0-sized struct to have
> distinct addresses is kind of a strange obscure case, and I'm not
> convinced it's worth the resulting slew of messy special cases just to
> cater to it.  But that ship has long since sailed, and I'm not sure it's
> worth fighting over anymore.

There's also

assert(&s);

which calls the struct's invariant, though I doubt that many people know
that, and unless someone is trying to do that in generic code or has an
invariant which checks something other than the member variables' state, it
wouldn't really matter if you couldn't do it with a struct with no member
variables.

- Jonathan M Davis





More information about the Digitalmars-d mailing list