Template interface and class

Kirk McDonald kirklin.mcdonald at gmail.com
Tue May 29 15:34:15 PDT 2007


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


More information about the Digitalmars-d-learn mailing list