void :: bind for void pointer参数

时间:2014-03-28 17:58:51

标签: boost void-pointers

boost :: bind如何用于函数的void指针参数?

boost::bind(foo, _1, boost::ref((void*)this))

(显然)不正确。

1 个答案:

答案 0 :(得分:1)

绑定是强类型的。使用void*的回调通常在C API中看到,否则无需使用它:

struct Object { /* .... */ };

int some_callback(Object& o) // or e.g. a thread function
{
     // use o
}

你绑定或称之为:

Object instance;
boost::function<int(Object&)> f = boost::bind(&some_callback, boost::ref(instance));

当然,你可以通过值传递

int by_value(Object o) {}

f = boost::bin(&by_value, instance); // or `std::move(instance)` if appropriate

如果您坚持指针(为什么?):

int by_pointer(Object* o) {}
f = boost::bin(&by_pointer, &instance);

如果你真的很脏(或者你希望与该C API签名兼容):

int by_void_ptr(void* opaque_pointer) {}
f = boost::bind(&by_void_ptr, &instance);
相关问题