在初始化签名值时避免来自我的C ++编译器的截断警告 [英] Avoiding truncation warnings from my C++ compiler when initializing signed values

查看:220
本文介绍了在初始化签名值时避免来自我的C ++编译器的截断警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将 short 初始化为十六进制值,但是我的编译器会给出截断警告。显然,它认为我试图将 short 设置为正值。

I would like to initialize a short to a hexadecimal value, but my compiler gives me truncation warnings. Clearly it thinks I am trying to set the short to a positive value.

short my_value = 0xF00D; // Compiler sees "my_value = 61453"

如何避免此警告?我只能使用负值,

How would you avoid this warning? I could just use a negative value,

short my_value = -4083; // In 2's complement this is 0xF00D

但在我的代码中,使用十六进制更容易理解。

but in my code it is much more understandable to use hexadecimal.

推荐答案

强制转换常量。

short my_value =(short)0xF00D;

编辑:初始解释在我的头上有意义,但是进一步的反思是有点错误。但是,这应该抑制警告,并给你你期望的。

Initial explanation made sense in my head, but on further reflection was kind of wrong. Still, this should suppress the warning and give you what you expect.

这篇关于在初始化签名值时避免来自我的C ++编译器的截断警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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