How to replace non-standard "for each" received from Visual C++ users

117 Views Asked by At

Visual Studio supports a pre-C++11 non-standard for each in their support for C++17 and earlier. I receive such code from users working with Visual Studio, and would like to replace it with standard C++:

#include <vector>

int main(int argc, char *argv[])
{
  std::vector<int> v{};
  for each(auto i in v){ }
  return 0;
}

What would be a suitable replacement for the for each? I'm thinking it should be for (auto i : v), for (auto& i : v), or for (const auto& i : v).

1

There are 1 best solutions below

0
Minxin Yu - MSFT On BEST ANSWER

From the doc: for each, in

using a standard Range-based for Statement (C++) is preferred, instead.

When identifier is a Tracking Reference Operator, you can modify the element.

For native C++, for each(auto i in v)
i is a copy of the vector element

for (auto i : v)