重定向提升绑定

redirecting boost bind

本文关键字:绑定 重定向      更新时间:2023-10-16

以下转换是否可行?我已经尝试了 boost::lambda 并且只是一个普通绑定,但我正在努力在没有处理 foo 和调用栏的特殊帮助程序类的情况下就地进行转换。

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

你正在做函数组合。所以你必须组成你的bind。你需要ProcessBar(ConvertFooToBar(...))发生。所以你必须真正做到这一点。

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