Suppose I have foo
which is a populated std::vector
.
I need to operate on the elements of this vector. I\'m motivated to writ
I would use auto&&
:
for (auto&& it : foo) {
// bla
}
The reason is spelt out in N3994 "Range-Based For-Loops: The Next Generation (Revision 1)" that it would better work with proxy objects (such as those coming from std::vector
).
In fact, that proposal for C++1z (supported already by Clang 3.5 SVN in -std=c++1z
mode) proposes the syntax:
// c++1z only
for (it : foo) {
// bla
}
as a short-hand for for (auto&& it : foo)
.
Update: the N3994 proposal was not voted into the C++17 working paper.