算术运算符是否必须将整数参数提升为int? [英] Do arithmetic operators have to promote integral arguments to int?

查看:125
本文介绍了算术运算符是否必须将整数参数提升为int?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

cppreference说:

特别是,算术运算符不接受小于int的类型作为参数,如果适用,则在左值到右值转换后会自动应用整数提升.

In particular, arithmetic operators do not accept types smaller than int as arguments, and integral promotions are automatically applied after lvalue-to-rvalue conversion, if applicable.

所有主要编译器(g ++,clang和msvc) decltype(a+b)是<ab均为short时的c1>.

All the major compilers (g++, clang, and msvc) say that decltype(a+b) is int when both a and b are short.

但是,标准说:

整体促销[conv.prom]/1
可以将boolchar16_­tchar32_­twchar_­t之外的整数类型的prvalue的整数转换等级小于int的等级的prvalue转换为类型int [的prvalue. ..].

Integral promotions [conv.prom]/1
A prvalue of an integer type other than bool, char16_­t, char32_­t, or wchar_­t whose integer conversion rank is less than the rank of int can be converted to a prvalue of type int [...].

常用算术转换[expr.arith.conv]/1.5 .1
-如果两个操作数具有相同的类型,则无需进一步转换.

Usual arithmetic conversions [expr.arith.conv]/1.5.1
-- If both operands have the same type, no further conversion is needed.

我只能看到可以转换",也找不到在哪里需要对算术运算符进行整数提升.
cppreference在这里错误吗?

I can only see that "can be converted" and I cannot find where it requires integral promotion for arithmetic operators.
Is the cppreference wrong here?

推荐答案

您离它只有一线之遥.来自[expr]/11(N4659):

You were one line away from it. From [expr]/11 (N4659):

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

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:

...

否则,必须对两个操作数执行积分提升(7.6) .然后,将以下规则应用于提升后的操作数:

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

已添加重点. [conv.prom]说,它们可以发生并且如何工作. [expr]/11指定发生 的次数之一.

Emphasis added. [conv.prom] says that they can take place and how they work. [expr]/11 specifies one of the times when they will take place.

这篇关于算术运算符是否必须将整数参数提升为int?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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