https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113064
Patrick Palka <ppalka at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |ppalka at gcc dot gnu.org
--- Comment #8 from Patrick Palka <ppalka at gcc dot gnu.org> ---
(In reply to m.cencora from comment #4)
> This also might be a just another symptom of the same root cause:
>
> struct bar
> {
> bar() = default;
>
> bar(const bar&);
> bar(bar&&);
>
> bar& operator=(const bar&);
> bar& operator=(bar&&);
> };
>
> struct foo
> {
> operator const bar& () const &;
>
> operator bar& () &;
>
> operator bar&&() &&;
> };
>
> void test()
> {
> bar a = foo{}; // ok
It seems this copy-init is valid according to
http://eel.is/c++draft/dcl.init#general-16.6.3 -- we first choose the best
conversion function according to the destination type bar (and independent of
bar's constructors) which in this case is operator bar&&() &&, and then perform
overload resolution of bar's constructors using the result of that conversion
function as the argument, which leads to bar(bar&&) unambiguously winning.
>
> a = foo{}; // not ok - ambiguous call, but why? &&-qualified looks like
> a better match
Whereas here, we perform the expected overload resolution of bar's constructor
set using the original argument, which gives two viable candidates
bar(const bar&) through operator const bar&() const&
bar(bar&&) through operator bar&&() &&
and user-defined conversion sequences that use different conversion functions
are incomparable, so the candidates are ambiguous.
>
> foo f;
> a = f; // ok
>
> a = static_cast<const foo&>(foo{}); // ok
> }