I just bumped into this little error with msvc. It looks like a parsing problem to me but I'm not sure.
The following gives me C2143 syntax error: missing ';' before'}'
#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 next 4 variations compile just fine, though.
This
X f(const std::vector<int> v)
{
for (auto i : v)
if (true)
return X(); // <--
return X{};
}
and this
X f(const std::vector<int> v)
{
for (auto i : v)
if (true) { // <--
return X{}; // <--
} // <--
return X{};
}
and this
X f(const std::vector<int> v)
{
for (auto i : v) { // <--
if (true)
return X{}; // <--
} // <--
return X{};
}
and this
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 arcane rule or is this a compiler bug?
Visual Studio 2015 (v140) Express Edition for Desktop
compiled as x64 in both debug and release mode
all compiler options to their defaults (except for warning level bumped to W4)
for
statement in the alternative. That will make it much clearer what the changes are too. – Martin Bonner supports Monica