2

I was working on a little C++ project at home, which I brought into school to show my teacher. At home I have Visual Studio 2012, whereas the school computers have Visual Studio 2010. In my code, from home, I had a for loop, like so, which compiled:

for(char c : myStr){...}

However, when I tried it on my school's computers, it did not compile, and I ended up having to do this instead:

for each(char c in myStr){...}

Why is this the case?

TheCodeArtist
  • 21,479
  • 4
  • 69
  • 130
user2976089
  • 347
  • 1
  • 5
  • 14
  • 1
    Are you using the same version of `C++` in each? – nhgrif Dec 11 '13 at 22:36
  • 2
    The `for each` is syntax introduced with C++/CLI, but is available without it (still, a Microsoft specific extension). – crashmstr Dec 11 '13 at 22:37
  • @nhgrif - I was thinking that, but I couldn't check (we have very short lessons) – user2976089 Dec 11 '13 at 22:37
  • 2
    Visual Studio 2010 does not support as much of the current C++ standard as 2012. http://msdn.microsoft.com/en-us/library/hh567368.aspx – Retired Ninja Dec 11 '13 at 22:37
  • See here, also: http://stackoverflow.com/questions/6898859/does-msvc10-visual-studio-2010-support-c-range-based-loops –  Dec 11 '13 at 22:38

6 Answers6

9

The for(char c : myStr){...} syntax is new with C++11, so anything using an older version of C++ won't compile with that syntax.

Previous to C++11, for_each is defined in the algorithm header.

nhgrif
  • 61,578
  • 25
  • 134
  • 173
4

C++11 range-based for loops aren't supported in Visual Studio 2010.

The second form is a syntax that leaked into the compiler from C++/CLI (an entirely different language that targets the .NET runtime). I filled a bug on this a while back. If you compile with the /Za switch, it will disable this language extension. You will need to use the C++03 for loop syntax using an iterator or std::for_each.

Peter Huene
  • 5,758
  • 2
  • 34
  • 35
4

The "range for" is a C++ 11 feature that was added in Visual Studio 2012. To learn more about which C++ 11 features are in Visual Studio 2008 (VC9) and Visual Studio 2010 (VC10), check the blog entry from the Visual C++ team. There are similar tables to let you know about Visual Studio 2012 and several different releases of Visual Studio 2013.

Bottom line: your for loop that you did at home is great if you have Visual Studio 2012. If you don't, use a regular for or std::for_each, not the for each you're using there.

Kate Gregory
  • 18,808
  • 8
  • 56
  • 85
3

Foreach loop was introduced in the C++11 standard, the compilers at your school probably aren't up to date with the new standard.

mewa
  • 1,532
  • 1
  • 12
  • 20
3

MS VC++ 2010 was released before the C++ 2011 Standard was adopted. So it does not support the range-based for statement. On the other hand MS VC++ 2010 has MS language extension for-each-in that was introduced in managed C++ to support foreach statement of C#.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
2

The for(char c: myStr) syntax is one of the new C++11 features and VC++ in Visual Studio does not support it.

See this for a list of C++ features which VS2010 and VS2012 C++ compilers implement: http://blogs.msdn.com/b/vcblog/archive/2011/09/12/10209291.aspx

digitalPhonix
  • 138
  • 1
  • 7