函数重载变得模糊
重载函数时:
void add(int a)
{
a=7;
cout<<"int";
}
void add(double a)
{
a=8.4;
cout<<"double";
}
void add(int *b)
{
*b=4;
cout<<"pointer";
}
int main()
{
char i='a'; //char
add(i);
return 0;
}
OUTPUT: int
这个工作很好,没有函数的数据类型char作为参数。
This worked fine inspite of no function with data type char as parameter.
但是编译时如下代码:
But when compiled below code:
void add(char a)
{
a='a';
cout<<"int";
}
void add(double a)
{
a=8.4;
cout<<"double";
}
void add(int *b)
{
*b=4;
cout<<"pointer";
}
int main()
{
int i=4; //int
add(i);
return 0;
}
Gave错误(编译器gcc):
Gave error(compiler gcc):
cpp | 21 |错误:调用重载的'add(int&)'是不明确的
cpp|21|error: call of overloaded 'add(int&)' is ambiguous
$ b b
这背后的逻辑是什么?
What is the logic behind this? And how to track the control passing or output of such codes?
此示例归纳为Integer Promotion和Integer之间的区别转换。总之,促销是:
This example boils down to the difference between Integer Promotion and Integer Conversion. A promotion is, in short:
除bool,char16_t,char32_t或wchar_t之外的整数类型的整数,其整数转换
rank(4.13)小于int的等级可以转换为int类型的prvalue如果int可以表示所有
源类型的值;否则,源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. [...] These conversions are called integral promotions.
而整数转换更一般:
整数类型的prvalue可以转换为另一整数类型的prvalue。 [...]允许作为整型促销的转化被排除在整数转换集合之外。
A prvalue of an integer type can be converted to a prvalue of another integer type. [...] The conversions allowed as integral promotions are excluded from the set of integral conversions.
>更好的转换比为了重载分辨率的积分转换。
An integral promotion is a better conversion than an integral conversion for the purposes of overload resolution.
在第一个示例中,我们有:
In the first example, we have:
add(int ); // (1)
add(double ); // (2)
add(int* ); // (3)
并且正在使用 char
。只有前两个是可行的,都涉及到转换。 (1)涉及具有等级升级的整数升级。 (2)涉及浮动积分转换,其具有等级转换。推广是比转换更高的排名,因此(1)是明确偏好的。
and are calling with a char
. Only the first two are viable, both involve a conversion. (1) involves an Integer Promotion, which has rank Promotion. (2) involves a Floating-Integral Conversion, which has rank Conversion. Promotion is a higher rank than Conversion, so (1) is unambiguously preferred.
现在,在第二个例子中,我们有:
Now, in the second example, we have:
add(char ); // (1)
add(double ); // (2)
add(int* ); // (3)
并且正在调用 int
。再次,只有前两个是可行的,并且都涉及转换。 (1)这次涉及积分转换(因为 char
的排名低于 int
),浮动积分转换,两者具有相同的排名:转换。由于我们有两个相同排名的转化,因此没有最佳转化,因此没有最佳可行候选人。因此,我们有一个模糊的解决方案。
and are calling with an int
. Once again, only the first two are viable, and both involve a conversion. (1) this time involves an Integral Conversion (sincechar
has lower rank than int
) and (2) still involves a Floating-integral conversion, both of which have the same rank: Conversion. As we have two conversions of the same rank, there is no "best" conversion, so there is no best viable candidate. Thus, we have an ambiguous resolution.