Template interface and class

Chris Nicholson-Sauls ibisbasenji at gmail.com
Wed May 30 15:52:53 PDT 2007


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