请VS编译器捕捉符号/无符号的分配? [英] Make VS compiler catch signed/unsigned assignments?

查看:145
本文介绍了请VS编译器捕捉符号/无符号的分配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Visual Studio编译器似乎并没有对警告符号/无符号的分配,只有比较​​。例如,code下面将产生的if语句警告,但不是初始分配。

反正让它捕捉这些?我已经在W4但认为(希望)有可能是另一个设置的地方。

谢谢,

  INT富(无效)
{
    unsigned int类型fooUnsigned = 0xffffffff的;
    INT fooSigned = fooUnsigned; //没有警告    如果(fooSigned< fooUnsigned)​​//警告
    {
    返回0;
    }    返回fooSigned;
}

更新:

Quamrana是正确的,这是由4365警告出现在默认情况下处于关闭状态,甚至在W4控制。然而,你可以明确地启用它,像这样一个给定的警戒线;

 的#pragma警告(4:4365)

导致;

 警告C4365:初始化:从无符号整数'到'廉政'的转换,符号/无符号不匹配


解决方案

您需要启用警告4365赶作业。

这可能是棘手 - 您需要启用所有警告 - 使用/沃尔这使得很多的警告,所以你可能有一些麻烦看到警告发生,但它确实

The Visual Studio compiler does not seem to warn on signed/unsigned assignments, only on comparisons. For example the code below will generate a warning on the if statement but not the initial assignments.

Is there anyway to make it catch these? I'm already at W4 but thought (hoped) there may be another setting somewhere.

Thanks,

int foo(void)
{
    unsigned int fooUnsigned = 0xffffffff;
    int fooSigned = fooUnsigned; // no warning

    if (fooSigned < fooUnsigned) // warning
    {
    	return 0;
    }

    return fooSigned;
}

Update:

Quamrana is right, this is controlled by warning 4365 which appears to be off by default, even at W4. However you can explicitly enable it for a given warning level like so;

#pragma warning (4 : 4365)

Which results in;

warning C4365: 'initializing' : conversion from 'unsigned int' to 'int', signed/unsigned mismatch

解决方案

You need to enable warning 4365 to catch the assignment.

That might be tricky - you need to enable ALL warnings - use /Wall which enables lots of warnings, so you may have some trouble seeing the warning occur, but it does.

这篇关于请VS编译器捕捉符号/无符号的分配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆