tags:

views:

135

answers:

3

I want to know if it's possible to create a template function and then create a function pointer that points to that template function. Is this possible?

I'm using VS2008.

The following code gets this compile time error:

"cannot convert from 'overloaded-function' to 'int (__cdecl *)(int &,int &)' None of the functions with this name in scope match the target type"

template<typename T>
T tfunc(const T &x, const T &y){
    return (x < y ? x : y);
}

int (*tfunc_ptr)(int &, int &) = &tfunc<int>;
+11  A: 

Your arguments are wrong. tfunc takes is arguments by const references so your function pointer must do the same;

int (*tfunc_ptr)(const int &, const int &) = &tfunc<int>;
R Samuel Klatchko
that worked. Thanks.
LoudNPossiblyRight
+2  A: 

Hmm...what happened to the answer you had? It was correct. You need to provide the template parameter:

int (*tfunc_ptr)(int const&,int const&) = &tfunc<int>;

Oh, and note the references. Your template has them, your funptr does not. That needs to match.

Noah Roberts
that did not work, i updated my posting with the compile error i get.
LoudNPossiblyRight
David Rodríguez - dribeas
+3  A: 
template<typename T>
T tfunc(const T &x, const T &y){
 return (x < y ? x : y);
}

int (*tfunc_ptr)(const int&, const int&) = tfunc<int>;

int main() {
    int b = tfunc_ptr( 1, 2 );
}
anon