digitalmars.D - Help with template problem
- Janice Caron (26/26) Nov 06 2007 The following is a simplified version of my program. It won't compile.
- torhu (4/12) Nov 06 2007 -auto a = new A!(N)();
- Janice Caron (35/38) Nov 06 2007 If you mean the one inside main(), you're right in that that shouldn't
- Matti Niemenmaa (4/31) Nov 07 2007 That compiles for me with DMD 1.023, 2.003, and 2.007, on Windows.
- Regan Heath (3/33) Nov 07 2007 But not for me with 2.005 on Windows.
- Matti Niemenmaa (4/8) Nov 07 2007 Then try upgrading first: it was probably a regression.
- Regan Heath (3/9) Nov 07 2007 You're right. It appears to be fixed in 2.007
- Janice Caron (3/4) Nov 07 2007 Oh well - forget I mentioned it then!
- Regan Heath (29/32) Nov 07 2007 I think it's a bug. I think the compiler is getting confused about the
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; }
Nov 06 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. :)
Nov 06 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.
Nov 06 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
Nov 07 2007
Matti Niemenmaa wrote:Janice Caron wrote:But not for me with 2.005 on Windows. ReganSo 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.
Nov 07 2007
Regan Heath wrote:Matti Niemenmaa wrote:Then try upgrading first: it was probably a regression. -- E-mail address: matti.niemenmaa+news, domain is iki (DOT) fiThat compiles for me with DMD 1.023, 2.003, and 2.007, on Windows.But not for me with 2.005 on Windows.
Nov 07 2007
Matti Niemenmaa wrote:Regan Heath wrote:You're right. It appears to be fixed in 2.007 ReganMatti Niemenmaa wrote:Then try upgrading first: it was probably a regression.That compiles for me with DMD 1.023, 2.003, and 2.007, on Windows.But not for me with 2.005 on Windows.
Nov 07 2007
On 11/7/07, Regan Heath <regan netmail.co.nz> wrote:You're right. It appears to be fixed in 2.007Oh well - forget I mentioned it then! :-)
Nov 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 accessibleI 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
Nov 07 2007