据我了解,比较两种不同的类型,包括 short
和 long
仍会导致转换。我相信short
会被提升为int
。但是,我似乎找不到比较 short
和 long
的直接答案。
例如:
将 Uint32
与 Uint8
进行比较是否不合适。
将Uint32
加到Uint8
上不合适吗?
Uint32/Uint8 是 SDL 中 uint32_t
和 uint8_t
的简写类型定义。
编辑:
我想我应该对我的整体问题更明确一点。我真的很想知道是否比较或评估两种不同类型的 int
,它们是相同的标牌(在示例中,unsigned
),但在SIZE(uint8_t
和 uint32_t
)是不恰当的做法。
可能由于隐式转换而不合适。可能由于转换以外的性能问题而不合适。也许由于我不知道的某种可读性问题,它不受欢迎。
在评论中链接了两个类似的问题,但是他们将 int
与 long
进行了比较。我认为这非常相似,但 int
不只是采用所需版本的形式(uint8_t
、sint16_t
等。 )?
最佳答案
我相信这个问题的答案是:http://en.cppreference.com/w/cpp/language/operator_arithmetic在“转化”小节下。
Otherwise, the operand has integer type (because bool, char, char16_t, char32_t, wchar_t, and unscoped enumeration were promoted at this point) and integral conversions are applied to produce the common type, as follows:
If both operands are signed or both are unsigned, the operand with lesser conversion rank is converted to the operand with the greater integer conversion rank
所以我的问题的总体答案是,是的,有一个转换。据我所知,比较两个 unsigned int
没有问题。类型,只要你不比较signed
和 unsigned
.
关于c++ - 比较短和长是隐式转换吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46548591/