因此,为了探索有关重载解析过程的更多信息,我正在阅读这篇文章:http://accu.org/index.php/journals/268
特殊性“用户定义的转换序列的排序”部分有以下示例:
struct A;
struct B {
B(A const&);
};
struct A {
operator B() const;
operator int() const;
};
void func(B);
void func(int);
func(A());
最初我认为 call 是不明确的,因为结构体 A
中存在以下转换运算符
operator B() const; //-> A::operator B(const A&)
operator int() const; //-> A::operator int(const A&)
然后他们是这样解释的:
The call is ambiguous, however, the parameter B has an ambiguous conversion sequence and if the function having this parameter was eliminated the call would not be ambiguous. This is because there would be only one function to select.
这完全超出了我的想象,所以我想我应该站起来再读一遍,但我仍然无法理解它笑
如果有人能解释一下事件的顺序以及上面引用的文章中的段落的含义,请简单地解释一下:)非常感谢:)
最佳答案
它试图说明文章上一段中提出的观点。
由于您确定的转换序列不明确,您永远无法成功地将 A
传递给 void func(B)
。但出于重载解析的目的,重载仍然被认为是可行的。
如果相反,它被从可行集中删除,那么对 func
的调用将明确调用 void func(int)
,因为没有其他选择。这可能不是正确的做法。
关于c++ - 为什么这个调用有歧义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34472473/