简体   繁体   中英

Compile error on VS2013 when range-based for loop and do while loop are used simultaneously

#include <vector>

int main()
{
    std::vector<int> v;
    for (int x : v) do {} while (0);
}

Compiling the code above on VS2013 will yield error C2059: syntax error : '}' . However, GCC can successfully compile the code.

To reproduce the error, the following requirements should be fulfilled:

  1. Use range-based for loop;
  2. Do not surround for-loop body by "{}";
  3. Write a single "do while" statement in the for-loop body.

Any insight on this?

这是一个错误,已在VS2015中修复。

You can use parentheses to avoid MSVC 2013 error. The following code is compiling fine in MSVC 2013 Update 4:

std::vector<int> v;
for (int x : v)
{
    do
    {
    } while (0);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM