将成员函数绑定到函数对象

时间:2022-01-14 19:43:10

I'm trying to set signal handler for some signals:

我正在尝试为某些信号设置信号处理程序:

std::function<void(int)> signalHandlerCallback;

MyApp app;
signalHandlerCallback = std::bind(std::mem_fn(&MyApp::SignalHandler, &app, std::placeholders::_1)); // *

// ...
class MyApp
{
public:
    void SignalHandler(int signum);
};

and compiler produces the following error (at line *):

和编译器产生以下错误(在行*):

test.cpp:53:111: error: no matching function for call to 'mem_fn(void (MyApp::*)(int), MyApp*, const std::_Placeholder<1>&)'

The same error appears if I don't specify the placeholder.

如果我没有指定占位符,则会出现相同的错误。

So how can I bind member function to a std::function in this case?

那么在这种情况下如何将成员函数绑定到std :: function?

Thanks in advance.

提前致谢。

1 个解决方案

#1


4  

You don't need std::mem_fn. Just rewrite it this way:

你不需要std :: mem_fn。只需这样重写:

signalHandlerCallback = std::bind(&MyApp::SignalHandler, &app, std::placeholders::_1);

#1


4  

You don't need std::mem_fn. Just rewrite it this way:

你不需要std :: mem_fn。只需这样重写:

signalHandlerCallback = std::bind(&MyApp::SignalHandler, &app, std::placeholders::_1);