External compiler error

I just ran into this little error with msvc. This seems like a parsing problem, but I'm not sure.
The following gives me syntax error C2143: missing ';' before that '}'

#include <vector> struct X { }; X f(const std::vector<int> v) { for (auto i : v) if (true) return X{}; // <-- return X{}; } int main() { const auto x = f(std::vector<int>{}); } 

The following 4 options compile just fine. it

 X f(const std::vector<int> v) { for (auto i : v) if (true) return X(); // <-- return X{}; } 

and this one

 X f(const std::vector<int> v) { for (auto i : v) if (true) { // <-- return X{}; // <-- } // <-- return X{}; } 

and this one

 X f(const std::vector<int> v) { for (auto i : v) { // <-- if (true) return X{}; // <-- } // <-- return X{}; } 

and this one

 X f(const std::vector<int> v) { //for (auto i : v) // <-- if (true) return X{}; // <-- return X{}; } 

(Sorry for the wall of stupid code.)
Am I missing some secret rule or is it a compiler error?

Visual Studio 2015 (v140) Express Edition for the desktop
compiled as x64 in debug and release mode

all default compiler options (except for the warning level superimposed on W4)

+5
source share
1 answer

It looks like a compiler error for me. I can reproduce your error on rextester.com , but on webcompiler.cloudapp.net the code compiles fine (the Visual C ++ compiler version is 19.10.24807.0 (x86)).

Also both last gcc and clang compile the code.

The syntax itself is completely correct.

+3
source

Source: https://habr.com/ru/post/1261626/


All Articles