I need to pass a std::function to some algorithm. The type of the function is
typedef std::function<bool(const double&)> Condition;
In the simplest case this function will look like this
bool simpleCondition(const double& d){return d<0.001;}
Now I want to pass the same condition but only if the condition is fullfilled a number of times in a row, the function should return true. I tried the following
class RepeatingCondition{
public:
static Condition getRepeatingCondition(Condition c,int reps){
return std::bind(&RepeatingCondition::evalCondition,
RepeatingCondition(c,reps),_1);
}
private:
RepeatingCondition(Condition cc,int reps) : counter(0),
reps(reps),cond(cc){}
bool evalCondition(const double& d){
if (cond(d)){counter += 1;}
else {counter = 0;}
return (counter >= reps);
}
Condition cond;
int counter,reps;
};
My compiler does not complain and it seems to work as expected. However, I dont really understand why (with a simple function pointer it would not work, right?). Also, I would like to know if there is a simpler way to achieve the same.
Aucun commentaire:
Enregistrer un commentaire