显然可以将右值引用传递给std::thread
构造函数。我的问题是这个构造函数的定义参考参数。它说这个构造函数:
template< class Function, class... Args >
explicit thread( Function&& f, Args&&... args );
创建新的 std::thread 对象并将其与线程关联
执行。首先,构造函数复制/移动所有参数(两个参数)
函数对象 f 和所有参数...) 到线程可访问的存储,就像
通过函数:
template <class T>
typename decay<T>::type decay_copy(T&& v) {
return std::forward<T>(v);
}
据我所知:
std::is_same<int, std::decay<int&&>::type>::value
返回真。这意味着std::decay<T>::type
将删除参数的右值引用部分。那怎么办std::thread
构造函数知道哪个参数是通过左值或右值引用传递的?因为所有T&
and T&&
将被转换为T
by std::decay<T>::type
The std::thread
构造函数知道其参数的值类别,因为它知道什么Function
and Args...
是,它用来完美地将其参数转发到decay_copy
(或同等学历)。
实际的线程函数不知道值类别。它总是作为右值调用,带有所有右值参数 - 这是有道理的:f
and args...
是线程本地的,不会在其他地方使用。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)