参考折叠局部变量

fre*_*low 5 c++ templates type-inference rvalue-reference c++11

在参数化的函数模板中T,类型T&&可能是也可能不是右值引用,具体取决于参数的值类别:

template <typename T>
void function(T&& x)
{
    // ...
}

std::string some_named_string;
function(some_named_string);       // T&& is std::string&

function(std::string("hello"));    // T&& is std::string&&
Run Code Online (Sandbox Code Playgroud)

同样的规则是否也适用于自动推断类型的局部变量?

auto&& x = some_named_string;      // is x a std::string& here?
auto&& y = std::string("hello");   // is y a std::string&& here?
Run Code Online (Sandbox Code Playgroud)

Pup*_*ppy 4

是的。auto被精确指定为模板参数推导。