To use boost::bind
, you'd need to make a function that sets a boolean to true, so you can bind to it:
#include <boost/bind.hpp>
#include <boost/function.hpp>
#include <boost/ref.hpp>
void make_true(bool& b)
{
b = true;
}
int main()
{
using namespace boost;
bool flag = false;
// without ref, calls with value of flag at the time of binding
// (and therefore would call make_true with a copy of flag, not flag)
function<void()> f = bind(make_true, ref(flag));
f();
assert(flag);
}
However, lambda's will help here. Lambda's are like bind, except they make functions too, so keep your code localized (no need for some external function). You'd do something like this:
#include <boost/function.hpp>
#include <boost/lambda/lambda.hpp>
int main()
{
using namespace boost;
using namespace boost::lambda;
bool flag = false;
function<void()> f = (var(flag) = true);
f();
assert(flag);
}
Same idea, except bind
and make_true
has been replaced with a lambda.