V
V
vipermagi2016-09-20 20:32:55
C++ / C#
vipermagi, 2016-09-20 20:32:55

How to pass a function object by reference to a stream function argument?

There are two functions:
void handler1(std::string& s) {}
and

void thread1(std::function<void(std::string&)>& f) {}

I do this with them:
auto f = std::bind(handler1, _1);
std::thread t(&thread1, std::ref(f)); //<-- Ошибка :(

The compiler throws an error. And if you do not pass by reference, then it does not produce an error.
When passing variables in the same way, everything works, but for some reason it doesn’t work with a functional object.
What is the catch and how can I accomplish my plan?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
MiiNiPaa, 2016-09-20
@vipermagi

The thread function takes a reference to some class. You are trying to stick a reference to a completely different class. The mistake is quite logical. Try giving the type you expect:

std::function<void(std::string&)> f = std::bind(handler1, _1);
//или тупо
std::function<void(std::string&)> f = handler1;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question