www.digitalmars.com         C & C++   DMDScript  

D - RE: Odd Template Error

reply resistor mac.com writes:
OK, I isolated that templating error down.  I have a class Tree!(T).  Every
Tree!(T) needs to have two 
member variables that are also Tree!(T)'s.  However, if I just declare them like
such:

Tree!(T) left;
Tree!(T) right;

I get an error.  What is the proper way to declare them?

Owen
Feb 25 2004
next sibling parent reply "Ben Hinkle" <bhinkle4 juno.com> writes:
<resistor mac.com> wrote in message news:c1jf0l$rla$1 digitaldaemon.com...
| OK, I isolated that templating error down.  I have a class Tree!(T).  Every
| Tree!(T) needs to have two
| member variables that are also Tree!(T)'s.  However, if I just declare them
like
| such:
|
| Tree!(T) left;
| Tree!(T) right;
|
| I get an error.  What is the proper way to declare them?
|
| Owen

I think that is a compiler bug in the undocumented class-template syntax.
The solution is to write

  class Tree(T)
  {
    Tree left;
    Tree right;
    ...
  }

[note you leave out the !(T) in the class body] or

  template Tree(T)
  {
    class Tree
    {
      Tree left;
      Tree right;
      ...
    }
  }

Sometimes I've wondered about using this! to refer to the
template currently being defined, but that seems a little
too wierd to use unless there is a good reason why other
syntax choices fail.

-Ben
Feb 25 2004
parent resistor mac.com writes:
Walter,

Would there be any chance of getting this fixed?  If a templated class can
create instances other 
templated classes as Class!(T), should it also be able create instances of
itself with the same syntax?

Owen

In article <c1jgav$tv7$1 digitaldaemon.com>, Ben Hinkle says...
<resistor mac.com> wrote in message news:c1jf0l$rla$1 digitaldaemon.com...
| OK, I isolated that templating error down.  I have a class Tree!(T).  Every
| Tree!(T) needs to have two
| member variables that are also Tree!(T)'s.  However, if I just declare them
like
| such:
|
| Tree!(T) left;
| Tree!(T) right;
|
| I get an error.  What is the proper way to declare them?
|
| Owen

I think that is a compiler bug in the undocumented class-template syntax.
The solution is to write

  class Tree(T)
  {
    Tree left;
    Tree right;
    ...
  }

[note you leave out the !(T) in the class body] or

  template Tree(T)
  {
    class Tree
    {
      Tree left;
      Tree right;
      ...
    }
  }

Sometimes I've wondered about using this! to refer to the
template currently being defined, but that seems a little
too wierd to use unless there is a good reason why other
syntax choices fail.

-Ben
Feb 25 2004
prev sibling parent Sark7 <sark7 mail333.com> writes:
On Thu, 26 Feb 2004 00:35:34 +0000 (UTC), <resistor mac.com> wrote:

 OK, I isolated that templating error down.  I have a class Tree!(T).  
 Every
 Tree!(T) needs to have two
 member variables that are also Tree!(T)'s.  However, if I just declare 
 them like
 such:

 Tree!(T) left;
 Tree!(T) right;

 I get an error.  What is the proper way to declare them?

 Owen
Try .Tree!(T) left; .Tree!(T) right; -- Sark7
Feb 26 2004