In these questions (also here) there is recommendation to write code like:
for( auto&& v : arr )
But doesn't moving the data from arr into v clobber the array arr
? How can this style of loop be safe at all?
In these questions (also here) there is recommendation to write code like:
for( auto&& v : arr )
But doesn't moving the data from arr into v clobber the array arr
? How can this style of loop be safe at all?
Copyright © 2021 Jogjafile Inc.
First,
auto&&
is not an rvalue reference. It is called a forwarding reference and it can either be an lvalue reference, or rvalue reference, depending on if the initializer is an lvalue or rvalue.In this case
arr
is a named object, soauto&&
is actually the same asauto&
in this case.Lets pretend then
arr
is an rvalue andauto&&
is actually an rvalue reference, you still don't have to worry. A reference is just an alias to the source object, it doesn't actually do anything to the source object. No move will happen as no move operation is taking place. All you are doing is referring to the element. You'd need code in the loop likein order to move what
v
is referring to.With the loop as is, the biggest concern is that you can change the values stored in
arr
sincev
has non-const access toarr
. Usinglets you still accept lvalues and rvalues, but also does not allow you to modify the elements.