Home > Net >  Bug in Microsoft x64 compiler for signed/unsigned mismatch warnings?
Bug in Microsoft x64 compiler for signed/unsigned mismatch warnings?

Time:05-04

Consider this piece of code:

#include <iostream>
#include <vector>
#include <cstdint>

int main() {
  std::vector<int> a{ 10, 11, 12, 13, 14, 15 };

  for (int32_t i = 0; i < a.size(); i  )
    std::cout << i << " ";
}

i < a.size() should trigger a "signed/unsigned mismatch" warning, at least if the appropriate warning switches are set.

gcc (with -Wall) and clang (with -Wall -Wextra) both trigger the warning with and without the -m32 switch.

The Microsoft x32 also compiler triggers the warning but the Microsoft x64 compiler doesn't trigger the warning.

You can try here: https://www.godbolt.org/z/x74Mzdh4Y

Is this a bug in the Microsoft x64 compiler?

CodePudding user response:

No, it's not a bug. There is no requirement for a C compiler to issue a diagnostic for every potential undefined, unspecified behavior, or unexpected result. Any such diagnostic messages are considered to be bonus extras.

Even if it was formally specified that the compiler should issue a diagnostic in case of a signed vs unsigned comparison behavior that's likely to produce an unexpected result:

In this case it is possible for a compiler to prove that i never becomes negative, and results in an unexpected result as a result of integer promotion rules, so there is no issue. x32 and x64 are vastly different and the compilers are likely to employ different optimization and code analysis techniques, so the x32 compiler, but not the x64 one, as a result of that, can prove that, negating the need for diagnostic.

CodePudding user response:

Please read this issue, unfortunately, according to this report, the problem has not been fixed. I suggest reopening this question in DC.

The following code can fix the warning if you need it.

#include <iostream>
#include <vector>
#include <cstdint>

int main() {
    std::vector<int> a{ 10, 11, 12, 13, 14, 15 };

    for (int32_t i = 0; i < (int)a.size(); i  )
        std::cout << i << " ";
}
  • Related