如果隐式转换序列使用"Promotion",为什么它不是最好的 ICS?

Why an implicit-conversion-sequence is not the best ICS if it's using a "Promotion"?

本文关键字:ICS 为什么 Promotion 转换 如果      更新时间:2023-10-16

编译这段代码:

int func( int ) {
return 1;
}
int func( char ) {
return 2;
}
struct foo {
operator int() { // Call to 'func(a)' is ambigous #1
// operator char() { // Call to 'func(a)' is not ambigous #2
return 1;
}
operator float() {
return 0.f;
}
};
int test_it (void) {
foo a;
return (func(a)==2);
}

如果我定义,为了fooint转换运算符而不是 char,许多编译器发现调用func(a)模棱两可,只有 1 个编译器认为它不模棱两可。

https://godbolt.org/g/zhRJZB

阅读标准,我并不期望,因为:

如果我编译#2结构a是从foo -> char转换而来的,然后char -> char这是候选集的最佳隐式转换序列(ICS):

(1) foo ->  char : char -> char
(2) foo ->  char : char -> int 
(3) foo ->  float : float -> int
(3) foo ->  float : float -> char

转换char -> int是促销,因此此转换的排名(2)为"促销",比"完全匹配"(1)最差,float -> int是(3)"转换"

如果改为#1

foo ->  int : int -> int

应该是最好的ICS,因为:

(1) foo ->  int : int -> int better than 
(3) foo ->  int : int -> char 
(3) foo ->  float : float -> int
(3) foo ->  float : float -> char

所以在这个候选集中,应该只有隐式转换序列,它比其他的要好。

有人可以解释为什么代码应该是模棱两可的吗?

因为重载分辨率不会直接比较四个转换。

在 #2 情况下,重载解析首先假设选择了int func(int),然后找到

foo -> char  : char -> int is better than
foo -> float : float -> int

因此,如果选择了int func(int),则会考虑转换foo -> char : char -> int。同样,如果选择了int func(char),则它找到

foo -> char  : char -> char is better than
foo -> float : float -> char

因此,如果选择了int func(char),则会考虑转换foo -> char : char -> char。最后,由于

foo -> char  : char -> char is better than
foo -> char  : char -> int

重载分辨率最终选择int func(char)以及转换foo -> char : char -> char


#1 的情况类似,只是重载分辨率在假定选择了int func(char)无法确定选择哪个转换。在这种情况下,重载解析引入了一个不明确的转换序列,目的是比较对应于两个func的隐式转换序列(以确定选择哪个func)。模棱两可的转换序列与任何用户定义的转换序列都无法区分,因此结果是模棱两可的。

引自[over.best.ics]/10:

如果存在多个不同的转换序列,每个转换序列都将参数转换为参数类型,则与该参数关联的隐式转换序列定义为指定为不明确转换序列的唯一转换序列。为了对隐式转换序列进行排名,如 [over.ics.rank] 中所述,不明确的

转换序列被视为用户定义的转换序列,与任何其他用户定义的转换序列无法区分。