C++ 线程构造函数如何检测右值引用?

C++ 线程构造函数如何检测右值引用?,c++,multithreading,c++11,constructor,perfect-forwarding,C++,Multithreading,C++11,Constructor,Perfect Forwarding,显然,可以将右值引用传递给std::thread构造函数。我的问题是中这个构造函数的定义。它说这个构造器: template< class Function, class... Args > explicit thread( Function&& f, Args&&... args ); 模板 显式线程(函数和函数、参数和…参数); 创建新的std::thread对象并将其与 执行。首先,构造函数复制/移动所有参数(包括 函数对象f和所有参数…)到

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

template< class Function, class... Args >
explicit thread( Function&& f, Args&&... args );
模板<类函数,类。。。Args>
显式线程(函数和函数、参数和…参数);
创建新的std::thread对象并将其与 执行。首先,构造函数复制/移动所有参数(包括 函数对象f和所有参数…)到线程可访问存储 按职能:

模板
类型名称衰减::类型衰减\复制(T&v){
返回标准::向前(v);
}
据我所知:

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

auto s = std::decay_copy(std::string("hello"));
相当于:

template<>
std::string std::decay_copy<std::string>(std::string&& src) {
    return std::string(std::move(src));
}

std::string s = decay_copy<std::string>(std::string("hello"));
模板
std::string std::decation\u copy(std::string&&src){
返回std::string(std::move(src));
}
std::string s=decation_copy(std::string(“hello”);

完美转发是常见的问题。如果要在函数中恢复有关右值的信息,必须使用std::forward。如果您对值类型检测感兴趣,可以阅读本文。从描述中,您可以找到编译器如何在编译时识别右值、x值、左值、prvalue、gvalue的信息。

std::thread构造函数知道其参数的值类别,因为它知道
函数和
参数是什么,它使用它将its参数完美地转发到
decay\u copy
(或同等文件)


实际的线程函数不知道值类别。它总是作为一个右值调用,带有所有右值参数——这很有意义:
f
args…
的副本是线程的本地副本,不会在其他任何地方使用

“将左值应用于右值,将数组应用于指针,将函数应用于指针隐式转换到类型T,删除cv限定符,并将生成的类型定义为成员typedef type”-您看到它在哪里删除引用?@xaxxon您缺少
::type
@T.C.已删除--但它们为什么相同?@xaxxon这就是问题所在”左值到右值”部分。我明白了。不是右值变成左值,而是int或多或少变成int&&@xaxxon。它试图演示模板扩展的结果。
template<>
std::string std::decay_copy<std::string>(std::string&& src) {
    return std::string(std::move(src));
}

std::string s = decay_copy<std::string>(std::string("hello"));