Is it legal to use attribute on init-statement in a range-based for loop?

118 Views Asked by At

Example:

std::array arr{1,2,3,4,5};
for([[maybe_unused]] int a = -10; auto i : arr)
    ^^^^^^^^^^^^^^^^
{
    std::cout << i + a;
}

This seems to work fine on GCC and Clang, but fails to compile with MSVC: https://godbolt.org/z/reP5vPM6h with the error:

error C2059: syntax error: ':'
error C2143: syntax error: missing ';' before '{'
error C3536: 'i': cannot be used before it is initialized

But for some reason this compiles fine on MSVC, despite I don't think it's legal:

for(int [[maybe_unused]] a = -10; auto i : arr) {}

Link: https://godbolt.org/z/snv7Tqahh

0

There are 0 best solutions below