566 – Adding non-static members and functions to classes using a template doesn't error

D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 566 - Adding non-static members and functions to classes using a template doesn't error
Summary: Adding non-static members and functions to classes using a template doesn't e...
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D1 (retired)
Hardware: x86 Windows
: P4 normal
Assignee: Walter Bright
URL: http://www.digitalmars.com/d/template...
Keywords: accepts-invalid, spec
: 878 2015 (view as issue list)
Depends on:
Blocks: 511
Show dependency tree / graph
Reported: 2006年11月18日 13:05 UTC by Matti Niemenmaa
Modified: 2014年02月15日 13:21 UTC (History)
3 users (show)

See Also:


Attachments
Add an attachment (proposed patch, testcase, etc.)

Note You need to log in before you can comment on or make changes to this issue.
Description Matti Niemenmaa 2006年11月18日 13:05:25 UTC
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 Christian Kamm 2007年01月29日 02:52:29 UTC
*** Bug 878 has been marked as a duplicate of this bug. ***
Comment 2 Christian Kamm 2007年01月29日 03:00:41 UTC
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 Christian Kamm 2008年04月19日 14:30:48 UTC
*** Bug 2015 has been marked as a duplicate of this bug. ***
Comment 4 David Friedman 2008年04月19日 16:17:11 UTC
*** Bug 2015 has been marked as a duplicate of this bug. ***
Comment 5 David Friedman 2008年04月19日 16:20:43 UTC
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 Walter Bright 2008年06月28日 19:18:27 UTC
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.
Comment 7 Walter Bright 2008年07月09日 22:35:19 UTC
Fixed dmd 1.032 and 2.016


AltStyle によって変換されたページ (->オリジナル) /