Strange syntax error reported in range-based loop

In Visual Studio 2013, I wrote the following in an empty, brand new command line solution:

int main(int argc, char* argv[]) { int xs[1]; for (auto x : xs) do ; while (0); return 0; } 

When compiling, I get the following error:

 error C2059: syntax error : '}' 

in a line containing one semicolon. I found a compiler error? Or is it a range based on the finer points of the loop beyond my comprehension?

+7
c ++ c ++ 11 visual-c ++ visual-c ++ - 2013
source share
1 answer

To summarize the comments for everyone coming in the future:

This is clearly a compiler error in Visual Studio 2012 and 2013. The error message given by Visual Studio is obviously fake, while other compilers work as expected.

The easiest way for me is to simply put curly braces around the entire do-while loop as follows:

 int main(int argc, char* argv[]) { int xs[1]; for (auto x : xs) { do ; while (0); } return 0; } 

Thank you all for your help.

+5
source share

All Articles