Auto-binding for boost :: thread in C ++?

By doing:

std::vector<int> vec; int number = 4; boost::thread workerThread(&Method, number, vec) given a method: template<typename T> void Method(int n, std::vector<T> & vec) { //does stuff } 

Why do I have to do it manually:

 boost::thread workerThread(&Method, number, boost::ref(vec))? 

Why doesn't it automatically pass it by reference?

Edit :: so it is theoretically possible for boost :: thread to do macro meta programming to adjust this, since C ++ has nothing like building in reflection / introspection.

So, the main part of boost / C ++ generally transmits meta information around?

+6
source share
2 answers

Since the boost::thread object cannot determine the signature of the Method .

It knows only the types of arguments passed and passes them to the provided function. If the types do not match, you get a message with a nice complex message in the place where boost::thread trying to call the function.

When looking at the types of arguments, it is impossible to distinguish between passing and passing as they look the same from the caller's side. Or from a more formal point of view: in the template argument, the argument T& will fade to T

Only by providing an explicit boost::ref on the caller side of boost::thread will it be possible to correctly identify the type as a reference type.

+3
source

Probably, workThread may try to deduce types in the signaure of the method, which it cannot deduce from the method itself.

0
source

Source: https://habr.com/ru/post/948328/


All Articles