Consider the code below:
struct Bar{};
struct Foo
{
Foo() = default;
Foo(const Bar&) {}
Foo(const Foo&) = delete;
operator Bar(){return {};}
};
int main()
{
Foo f1;
Foo f2(static_cast<Bar>(f1));
Foo f3(f1);
}
The class Barhas a custom conversion operator in Foo, which takes Bar&s. However, in the last line, mainI expected it to Foo f1be converted to Bar, and then passed to Foo(const Bar&). However, only the remote constructor is considered Foo(const Foo&) = delete;. I understand that this constructor is better suited, but why is it not Foo(const Bar&)also in the overload set, and why does the compiler not perform the implicit conversion?
source
share