Bug in D!!!

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Aug 30 14:33:30 PDT 2017


On Wednesday, August 30, 2017 20:47:12 EntangledQuanta via Digitalmars-d-
learn wrote:
> This is quite surprising!
>
> public struct S(T)
> {
>   T s;
> }
>
>
> interface I
> {
>   void Go(T)(S!T s);
>
>   static final I New()
>   {
>       return new C();
>   }
> }
>
> abstract class A : I
> {
>
> }
>
>
> class C : A
> {
>   void Go(T)(S!T s)
>   {
>
>   }
> }
>
>
> void main()
> {
>   S!int s;
>   auto c = I.New();
>
>   c.Go(s);    // fails!
>   //(cast(C)c).Go(s);  // Works, only difference is we have made c
> an explicit C.
>
> }
>
> https://dpaste.dzfl.pl/dbc5a0663802
>
> Everything works when Go is not templatized(we explicitly make T
> an int)
>
>
> This is a blocker for me! Can someone open a ticket?

It is not possible to have a function be both virtual and templated. A
function template generates a new function definition every time that it's a
called with a new set of template arguments. So, the actual functions are
not known up front, and that fundamentally does not work with virtual
functions, where the functions need to be known up front, and you get a
different function by a look-up for occurring in the virtual function call
table for the class. Templates and virtual functions simply don't mix.
You're going to have to come up with a solution that does not try and mix
templates and virtual functions.

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list