Is this valid code ?

Hi

Clang and MSVC accepts the following code.
But I get
error: use of ‘x’ before deduction of ‘auto’
with GCC (5.1, C++14 enabled)

int main() {
int x = {1,2,3,4,5};
for (auto x : x) {
}
}

Thanks,
Manasij Mukherjee

Hi
Clang and MSVC accepts the following code.
But I get
error: use of ‘x’ before deduction of ‘auto’
with GCC (5.1, C++14 enabled)

int main() {
int x = {1,2,3,4,5};
for (auto x : x) {
}
}

It’s a gcc bug, they get the scope of range-for variables wrong.

This is a longstanding bug in GCC (and arguably the wrong mailing list for
a GCC-centric question).

See 54430 – [C++11] For-Loop: Scope of iterating variable begins too early for the bug's
history.

-- James