Well that was stupid of me (feature request)

BCS ao at pathlink.com
Mon Sep 10 22:58:49 PDT 2007


I just came across a stupid little mistake I made that probably added a few 
dozen K to may app.

template Foo(T)
{
	int Bar()
	{
		struct Baz {int i; char j}
		Stack!(Baz) newton;
	}
}

struct Stack(T)
{
	unittest
	{
		writef("%s\n", T.stringof);
	}
}

Look at that for a few seconds. Do you see it?




Well I didn't at first. In fact it wasn't until I added the status report 
line to my unittest that I notice the problem.
What I saw was about 10 time too many copies of the unittest running. And 
all on the same type. After staring at it for a while (and eating dinner, 
and doing some homework) I figured it out.
The Baz type is not one type, but one type for /every/ version of the Foo 
template. Therefor you get a different (identical) version of the Stack template 
for every copy of the Foo template.

The solution is easy in this case. Move Baz out of Foo. However What would 
be nice is a way to say; "This thing that is inside of a template is the 
same thing for all versions of the template".

Note: if you want a type to be reused for only some of the template args, 
you could make it a template marked for reuses but specialize it on the args 
you want to keep.





More information about the Digitalmars-d mailing list