Limited member function templates?
Bill Baxter
dnewsgroup at billbaxter.com
Fri Nov 9 23:09:23 PST 2007
Janice Caron wrote:
> I get why member function templates would be a bad idea in general.
> They'd be a nightmare to implement! But it occurs to me that if we
> insist on some limitations, it might be quite feasible, within those
> limitations. The limitation I have in mind is explicit instantiation.
>
> Here's an example of what I'm thinking of.
>
> class A
> {
> string s;
>
> int f(T)(T t) { s = toUTF8(t); } /* template member function */
>
> alias f!(string) fs; /* explicit instantiation */
> alias f!(wstring) fw; /* explicit instantiation */
> }
>
> The trick is that the instantiation must be done *within the class
> definition*. We can still disallow anything not explicitly
> instantiated inside the class definition, so
>
> A a;
> a.fs("hello"); /* OK - uses alias*/
>
> a.f!(string)("hello") /* OK - template is instantiated for string */
>
> string s;
> a.f(s); /* OK - type deduction */
>
> a.f!(dstring)("hello"d) /* Not OK - template not instantiated for dstring */
>
> Thoughts?
Member function templates have worked fine as long as I've been around.
The only thing you can't have is _virtual_ member function templates.
Is making them virtual what you are talking about?
--bb
More information about the Digitalmars-d
mailing list