c++ - 为什么这个调用有歧义?

标签 c++ overload-resolution

因此,为了探索有关重载解析过程的更多信息,我正在阅读这篇文章: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/

相关文章:

c++ - 可以过早地离开递归吗?

c++ - 视频稳定的最佳算法

c++ - 无论第二个用户输入在 switch 的第二种情况下是什么,while 循环都会继续执行

c++ - 如何防止 C++ 猜测第二个模板参数?

c++ - 确定选择了哪个重载

C++/SDL "initial value of reference to a non-const must be an lvalue"

c++ - CreateProcess 在 Windows7 64 位中失败,错误代码为 '740'

c++ - 通用引用与非模板 `const&`

c++ - 强制 C++ 更喜欢带有隐式转换的重载而不是模板

C++ 重载函数和隐式转换