Template interface and class

gerleim elf_qt at _deletethisifyouarenotaspammer_yahoo.com
Wed May 30 13:06:08 PDT 2007


Kirk McDonald Wrote:

> gerleim wrote:
> > The following will result in
> > "class test.C1 interface function I1.Foo isn't implemented"
> > 
> > class C1 : I1
> > {}
> > public interface I1
> > {
> > 	void Foo();
> > }
> > 
> > , but 
> > class C(T) : I!(T)
> > {}
> > 
> > public interface I(T)
> > {
> > 	T Foo();
> > }
> > 
> > will give compile error only if a declaration of the class is present somewhere.
> > 
> > Is this normal?
> 
> Yes. Class templates exist only at compile-time. The compiler can't 
> really determine if the class satisfies the interface until you 
> instantiate the template. Consider:
> 
> interface I {
>      int foo();
> }
> 
> class C(T) : I {
>      T foo();
> }
> 
> Does C implement I? It /might/, if T == int. The compiler can't tell 
> until you instantiate the template.
> 
> -- 
> Kirk McDonald
> http://kirkmcdonald.blogspot.com
> Pyd: Connecting D and Python
> http://pyd.dsource.org

Thanks for the answer, but I'm not fully convinced. A better example:

class C(T) : I!(T)
{ }

public interface I(T)
{
	void Foo();
}

Aside from the template it is sure that C misses implementation.
That is also sure from my first example. That's not decided what type is T, but a method must be present with the name Foo.

gerleim


More information about the Digitalmars-d-learn mailing list