考虑下面的代码:
struct Bar{};
struct Foo
{
Foo() = default;
Foo(const Bar&) {}
Foo(const Foo&) = delete;
// IMPLICIT conversion to Bar
operator Bar(){return {};}
};
int main()
{
Foo f1;
Foo f2(static_cast<Bar>(f1)); // this is OK
Foo f3(f1); // does not compile, why not implicit conversion to `Bar`?
}
该类Bar有一个用户定义的转换运算符 to Foo,它接受Bar&s。但是,在 的最后一行中main,我本来希望Foo f1将其转换为Bar然后传递给Foo(const Bar&). 但是,仅考虑已删除的构造函数Foo(const Foo&) = delete;。我知道这个构造函数是一个更好的匹配,但为什么也不是Foo(const Bar&)重载集中,为什么编译器不执行隐式转换?