2012-05-12 58 views
3

是否可以進行以下轉換?我已經嘗試了boost :: lambda,只是一個普通的綁定,但我很努力地進行轉換,而不需要一個特殊的輔助類來處理foo和調用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 

回答

2

你在做功能組合。所以你必須撰寫你的bind。你需要ProcessBar(ConvertFooToBar(...))發生。所以你必須真的這樣做。

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

請記住,如果您不想在頂級表達式中替換所有佔位符,並且需要使用boost :: protect,那麼使用'bind'編寫可能會非常棘手。 – pmr