为什么编译器匹配"char"字符?到"int"但不是“空头"? [英] Why does the compiler match "char" to "int" but not "short"?

查看:98
本文介绍了为什么编译器匹配"char"字符?到"int"但不是“空头"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小程序:

#include<iostream>
using namespace std;

void f(int)   { cout << "int\n";   }
void f(short) { cout << "short\n"; }

int main(void){
    char c = 0;
    f(c);
    return 0;
}

它打印int.我认为,如果这是因为整数提升",为什么不优选short?

It prints int. I felt that, if this is because of "Integer promotion", why is not short preferred?

我还知道整数提升会在表达式中发生(例如A = B).但是我在调​​用f(),时没有表达吗?

I also know that integer promotion happens in an expression (like A=B). But I don't have expression in call to f(), right?

如果这与重载解析规则有关,为什么将char传递给f会导致编译器更喜欢int而不是short?

If this is related to overload resolution rule, why passing char to f will result into compilers preferring int to short?

如果我删除f(int),则f(c)将呼叫f(short)

If I delete f(int), then f(c) will call f(short)!

总而言之,我的问题是,它与整数提升"还是过载解决规则"有关?为什么呢?

So in summary, my question is, is it related to "Integer promotion" or just "overload resolution rule"? And why?

推荐答案

(整体)相对于其他(整体)转化,促销方式更受

(Integral) Promotion is preferred to other (integral) conversions by overload resolution

隐式转换序列的排名

1)完全匹配:无需转换,左值到右值转换,限定转换,函数指针转换,(自C ++ 17起)用户定义的类类型到相同类的转换

1) Exact match: no conversion required, lvalue-to-rvalue conversion, qualification conversion, function pointer conversion, (since C++17) user-defined conversion of class type to the same class

2)促销:积分促销,浮点促销

2) Promotion: integral promotion, floating-point promotion

3)转换:积分转换,浮点转换,浮点积分转换,指针转换,指针到成员转换,布尔值转换,用户定义的派生类到其转换的转换基地

3) Conversion: integral conversion, floating-point conversion, floating-integral conversion, pointer conversion, pointer-to-member conversion, boolean conversion, user-defined conversion of a derived class to its base

因此,优先选择从char升级到int,而不是从char升级到short.

So, the promotion from char to int is preferred over conversion from char to short.

什么是晋升?.这是标准所描述的一种特殊转换.

What is promotion? you may ask. It is a special kind of conversion described by the standard.

为什么charshort不能晋升?,您可以继续. 积分促销始终是int或更大的类型. short没有促销.

Why is char to short not a promotion?, you may continue. Integral promotion is always to int or a larger type. There are no promotions to short.

以下隐式转换被归类为整体促销:

The following implicit conversions are classified as integral promotions:

  • 带符号的char或带符号的short可以转换为int;

  • signed char or signed short can be converted to int;

无符号字符或无符号short如果可以保留其整个值范围,则可以转换为int;否则可以将unsigned int转换为int;

unsigned char or unsigned short can be converted to int if it can hold its entire value range, and unsigned int otherwise;

char可以转换为int或unsigned int ,具体取决于基础类型:signed char或unsigned char(请参见上文);

char can be converted to int or unsigned int depending on the underlying type: signed char or unsigned char (see above);

wchar_t,char16_t和char32_t转换为第一种类型,该列表可以保留它们的整个值范围:int,unsigned int,long,unsigned long,long long,unsigned long long; 可以将其基本类型固定的无作用域枚举类型从下面的列表转换为第一种类型,该列表可以保留其整个值范围:int,unsigned int,long,unsigned long,long long或unsigned long long.如果值范围更大,则不应用整体促销;

wchar_t, char16_t, and char32_t can be converted to the first type from the following list able to hold their entire value range: int, unsigned int, long, unsigned long, long long, unsigned long long; an unscoped enumeration type whose underlying type is not fixed can be converted to the first type from the following list able to hold their entire value range: int, unsigned int, long, unsigned long, long long, or unsigned long long. If the value range is greater, no integral promotions apply;

固定基础类型的无范围枚举类型可以转换为其提升的基础类型;

an unscoped enumeration type whose underlying type is fixed can be converted to its promoted underlying type;

(自C ++ 11起)

(since C++11)

如果位字段类型可以表示位字段的整个值范围,则可以将其转换为int;如果可以表示位字段的整个值范围,则可以将其转换为unsigned int,否则不应用整型提升; 布尔类型可以转换为int,其值false变为"0",true变为1.

a bit field type can be converted to int if it can represent entire value range of the bit field, otherwise to unsigned int if it can represent entire value range of the bit field, otherwise no integral promotions apply; the type bool can be converted to int with the value false becoming ​0​ and true becoming 1.


标准参考文献(当前标准草案):


Standard references (current standard draft):

[over.ics.scs]§ 3

[conv.prom]§1

这篇关于为什么编译器匹配"char"字符?到"int"但不是“空头"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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