在 C++ 中解除绑定 std::bind

Bab*_*ham 3 c++ function-pointers

我有许多与它们各自的类对象绑定的函数指针:

ExampleClass EO;
std::function<void()> Example=std::bind(&ExampleClass::ExampleFunction, &EO);
Run Code Online (Sandbox Code Playgroud)

但是,我想稍后“解除绑定”这些,特别是确定每个“std::function”相关的特定类。

auto Unbind(std::function<void()> &Example)->void
{
  //Find which object &Example is bound with (in this case EO/ExampleClass)
}
Run Code Online (Sandbox Code Playgroud)

这样做的最佳方法是什么?

Ric*_*ges 5

std::function执行类型擦除。顾名思义,它从接口中删除了真正的底层类型。

没有办法从那里回来。

如果你想保留目标对象的类型,那么std::mem_fn可能是你想要的:

http://en.cppreference.com/w/cpp/utility/functional/mem_fn