I would like opinions on what I suspect is a potential compiler bug, when using a postfix increment operator in a if
statement.
Minimal code below:
using IntVector = std::vector<int>;
std::pair<IntVector, IntVector> my_unweave(const IntVector& xs)
{
std::pair<IntVector, IntVector> result;
std::size_t counter = 0;
for (const auto& x : xs)
{
if (counter++ % 2 == 0) // This line will fail with MSVC 2022 Preview for ARM
{ // It will output OOODD / EEEVEN / OOODD / EEEVEN
printf("EEEVENn"); // (the postfix ++ fails...)
result.first.push_back(x);
}
else
{
printf("OOODDn");
result.second.push_back(x);
}
// Placing ++counter here instead of in the if test would solve the issue
// ++counter;
}
return result;
}
void foo(const IntVector& xs)
{
// This version works however
std::size_t counter = 0;
for (const auto& x : xs)
{
if (counter++ % 2 == 0)
printf("Evenn");
else
printf("Oddn");
}
}
int main()
{
foo(IntVector({ 0, 1, 2, 3 }));
auto u = my_unweave(IntVector({ 0, 1, 2, 3 }));
}
will print:
Even // foo() works
Odd
Even
Odd
OOODD // but in my_unweave() we have an inversion
EEEVEN // (it starts with "OOODD")
OOODD
EEEVEN
I suspect that the line
if (counter++% 2 == 0)
is where it fails.
This is a very rare occurrence and it fails only with *Visual Studio 2022 ARM Preview” (Version 17.4.0 Preview 5.0). What’s more it fails only in release builds and not in debug builds.
So my question is: is that a compiler bug or is this use of the postfix increment operator likely to be a UB?