草庐IT

UNSIGNED_INT

全部标签

c++ - 在新代码中,为什么要使用 `int` 而不是 `int_fast16_t` 或 `int_fast32_t` 作为计数变量?

如果您需要一个计数变量,那么您的整数肯定必须有一个上限和下限。那么为什么不通过选择适当的(u)int_fastxx_t数据类型来指定这些限制呢? 最佳答案 最简单的原因是人们更习惯于int,而不是C++11中引入的附加类型,并且它是语言的“默认”整数类型(C++有一个);该标准在[basic.fundamental/2]中规定:Plainintshavethenaturalsizesuggestedbythearchitectureoftheexecutionenvironment46;theothersignedintegerty

c++ - 缩小从 `int`(常量表达式)到 `unsigned int` 的转换 - MSVC vs gcc vs clang

constexprinti=100;structF{F(unsignedint){}};intmain(){F{i};}上面的代码片段:使用-Wall-Wextra-Wpedantic在g++7上编译没有警告。使用-Wall-Wextra-Wpedantic在clang++4上编译没有警告。无法在MSVC2017上编译:conversionfrom'constint'to'unsignedint'requiresanarrowingconversion问:这里MSVC是不是错了?liveexampleongodbolt.orginti=100;structF{F(unsignedint

c++ - Visual Studio 调试与发布版本 : comparing int and float missmatch

看看这个例子:#includeintmain(){inti=16777217;floatf=16777216.0;floatg=i;if(i==f)printf("eq\n");elseprintf("neq\n");if(g==f)printf("eq\n");elseprintf("neq\n");return0;}在Release模式、gcc或g++(4.9.2)中使用VisualStudio2010C++(VS),具有输出eqeq这对我来说是合理的:在第一次比较期间,i被隐式转换为float,其中尾数中的有效位被截断。因此,i和f都具有相同的位模式,相当于相等性。在第二个if中

c++ - 我可以通过 reinterpret_cast 将 int 的空指针转换为 long 类型吗

int*pt=0;longi=reinterpret_cast(pt);我保证为0吗?这是明确定义的还是实现定义的?我检查了c++标准,但它只说明了Apointertoadataobjectortoafunction(butnotapointertomember)canbeconvertedtoanyintegertypelargeenoughtocontainit.在这种情况下,pt不指向任何数据对象。该规则适用于这种情况吗? 最佳答案 否,i不一定是任何值。结果是实现定义的。†在C++中,指针的表示是实现定义的,包括空指针的表示

c++ - -Wconversion warning while using operator <<= on unsigned char

当我用gcc编译以下代码时:intmain(){unsignedcharc=1;c我收到这个警告:conversionto‘unsignedchar’from‘int’mayalteritsvalue[-Wconversion]为什么?这段代码有什么问题?其实,我真的可以使用oprator吗?在unsignedchar上变量?编译命令:g++test.cpp-Wconversion-otest.exe 最佳答案 这是一个有效的警告:c相当于:c=c和的规则假设操作数被提升,在这种情况下将提升为int结果是提升的类型。所以会有一个从i

无法从“字符串”转换为“ int”。统一,尝试使用player.position.z添加高分

我试图根据玩家的“Z”位置添加高分。我无法理解怎么了。voidStart(){highScore.text=PlayerPrefs.GetInt("HighScore",0).ToString();}voidUpdateScore(){stringnumber=player.position.z.ToString();highScore.text=score.text.ToString();PlayerPrefs.SetInt("HighScore",number);//hereiswhereigettheerror}看答案为什么要将位置(float)转换为字符串,然后尝试将字符串转换为int

c++ - 无法理解 int 和用户定义类型之间的名称查找差异 - 可能与 ADL 相关

为什么下面的代码可以编译:templatevoidfoo(Tin){bar(in);}structtype{};voidbar(type){}intmain(){foo(type());}当以下情况不存在时:templatevoidfoo(Tin){bar(in);}voidbar(int){}intmain(){foo(42);}使用GnuC++7编译:a.cpp:Ininstantiationof'voidfoo(T)[withT=int]':a.cpp:9:20:requiredfromherea.cpp:2:21:error:'bar'wasnotdeclaredinthiss

c++ - 转换为无符号时,标准表示结果为 "the least unsigned integer"。为什么 "least"在这里很重要?

C++标准在[conv.integral/2]中说,关于整数转换为无符号:Ifthedestinationtypeisunsigned,theresultingvalueistheleastunsignedintegercongruenttothesourceinteger(modulo2nwherenisthenumberofbitsusedtorepresenttheunsignedtype).我的问题是,为什么会有“最少”这个词?有没有可能有多个结果,我们需要从中选择一个? 最佳答案 有无限多个整数等于任何值k模2n。有k,k

c++ - double 会在算术表达式中隐式转换为 unsigned int 吗?

在StanleyB.Lippman的C++Primer中,关于“隐式转换”的部分说:intival;unsignedintui;floatfval;fval=ui-ival*1.0;ivalisconvertedtodouble,thenmultipliedby1.0.Theresultisconvertedtounsignedint,thensubtractedbyui.Theresultisconvertedtofloat,thenassignedtofval.但我不这么认为:我认为实际上ival被转换为double然后乘以1.0然后ui是哪个是类型unsignedint转换为do

c++ - 在 C++ 中将指针变量分配给 const int?

我想知道是否有人可以向我解释以下内容:如果我写inti=0;float*pf=i;我得到一个编译错误(gcc4.2.1):error:invalidconversionfrom‘int’to‘float*’有道理——它们显然是两种完全不同的类型。但是如果我写constinti=0;float*pf=i;它编译没有错误。为什么“const”应该在赋值的右侧有所不同?“const”关键字的想法不是能够对常量值强制类型约束吗?我能想到的任何解释都感觉像是假的。而且我的解释都没有解释这个事实constinti=1;float*pf=i;编译失败。谁能解释一下? 最佳