如果隐式转换序列使用"Promotion",为什么它不是最好的 ICS?
Why an implicit-conversion-sequence is not the best ICS if it's using a "Promotion"?
编译这段代码:
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);
}
如果我定义,为了foo
int
转换运算符而不是 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] 中所述,不明确的
转换序列被视为用户定义的转换序列,与任何其他用户定义的转换序列无法区分。
- 为什么"do while"循环不断退出,即使条件计算结果为 false?
- 为什么在全局范围内使用"extern int a"似乎不行?
- 为什么在popback()操作之后,它仍然打印完整的矢量
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 为什么会发生堆损坏
- 为什么使用 "this" 指针调用派生成员函数?
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 为什么比较运算符如此快速
- 为什么 Serial.println(<char[]>);返回随机字符?
- 为什么这个运算符<重载函数对 STL 算法不可见?
- 为什么不;名字在地图上是按顺序排列的吗
- 我的字符计数代码计算错误.为什么
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- 为什么在C++中使用私有复制构造函数与删除复制构造函数
- 为什么野牛仍在使用"int yylex(void)",却找不到"int yylex(YYS
- 为什么 std::unique 不调用 std::sort?
- 既然存在危险,为什么项目要使用-I include开关
- 如果隐式转换序列使用"Promotion",为什么它不是最好的 ICS?