简体   繁体   中英

bind member function to a function object

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?

Thanks in advance.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM