boost::void指针参数的绑定

boost::void指针参数的绑定,boost,void-pointers,Boost,Void Pointers,如何将boost::bind用于函数的空指针参数 boost::bind(foo, _1, boost::ref((void*)this)) 显然是不正确的。Bind是强类型的。带有void*的回调通常出现在C API中,不需要使用它: struct Object { /* .... */ }; int some_callback(Object& o) // or e.g. a thread function { // use o } 您可以将其绑定或称为: Object

如何将boost::bind用于函数的空指针参数

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

显然是不正确的。

Bind是强类型的。带有
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));
如果你坚持用指针(为什么?)

如果您想变得非常脏(或者希望签名与该C API兼容):


foo
的完整签名是什么?它是成员函数还是自由函数?类似于:void foo(const&argument1,Object*obj)
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);
int by_void_ptr(void* opaque_pointer) {}
f = boost::bind(&by_void_ptr, &instance);