简体   繁体   English

unsigned short + int实现的类型是否已定义?

[英]Is type of unsigned short + int implementation-defined?

Given this code, 有了这段代码,

unsigned short us = 0; 
int i = 0;
auto sum = us + i;

is the type of sum implementation-defined? 实现的sum类型是定义的吗? My reading of the C++11 standard says yes: 我对C ++ 11标准的阅读是:

  • 5.7/1 says that the usual arithmetic conversions are applied. 5.7 / 1说应用了通常的算术转换。
  • 4.13/1 bullets 2 and 3 say that the rank of int is greater than the rank of unsigned short. 4.13 / 1项目符号2和3表示int的等级大于unsigned short的等级。
  • 5/9 bullet 5 subbullet 4 says that if int can represent all values in unsigned short, the unsigned short is converted to int, and the type of sum is int. 5/9子弹5 subbullet 4说,如果INT可以表示在无符号短的所有值时,无符号短转换为int和类型sum为int。
  • 5/9 bullet 5 subbullet 5 says that if int can't represent all the values in unsigned short, both operands are converted to unsigned int, and the type of sum is unsigned int. 5/9项目符号5子项目5表示,如果int不能以unsigned short表示所有值,则两个操作数都将转换为unsigned int,并且sum的类型为unsigned int。

If the above analysis is legitimate, it means that using auto to declare variables initialized with arithmetic expressions on built-in types can lead to implementation-defined results. 如果以上分析是合理的,则意味着使用auto声明对内置类型使用算术表达式初始化的变量可能会导致实现定义的结果。 I'm guessing it would surprise a lot of programmers that the type of sum above is not fully determined by the standard. 我猜想这会让很多程序员感到惊讶,上述sum的类型不是完全由标准确定的。

Is my reasoning legitimate? 我的推理合法吗?

The type is implementation-specific, but not implementation-defined. 类型是特定于实现的,但不是实现定义的。 Implementation-defined means that the implementation must document what it does. 实施定义的意思是实施必须记录其功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有效的无符号到有符号转换避免实现定义的行为 - Efficient unsigned-to-signed cast avoiding implementation-defined behavior 为什么boost :: hana :: tuple_c实现定义的类型? - Why is the type of boost::hana::tuple_c implementation-defined? 实施定义的缩小转换? - Implementation-defined narrowing conversions? 未定义/未指定/实现定义的行为警告? - Undefined/Unspecified/Implementation-defined behaviour warnings? 为什么malloc(0)的返回值是实现定义的? - Why is the return value of malloc(0) implementation-defined? 实现定义的文件流同步 - 为什么? - Implementation-defined synchronization of file streams - Why? 未定义、未指定和实现定义的行为 - Undefined, unspecified and implementation-defined behavior 将 unsigned short * 转换为 unsigned int * - Cast unsigned short * to unsigned int * 此UTF-8实现是实现定义的还是定义明确的? - Is this UTF-8 implementation implementation-defined or well-defined? unsigned short int和unsigned int或unsigned short之间有什么区别? - What the difference between unsigned short int and unsigned int or unsigned short?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM