Incorrect overload resolution with template constructor

Martin v. Loewis martin@loewis.home.cs.tu-berlin.de
Wed May 10 23:01:00 GMT 2000


> I expeceted the following code to use the operator+ supplied with B.
> However, the compiler tries to use the template constructor for C, and
> considers C's operator+ functions as equal fits. It shouldn't, as they
> have template arguments. Note that the ordinary constructors C(A) and
> C(B) are correctly not considered.

Thanks for your bug report. This is not a bug in the compiler, but in
your code. The compiler sees all three operator+ declarations. For the
templates, it performs template argument deduction and gets

class C operator +<B>(const C &, const B &)
class C operator +<A>(const A &, const C &)

It compares each of these signatures with the non-template version,
namely

struct B operator +(const B &, const B &)

None of these functions is better then any other. They all have an
exact match in one argument and a user-defined conversion sequence in
the other, so the call is ambiguous.

If you question this line of reasoning, please discuss it in one of
the public C++ fora first, eg. comp.lang.c++.moderated, or
comp.std.c++.

Regards,
Martin


More information about the Gcc-bugs mailing list