为什么在 C 和 C++ 中的算术运算之前必须将 short 转换为 int? [英] Why must a short be converted to an int before arithmetic operations in C and C++?

查看:32
本文介绍了为什么在 C 和 C++ 中的算术运算之前必须将 short 转换为 int?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从我从这个问题得到的答案来看,C++似乎继承了对short的转换的这个要求 在从 C 执行算术运算时转换为 int.我可以请您思考一下为什么这是第一个在 C 中引入的地方?为什么不将这些操作作为 short 进行?

From the answers I got from this question, it appears that C++ inherited this requirement for conversion of short into int when performing arithmetic operations from C. May I pick your brains as to why this was introduced in C in the first place? Why not just do these operations as short?

例如(摘自 dyp 在评论中的建议):

short s = 1, t = 2 ;
auto  x = s + t ;

x 的类型为 int.

推荐答案

如果我们看国际标准的基本原理—编程语言—C6.3.1.8 部分 通常的算术转换 它说 (强调我的未来):

If we look at the Rationale for International Standard—Programming Languages—C in section 6.3.1.8 Usual arithmetic conversions it says (emphasis mine going forward):

标准中对这些转换的规则是轻微的K&R 中的那些修改:修改容纳了添加的类型和值保留规则.显式许可证已添加到以比绝对必要的更广泛"的类型执行计算,因为这有时会产生更小更快的代码,而不是更频繁地提及正确答案.计算也可以由 as if 规则以更窄"的类型执行,只要相同得到最终结果.显式转换总是可以用来获得所需类型的值

The rules in the Standard for these conversions are slight modifications of those in K&R: the modifications accommodate the added types and the value preserving rules. Explicit license was added to perform calculations in a "wider" type than absolutely necessary, since this can sometimes produce smaller and faster code, not to mention the correct answer more often. Calculations can also be performed in a "narrower" type by the as if rule so long as the same end result is obtained. Explicit casting can always be used to obtain a value in a desired type

第 6.3.1.8 节来自 C99 标准草案涵盖了通常的算术转换 适用于算术表达式的操作数,例如 6.5.6 加法运算符 说:

Section 6.3.1.8 from the draft C99 standard covers the Usual arithmetic conversions which is applied to operands of arithmetic expressions for example section 6.5.6 Additive operators says:

如果两个操作数都是算术类型,通常的算术对它们进行转换.

If both operands have arithmetic type, the usual arithmetic conversions are performed on them.

我们在 6.5.5 乘法运算符 也是.在操作数的情况下,首先从6.3.1.1 布尔值、字符和整数 表示:

We find similar text in section 6.5.5 Multiplicative operators as well. In the case of a short operand, first the integer promotions are applied from section 6.3.1.1 Boolean, characters, and integers which says:

如果一个 int 可以表示原始类型的所有值,则该值是转换为整数;否则,它被转换为无符号整数.这些被称为整数提升.48)所有其他类型都是整数提升不变.

If an int can represent all values of the original type, the value is converted to an int; otherwise, it is converted to an unsigned int. These are called the integer promotions.48) All other types are unchanged by the integer promotions.

基本原理或国际标准—编程语言—C6.3.1.1部分关于整数提升的讨论实际上更有趣,我将有选择地引用 b/c 完全引用太长了:

The discussion from section 6.3.1.1 of the Rationale or International Standard—Programming Languages—C on integer promotions is actually more interesting, I am going to selectively quote b/c it is too long to fully quote:

实施分为两大阵营,其特点可能是作为无符号保留和值保留.

Implementations fell into two major camps which may be characterized as unsigned preserving and value preserving.

[...]

未签名保留方法要求促进两个较小的无符号类型到无符号整数.这是一个简单的规则,并产生一个独立于执行环境的类型.

The unsigned preserving approach calls for promoting the two smaller unsigned types to unsigned int. This is a simple rule, and yields a type which is independent of execution environment.

保值方法要求将这些类型推广到如果该类型可以正确表示原始类型,否则将这些类型提升为无符号内部因此,如果执行环境将 short 表示为某物小于int,unsigned short 变为int;否则就会变成无符号整数.

The value preserving approach calls for promoting those types to signed int if that type can properly represent all the values of the original type, and otherwise for promoting those types to unsigned int. Thus, if the execution environment represents short as something smaller than int, unsigned short becomes int; otherwise it becomes unsigned int.

在某些情况下,这可能会产生一些相当意外的结果,如 无符号类型和更大有符号类型之间隐式转换的不一致行为 所示,还有很多这样的例子.尽管在大多数情况下,这会导致操作按预期进行.

This can have some rather unexpected results in some cases as Inconsistent behaviour of implicit conversion between unsigned and bigger signed types demonstrates, there are plenty more examples like that. Although in most cases this results in the operations working as expected.

这篇关于为什么在 C 和 C++ 中的算术运算之前必须将 short 转换为 int?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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