Template docs
Sean Kelly
sean at f4.ca
Wed Oct 11 09:58:45 PDT 2006
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
Sean
More information about the Digitalmars-d-learn
mailing list