In the latest C++ standard it implies that:
for (foo : bar)
baz;
is equivilant to:
{
auto && r = bar;
for ( auto it = r.begin(), end = r.end(); it != end; ++it )
{
foo = *it;
baz;
}
}
When bar in the above is a function call that returns a collection, eg:
vector<string> boo();
ie
for (auto bo : boo())
...
Doesn't the line become:
auto&& r = boo();
...
And so the temporary return value of boo() is destroyed at the end of the statement "auto&&r=boo()", and then r is a hanging reference at the entry of the loop. ?? Is this reasoning correct? If not, why not?