1

例子:

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

这似乎在 GCC 和 Clang 上运行良好,但无法使用 MSVC 编译:https ://godbolt.org/z/reP5vPM6h并出现错误:

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

但由于某种原因,这在 MSVC 上编译得很好,尽管我认为它不合法:

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

链接:https ://godbolt.org/z/snv7Tqahh

4

0 回答 0