Limited member function templates?

Yigal Chripun yigal100 at gmail.com
Sat Nov 10 07:27:47 PST 2007


Christopher Wright wrote:
> Janice Caron wrote:
>> On 11/10/07, Janice Caron <caron800 at googlemail.com> wrote:
>>> On 11/10/07, Bill Baxter <dnewsgroup at billbaxter.com> wrote:
>>>> The only thing you can't have is _virtual_ member function
>>>> templates. Is making them virtual what you are talking about?
>>> All member functions in D are virtual (unless you explicitly use
>>> the "final" keyword). So obviously, yes.
>> 
>> When we get the common calling convention whereby f(x) and x.f()
>> are interchangable, then adding non-polymorphic template functions
>> will be a piece of cake.
>> 
>> Just declare as f(T)(X x, T t) { ... } and call as x.f(t).
>> 
>> Maybe that will be enough. But yeah - I intended to mean "normal" 
>> functions. Polymorphic. Virtual.
> 
> I was just about to feature-request virtual templated member
> functions. I can't mock them, which is one of two major problems in
> my mocks library. You also can't have a templated method in an
> interface:
> 
> interface Collection (T) { void addAll(U : T) (Collection!(U) toAdd);
> // fail }
> 
> Which leads to annoyances: "why can't I add all my Bicycles to my 
> Vehicles collection?"

I may be mistaken, but isn't it enough to use the following:
----
interface Collection (T) {
    void addAll(T) (Collection!(T) toAdd);
}
----
a bicycle should conform to a is-a relationship
(a bicycle is a vehicle). so you can in fact add them all due to 
inheritance. or am I missing something here?

templates are only useful at compile time. in run time we need to rely 
on polymorphism (inheritance).
Maybe you could give a better example for the case when you need to have 
template (virtual) member functions?

Yigal





More information about the Digitalmars-d mailing list