c++ - 重定向 boost 绑定(bind)

标签 c++ boost boost-bind

下面的转换是否可行?我已经尝试过 boost::lambda 和一个简单的绑定(bind),但我正在努力在没有处理 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

最佳答案

您正在执行功能组合。所以你必须编写你的 bind。您需要 ProcessBar(ConvertFooToBar(...)) 才能发生。所以你必须真正做到这一点。

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

关于c++ - 重定向 boost 绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10567903/

相关文章:

c++ - 创建数组 C++ 的深拷贝

c++ - 复制指针 vector

c++ - CMake 未找到 Boost 库二进制文件(二进制文件的新命名约定)

c++ - 将 Boost 与 Makefile/缺少分隔符一起使用

c++ - 如何使用 boost bind 组合多个仿函数?

c++ - boost::function 和 boost::bind 是如何工作的

c++ - CMake - 无法链接共享库(子目录)

c++ - 如何在 C 或 C++ 中反转字符串?

c++ - 为什么 boost::mutex 使用原子操作和事件而不是临界区

c++ - 具有不同签名的函数对象参数的函数