Hi I have a quick question - over here it says ranged-based for loops of the form
for ( init-statement (optional) range-declaration : range-expression )
are equivalent to the code:
{
auto && __range = range-expression ;
for (auto __begin = begin-expr, __end = end-expr; __begin != __end; ++__begin)
{
range-declaration = *__begin;
loop-statement
}
}
So if you do
std::vector<datatype> vector1;
for (auto& datatype : vector1)
it's perfectly acceptable. However if you do
std::vector<datatype>&& vector2 = vector1;
or
std::vector<datatype>&& vector2(vector1);
it doesn't compile. As far as I know you cannot initialize an rvalue reference with an lvalue like vector1. What exactly is going on here and what is the difference between the ranged-based for loop's underlying code and the assignment/construct statements? Thanks!
auto &&is a forwarding reference, it can bind to both lvalues and rvalues. This is useful for perfect forwarding.autoworks using/like template argument deduction. That should answer the question.