Why auto&& is not rvalue reference?
Widget&& var1 = Widget(); // rvalue reference
auto&& var2 = var1; //var2 not rvalue reference
below are rvalue reference example
void f(Widget&& param); // rvalue reference
Widget&& var1 = Widget(); // rvalue reference
Why var2 is not rvalue reference but f and var2 are rvalue references?
auto&&
is a declaration's equivalent of forwarding references (with identical deduction rules). As such, it will be deduced to an lvalue reference when the initializer is an lvalue. However,var
is an lvalue (as it is the name of a variable), hencevar2
is an lvalue reference.Once the type of the initializer has been determined, the compiler determines the type that will replace the keyword
auto
using the rules for template argument deduction from a function call (see template argument deduction#Other contexts for details). The keywordauto
may be accompanied by modifiers, such asconst
or&
, which will participate in the type deduction.For example, given
The type of
i
is exactly the type of the argumentu
in an imaginaryIf the function call
f(expr)
was compiled.In general , it can be think as below .
Therefore,
auto&&
may be deduced either as an lvalue reference or rvalue reference according to the initializer.In your case , imaginary template would look like
Here ,
var1
is named rvalue which is being treated as lvalue, sovar2
will be deduced as lvalue .Consider the following examples: