Question

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 I compile, I get the following error:

error C2059: syntax error : '}'

on the line containing the single semicolon. Have I found a compiler bug? Or is the range-based for loop subtle beyond my comprehension?

Was it helpful?

Solution

To summarise the comments for anyone coming this way in the future:

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

The simplest workaround for me is to just put braces around the whole do-while loop like so:

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

Thanks to everyone for your help.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top