c++ - 当比较短和长时,哪个会被转换到哪个?

标签 c++ casting

如果我写

unsigned long long x = ....
unsigned short y = ...
if(x == y) ....

长的长会变成短的吗?或者空头会被转换为多头吗?我假设较短的数据类型将被转换为较长的数据类型以防止截断/数据丢失。

最佳答案

将级别较低的整型对象转换为级别较高的对象的整型。在您的情况下,无符号短将隐式转换为无符号长长。就是所谓的‘普通算术转换’。

根据C++标准

If both operands are of arithmetic or enumeration type, the usual arithmetic conversions are performed on both operands;

关于c++ - 当比较短和长时,哪个会被转换到哪个?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22649440/

相关文章:

c++ - 转换为 bool 时没有得到 1

c++ - 并发事件处理问题

c++ - 如何正确地将 _bstr_t 重置为 `NULL`

c++ - GNU radio : How to define a "get_*" method inside a sink block

c++ - ProtocolBuffer 无法定义数据,尽管它已被接收

c++ - 为什么在将 short[] 转换为 char* 时数组会反转?

php - 为什么 PHP 转换 MySQL BIT(1) 值 b'0' to boolean ' true'

postgresql - 在 PostgreSQL 的 SQL 函数中将元素转换为用户定义的类型

c++ - 使用分隔符一个空格和一个分号读取文件

c++ - QByteArray 转换和指针转换