问题描述
我想在msvc中发出类似于-Wsign-compare
的警告。例如。它应该触发:
bool foo(){
int i = -1;
size_t j = 1;
return i == j;
}
有C4389,但仅在j
为unsigned
时才触发,我不太了解:https://godbolt.org/z/Yd5z1P IMO,它应该针对以上情况触发。
是否存在另一种适用于上述情况的警告,但在其他情况下很少出现?例如https://docs.microsoft.com/de-de/cpp/error-messages/compiler-warnings/compiler-warning-level-4-c4365?view=vs-2019是好斗的
解决方法
有一个(显然)未记录的警告 C4388 ,将针对您的代码显示,与the documented C4389 非常相似。
以下使用/W4
开关编译的代码会同时生成这两个警告:
#pragma warning(default:4388)
#pragma warning(default:4389)
bool foo()
{
int i = -1;
unsigned j = 1;
size_t k = 1;
bool a = i == j; // warning C4389: '==': signed/unsigned mismatch
bool b = i == k; // warning C4388: '==': signed/unsigned mismatch
return 1;
}