我有以下简单的 C++ 代码:
#include "stdafx.h"
int main()
{
int a = -10;
unsigned int b = 10;
// Trivial error is placed here on purpose to trigger a warning.
if( a < b ) {
printf( "Error in line above: this will not be printed\n" );
}
return 0;
}
使用 Visual Studio 2010(默认 C++ 控制台应用程序)编译,它给出 warning C4018: '<' : signed/unsigned mismatch" on line 7 如预期的那样(代码有逻辑错误)。
但如果我改变 unsigned int b = 10;进入 const unsigned int b = 10;警告消失!这种行为有什么已知的原因吗? gcc无论const如何,都会显示警告.
更新
我可以从评论中看到很多人建议“它只是以某种方式进行了优化,因此不需要警告”。不幸的是,需要警告 ,因为我的代码示例有实际的逻辑错误小心放置以触发警告:print声明不会被调用,不管-10实际上小于 10 .这个错误是众所周知的,“有符号/无符号警告”正是为了找到这样的错误而引发的。
更新
我还可以从评论中看到,很多人在我的代码中“发现”了一个有符号/无符号逻辑错误并正在解释它。在哪里不需要这样做 - 这个错误纯粹是为了触发警告,是微不足道的( -10 被转换为 (unsigned int)-10 即 0xFFFFFFFF-10 )并且问题与它无关:)。
最佳答案
这是一个 Visual Studio 错误,但让我们从不是错误的方面开始。
then applicable C++ standard 第 5 节注 9首先讨论如果操作数的位宽不同怎么办,然后再讨论如果它们相同但符号不同怎么办:
... Otherwise, if the operand that has unsigned integer type has rank greater than or equal to the rank of the type of the other operand, the operand with signed integer type shall be converted to the type of the operand with unsigned integer type.
这是我们了解到比较必须在无符号算术中进行的地方。我们现在需要了解这对值 -10 意味着什么。
第 4.6 节告诉我们:
If the destination type is unsigned, the resulting value is the least unsigned integer congruent to the source integer (modulo 2 n where n is the number of bits used to represent the unsigned type). [Note: In a two’s complement representation, this conversion is conceptual and there is no change in the bit pattern (if there is no truncation). — end note ] 3 If the destination type is signed, the value is unchanged if it can be represented in the destination type (and bit-field width); otherwise, the value is implementation-defined.
如您所见,一个特定的相当高的值(4294967286,或 0xFFFFFFF6,假设 unsigned int 是一个 32 位数字)与 10 进行比较,因此标准保证 printf 真的从来没有被调用过。
现在您可以相信我,在这种情况下,标准中没有要求进行诊断的规则,因此编译器可以自由地不发布任何规则。 (事实上,有些人写 -1 的目的是产生一个全一的位模式。其他人使用 int 来迭代数组,这会导致 size_t 和 int。丑,但保证能编译。)
现在 Visual Studio 会“自愿”发出一些警告。
这会导致默认设置下已经出现警告(级别 3):
int a = -10;
unsigned int b = 10;
if( a < b ) // C4018
{
printf( "Error in line above: this will not be printed\n" );
}
以下内容需要 /W4 才能获得警告。请注意,警告已重新分类。它从警告 C4018 变为警告 C4245 .这显然是设计使然。破坏比较的逻辑错误几乎总是比看似适用于正-正比较但因正-负比较而破坏的逻辑错误要小。
const int a = -10;
unsigned int b = 10;
if( a < b ) // C4245
{
printf( "Error in line above: this will not be printed\n" );
}
但你的情况不同:
int a = -10;
const unsigned int b = 10;
if( a < b ) // no warning
{
printf( "Error in line above: this will not be printed\n" );
}
而且没有任何警告。 (好吧,如果你想确定的话,你应该用 -Wall 重试。)这是一个错误。微软says关于它:
Thank you for submitting this feedback. This is a scenario where we should emit a C4018 warning. Unfortunately, this particular issue is not a high enough priority to fix in the next release given the resources that we have available.
出于好奇,我使用 Visual Studio 2012 SP1 进行了检查,发现缺陷仍然存在 - -Wall 没有警告。
关于c++ - 视觉 C++ 2010 : Why "signed/unsigned mismatch" disappears if i add "const" to one comparand?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15623889/
我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou
我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-
为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
让我们计算MRI范围内的类别:defcount_classesObjectSpace.count_objects[:T_CLASS]endk=count_classes用类方法定义类:classAdefself.foonilendend然后运行:putscount_classes-k#=>3请解释一下,为什么是三个? 最佳答案 查看MRI代码,每次你创建一个Class时,在Ruby中它是Class类型的对象,ruby会自动为这个新类创建“元类”类,这是另一个单例类型的Class对象。C函数调用(class.c)是:rb_define
我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test
我正在尝试从Postgresql表(table1)中获取数据,该表由另一个相关表(property)的字段(table2)过滤。在纯SQL中,我会这样编写查询:SELECT*FROMtable1JOINtable2USING(table2_id)WHEREtable2.propertyLIKE'query%'这工作正常:scope:my_scope,->(query){includes(:table2).where("table2.property":query)}但我真正需要的是使用LIKE运算符进行过滤,而不是严格相等。然而,这是行不通的:scope:my_scope,->(que
我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file
当我尝试安装Ruby时遇到此错误。我试过查看this和this但无济于事➜~brewinstallrubyWarning:YouareusingOSX10.12.Wedonotprovidesupportforthispre-releaseversion.Youmayencounterbuildfailuresorotherbreakages.Pleasecreatepull-requestsinsteadoffilingissues.==>Installingdependenciesforruby:readline,libyaml,makedepend==>Installingrub
我在新的Debian6VirtualBoxVM上安装RVM时遇到问题。我已经安装了所有需要的包并使用下载了安装脚本(curl-shttps://rvm.beginrescueend.com/install/rvm)>rvm,但以单个用户身份运行时bashrvm我收到以下错误消息:ERROR:Unabletocheckoutbranch.安装在这里停止,并且(据我所知)没有安装RVM的任何文件。如果我以root身份运行脚本(对于多用户安装),我会收到另一条消息:Successfullycheckedoutbranch''安装程序继续并指示成功,但未添加.rvm目录,甚至在修改我的.bas