The range based for loop has the syntax
attr(optional) for ( init-statement(optional)range_declaration : range_expression ) loop_statement
and it expands into
{
init-statement
auto && __range = range_expression ;
auto __begin = begin_expr ;
auto __end = end_expr ;
for ( ; __begin != __end; ++__begin) {
range_declaration = *__begin;
loop_statement
}
}
as you can see, the rvalue's lifetime gets extended by __range
, so it's valid to have lvalue references into the collection __range
binds to. Once the loop is over, __range
will go out of scope destroying the rvalue object that was created.
This is why it works even though you are modifying an object that is destined to be destroyed.