std::thread 构造函数如何检测右值引用?

Maj*_*imi 3 c++ multithreading constructor perfect-forwarding c++11

显然,可以将右值引用传递给std::thread构造函数。我的问题是在cppreference 中定义了这个构造函数。它说这个构造函数:

template< class Function, class... Args >
explicit thread( Function&& f, Args&&... args );
Run Code Online (Sandbox Code Playgroud)

创建新的 std::thread 对象并将其与执行线程相关联。首先,构造函数将所有参数(函数对象 f 和所有 args...)复制/移动到线程可访问的存储中,就像通过函数一样:

template <class T>
typename decay<T>::type decay_copy(T&& v) {
    return std::forward<T>(v);
}
Run Code Online (Sandbox Code Playgroud)

据我所知:

std::is_same<int, std::decay<int&&>::type>::value
Run Code Online (Sandbox Code Playgroud)

返回真。这意味着std::decay<T>::type将删除参数的右值引用部分。那么std::thread构造函数如何知道哪个参数是由左值或右值引用传递的呢?因为所有的T&T&&将被转换为T通过std::decay<T>::type

T.C*_*.C. 5

std::thread构造知道它的参数的值类,因为它知道什么Function以及Args...是,它使用完美向前它的参数decay_copy(或同等学历)。

实际的线程函数不知道值类别。它总是援引为右值,与所有右值参数-这是有道理的:的副本f,并args...在本地的线程,而不会在其他地方使用。