HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppCritical

Range based loop: get item by value or reference to const?

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
constreferencevaluerangeloopitembasedget

Problem

Reading some examples of range based loops they suggest two main ways 1, 2, 3, 4

std::vector 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?

Solution

If you don't want to change the items as well as want to avoid making copies, then auto const & is the correct choice:

for (auto const &x : vec)


Whoever suggests you to use auto & is wrong. Ignore them.

Here is recap:

  • Choose auto x when you want to work with copies.



  • Choose auto &x when you want to work with original items and may modify them.



  • Choose auto const &x when you want to work with original items and will not modify them.

Code Snippets

for (auto const &x : vec)

Context

Stack Overflow Q#15176104, score: 561

Revisions (0)

No revisions yet.