>
>
>
V714. Variable is not passed into forea…


V714. Variable is not passed into foreach loop by reference, but its value is changed inside of the loop.

The analyzer has detected a suspicious situation: there is a foreach loop in the code, the loop control variable being assigned some value. At the same time, the loop control variable is passed by value. It is more likely to have been meant to be passed by reference.

An example:

for (auto t : myvector)
  t = 17;

It will cause copying the 't' variable at each iteration and changing the local copy, which is hardly what the programmer wanted. Most likely, he intended to change the values in the 'myvector' container. A correct version of this code fragment should look as follows:

for (auto & t : myvector)
  t = 17;

This diagnostic detects only the simplest cases of incorrect use of the foreach loop, where there's a higher risk of making a mistake. In more complex constructs, the programmer is more likely to have a clear idea of what he's doing, so you can see constructs like the following one sometimes used in real-life code:

for (auto t : myvector)
{
  function(t); // t used by value
  // t is used as local variable further on
  t = anotherFunction();
  if (t)
    break;
}

The analyzer won't generate the V714 warning on this code.

This diagnostic is classified as:

You can look at examples of errors detected by the V714 diagnostic.