Avoiding default generic types, and allocator awareness
helxi
brucewayneshit at gmail.com
Sat Dec 30 15:00:32 UTC 2017
As an exercise in http://ddili.org/ders/d.en/pointers.html, I was
implementing a very stripped down version of singly linked list
like below:
struct Node(T)
{
T item;
Node!T* next_item;
}
string to_string(T)(in Node!T node)
{
import std.format;
return node.nextItem ? "%s -> %s".format(node.item,
*(node.nextItem))
: "%s".format(node.item);
}
struct Forward_List(T, N = size_t)
{
Node!T* head;
N size;
}
string to_string(T, N = size_t)(ref Forward_List!(T, N) list)
{
//todo
import std.format;
return "head*: %s, size: %s)".format(&list, list.size);
}
void push_front(T, N = size_t)(ref Forward_List!(T, N) list, T
item)
{
list.head = new Node!T(item, list.head);
++list.size;
}
1. If you have noticed, I am needlessly repeating `N = size_t` in
to_string and push_front. If I leave the signature like these, is
it actually going to affect the structure in anyway?:
void push_front(T, N = size_t)(ref Forward_List!(T, N) list, T
item);
string to_string(T, N = size_t)(ref Forward_List!(T, N) list);
2. How does one make it "allocator aware"? What other allocators
can I use?
More information about the Digitalmars-d-learn
mailing list