[英]C4389 signed/unsigned mismatch only for x86 compilation c++
I am seeing a C4389: "'==': signed/unsigned mismatch" compiler warning when I execute the following code in Visual Studio using the x86 compiler using a warning level of 4.当我在 Visual Studio 中使用 x86 编译器使用警告级别 4 执行以下代码时,我看到 C4389: "'==': signed/unsigned mismatch" 编译器警告。
#include <algorithm>
#include <vector>
void functionA()
{
std::vector<int> x(10);
for(size_t i = 0; i < x.size(); i++)
{
if (std::find(x.begin(), x.end(), i) != x.end())
continue;
}
}
Can someone explain to me why this happens and how I can resolve this?有人可以向我解释为什么会发生这种情况以及我该如何解决这个问题?
Here is a link https://godbolt.org/z/81v3d5asP to the online compiler where you can observe this problem.这是一个指向在线编译器的链接https://godbolt.org/z/81v3d5asP ,您可以在其中观察到这个问题。
You have declared x
as a vector of int
– but the value you are looking for in the call to std::find
(the i
variable) is a size_t
.您已将
x
声明为int
的向量——但您在调用std::find
( i
变量)时要查找的值是size_t
。 That is an unsigned type, hence the warning.那是一个无符号类型,因此是警告。
One way to fix this is to cast i
to an int
in the call:解决此问题的一种方法是将
i
转换为调用中的int
:
if (std::find(x.begin(), x.end(), static_cast<int>(i)) != x.end())
Another option (depending on your use case) would be to declare x
as a vector of an unsigned integer type:另一种选择(取决于您的用例)是将
x
声明为无符号整数类型的向量:
std::vector<unsigned int> x(10);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.