Using a template callback function in C++
I want to have a function that checks certain conditions based on a given
callback function.
Considers this 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 I want is to call it with:
bool checkEqualInt(int i, int j){return i==j;}
bool checkEqualFloat(float i, float j){return i==j;}
DoSmth(checkEqualInt, 5);
DoSmth(checkEqualFloat, 2.5);
(All constructed examples to show the problem)
The compiler won't get that and throws me error C2664 "converting param 1
from bool(int,int) in bool(ParamType,int) not possible"
I there a solution without using
template< typename ParamType, Check >
void DoSmth(Check isGood, const ParamType param)
Which ommits the necessary declaration of the check function?
No comments:
Post a Comment