0

Does it make any sense to do something like:

void my_fun(std::vector<int>& n)
    {
        for (int& i : n) 
            {
                do something(i);
            }
    }

compared to a normal foreach loop without the reference? Would the value be passed by copy otherwise?

Ilya
  • 5
  • 2

1 Answers1

0

It does what you think it does assuming that the signature of do_something is void do_something(int& i).

If you don't put the ampersand in the range-based for-loop you'll get a copy.

jwezorek
  • 8,592
  • 1
  • 29
  • 46