Using a template callback function in C++ -


i want have function checks conditions based on given callback function.

considers code:

class foo{ template <class paramtype> struct isgood {     typedef bool (*check)(typename const paramtype*, int other); }; template< typename paramtype > void dosmth(isgood<paramtype>::check isgood, const paramtype* param){    //...    if(isgood(param, some_int_calculated_here)) dosmthelse(); } 

what want call with:

bool checkequalint(int* i, int j){return *i==j;} bool checkequalfloat(float* i, float j){return *i==j;}  dosmth(checkequalint, &i); dosmth(checkequalfloat, &i_float); 

(all constructed examples show problem)

the compiler won't , throws me error c2664 "converting param 1 bool(int*,int) in bool(paramtype,int) not possible"

i there solution without using

template< typename paramtype, check > void dosmth(check isgood, const paramtype param) 

which ommits necessary declaration of check function?

best solution isgood() header in function itself.

using functor template solve issues:

template< typename functor, typename paramtype > void dosmth(functor isgood, const paramtype param){    //...    if(isgood(param, some_int_calculated_here)) dosmthelse(); } 

now can use function or functor object has compatible signature(not 1 takes paramtype , int parameters). otherwise, you'll need use functions exact signature.


Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -