I have a function of the form:
void DoSomething(const boost::function<bool ()>& condition, other stuff);
This function does some work and returns only when the condition is true. The condition is expressed as a functor argument because I want to provide different conditions on different sites.
Now this is pretty straightforward to use directly, but this requires declaring a lot of small throwaway functions or functor objects that I would like to avoid if possible. I look at the Boomb lambda library to find possible ways to end this, but I think I am missing something fundamental; I just can't get him to do what I want.
One case that alerted me: I have a collection std::vectorcalled data; the condition I'm in is when size()this collection reaches a certain threshold. Essentially, then I want my functor to conditionreturn true when data.size() >= thresholdand false otherwise. But I am having trouble expressing this in lambda syntax.
The best I've been able to come up with so far (which at least compiles, although it doesn't work):
boost::function<bool (size_t)> ge = boost::bind(std::greater_equal<size_t>(),
_1, threshold);
boost::function<size_t ()> size = boost::bind(&std::vector<std::string>::size,
data);
DoSomething(boost::lambda::bind(ge, boost::lambda::bind(size)), other stuff);
When you enter DoSomethingsize, it is 0 - and although the size increases during operation, calls do not condition()always seem to be size 0. Tracing it (which is a bit complicated through the internals of Boost), although it seems to call greater_equalevery time it is evaluated condition(), it seems to be does not cause size().
, ? ( )?
#:
DoSomething(delegate() { return data.size() >= threshold; }, other stuff);
DoSomething(() => (data.size() >= threshold), other stuff);