www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Is the template method non virtual?

reply baleog <maccarka yahoo.com> writes:
Here is the test code:
--
class A {
  bool f1() { return false; }
  bool f2(T)() { return false; }  
}

class B : A {
  bool f1() { return true; }
  bool f2(T)() { return true; }
}

void main()
{  
  A a = new B;
  assert(a.f1()); // OK
  assert(a.f2!(int)()); // assert error
}
--
Is it a regular behaviour? And what should I do to implement a virtual
templated method?

thanks
p.s. dmd-2.0.16
Jul 16 2008
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"baleog" <maccarka yahoo.com> wrote in message 
news:g5lmu2$2del$1 digitalmars.com...
 Here is the test code:
 --
 class A {
  bool f1() { return false; }
  bool f2(T)() { return false; }
 }

 class B : A {
  bool f1() { return true; }
  bool f2(T)() { return true; }
 }

 void main()
 {
  A a = new B;
  assert(a.f1()); // OK
  assert(a.f2!(int)()); // assert error
 }
 --
 Is it a regular behaviour? And what should I do to implement a virtual 
 templated method?
Yes, all templated methods are implicitly final (non-virtual). There is no way to have a virtual templated method. It would not be possible to implement, since there can be any number of instantiations of the method.
Jul 16 2008
parent BCS <ao pathlink.com> writes:
Reply to Jarrett,


 There is no way to have a virtual templated method.  It would not be
 possible to implement, since there can be any number of instantiations
 of the method.
 
It would not be possible to implement *with current linkers* I can think of some horrid hack's involving using relocations for vtbl offsets and generating object files by parsing link errors (or with a hacked linker) that would let it be done. OTOH it would break with .so/.dll usage and would generate some really funky build processes.
Jul 16 2008