6

做的时候:

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
}

为什么我必须手动执行:

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

为什么它不会自动通过引用传递它?

编辑:: 所以理论上 boost::thread 是否有可能做一些宏元编程来调整它,因为 C++ 没有内置反射/内省的方式。

那么 boost / C++ 的主要部分通常是传递元信息吗?

4

2 回答 2

3

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

He only knows the types of the arguments being passed in and will forward them to the provided function. If the types don't match you get a nice complicated error message at the place where boost::thread attempts to call the function.

When looking at the types of the arguments, it is impossible to differ between pass-by-reference and pass-by-value as they look the same from the caller's side. Or from a more formal perspective: In template argument deduction T& will decay to T.

Only by providing the explicit boost::ref on the caller's side boost::thread will be able to correctly identify the type as a reference type.

于 2013-06-28T15:40:33.857 回答
0

Probably the workerThread might be trying to deduce the types in signaure of Method , which it cant deduce from &Method, itself.

于 2013-06-28T15:40:21.740 回答