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?
Yes. auto
is exactly specified as template argument deduction.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With