Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pointers to template functions

Tags:

c++

so i've gotten comfortable storing template function pointers in map containers now, mainly as a method of templating a function that returns a new instance of the templated object and storing that function pointer in a map to be called when given a corresponding string index (that was a mouthful). my question lies with using the template function pointers. the only reason this seems to work is because neither the return type or the parameters are of the type template parameter. It looks like this:

template<class T>
base * createT() {return new T;}

where T should be a class derived from base. now i can make a function pointer that works fine

base*(*funcptr)() = &createT<derived>;

but let's say i wanted to setup something like this:

template <class returnType, class T>
returnType * createT() { return new T;}

what would the declaration of a function pointer look like for this function?

like image 344
FatalCatharsis Avatar asked Jun 05 '26 13:06

FatalCatharsis


1 Answers

You can't have a pointer to a function template, only to a function. So really the templating is irrelevant; you'd do something like

A *(*funcptr)() = &createT<A,B>;

Demo: http://ideone.com/NoHYS.

like image 83
Oliver Charlesworth Avatar answered Jun 08 '26 02:06

Oliver Charlesworth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!