RefCounted
JG
someone at somewhere.com
Fri Apr 15 07:02:06 UTC 2022
On Wednesday, 13 April 2022 at 20:47:33 UTC, JG wrote:
> Hi,
>
> I would have thought that RefCounted!(T,
> RefCountedAutoInitialize.no) is to be used in place T* when I
> want reference counting instead of the usual garbage collection
> (or manual allocation). Perhaps this is wrong?
>
> [...]
In case some one has a similar problem, try:
https://code.dlang.org/packages/automem
```
import std.stdio;
import std.experimental.allocator.mallocator;
import std.experimental.allocator;
import automem;
struct Node(T) {
RefCounted!(Node!T) next;
T val;
}
struct List(T) {
RefCounted!(Node!T) head;
bool empty() { return head == null; }
T front() { return head.val; }
void popFront() { head = head.next; }
typeof(this) save() { return typeof(this)(head); }
void insert(T x) {
head = RefCounted!(Node!T)(head,x);
}
}
void main() {
theAllocator = allocatorObject(Mallocator.instance);
List!long l;
l.insert(5);
l.insert(4);
l.insert(3);
l.insert(2);
l.insert(1);
writeln(l);
}
```
More information about the Digitalmars-d-learn
mailing list