Looking for a Simple Doubly Linked List Implementation

Jonathan M Davis newsgroup.d at jmdavisprog.com
Sat Sep 21 23:03:32 UTC 2019


On Saturday, September 21, 2019 12:52:23 PM MDT Dennis via Digitalmars-d-
learn wrote:
> Since I marked the method as const, `auto a = head` got the type
> const(Node!T) and `a = a.next` no longer compiled. With structs
> you can declare a const(Node!T)* (mutable pointer to const node),
> but I don't know if I can declare a mutable reference to a const
> class, so I switched to structs.

You have to use std.typecons.Rebindable if you want to have the equivalent
of const(T)* for class references, because the type system doesn't
distinguish between a class and a reference to a class. As it is, Rebindable
is pretty much a hack that's questionably legal per the type system (but
it's in Phobos, so I'm sure that it will continue to work). Ideally, there
would be a way to do it in the language, but the assumptions that the
compiler currently makes when dealing with classes makes that difficult.

In general though, if you're not going to use inheritance, then there isn't
much point in using a class instead of a struct unless you want to force it
to live on the heap (and that only really matters if you're dealing with
something that's publicly available for others to muck with, whereas nodes
in a linked list are normally private to the list, so it's easy to ensure
that they're only ever on the heap even if they're structs).

- Jonathan M Davis





More information about the Digitalmars-d-learn mailing list