Reputation: 31
I seem to not be getting sign compare errors on my g++ gcc version 5.1.1 20150618 (Red Hat 5.1.1-4) (GCC).
When I compile the following with the options I don't get an error - but when I get rid of the const - it shows the warning.
Question: Is this expected behavior?
g++ typecompat.cxx -Wsign-compare -std=c++14
This is the code
#include <stddef.h>
#include <stdio.h>
#include <assert.h>
#include <vector>
int main() {
std::vector<int> v {1,2,3};
const int b = 2;
assert(v.size()>b);
return 0;
}
Upvotes: 1
Views: 470
Reputation: 158469
From a godbolt session of this code it looks like gcc is performing constant folding:
cmpq $2, %rax
So then it knows for sure the comparison is ok, any attempting to change a constant variable is undefined behavior and the compiler will assume no undefined behavior. If we look at the documents it does say:
Warn when a comparison between signed and unsigned values could produce an incorrect result when the signed value is converted to unsigned
This gcc bug report Bogus warning with -Wsign-compare looks like it covers this kind of false positive detection:
To be useful, these warnings are meant to be smart - not warning if it can be proved that the signed value is nonnegative [...]
Upvotes: 3