Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange syntax error reported in a range-based for 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 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?

like image 660
JoshuaLawrence Avatar asked Mar 06 '14 01:03

JoshuaLawrence


1 Answers

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.

like image 148
JoshuaLawrence Avatar answered Sep 28 '22 15:09

JoshuaLawrence