1
我似乎没有在g ++ gcc版本5.1.1 20150618(Red Hat 5.1.1-4)(GCC)上得到符号比较错误。gcc warning -Wsign-compare在比较const时似乎不起作用
当我编译下面的选项我没有得到一个错误 - 但是当我摆脱了const - 它显示警告。
问题:这是预期的行为吗?
g++ typecompat.cxx -Wsign-compare -std=c++14
这是代码
#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;
}
可能是因为它是const的,它知道值在signed和unsigned的范围内,因此不会有问题。如果它是非常量,值可能会改变,并且比较可能会导致麻烦。 –