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.
*** Bug 878 has been marked as a duplicate of this bug. ***
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 }
*** Bug 2015 has been marked as a duplicate of this bug. ***
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)(); } ---
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.
Fixed dmd 1.032 and 2.016
AltStyle によって変換されたページ (->オリジナル) / アドレス: モード: デフォルト 音声ブラウザ ルビ付き 配色反転 文字拡大 モバイル