Thread overview
Alias in templates
Dec 14, 2006
Peer Sommerlund
Dec 14, 2006
Don Clugston
Dec 14, 2006
John Reimer
December 14, 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.
December 14, 2006
Peer Sommerlund wrote:
> 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?

Because an alias parameter is passed as a fully-qualified name.
So in this case:
> module foo;
> class MyAdvanceNode {
>   ListLink!(MyAdvanceNode,allocated) allocated;

'm' is not 'allocated', but rather 'foo.MyAdvanceNode.allocated'.
So 'n.m.next' is not going to make any sense.

You _might_ be able do this as a mixin instead, with a syntax something like:

class MyAdvanceNode {
mixin ListLink!(MyAdvanceNode) allocated;
}

-- but you'll have to develop a few new tricks, I think.
December 14, 2006
On Wed, 13 Dec 2006 23:39:07 -0800, Peer Sommerlund <peer.sommerlund@gmail.com> wrote:

> 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.

In addition to Don's comments about allocation:

I don't believe "prev" and "next" should be pointers in this case.  The class types are references, so the result is a pointer to a reference, which I don't think is the intention.  This means "n.m.next = next" merely assigns class pointers.  Using the reference alone may be adequate.

-JJR