Function pointer argument to function template

Kirk McDonald kirklin.mcdonald at gmail.com
Tue May 30 13:08:28 PDT 2006


By way of example, take this simple function template (taken from an 
example in the spec):

template Foo(T, U=T*) {
     void Foo(T t) {
         U p;
         // ...
     }
}

int x;
Foo(x); // T is int, U is int*

Say I have a template function that takes a funtion pointer (or function 
object, I suppose) as an argument.

template Bar(T) {
     void Bar(T t) {
         t();
     }
}

void bar() {
     writef("Blah blah\n");
}

Bar(&bar);

Simple enough. But I want to deduce the return type of this function 
pointer, and make it the default value of a second template parameter.

template Bar(T, U=<something>) {
     U Bar(T t) {
         return t();
     }
}

Now, it's easy enough to explicitly instantiate this template, but I 
want to use this with the oh-so-useful implicit function template 
instantiation, so I could write e.g.:

int bar() {
     return 7;
}

writef("value is: %d\n", Bar(&bar));

Is there something big and obvious stopping this that I'm not seeing?

-Kirk McDonald



More information about the Digitalmars-d mailing list