I need to pass std :: function to some algorithm. Function type
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 filled several 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 seems to work as expected. However, I really don't understand why (with a simple pointer to a function, this will not work, right?). Also, I would like to know if there is an easier way to achieve the same.
source share