www.digitalmars.com         C & C++   DMDScript  

D - Another template question

reply Chris Sokol <chris echosproject.org> writes:
What am I doing wrong?

template Foo(T) {
	struct Bar {
		T fooBar;
	}
}

alias instance Foo(Fu) FooT;

struct Fu {
	FooT.Bar	x;
}

no property 'Bar' for type 'instance Foo(Fu )'
Aug 07 2003
next sibling parent Chris Sokol <chris echosproject.org> writes:
On Thu, 07 Aug 2003 23:51:32 -0400, Chris Sokol wrote:
 template Foo(T) {
 	struct Bar {
 		T fooBar;
 	}
Oops, I meant to say T* fooBar;
Aug 07 2003
prev sibling next sibling parent reply Burton Radons <loth users.sourceforge.net> writes:
Chris Sokol wrote:

 What am I doing wrong?
 
 template Foo(T) {
 	struct Bar {
 		T fooBar;
 	}
 }
 
 alias instance Foo(Fu) FooT;
 
 struct Fu {
 	FooT.Bar	x;
 }
 
 no property 'Bar' for type 'instance Foo(Fu )'
Wrong error message, but this is the same thing as doing: struct x { struct y { x xitem; } y yitem; } So it has an infinite size. Use "class" instead, or pointers.
Aug 07 2003
parent Chris Sokol <chris echosproject.org> writes:
On Fri, 08 Aug 2003 00:35:19 -0400, Burton Radons wrote:
 So it has an infinite size.  Use "class" instead, or pointers.
Yeah, I meant to use a pointer. Typo. But that is the error that I get with T* fooBar inside the template.
Aug 07 2003
prev sibling parent reply "Mike Wynn" <mike.wynn l8night.co.uk> writes:
"Chris Sokol" <chris echosproject.org> wrote in message
news:bgv6f0$1mic$1 digitaldaemon.com...
 What am I doing wrong?

 no property 'Bar' for type 'instance Foo(Fu )'
I had a play with this ..... template Foo(T) { struct Bar { T * fooBar; } } alias instance Foo(Fu).Bar FooT; struct Fu { FooT x; } int main() { Fu f; return 0; } now it reports main.d(7): size of type instance Foo(Fu ).Bar is not known but ................................. template Foo(T) { struct Bar { T * fooBar; } } struct Fu { instance Foo(Fu).Bar x; } int main() { Fu f; return 0; } WORKS
Aug 07 2003
next sibling parent reply "Mike Wynn" <mike.wynn l8night.co.uk> writes:
and I tried a couple of other arrangements each has its own unique error
....

template Foo(T) {
 struct Bar {
  T * fooBar;
 }
}

instance Foo(Fu).Bar foo_bar;
struct Fu {
  foo_bar x;
}

int main() {
 Fu f;

 return 0;
}

//give the error
// test_005.d(9): foo_bar is used as a type
//

template Foo(T) {
 struct Bar {
  T * fooBar;
 }
}

instance Foo(Fu) foo_fu;
struct Fu {
  foo_fu.Bar x;
}

int main() {
 Fu f;

 return 0;
}

// gives the error
// test_004.d(9): identifier 'Bar' of 'foo_fu.Bar' is not defined
//



"Mike Wynn" <mike.wynn l8night.co.uk> wrote in message
news:bgvbr5$1rmo$1 digitaldaemon.com...
 "Chris Sokol" <chris echosproject.org> wrote in message
 news:bgv6f0$1mic$1 digitaldaemon.com...
 What am I doing wrong?

 no property 'Bar' for type 'instance Foo(Fu )'
I had a play with this ..... template Foo(T) { struct Bar { T * fooBar; } } alias instance Foo(Fu).Bar FooT; struct Fu { FooT x; } int main() { Fu f; return 0; } now it reports main.d(7): size of type instance Foo(Fu ).Bar is not known but ................................. template Foo(T) { struct Bar { T * fooBar; } } struct Fu { instance Foo(Fu).Bar x; } int main() { Fu f; return 0; } WORKS
Aug 07 2003
parent Chris Sokol <chris echosproject.org> writes:
On Fri, 08 Aug 2003 01:38:41 -0400, Mike Wynn wrote:
 and I tried a couple of other arrangements each has its own unique error
 ....
 
 template Foo(T) {
  struct Bar {
   T * fooBar;
  }
  }
  }
 instance Foo(Fu).Bar foo_bar;
 struct Fu {
   foo_bar x;
 }
 }
 int main() {
  Fu f;
 
  return 0;
 }
 }
 //give the error
 // test_005.d(9): foo_bar is used as a type //
I have a favorite set of c++ list and tree templates that I did, which I want to port to D as a learning experience. They work without performing any allocation; the nodes which are to be placed in the list need to have linkage already present. They go kind of like this: template<typename T> class List { struct Node { T* prev; T* next; }; // all sorts of stuff }; typedef List<struct Element> ElementList; struct Element { ElementList::Node listNode; // blah blah blah }; Is this the proper D-like approach? I definitely do not want to require the List to allocate it's own linkage; one of the things this is used for is implementing malloc / free. Having to do struct Element { instance ListT(Element).Node listNode; }; feels somewhat unweildy; is there no way to something closer to my original example or the typedef approach above?
Aug 08 2003
prev sibling parent Chris Sokol <chris echosproject.org> writes:
Ah, thank you.

On Fri, 08 Aug 2003 01:30:51 -0400, Mike Wynn wrote:


 "Chris Sokol" <chris echosproject.org> wrote in message
 news:bgv6f0$1mic$1 digitaldaemon.com...
 What am I doing wrong?

 no property 'Bar' for type 'instance Foo(Fu )'
I had a play with this ..... template Foo(T) { struct Bar { T * fooBar; } } } alias instance Foo(Fu).Bar FooT; struct Fu { FooT x; } } int main() { Fu f; return 0; } } now it reports main.d(7): size of type instance Foo(Fu ).Bar is not known but ................................. template Foo(T) { struct Bar { T * fooBar; } } } struct Fu { instance Foo(Fu).Bar x; } } int main() { Fu f; return 0; } } WORKS
Aug 08 2003