Help with template problem

Regan Heath regan at netmail.co.nz
Wed Nov 7 02:20:41 PST 2007


Janice Caron wrote:
> The compile errors are:
> test.d(7): Error: this for n needs to be type A not type test.A!(N).A
> test.d(7): class test.A!(N).A member n is not accessible

I think it's a bug.  I think the compiler is getting confused about the 
type of 'this' in the constructor.  It seems to think you're referring 
to 'n' from a non-templated class, but of course 'this' in the 
constructor is a templated class type instance.

Yet, when I add:
writefln("%s", typeof(this).stringof);
to the constructor it outputs "A".  Lets assume this is a bug in 
stringof, or something.

The type:
"test.A!(N).A"

Is, the type of the full templated form of the class/template shorthand 
you used:

template A(uint N)
{
	class A
	{
		uint n = 0;

		this()
		{
			uint m = n;
		}
	}
}

So, the type is built from the parts:
<file>.<template_name>!(<params>).<template_item>

But you probably already knew that.

WORKAROUND: Adding "this." in front of "n" makes it compile :)

Regan



More information about the Digitalmars-d mailing list