I have a class with a function that takes a std::function and stores it. This part seems to compile ok (but please point out any issue if there are any)
class worker
{
std::function<bool(std::string)> m_callback;
void do_work(std::function<bool(std::string)> callback)
{
m_callback = std::bind(callback, std::placeholders::_1);
callback("hello world\n");
}
};
Now I have my main and another "helper" class where I want to call this above function and pass in a function from the helper class to it:
// pretty boring class - a cut down of my actual class
class helper
{
worker the_worker;
bool work_callback(std::string str)
{
std::cout << str << std::endl;
}
}
int main
{
helper the_helper;
the_worker.do_work(std::bind(&helper::work_callback, the_helper, std::placeholders::_1)); // <---- SEGFAULT
}
Note: hand copied code, so there might be a type-o in here... please forgive!
I get a segfault, but I am not sure why :( I have used this before, in fact I copied this example from another place I used it. The only real difference that that the member function was part of the class I called it from (i.e. this
instead of the_helper
).
So this is why I am also asking if there is anything else I am doing wrong in general? Like should I be passing the std::function as:
void do_work(std::function<bool(std::string)>&& callback)
or
void do_work(std::function<bool(std::string)>& callback)
Aucun commentaire:
Enregistrer un commentaire