Comment #0 by matti.niemenmaa+dbugzilla — 2006-11-18T13:05:25Z
Under the "Limitations" section, the spec states "[t]emplates cannot be used to add non-static members or functions to classes" and showcases the following code:
class Foo
{
template TBar(T)
{
T xx; // Error
int func(T) { ... } // Error
static T yy; // Ok
static int func(T t, int y) { ... } // Ok
}
}
The lines marked with "// Error" don't fail to compile, they simply behave as though they were declared static. The static attribute on yy and the second func() is thus redundant.
Either the spec or DMD is wrong here.
Comment #1 by kamm-removethis — 2007-01-29T02:52:29Z
*** Bug 878 has been marked as a duplicate of this bug. ***
Comment #2 by kamm-removethis — 2007-01-29T03:00:41Z
Things seem to have changed a bit: now only the member variable is silently made static while the member function works as a real non-static member template.
class Foo
{
template TBar(T)
{
T x; // Compiles, but is implicitly static
void func(T t) // Ok, non-static member template function
{ writefln(t); writefln(this.bar); }
}
int bar = 42;
}
void main()
{
Foo.TBar!(int).x = 2;
Foo.TBar!(int).func(2); // error, since funcx is not static
Foo f = new Foo;
Foo g = new Foo;
f.TBar!(int).func(2); // works
f.TBar!(int).x = 10;
g.TBar!(int).x = 20;
writefln(f.TBar!(int).x); // prints 20
}
Comment #3 by kamm-removethis — 2008-04-19T14:30:48Z
*** Bug 2015 has been marked as a duplicate of this bug. ***
Comment #4 by dvdfrdmn — 2008-04-19T16:17:11Z
*** Bug 2015 has been marked as a duplicate of this bug. ***
Comment #5 by dvdfrdmn — 2008-04-19T16:20:43Z
A slightly different test case. The following compiles, but fails to link.
----
interface TestInterface
{ void tpl(T)(); }
class TestImplementation : TestInterface
{ void tpl(T)() { } }
void main()
{
/* TestImplementation t = new TestImplementation(); // works */
TestInterface t = new TestImplementation(); // fails
t.tpl!(int)();
}
---
Comment #6 by bugzilla — 2008-06-28T19:18:27Z
I think the current behavior is useful, but it needs to be documented. The second issue is an error that should be detected by the compiler.