Limited member function templates?

Bill Baxter dnewsgroup at billbaxter.com
Sat Nov 10 00:36:41 PST 2007


Janice Caron wrote:
> On 11/10/07, Bill Baxter <dnewsgroup at billbaxter.com> wrote:
>> Member function templates have worked fine as long as I've been around.
> 
> Really? The documentation at http://digitalmars.com/d/template.html says:
> 
> <quote>
> Templates cannot be used to add non-static members or functions to
> classes. For example:
> 
> class Foo
> {
>     template TBar(T)
>     {
> 	T xx;			// Error
> 	int func(T) { ... }	// Error
> 
> 	static T yy;				// Ok
> 	static int func(T t, int y) { ... } 	// Ok
>     }
> }
> 
> Templates cannot be declared inside functions.
> </quote>
> 
>>   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.

Well, this works (dmd 1.023):

-----------
import std.stdio;
class Man
{
     void member_function(T)(T arg) {
         writefln("Got me an x and it's value is %s", x);
     }
     int x = 23;
}

void main()
{
     auto ima = new Man;
     ima.member_function!(int)(3);
}
--------------

Seems like the doc is wrong.

--bb



More information about the Digitalmars-d mailing list