当二进制运算符的任一边的符号不同时,促销规则如何工作? [英] How do promotion rules work when the signedness on either side of a binary operator differ?

查看:208
本文介绍了当二进制运算符的任一边的符号不同时,促销规则如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下程序:

// http://ideone.com/4I0dT
#include <limits>
#include <iostream>

int main()
{
    int max = std::numeric_limits<int>::max();
    unsigned int one = 1;
    unsigned int result = max + one;
    std::cout << result;
}

// http://ideone.com/UBuFZ
#include <limits>
#include <iostream>

int main()
{
    unsigned int us = 42;
    int neg = -43;
    int result = us + neg;
    std::cout << result;
}

+操作符知道这是正确的类型返回?一般规则是将所有参数转换为最宽的类型,但是在 int unsigned int 。在第一种情况下,作为 operator + 的结果,必须选择 unsigned int ,因为我得到 2147483648 。在第二种情况下,它必须选择 int ,因为我得到 -1 的结果。然而,在一般情况下,我看不出这是可以决定的。

How does the + operator "know" which is the correct type to return? The general rule is to convert all of the arguments to the widest type, but here there's no clear "winner" between int and unsigned int. In the first case, unsigned int must be being chosen as the result of operator+, because I get a result of 2147483648. In the second case, it must be choosing int, because I get a result of -1. Yet I don't see in the general case how this is decidable. Is this undefined behavior I'm seeing or something else?

推荐答案

这是在§5/ 9中明确规定的:

This is outlined explicitly in §5/9:


许多期望算术或枚举类型操作数的二进制运算符以类似的方式导致转换和产生结果类型。目的是产生一个共同的类型,这也是结果的类型。这种模式称为通常的算术转换,定义如下:


  • 如果任一操作数是类型 long double ,其他将转换为 long double

  • ,如果任一操作数 double ,则另一个将转换为 double

  • 否则,如果任一操作数 float ,则另一个将转换为 float

  • 否则,将对两个操作数执行整数促销。

  • 然后,如果任一操作数 unsigned long 其他应转换为 unsigned long

  • 否则,如果一个操作数是 long int 而另一个 unsigned int ,则如果 long int 可以表示 unsigned int unsigned int 将转换为 long int ;否则,两个操作数都应转换为 unsigned long int

  • code>,另一个将转换为 long

  • 否则,如果任一操作数 unsigned ,其他将转换为 unsigned

  • If either operand is of type long double, the other shall be converted to long double.
  • Otherwise, if either operand is double, the other shall be converted to double.
  • Otherwise, if either operand is float, the other shall be converted to float.
  • Otherwise, the integral promotions shall be performed on both operands.
  • Then, if either operand is unsigned long the other shall be converted to unsigned long.
  • Otherwise, if one operand is a long int and the other unsigned int, then if a long int can represent all the values of an unsigned int, the unsigned int shall be converted to a long int; otherwise both operands shall be converted to unsigned long int.
  • Otherwise, if either operand is long, the other shall be converted to long.
  • Otherwise, if either operand is unsigned, the other shall be converted to unsigned.

[注意:否则,唯一剩下的情况是两个操作数 int ]

[Note: otherwise, the only remaining case is that both operands are int]

在这两种情况下, operator + 的结果是 unsigned 。因此,第二种情况是有效的:

In both of your scenarios, the result of operator+ is unsigned. Consequently, the second scenario is effectively:

int result = static_cast<int>(us + static_cast<unsigned>(neg));


$ b c> int 不能表示, result 的值是实现定义的– §4.7/ 3:

Because in this case the value of us + neg is not representable by int, the value of result is implementation-defined – §4.7/3:


如果目标类型是带符号的,如果它可以在目标类型场宽度);否则,值是实现定义的。

If the destination type is signed, the value is unchanged if it can be represented in the destination type (and bit-field width); otherwise, the value is implementation-defined.

这篇关于当二进制运算符的任一边的符号不同时,促销规则如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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