3

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
Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982

1 Answers1

2

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));
Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • 1
    Keep in mind that composing with `bind` can be tricky if you don't want all placeholders to be replaced in the top-level expression and need to use `boost::protect`. – pmr May 13 '12 at 01:37