编译器警告(等级 3)C4018

“token”:有符号/无符号不匹配

使用 token 运算符比较 signedunsigned 数字需要编译器将 signed 值转换为 unsigned

注解

解决此警告的一种方法是在比较 signedunsigned 类型时强制转换这两种类型中的一种。

示例

此示例生成 C4018,并演示如何对其进行修复:

// C4018.cpp
// compile with: cl /EHsc /W4 C4018.cpp
int main() {
    unsigned int uc = 0;
    int c = 0;
    unsigned int c2 = c; // implicit conversion

    if (uc < c)           // C4018
        uc = 0;

    if (uc < unsigned(c)) // OK
        uc = 0;

    if (uc < c2)          // Also OK
       uc = 0;
}

另请参阅

编译器警告(等级 4)C4388
编译器警告(等级 4)C4389