Limited member function templates?

Janice Caron caron800 at googlemail.com
Fri Nov 9 22:30:46 PST 2007


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?



More information about the Digitalmars-d mailing list