Template interface and class

gerleim elf_qt at _deletethisifyouarenotaspammer_yahoo.com
Thu May 31 02:05:10 PDT 2007


Thanks for the answers.

I don't see a way in my example when class C doesn't have the void Foo() method.

My test and post is coming from my experience with c# where you get error message at compile time, which I think is convenient.
Implicit function template instantiation is also very convenient - that's why I'm trying to do it that way.

gerleim

Chris Nicholson-Sauls Wrote:
> Jarrett Billingsley wrote:
> > "gerleim" <elf_qt at _deletethisifyouarenotaspammer_yahoo.com> wrote in message 
> > news:f3klfg$nrq$1 at digitalmars.com...
> >> 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.
> > 
> > Until you instantiate the template, no class exists.  Period.  No class 
> > means no way to check if it's right.  The compiler does not do semantic 
> > analysis on template contents until they're instantiated, because there is 
> > absolutely no way to know, without knowing the template parameters, whether 
> > the code inside is valid or not.  Because just like with templated 
> > functions, templated classes are syntactic sugar.  Your class declaration is 
> > really saying:
> > 
> > template C(T)
> > {
> >     class C : I!(T) { }
> > }
> > 
> > Now it's obvious that there is no class at top-level. 
> > 
> > 
> 
> Furthermore, even if the compiler went so far as to check whether C contained at least 
> symbols corresponding to those in I, there could still be later specializations of I which 
> would change the situation.  There would then need to be a full lookup on I, late in the 
> process -- the sort of thing best performed during template instantiation.
> 
> -- Chris Nicholson-Sauls



More information about the Digitalmars-d-learn mailing list