Search code examples
c++boostboost-bind

redirecting boost bind


Is the following conversion possible? I've tried boost::lambda and just a plain bind, but I'm struggling to make the conversion in-place without a special helper class that would process foo and invoke bar.

struct Foo {}; // untouchable
struct Bar {}; // untouchable

// my code
Bar ConvertFooToBar(const Foo& foo) { ... }

void ProcessBar(const Bar& bar) { ... }

boost::function<void (const Foo&)> f = 
 boost::bind(&ProcessBar, ?);

f(Foo()); // ProcessBar is invoked with a converted Bar

Solution

  • You're doing functional composition. So you have to compose your binds. You need ProcessBar(ConvertFooToBar(...)) to happen. So you have to actually do that.

    boost::function<void (const Foo&)> f = 
     boost::bind(&ProcessBar, boost::bind(ConvertFooToBar, _1));