Reading some examples of range based loops they suggest two main ways 1, 2, 3, 4
std::vector<MyClass> vec; for (auto &x : vec) { // x is a reference to an item of vec // We can change vec's items by changing x } or
for (auto x : vec) { // Value of x is copied from an item of vec // We can not change vec's items by changing x } Well.
When we don't need changing vec items, IMO, Examples suggest to use second version (by value). Why they don't suggest something which const references (At least I have not found any direct suggestion):
for (auto const &x : vec) // <-- see const keyword { // x is a reference to an const item of vec // We can not change vec's items by changing x } Isn't it better? Doesn't it avoid a redundant copy in each iteration while it's a const?