www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Templated member functions in interfaces

reply Jason House <jason.james.house gmail.com> writes:
Here's a simplified version of the code I'm trying to get working. 
Honestly, I didn't expect it to work, but I was hoping...

Is there any hope for this style of usage to work?  I'm pleasantly 
surprised by it compiling at all.  In the full implementation, the 
templated calls to X occur after a few more layers of obfuscation.  If 
the below code really does compile and link correctly, I can give a more 
complex example.

Essentially, I only expect the compiler to be able to figure out how the 
interface is used, but it has to also force the classes extending from 
it to define the extra versions of foo.

File 1:
interface X{
   void foo(A)();
}

class Y: X{
   void foo(A)(){};
}

class Z: X{
   void foo{A)(){};

// etc...

File 2:
int main(char [] args){
   X y = new Y();
   X z = new Z();
   y.foo!(int)();
   z.foo!(char)();
}
Apr 18 2007
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Jason House" <jason.james.house gmail.com> wrote in message 
news:f06cse$h1b$1 digitalmars.com...
 Here's a simplified version of the code I'm trying to get working. 
 Honestly, I didn't expect it to work, but I was hoping...

 Is there any hope for this style of usage to work?  I'm pleasantly 
 surprised by it compiling at all.  In the full implementation, the 
 templated calls to X occur after a few more layers of obfuscation.  If the 
 below code really does compile and link correctly, I can give a more 
 complex example.
It might compile, but it might not do what you expect it to. Templated member functions (and interface methods) are never virtual and therefore cannot participate in polymorphism. :\
Apr 18 2007
parent reply Jason House <jason.james.house gmail.com> writes:
Jarrett Billingsley wrote:
 "Jason House" <jason.james.house gmail.com> wrote in message 
 news:f06cse$h1b$1 digitalmars.com...
 Here's a simplified version of the code I'm trying to get working. 
 Honestly, I didn't expect it to work, but I was hoping...

 Is there any hope for this style of usage to work?  I'm pleasantly 
 surprised by it compiling at all.  In the full implementation, the 
 templated calls to X occur after a few more layers of obfuscation.  If the 
 below code really does compile and link correctly, I can give a more 
 complex example.
It might compile, but it might not do what you expect it to. Templated member functions (and interface methods) are never virtual and therefore cannot participate in polymorphism. :\
That seems to imply that having a variable of interface type should not be allowed if that interface contains templated member functions...
Apr 18 2007
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Jason House" <jason.james.house gmail.com> wrote in message 
news:f06gv1$mcd$1 digitalmars.com...
 That seems to imply that having a variable of interface type should not be 
 allowed if that interface contains templated member functions...
Or make templated member functions in interfaces illegal, since they can't ever be overloaded, and what's an interface for, after all?
Apr 18 2007
parent Jason House <jason.james.house gmail.com> writes:
Jarrett Billingsley wrote:
 "Jason House" <jason.james.house gmail.com> wrote in message 
 news:f06gv1$mcd$1 digitalmars.com...
 That seems to imply that having a variable of interface type should not be 
 allowed if that interface contains templated member functions...
Or make templated member functions in interfaces illegal, since they can't ever be overloaded, and what's an interface for, after all?
I was thinking about that. An interface can be used to define what a class should contain even if it can't be used in any polymorphic sense. I can then make a function templated to accept a class and assume it conforms to an interface (maybe a static check?). Sadly, when I did that, the call to the templated member function failed to get linked in. Commenting out the interface use fixed the problem. Weird...
Apr 19 2007