C ++标准要求提升为int吗? [英] Is promotion to int required by the C++ standard?

查看:100
本文介绍了C ++标准要求提升为int吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

获取以下代码片段.

short int a, b = 30001, c = 30002, d = 30003;
a = b + c - d;

假设short int是16位,而int是32位.这是C ++中的未定义行为吗?

Assume that short int is 16 bits and int is 32 bits. Is this undefined behaviour in C++?

我对C标准的理解是b和c都必须提升为int,因此整个计算必须使用int算术进行.最终值很短,因此不会发生UB.

My reading of the C standard is that b and c must both be promoted to int, so the entire calculation must be performed using int arithmetic. The final value fits in a short, so UB does not occur.

我在C ++标准中找不到相应的语言.关于整数提升的部分(n3797 S4.5/1)说:

I cannot find corresponding language in the C++ standard. The section on integer promotion (n3797 S4.5/1) says:

除bool,char16_t,char32_t或wchar_t以外的整数类型的prvalue,其整数转换 等级(4.13)小于int的等级如果int可以表示源类型的所有值,则可以将其转换为int类型的prvalue;否则,可以将源prvalue 转换为unsigned int类型的prvalue.

A prvalue of an integer type other than bool, char16_t, char32_t, or wchar_t whose integer conversion rank (4.13) is less than the rank of int can be converted to a prvalue of type int if int can represent all the values of the source type; otherwise, the source prvalue can be converted to a prvalue of type unsigned int.

重点是我的. 可以必须不同.

The emphasis is mine. Can be is not the same as must be.

S 5/10说:

许多期望算术或枚举类型的操作数的二进制运算符都以类似的方式引起转换并产生结果类型.目的是产生一个通用类型,它也是结果的类型.这种模式称为通常的算术转换,其定义如下:

Many binary operators that expect operands of arithmetic or enumeration type cause conversions and yield result types in a similar way. The purpose is to yield a common type, which is also the type of the result. This pattern is called the usual arithmetic conversions, which are defined as follows:

...

-否则,应在两个操作数上执行积分提升(4.5).然后以下 规则应应用于提升的操作数:

— Otherwise, the integral promotions (4.5) shall be performed on both operands. Then the following rules shall be applied to the promoted operands:

-如果两个操作数具有相同的类型,则无需进一步转换.

— If both operands have the same type, no further conversion is needed.

如果不提升值,则结果是计算的中间值超出了short int的范围,这意味着答案应该是,即UB.

If the values are not promoted then the consequence is that the intermediate value of the calculation exceeds the range for short int, which means that the answer should be yes, this is UB.

这也意味着C和C ++之间不太可能存在差异.兼容性部分中未提及.

It also means there is an unlikely difference between C and C++. It is not mentioned in the Compatibility section.

我能找到的所有其他问题涉及类型的混合,有符号/无符号等.这个问题没有特别说明.它出现在我对的回答中.

All the other questions I could find involved mixtures of types, signed/unsigned, etc. Nothing specifically on this question. It arose in my answer to this.

有没有人?

推荐答案

在4/5中更远的位置是:

Farther down in 4/5 is:

这些转化称为整体促销.

因此,当5/10说必须进行整体促销"时,这意味着(在此情况下)需要列出的转化中的任何一个匹配(只有一个匹配).

So when 5/10 says that "integral promotions shall be performed", it means that whichever one of the listed conversions matches (there is only ever one match) is required (in this context).

可以是"一词保证了这些转换是可能的,并且不排除不支持其他转换,包括身份转换.但是没有其他转换带有整体促销的名称.

The "can be" wording provides a guarantee that these conversions are possible, and does not exclude other conversions from being supported, including identity conversion. But no other conversions carry the name of integral promotion.

这篇关于C ++标准要求提升为int吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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