Template docs

Georg Wrede georg.wrede at nospam.org
Sat Oct 14 05:57:36 PDT 2006


Sean Kelly wrote:
> Josh Stern wrote:
> 
>> The "Limitations" section of the language page on templates,
>> http://www.digitalmars.com/d/template.html  is confusing.   In 
>> particular, the sentence and example "Templates cannot be used to add 
>> non-static members or functions to classes. For example:
>>
>> *************************************************
>> class Foo
>> {
>>     template TBar(T)
>>     {
>>     T xx;            // Error
>>     int func(T) { ... }    // Error
>>
>>     static T yy;                // Ok
>>     static int func(T t, int y) { ... }     // Ok
>>
>> }
>>
>> *******************************************************************
>>
>> What is the "Error" in the code above?   Are the docs just out of
>> date?
> 
> 
> Yes.  However, all template member functions *are* implicitly final:
> 
>     class C
>     {
>         void tf(T)( T val ) { printf( "C\n" ); }
>         alias tf!(int) f1;
>         void f2( int val ) { tf( val ); }
>     }
> 
>     class D : C
>     {
>         void f1( int val ) { printf( "D\n" );  }
>         void f2( int val ) { printf( "D\n" );  }
>     }
> 
>     void main( char[][] args )
>     {
>         C c = new D;
> 
>         c.f1( 0 );
>         c.f2( 0 );
>     }
> 
> prints:
> 
>     C
>     D

I would have hoped for an error about attempting to override a final 
member function.



More information about the Digitalmars-d-learn mailing list