Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

reference collapsing for local variables

In a function template parameterized on T, the type T&& may or may not be an rvalue reference, depending on the value category of the argument:

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&&

Does the same rule also apply for local variables where the type is automatically inferred?

auto&& x = some_named_string;      // is x a std::string& here?
auto&& y = std::string("hello");   // is y a std::string&& here?
like image 291
fredoverflow Avatar asked Oct 10 '22 07:10

fredoverflow


1 Answers

Yes. auto is exactly specified as template argument deduction.

like image 88
Puppy Avatar answered Oct 18 '22 14:10

Puppy