Limited member function templates?

Steven Schveighoffer schveiguy at yahoo.com
Sat Nov 10 05:55:20 PST 2007


"Janice Caron" wrote
> On 11/10/07, Bill Baxter <dnewsgroup at billbaxter.com> wrote:
>> Well, this works (dmd 1.023):
>
> Wow!
>
> Well, now I'm confused. If you'd said it works in 2.X, I would have
> thought "Ooh - Walter's experimenting with a new feature" - but in
> 1.X?

Also works in 2.X :)

>
> Definitely suggests a documentation change would be in order.

In fact, the code identified in the document that is an "Error" actually 
compiles.  The results I have found with testing are:

 - template members variables seem to act like static members of the class
 - template member functions seem to act like non-virtual member functions 
of the class (even with override).

I think the docs probably should be changed.  At least to say Unsupported 
instead of Error (although I'd much rather see it changed to reflect what 
actually works :)

BTW, given that member templates are supported this way, you can kind of 
mimic what you want this way:

    class A
    {
        string s;

        // override in derived classes to alter implementation
        protected void _s(string t) { s = t;}

        int f(T)(T t) { _s = toUTF8(t); } // non-virtual function, cannot 
override
    }

General rule would be: use templates to convert args to what actual members 
are stored as, use non-template functions to actually interact with members.

>
> (For that matter, all the docs relate to 2.X anyway)

Click on that D 1.0 link in the upper left, you can get the D 1.0 docs 
(should be the default IMHO since the default download is D 1.0)

-Steve 





More information about the Digitalmars-d mailing list