5
在參數上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&&
做同樣的規則也適用對於自動推斷類型的局部變量?
auto&& x = some_named_string; // is x a std::string& here?
auto&& y = std::string("hello"); // is y a std::string&& here?