c++ - 比较短和长是隐式转换吗?

标签 c++ visual-c++

据我了解,比较两种不同的类型,包括 shortlong 仍会导致转换。我相信short 会被提升为int。但是,我似乎找不到比较 shortlong 的直接答案。

例如:

Uint32Uint8 进行比较是否不合适。

Uint32加到Uint8上不合适吗?

Uint32/Uint8 是 SDL 中 uint32_tuint8_t 的简写类型定义。

编辑: 我想我应该对我的整体问题更明确一点。我真的很想知道是否比较或评估两种不同类型的 int,它们是相同的标牌(在示例中,unsigned),但在SIZE(uint8_tuint32_t)是不恰当的做法。

可能由于隐式转换而不合适。可能由于转换以外的性能问题而不合适。也许由于我不知道的某种可读性问题,它不受欢迎。

在评论中链接了两个类似的问题,但是他们将 intlong 进行了比较。我认为这非常相似,但 int 不只是采用所需版本的形式(uint8_tsint16_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 没有问题。类型,只要你不比较signedunsigned .

关于c++ - 比较短和长是隐式转换吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46548591/

相关文章:

c++ - 在 Xcode 中使用类的前向声明时出错

python - Cython 中的继承和 std::shared_ptr

c++ - 列表初始化结合构造函数

C++如何将unicode字符转换为int

c++ - 更新到 Visual Studio 17.4.0 会产生与 TLS 相关的链接器错误

c++ - 不调试就不能运行VC++程序

c# - 如何将快照文件转换为任何其他格式?

c++ - 如何在 QT 中正确显示 std::vector?

c++ - 如何在Qt中显示选定的日期和数字

c++ - 错误 "Expression: string subscript is out of range"