Alias in templates

Peer Sommerlund peer.sommerlund at gmail.com
Wed Dec 13 23:39:07 PST 2006


Hi.

I'm trying to write a template for lists, using a slightly different approach
than normal. I think I have not quite understood how alias works.

Could somebody please explain why the following gives compiler errors? I'm
trying to use a member variable as a template parameter. (Forgive typos, this
is written from memory and my C++ background may shine through)

// This should be used as a member variable m in some class T
class ListLink(T, alias m) {
  T* next;
  T* prev;
  void insert_after(T* n) {
    n.m.next = next;
    n.m.prev = prev;
    next = n.m;
    n.m.next.prev = n.m;
  }
}

class MyNode {
  ListLink!(MyNode,allocated) allocated;
  int some_data;
}

Why do it this way?

The goal is to be able to write some structure like:

class MyAdvanceNode {
  ListLink!(MyAdvanceNode,allocated) allocated;
  ListLink!(MyAdvanceNode,neighbours) neighbours;
  string name;
  MapLink!(MyAdvanceNode,nameIndex,name) nameIndex;
  TreeLink!(MyAdvanceNode,topology) topology;
  // ... and lots of user data here
}

So I can erase a node using O(1) time, navigate between topology, and
neighbours, and back again.


More information about the Digitalmars-d-learn mailing list