Thread overview
Help with template problem
Nov 06, 2007
Janice Caron
Nov 07, 2007
torhu
Nov 07, 2007
Janice Caron
Nov 07, 2007
Matti Niemenmaa
Nov 07, 2007
Regan Heath
Nov 07, 2007
Matti Niemenmaa
Nov 07, 2007
Regan Heath
Nov 07, 2007
Janice Caron
Nov 07, 2007
Regan Heath
November 06, 2007
The following is a simplified version of my program. It won't compile. The error lines are marked with comments. What's going on, and how do I fix it? Is this a bug in my code? Or in the compiler? Or what?

///////////////////////////////////
module main;

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

    this()
    {
        uint m = n; // Error: this for n needs to be type A not type
main.A!(N).A
                    // class main.A!(N).A member n is not accessible
    }
}

void doTest(uint N)()
{
    auto a = new A!(N)(); // template instance main.A!(N) error instantiating
}

int main()
{
    doTest!(1)();
    doTest!(2)(); // template instance main.doTest!(2) error instantiating

    auto a = new A!(N)();
    return 0;
}
November 07, 2007
Janice Caron wrote:
> int main()
> {
>     doTest!(1)();
>     doTest!(2)(); // template instance main.doTest!(2) error instantiating
> 
>     auto a = new A!(N)();
>     return 0;
> }

-auto a = new A!(N)();
+auto a = new A!(123)();

Try that. :)
November 07, 2007
> -auto a = new A!(N)();
> +auto a = new A!(123)();
>
> Try that. :)

If you mean the one inside main(), you're right in that that shouldn't be there. That was a copy/paste error. But I don't need to rewrite the line, I can just delete it! The compile errors are still there. (I also took out the comments). So here's the newest version:

/////////////////////////////////////////////////
module main;

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

   this()
   {
       uint m = n;
   }
}

void doTest(uint N)()
{
   auto a = new A!(N)();
}

int main()
{
   doTest!(1)();
   doTest!(2)();

   return 0;
}

/////////////////////////////////////////////////


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
test.d(13): template instance test.A!(N) error instantiating
test.d(19): template instance test.doTest!(2) error instantiating

Replacing the N with 123 inside doTest() makes all the compilation errors go away, and it now compiles. But of course, now it doesn't do what it needs to do. At that point in the code, N is a compile time constant (being the tempate parameter), and so - unless I've misunderstood something - should be every bit as compilable as 123.
November 07, 2007
Janice Caron wrote:
> So here's the newest version:
> 
> /////////////////////////////////////////////////
> module main;
> 
> class A(uint N)
> {
>    uint n = 0;
> 
>    this()
>    {
>        uint m = n;
>    }
> }
> 
> void doTest(uint N)()
> {
>    auto a = new A!(N)();
> }
> 
> int main()
> {
>    doTest!(1)();
>    doTest!(2)();
> 
>    return 0;
> }

That compiles for me with DMD 1.023, 2.003, and 2.007, on Windows.

-- 
E-mail address: matti.niemenmaa+news, domain is iki (DOT) fi
November 07, 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
November 07, 2007
Matti Niemenmaa wrote:
> Janice Caron wrote:
>> So here's the newest version:
>>
>> /////////////////////////////////////////////////
>> module main;
>>
>> class A(uint N)
>> {
>>    uint n = 0;
>>
>>    this()
>>    {
>>        uint m = n;
>>    }
>> }
>>
>> void doTest(uint N)()
>> {
>>    auto a = new A!(N)();
>> }
>>
>> int main()
>> {
>>    doTest!(1)();
>>    doTest!(2)();
>>
>>    return 0;
>> }
> 
> That compiles for me with DMD 1.023, 2.003, and 2.007, on Windows.

But not for me with 2.005 on Windows.

Regan
November 07, 2007
Regan Heath wrote:
> Matti Niemenmaa wrote:
>> That compiles for me with DMD 1.023, 2.003, and 2.007, on Windows.
> 
> But not for me with 2.005 on Windows.

Then try upgrading first: it was probably a regression.

-- 
E-mail address: matti.niemenmaa+news, domain is iki (DOT) fi
November 07, 2007
Matti Niemenmaa wrote:
> Regan Heath wrote:
>> Matti Niemenmaa wrote:
>>> That compiles for me with DMD 1.023, 2.003, and 2.007, on Windows.
>> But not for me with 2.005 on Windows.
> 
> Then try upgrading first: it was probably a regression.

You're right.  It appears to be fixed in 2.007

Regan
November 07, 2007
On 11/7/07, Regan Heath <regan@netmail.co.nz> wrote:
> You're right.  It appears to be fixed in 2.007

Oh well - forget I mentioned it then!

:-)